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

174 lines
5.2 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);
},
addFile: function(file) {
this.files.add(file);
file.get('groups').forEach(function(group) { this.menu.groupsSection.addItem(group); }, this);
this._addTags(file.db);
this._tagsChanged();
this.menu.filesSection.addItem({
icon: 'lock',
title: file.get('name'),
page: 'file',
file: file
});
2015-10-24 21:06:44 +02:00
if (file.get('path')) {
AppSettingsModel.instance.set('lastOpenFile', file.get('path'));
}
2015-10-17 23:49:24 +02:00
this.refresh();
},
_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() {
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({});
},
setFilter: function(filter) {
this.filter = filter;
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);
if (entries.length) {
entries.setActive(entries.first());
}
return entries;
},
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;