You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/src/UI/Mixins/AsPersistedStateCollection.js

73 lines
2.1 KiB

9 years ago
var _ = require('underscore');
var Config = require('../Config');
module.exports = function() {
9 years ago
var originalInit = this.prototype.initialize;
this.prototype.initialize = function(options) {
9 years ago
options = options || {};
if (options.tableName) {
9 years ago
this.tableName = options.tableName;
}
if (!this.tableName && !options.tableName) {
9 years ago
throw 'tableName is required';
}
9 years ago
_setInitialState.call(this);
9 years ago
this.on('backgrid:sort', _storeStateFromBackgrid, this);
this.on('drone:sort', _storeState, this);
if (originalInit) {
9 years ago
originalInit.call(this, options);
}
};
if (!this.prototype._getSortMapping) {
this.prototype._getSortMapping = function(key) {
9 years ago
return {
name : key,
sortKey : key
};
};
}
var _setInitialState = function() {
9 years ago
var key = Config.getValue('{0}.sortKey'.format(this.tableName), this.state.sortKey);
var direction = Config.getValue('{0}.sortDirection'.format(this.tableName), this.state.order);
var order = parseInt(direction, 10);
9 years ago
this.state.sortKey = this._getSortMapping(key).sortKey;
this.state.order = order;
};
var _storeStateFromBackgrid = function(column, sortDirection) {
9 years ago
var order = _convertDirectionToInt(sortDirection);
var sortKey = this._getSortMapping(column.get('name')).sortKey;
9 years ago
Config.setValue('{0}.sortKey'.format(this.tableName), sortKey);
Config.setValue('{0}.sortDirection'.format(this.tableName), order);
};
var _storeState = function(sortModel, sortDirection) {
9 years ago
var order = _convertDirectionToInt(sortDirection);
var sortKey = this._getSortMapping(sortModel.get('name')).sortKey;
9 years ago
Config.setValue('{0}.sortKey'.format(this.tableName), sortKey);
Config.setValue('{0}.sortDirection'.format(this.tableName), order);
};
var _convertDirectionToInt = function(dir) {
if (dir === 'ascending') {
9 years ago
return '-1';
}
9 years ago
return '1';
};
9 years ago
return this;
};