keeweb/app/scripts/models/app-model.js

219 lines
6.6 KiB
JavaScript
Raw Normal View History

2015-10-17 23:49:24 +02:00
'use strict';
var Backbone = require('backbone'),
AppSettingsModel = require('./app-settings-model'),
MenuModel = require('./menu/menu-model'),
EntryModel = require('./entry-model'),
2015-10-31 20:09:32 +01:00
GroupModel = require('./group-model'),
2015-10-17 23:49:24 +02:00
FileCollection = require('../collections/file-collection'),
EntryCollection = require('../collections/entry-collection');
var AppModel = Backbone.Model.extend({
defaults: {},
initialize: function() {
this.tags = [];
this.files = new FileCollection();
this.menu = new MenuModel();
this.filter = {};
this.sort = 'title';
this.settings = AppSettingsModel.instance;
this.listenTo(Backbone, 'refresh', this.refresh);
this.listenTo(Backbone, 'set-filter', this.setFilter);
this.listenTo(Backbone, 'add-filter', this.addFilter);
this.listenTo(Backbone, 'set-sort', this.setSort);
2015-11-07 21:37:54 +01:00
this.listenTo(Backbone, 'close-file', this.closeFile);
2015-11-08 22:25:00 +01:00
this.listenTo(Backbone, 'empty-trash', this.emptyTrash);
2015-10-17 23:49:24 +02:00
},
addFile: function(file) {
2015-12-02 21:50:31 +01:00
if (this.files.getById(file.get('id'))) {
return false;
}
2015-10-17 23:49:24 +02:00
this.files.add(file);
2015-12-02 21:50:31 +01:00
file.get('groups').forEach(function (group) {
this.menu.groupsSection.addItem(group);
}, this);
2015-10-17 23:49:24 +02:00
this._addTags(file.db);
this._tagsChanged();
this.menu.filesSection.addItem({
icon: 'lock',
title: file.get('name'),
page: 'file',
file: file
});
this.refresh();
2015-12-05 14:04:09 +01:00
this.listenTo(file, 'reload', this.reloadFile);
2015-12-02 21:50:31 +01:00
return true;
2015-10-17 23:49:24 +02:00
},
2015-12-05 14:04:09 +01:00
reloadFile: function(file) {
this.menu.groupsSection.removeByFile(file, true);
file.get('groups').forEach(function (group) {
this.menu.groupsSection.addItem(group);
}, this);
this.updateTags();
},
2015-10-17 23:49:24 +02:00
_addTags: function(group) {
var tagsHash = {};
this.tags.forEach(function(tag) {
tagsHash[tag.toLowerCase()] = true;
});
_.forEach(group.entries, function(entry) {
_.forEach(entry.tags, function(tag) {
if (!tagsHash[tag.toLowerCase()]) {
tagsHash[tag.toLowerCase()] = true;
this.tags.push(tag);
}
}, this);
}, this);
_.forEach(group.groups, function(subGroup) {
this._addTags(subGroup);
}, this);
this.tags.sort();
},
_tagsChanged: function() {
if (this.tags.length) {
this.menu.tagsSection.set('scrollable', true);
this.menu.tagsSection.setItems(this.tags.map(function (tag) {
return {title: tag, icon: 'tag', filterKey: 'tag', filterValue: tag};
}));
} else {
this.menu.tagsSection.set('scrollable', false);
this.menu.tagsSection.removeAllItems();
}
},
updateTags: function() {
var oldTags = this.tags.slice();
this.tags.splice(0, this.tags.length);
this.files.forEach(function(file) {
this._addTags(file.db);
}, this);
if (!_.isEqual(oldTags, this.tags)) {
this._tagsChanged();
}
},
closeAllFiles: function() {
2015-11-17 21:57:32 +01:00
this.files.each(function(file) { file.close(); });
2015-10-17 23:49:24 +02:00
this.files.reset();
this.menu.groupsSection.removeAllItems();
this.menu.tagsSection.set('scrollable', false);
this.menu.tagsSection.removeAllItems();
this.menu.filesSection.removeAllItems();
this.tags.splice(0, this.tags.length);
this.setFilter({});
},
2015-11-07 21:37:54 +01:00
closeFile: function(file) {
this.files.remove(file);
this._tagsChanged();
this.menu.groupsSection.removeByFile(file);
this.menu.filesSection.removeByFile(file);
this.refresh();
},
2015-11-08 22:25:00 +01:00
emptyTrash: function() {
this.files.forEach(function(file) {
file.emptyTrash();
}, this);
this.refresh();
},
2015-10-17 23:49:24 +02:00
setFilter: function(filter) {
this.filter = filter;
this.filter.subGroups = this.settings.get('expandGroups');
2015-10-17 23:49:24 +02:00
var entries = this.getEntries();
Backbone.trigger('filter', { filter: this.filter, sort: this.sort, entries: entries });
Backbone.trigger('select-entry', entries.length ? entries.first() : null);
},
refresh: function() {
this.setFilter(this.filter);
},
addFilter: function(filter) {
this.setFilter(_.extend(this.filter, filter));
},
setSort: function(sort) {
this.sort = sort;
this.setFilter(this.filter);
},
getEntries: function() {
var entries = new EntryCollection();
var filter = this.prepareFilter();
this.files.forEach(function(file) {
file.forEachEntry(filter, function(entry) {
entries.push(entry);
});
});
entries.sortEntries(this.sort);
2015-11-08 22:25:00 +01:00
if (this.filter.trash) {
this.addTrashGroups(entries);
}
2015-10-17 23:49:24 +02:00
if (entries.length) {
entries.setActive(entries.first());
}
return entries;
},
2015-11-08 22:25:00 +01:00
addTrashGroups: function(collection) {
this.files.forEach(function(file) {
var trashGroup = file.getTrashGroup();
if (trashGroup) {
trashGroup.getOwnSubGroups().forEach(function(group) {
collection.unshift(GroupModel.fromGroup(group, file, trashGroup));
});
}
});
},
2015-10-17 23:49:24 +02:00
prepareFilter: function() {
var filter = _.clone(this.filter);
if (filter.text) {
filter.textLower = filter.text.toLowerCase();
}
if (filter.tag) {
filter.tagLower = filter.tag.toLowerCase();
}
return filter;
},
2015-10-31 20:09:32 +01:00
getFirstSelectedGroup: function() {
2015-10-17 23:49:24 +02:00
var selGroupId = this.filter.group;
var file, group;
if (selGroupId) {
this.files.forEach(function(f) {
group = f.getGroup(selGroupId);
if (group) {
file = f;
return false;
}
}, this);
}
if (!group) {
file = this.files.first();
group = file.get('groups').first();
}
2015-10-31 20:09:32 +01:00
return { group: group, file: file };
},
createNewEntry: function() {
var sel = this.getFirstSelectedGroup();
return EntryModel.newEntry(sel.group, sel.file);
},
createNewGroup: function() {
var sel = this.getFirstSelectedGroup();
return GroupModel.newGroup(sel.group, sel.file);
2015-10-17 23:49:24 +02:00
}
});
module.exports = AppModel;