|
(function() { |
|
var App; |
|
|
|
App = Ember.Application.create(); |
|
|
|
window.App = App; |
|
|
|
App.Store = DS.Store.extend({ |
|
revision: 1, |
|
adapter: DS.RESTAdapter.extend({ |
|
namespace: 'api' |
|
}) |
|
}); |
|
|
|
|
|
Ember.Handlebars.registerBoundHelper('getObjValue', function(obj, key) { |
|
return obj.get(key); |
|
}); |
|
|
|
/* |
|
|
|
To make a route filterable |
|
specify @modelName property in your route as in example below |
|
|
|
App.UsersIndexRoute = Ember.Route.extend Ember.FilterableRouteMixin, |
|
modelName: 'user' |
|
*/ |
|
|
|
Ember.FilterableRouteMixin = Ember.Mixin.create({ |
|
filterParams: {}, |
|
modelName: '', |
|
model: function(params) { |
|
if (this.filterParams) { |
|
params = $.extend(params, this.filterParams); |
|
} |
|
return this.get('store').find(this.modelName, params); |
|
}, |
|
actions: { |
|
filterModel: function(params) { |
|
var query; |
|
this.set('filterParams', params); |
|
query = $.extend({}, params); |
|
if (this.get('context').query && this.get('context').query.page) { |
|
query.page = this.get('context').query.page; |
|
} |
|
return this.get('controller').set('content', this.model(query)); |
|
} |
|
} |
|
}); |
|
|
|
Ember.PageableControllerMixin = Ember.Mixin.create({ |
|
queryParams: ['page'], |
|
currentPage: 1, |
|
totalPages: 1, |
|
_getContent: function() { |
|
var content; |
|
if (this.get('content').content instanceof Array) { |
|
return content = this.get('content'); |
|
} else { |
|
return content = this.get('content').content; |
|
} |
|
}, |
|
currentPage: (function() { |
|
var content; |
|
content = this._getContent(); |
|
if (!content || !content.meta) { |
|
return 1; |
|
} else { |
|
return content.meta.current_page; |
|
} |
|
}).property('content.content'), |
|
totalPages: (function() { |
|
var content; |
|
content = this._getContent(); |
|
if (!content || !content.meta) { |
|
return 1; |
|
} else { |
|
return content.meta.total_pages; |
|
} |
|
}).property('content.content') |
|
}); |
|
|
|
|
|
/* |
|
|
|
There are two methods to make route reload when query parameter changes. |
|
|
|
1. Refresh model on any query parameter change: |
|
actions: |
|
queryParamsDidChange: -> |
|
@refresh() |
|
|
|
2. Change only on specified parameters change |
|
queryParams: |
|
page: |
|
refreshModel: true |
|
*/ |
|
|
|
Ember.PageableRouteMixin = Ember.Mixin.create({ |
|
queryParams: { |
|
page: { |
|
refreshModel: true |
|
} |
|
} |
|
}); |
|
|
|
|
|
App.User = DS.Model.extend({ |
|
first_name: DS.attr('string'), |
|
last_name: DS.attr('string'), |
|
group: DS.attr('string'), |
|
email: DS.attr('string'), |
|
bio: DS.attr('string'), |
|
photo: DS.attr('string') |
|
}); |
|
|
|
App.Router.reopen({ |
|
rootURL: '/admin/', |
|
location: 'auto' |
|
}); |
|
|
|
App.Router.map(function() { |
|
this.resource('users', function() { |
|
return this.route('show', { |
|
path: ':user_id' |
|
}); |
|
}); |
|
}); |
|
|
|
App.ApplicationRoute = Ember.Route.extend({ |
|
actions: { |
|
error: function(err) { |
|
return console.log(err); |
|
} |
|
} |
|
}); |
|
|
|
App.UsersIndexRoute = Ember.Route.extend(Ember.FilterableRouteMixin, Ember.PageableRouteMixin, { |
|
modelName: 'user' |
|
}); |
|
|
|
App.UsersShowRoute = Ember.Route.extend({ |
|
model: function(params) { |
|
return this.get('store').find('user', params.user_id); |
|
} |
|
}); |
|
|
|
App.UsersIndexController = Ember.ArrayController.extend(Ember.PageableControllerMixin, { |
|
users: (function() { |
|
return this.get('content'); |
|
}).property('content') |
|
}); |
|
|
|
App.PaginatorLinksComponent = Ember.Component.extend({ |
|
layoutName: 'components/paginator-links/paginator-links', |
|
linksCount: 5, |
|
pages: [], |
|
next: 1, |
|
prev: 1, |
|
calculate: (function() { |
|
var current, i, pageEnd, pagesStart, total, _i, _results; |
|
current = this.get('currentPage'); |
|
total = this.get('totalPages'); |
|
this.next = current < total ? current + 1 : 1; |
|
this.prev = current > 1 ? current - 1 : total; |
|
pagesStart = current - this.linksCount; |
|
if (pagesStart < 1) { |
|
pagesStart = 1; |
|
} |
|
pageEnd = current + this.linksCount; |
|
if (pageEnd > total) { |
|
pageEnd = total; |
|
} |
|
this.pages = []; |
|
_results = []; |
|
for (i = _i = pagesStart; pagesStart <= pageEnd ? _i <= pageEnd : _i >= pageEnd; i = pagesStart <= pageEnd ? ++_i : --_i) { |
|
_results.push(this.pages.push({ |
|
page: i, |
|
active: i === current |
|
})); |
|
} |
|
return _results; |
|
}).on('init'), |
|
contentDidChange: (function() { |
|
this.calculate(); |
|
return this.rerender(); |
|
}).observes('currentPage', 'totalPages'), |
|
didInsertElement: function() { |
|
if (this.get('totalPages') <= 1) { |
|
return this.$().hide(); |
|
} else { |
|
$().show(); |
|
if (this.get('currentPage') < 2) { |
|
this.$().find('.prev').addClass('disabled'); |
|
} |
|
if (this.get('currentPage') === this.get('totalPages')) { |
|
return this.$().find('.next').addClass('disabled'); |
|
} |
|
} |
|
} |
|
}); |
|
|
|
App.TableFilterableComponent = Ember.Component.extend(Ember.TargetActionSupport, { |
|
layoutName: 'components/table-filterable/table-filterable', |
|
title: ' ', |
|
searchType: '~', |
|
columns: [ |
|
{ |
|
column: 'id', |
|
name: '#' |
|
} |
|
], |
|
showFilters: function() { |
|
var $filters, $tbody; |
|
$filters = this.$().find('.filters input'); |
|
$tbody = this.$().find('.table tbody'); |
|
if ($filters.prop('disabled') === true) { |
|
$filters.prop('disabled', false); |
|
return $filters.first().focus(); |
|
} else { |
|
$filters.val('').prop('disabled', true); |
|
$tbody.find('.no-result').remove(); |
|
return $tbody.find('tr').show(); |
|
} |
|
}, |
|
runFilter: function() { |
|
var $filters, actionContext, self; |
|
self = this; |
|
$filters = this.$().find('.filters input'); |
|
actionContext = {}; |
|
$filters.each(function() { |
|
var column, name, searchType, val; |
|
name = $(this).attr('name'); |
|
val = $(this).val(); |
|
if (val.length === 0) { |
|
return; |
|
} |
|
column = self.columns.filter(function(e) { |
|
return e.column === name; |
|
}); |
|
if (column[0] && column[0].searchType) { |
|
searchType = column[0].searchType; |
|
} else { |
|
searchType = self.searchType; |
|
} |
|
return actionContext[name] = searchType + val; |
|
}); |
|
return this.triggerAction({ |
|
action: 'filterModel', |
|
actionContext: actionContext |
|
}); |
|
}, |
|
didInsertElement: function() { |
|
var self; |
|
self = this; |
|
return this.$().each(function() { |
|
var $filters, $widget; |
|
$widget = $(this); |
|
$filters = $widget.find('.filters input'); |
|
$widget.find('.btn-filter').on('click', function() { |
|
return self.showFilters(); |
|
}); |
|
return $filters.on('keyup', $.debounce(250, function() { |
|
return self.runFilter(); |
|
})); |
|
}); |
|
} |
|
}); |
|
|
|
App.UsersTableComponent = App.TableFilterableComponent.extend({ |
|
title: 'Список пользователей', |
|
linkTo: 'users.show', |
|
columns: [ |
|
{ |
|
column: 'id', |
|
name: '#', |
|
linked: true |
|
}, { |
|
column: 'first_name', |
|
name: 'Имя' |
|
}, { |
|
column: 'last_name', |
|
name: 'Фамилия' |
|
}, { |
|
column: 'email', |
|
name: 'email' |
|
}, { |
|
column: 'group', |
|
name: 'Группа', |
|
searchType: '=' |
|
} |
|
] |
|
}); |
|
|
|
}).call(this); |