keeweb/app/scripts/collections/entry-collection.js

54 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import Backbone from 'backbone';
import { EntryModel } from 'models/entry-model';
import { Comparators } from 'util/data/comparators';
2015-10-17 23:49:24 +02:00
2017-01-31 07:50:28 +01:00
const EntryCollection = Backbone.Collection.extend({
2015-10-17 23:49:24 +02:00
model: EntryModel,
2017-05-07 19:45:53 +02:00
comparator: null,
2015-10-17 23:49:24 +02:00
comparators: {
2017-05-07 19:45:53 +02:00
'none': null,
2015-10-17 23:49:24 +02:00
'title': Comparators.stringComparator('title', true),
'-title': Comparators.stringComparator('title', false),
'website': Comparators.stringComparator('url', true),
'-website': Comparators.stringComparator('url', false),
'user': Comparators.stringComparator('user', true),
'-user': Comparators.stringComparator('user', false),
'created': Comparators.dateComparator('created', true),
'-created': Comparators.dateComparator('created', false),
'updated': Comparators.dateComparator('updated', true),
'-updated': Comparators.dateComparator('updated', false),
2019-08-18 10:17:09 +02:00
'-attachments'(x, y) {
2019-08-16 23:05:39 +02:00
return this.attachmentSortVal(x).localeCompare(this.attachmentSortVal(y));
},
2019-02-05 19:08:06 +01:00
'-rank': Comparators.rankComparator()
2015-10-17 23:49:24 +02:00
},
defaultComparator: 'title',
2019-02-05 19:08:06 +01:00
filter: null,
2019-08-18 10:17:09 +02:00
initialize(models, options) {
2019-08-16 23:05:39 +02:00
const comparatorName = (options && options.comparator) || this.defaultComparator;
2017-05-07 19:45:53 +02:00
this.comparator = this.comparators[comparatorName];
2015-10-17 23:49:24 +02:00
},
2019-08-18 10:17:09 +02:00
sortEntries(comparator, filter) {
2019-02-05 19:08:06 +01:00
this.filter = filter;
2015-10-17 23:49:24 +02:00
this.comparator = this.comparators[comparator] || this.comparators[this.defaultComparator];
this.sort();
},
2019-08-18 10:17:09 +02:00
attachmentSortVal(entry) {
2017-01-31 07:50:28 +01:00
const att = entry.attachments;
let str = att.length ? String.fromCharCode(64 + att.length) : 'Z';
2015-10-17 23:49:24 +02:00
if (att[0]) {
str += att[0].title;
}
return str;
}
});
2019-09-15 14:16:32 +02:00
export { EntryCollection };