UNPKG

wejsv2old-plugin-cdp-profile

Version:
152 lines (135 loc) 4.13 kB
App.UserRelatosController = Ember.ObjectController.extend({ queryParams: ['type', 'page'], type: null, page: 1, relatosPerPage: 9, relatosPerRow: 3, maxPagesToDisplay: 5, autorCustomClass: false, atorCustomClass: false, relatosProxy: function () { // body... var relatos = this.get('model.relatos'); var sortedRelatos = Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, { sortProperties: ['createdAt'], sortAscending: false, content: relatos }); return sortedRelatos.map(function (relato, i) { relato.set('bgdImg', relato.get('imagemDestaque.urls.large') || we.configs.server.providers.api + '/core/images/blank-image.jpg'); return relato; }); }.property('relatos.@each'), filteredArray: function (){ var self = this; var content = this.get('relatosProxy'); var filteredArray = []; if ( self.get('type') ) { filteredArray = content.filter(function (relato){ return relato.get('computedRelation').toLowerCase() === self.get('type'); }); } else { filteredArray = content; } return filteredArray.map(function (relato, i){ relato.set('page', Math.floor( i / self.get('relatosPerPage') ) + 1); return relato; }).filter(function (relato){ return relato.get('page') === self.get('page'); }).map(function (relato, i){ relato.set('row', Math.floor( i / self.get('relatosPerRow') ) + 1); return relato; }).reduce(function (prev, curr){ if ( !prev[curr.get('row') - 1] ) { prev[curr.get('row') - 1] = []; } prev[curr.get('row') - 1].push(curr); return prev; }, []); }.property('type', 'page', 'relatosProxy'), totalRelatosAutor: function (){ var counts = this.get('counts.relatos'); var total = 0; for ( var count in counts ) { if ( count === 'creator' || count === 'autor' ) { total += counts[count]; } } return total; }.property('counts.relatos'), totalRelatos: function (){ var counts = this.get('counts.relatos'); var total = 0; for ( var count in counts ) { total += counts[count]; } return total; }.property('counts.relatos'), totalComputed: function (){ if ( this.get('type') === 'autor' ) { return Math.ceil( this.get('totalRelatosAutor') / this.get('relatosPerPage') ) || 1; } else if ( this.get('type') === 'ator' ) { return Math.ceil( this.get('counts.relatos.ator') / this.get('relatosPerPage') ) || 1; } else { return Math.ceil( this.get('totalRelatos') / this.get('relatosPerPage') ) || 1; } }.property('filteredArray'), computeClass: function (){ Ember.run.scheduleOnce('afterRender', this, function (){ if ( this.get('type') === 'autor' ){ return this.setProperties({ autorCustomClass: true, atorCustomClass: false }); } else if ( this.get('type') === 'ator') { return this.setProperties({ autorCustomClass: false, atorCustomClass: true }); } else { return this.setProperties({ autorCustomClass: false, atorCustomClass: false }); } }); }.observes('type'), actions: { filterAtor: function (){ this.setProperties({ type: 'ator', page: 1 }); }, filterAutor: function (){ this.setProperties({ type: 'autor', page: 1 }); }, clearFilter: function (){ this.setProperties({ type: null, page: 1 }); }, goToList: function (){ if (!$('#anchor-user-relatos-list').offset()) return; $('html, body').animate({ scrollTop: $('#anchor-user-relatos-list').offset().top }, 1000); }, pagerChangePage: function(page) { this.set('page', page); this.send('goToList'); }, pagerStepForward: function() { this.incrementProperty('page'); this.send('goToList'); }, pagerStepBackward: function() { this.decrementProperty('page'); this.send('goToList'); } } });