2013-05-01 05:02:33 +03:00
|
|
|
"use strict";
|
|
|
|
|
2013-05-17 06:47:28 +03:00
|
|
|
Backgrid.Column.prototype.defaults = {
|
2013-06-07 04:29:54 +03:00
|
|
|
name : undefined,
|
|
|
|
label : undefined,
|
|
|
|
sortable : true,
|
|
|
|
editable : false,
|
2013-05-17 06:47:28 +03:00
|
|
|
renderable: true,
|
2013-06-07 04:29:54 +03:00
|
|
|
formatter : undefined,
|
|
|
|
cell : undefined,
|
2013-05-17 06:47:28 +03:00
|
|
|
headerCell: 'nzbDrone'
|
|
|
|
};
|
|
|
|
|
2013-04-23 09:06:55 +03:00
|
|
|
|
2013-04-23 10:11:52 +03:00
|
|
|
Backgrid.NzbDroneHeaderCell = Backgrid.HeaderCell.extend({
|
|
|
|
events: {
|
|
|
|
'click': 'onClick'
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function () {
|
|
|
|
this.$el.empty();
|
|
|
|
this.$el.append(this.column.get("label"));
|
|
|
|
|
|
|
|
if (this.column.get('sortable')) {
|
2013-06-08 03:46:57 +03:00
|
|
|
this.$el.addClass('clickable');
|
|
|
|
this.$el.append(" <i class='pull-right'></i>");
|
2013-05-02 10:09:35 +03:00
|
|
|
|
|
|
|
if (this.collection.state) {
|
|
|
|
var sortKey = this.collection.state.sortKey;
|
|
|
|
var sortDir = this._convertIntToDirection(this.collection.state.order);
|
|
|
|
|
|
|
|
if (sortKey === this.column.get('name')) {
|
|
|
|
this.$el.children('i').addClass(this._convertDirectionToIcon(sortDir));
|
|
|
|
this._direction = sortDir;
|
|
|
|
}
|
|
|
|
}
|
2013-04-23 10:11:52 +03:00
|
|
|
}
|
|
|
|
this.delegateEvents();
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
direction: function (dir) {
|
|
|
|
if (arguments.length) {
|
2013-06-07 04:29:54 +03:00
|
|
|
if (this._direction) {
|
|
|
|
this.$el.children('i').removeClass(this._convertDirectionToIcon(this._direction));
|
|
|
|
}
|
|
|
|
if (dir) {
|
|
|
|
this.$el.children('i').addClass(this._convertDirectionToIcon(dir));
|
|
|
|
}
|
2013-04-23 10:11:52 +03:00
|
|
|
this._direction = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._direction;
|
|
|
|
},
|
|
|
|
|
2013-05-02 08:50:34 +03:00
|
|
|
onClick: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var columnName = this.column.get("name");
|
|
|
|
|
|
|
|
if (this.column.get("sortable")) {
|
|
|
|
if (this.direction() === "ascending") {
|
|
|
|
this.sort(columnName, "descending", function (left, right) {
|
|
|
|
var leftVal = left.get(columnName);
|
|
|
|
var rightVal = right.get(columnName);
|
|
|
|
if (leftVal === rightVal) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-07 04:29:54 +03:00
|
|
|
else if (leftVal > rightVal) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-02 08:50:34 +03:00
|
|
|
return 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.sort(columnName, "ascending", function (left, right) {
|
|
|
|
var leftVal = left.get(columnName);
|
|
|
|
var rightVal = right.get(columnName);
|
|
|
|
if (leftVal === rightVal) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-07 04:29:54 +03:00
|
|
|
else if (leftVal < rightVal) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-02 08:50:34 +03:00
|
|
|
return 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-04-23 10:11:52 +03:00
|
|
|
_convertDirectionToIcon: function (dir) {
|
|
|
|
if (dir === 'ascending') {
|
|
|
|
return 'icon-sort-up';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'icon-sort-down';
|
2013-05-02 10:09:35 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_convertIntToDirection: function (dir) {
|
|
|
|
if (dir === '-1') {
|
|
|
|
return 'ascending';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'descending';
|
2013-04-23 10:11:52 +03:00
|
|
|
}
|
2013-05-02 08:50:34 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
Backgrid.NzbDronePaginator = Backgrid.Extension.Paginator.extend({
|
|
|
|
|
2013-06-08 03:31:08 +03:00
|
|
|
template: 'Shared/BackgridPaginatorTemplate',
|
|
|
|
|
2013-05-02 08:50:34 +03:00
|
|
|
events: {
|
2013-06-08 03:31:08 +03:00
|
|
|
"click .pager-btn": "changePage"
|
2013-05-02 08:50:34 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
windowSize: 1,
|
|
|
|
|
|
|
|
fastForwardHandleLabels: {
|
2013-06-08 03:31:08 +03:00
|
|
|
first: 'icon-fast-backward',
|
|
|
|
prev : 'icon-backward',
|
|
|
|
next : 'icon-forward',
|
|
|
|
last : 'icon-fast-forward'
|
2013-05-02 08:50:34 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
changePage: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var target = $(e.target);
|
|
|
|
|
|
|
|
if (target.closest('li').hasClass('disabled')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-08 03:31:08 +03:00
|
|
|
var label = target.attr('data-action');
|
2013-05-02 08:50:34 +03:00
|
|
|
var ffLabels = this.fastForwardHandleLabels;
|
|
|
|
|
|
|
|
var collection = this.collection;
|
|
|
|
|
|
|
|
if (ffLabels) {
|
|
|
|
switch (label) {
|
2013-06-08 03:31:08 +03:00
|
|
|
case 'first':
|
2013-05-02 08:50:34 +03:00
|
|
|
collection.getFirstPage();
|
|
|
|
return;
|
2013-06-08 03:31:08 +03:00
|
|
|
case 'prev':
|
2013-05-02 08:50:34 +03:00
|
|
|
if (collection.hasPrevious()) {
|
|
|
|
collection.getPreviousPage();
|
|
|
|
}
|
|
|
|
return;
|
2013-06-08 03:31:08 +03:00
|
|
|
case 'next':
|
2013-05-02 08:50:34 +03:00
|
|
|
if (collection.hasNext()) {
|
|
|
|
collection.getNextPage();
|
|
|
|
}
|
|
|
|
return;
|
2013-06-08 03:31:08 +03:00
|
|
|
case 'last':
|
2013-05-02 08:50:34 +03:00
|
|
|
collection.getLastPage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var state = collection.state;
|
|
|
|
var pageIndex = $(e.target).text() * 1;
|
2013-06-07 04:29:54 +03:00
|
|
|
collection.getPage(state.firstPage === 0 ? pageIndex - 1 :pageIndex);
|
2013-05-02 08:50:34 +03:00
|
|
|
},
|
|
|
|
|
2013-06-08 03:31:08 +03:00
|
|
|
makeHandles: function () {
|
|
|
|
|
|
|
|
var handles = [];
|
|
|
|
var collection = this.collection;
|
|
|
|
var state = collection.state;
|
|
|
|
|
|
|
|
// convert all indices to 0-based here
|
|
|
|
var firstPage = state.firstPage;
|
|
|
|
var lastPage = +state.lastPage;
|
|
|
|
lastPage = Math.max(0, firstPage ? lastPage - 1 : lastPage);
|
|
|
|
var currentPage = Math.max(state.currentPage, state.firstPage);
|
|
|
|
currentPage = firstPage ? currentPage - 1 : currentPage;
|
|
|
|
var windowStart = Math.floor(currentPage / this.windowSize) * this.windowSize;
|
|
|
|
var windowEnd = Math.min(lastPage + 1, windowStart + this.windowSize);
|
|
|
|
|
|
|
|
if (collection.mode !== "infinite") {
|
|
|
|
for (var i = windowStart; i < windowEnd; i++) {
|
|
|
|
handles.push({
|
|
|
|
label: i + 1,
|
|
|
|
title: "No. " + (i + 1),
|
|
|
|
className: currentPage === i ? "active" : undefined,
|
|
|
|
pageNumber: i + 1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ffLabels = this.fastForwardHandleLabels;
|
|
|
|
if (ffLabels) {
|
|
|
|
|
|
|
|
if (ffLabels.prev) {
|
|
|
|
handles.unshift({
|
|
|
|
label: ffLabels.prev,
|
|
|
|
className: collection.hasPrevious() ? void 0 : "disabled",
|
|
|
|
action: 'prev'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ffLabels.first) {
|
|
|
|
handles.unshift({
|
|
|
|
label: ffLabels.first,
|
|
|
|
className: collection.hasPrevious() ? void 0 : "disabled",
|
|
|
|
action: 'first'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ffLabels.next) {
|
|
|
|
handles.push({
|
|
|
|
label: ffLabels.next,
|
|
|
|
className: collection.hasNext() ? void 0 : "disabled",
|
|
|
|
action: 'next'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ffLabels.last) {
|
|
|
|
handles.push({
|
|
|
|
label: ffLabels.last,
|
|
|
|
className: collection.hasNext() ? void 0 : "disabled",
|
|
|
|
action: 'last'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handles;
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function () {
|
|
|
|
this.$el.empty();
|
|
|
|
|
|
|
|
var templateFunction = Marionette.TemplateCache.get(this.template);
|
|
|
|
|
|
|
|
this.$el.html(templateFunction({
|
|
|
|
handles: this.makeHandles()
|
|
|
|
}));
|
|
|
|
|
|
|
|
this.delegateEvents();
|
|
|
|
|
|
|
|
return this;
|
2013-05-02 08:50:34 +03:00
|
|
|
}
|
2013-06-07 04:29:54 +03:00
|
|
|
});
|