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

218 lines
6.4 KiB
JavaScript
Raw Normal View History

2015-10-17 23:49:24 +02:00
'use strict';
var MenuItemModel = require('./menu/menu-item-model'),
EntryModel = require('../models/entry-model'),
IconMap = require('../const/icon-map'),
2015-11-21 21:14:21 +01:00
IconUrl = require('../util/icon-url'),
kdbxweb = require('kdbxweb'),
KdbxIcons = kdbxweb.Consts.Icons,
2015-10-17 23:49:24 +02:00
GroupCollection, EntryCollection;
var GroupModel = MenuItemModel.extend({
defaults: _.extend({}, MenuItemModel.prototype.defaults, {
iconId: 0,
entries: null,
2015-10-31 20:09:32 +01:00
filterKey: 'group',
editable: true,
2015-11-08 09:59:46 +01:00
top: false,
drag: true,
drop: true
2015-10-17 23:49:24 +02:00
}),
initialize: function() {
if (!GroupCollection) { GroupCollection = require('../collections/group-collection'); }
if (!EntryCollection) { EntryCollection = require('../collections/entry-collection'); }
this.set('entries', new EntryCollection());
},
setFromGroup: function(group, file) {
var isRecycleBin = file.db.meta.recycleBinUuid && file.db.meta.recycleBinUuid.id === group.uuid.id;
this.set({
id: group.uuid.id,
expanded: true,
visible: !isRecycleBin,
items: new GroupCollection(),
filterValue: group.uuid.id
}, { silent: true });
this.group = group;
this.file = file;
2015-10-31 20:09:32 +01:00
this._fillByGroup(true);
2015-10-17 23:49:24 +02:00
var items = this.get('items'),
entries = this.get('entries');
group.groups.forEach(function(subGroup) {
2015-10-31 20:09:32 +01:00
items.add(GroupModel.fromGroup(subGroup, file, this));
}, this);
2015-10-17 23:49:24 +02:00
group.entries.forEach(function(entry) {
entries.add(EntryModel.fromEntry(entry, this, file));
}, this);
},
2015-10-31 20:09:32 +01:00
_fillByGroup: function(silent) {
this.set({
title: this.group.name,
iconId: this.group.icon,
2015-11-21 21:14:21 +01:00
icon: this._iconFromId(this.group.icon),
2015-11-21 23:15:51 +01:00
customIcon: this._buildCustomIcon(),
customIconId: this.group.customIcon ? this.group.customIcon.toString() : null
2015-10-31 20:09:32 +01:00
}, { silent: silent });
},
2015-10-17 23:49:24 +02:00
_iconFromId: function(id) {
if (id === KdbxIcons.Folder || id === KdbxIcons.FolderOpen) {
return undefined;
}
return IconMap[id];
},
2015-11-21 21:14:21 +01:00
_buildCustomIcon: function() {
this.customIcon = null;
if (this.group.customIcon) {
return IconUrl.toDataUrl(this.file.db.meta.customIcons[this.group.customIcon]);
}
return null;
},
2015-10-31 20:09:32 +01:00
_groupModified: function() {
this.file.setModified();
if (this.isJustCreated) {
this.isJustCreated = false;
}
this.group.times.update();
},
2015-10-17 23:49:24 +02:00
forEachGroup: function(callback, includeDisabled) {
var result = true;
this.get('items').forEach(function(group) {
if (includeDisabled || group.group.enableSearching !== false) {
result = callback(group) !== false && group.forEachGroup(callback, includeDisabled) !== false;
}
});
return result;
},
forEachOwnEntry: function(filter, callback) {
this.get('entries').forEach(function(entry) {
if (entry.matches(filter)) {
callback(entry, this);
}
});
},
2015-11-08 22:25:00 +01:00
getOwnSubGroups: function() {
return this.group.groups;
},
2015-10-17 23:49:24 +02:00
removeEntry: function(entry) {
this.get('entries').remove(entry);
},
addEntry: function(entry) {
this.get('entries').add(entry);
2015-10-31 20:09:32 +01:00
},
removeGroup: function(group) {
this.get('items').remove(group);
this.trigger('remove', group);
2015-10-31 20:09:32 +01:00
},
addGroup: function(group) {
this.get('items').add(group);
this.trigger('insert', group);
},
setName: function(name) {
this._groupModified();
this.group.name = name;
this._fillByGroup();
},
setIcon: function(iconId) {
this._groupModified();
this.group.icon = iconId;
2015-11-21 23:15:51 +01:00
this.group.customIcon = undefined;
this._fillByGroup();
},
setCustomIcon: function(customIconId) {
this._groupModified();
this.group.customIcon = new kdbxweb.KdbxUuid(customIconId);
2015-10-31 20:09:32 +01:00
this._fillByGroup();
},
moveToTrash: function() {
this.file.setModified();
2015-11-04 09:06:49 +01:00
this.file.db.remove(this.group);
2015-10-31 20:09:32 +01:00
this.parentGroup.removeGroup(this);
var trashGroup = this.file.getTrashGroup();
if (trashGroup) {
2015-11-08 22:25:00 +01:00
trashGroup.addGroup(this);
2015-10-31 20:09:32 +01:00
this.parentGroup = trashGroup;
}
this.trigger('delete');
},
2015-11-09 19:15:39 +01:00
deleteFromTrash: function() {
this.file.db.move(this.group, null);
this.parentGroup.removeGroup(this);
},
2015-10-31 20:09:32 +01:00
removeWithoutHistory: function() {
var ix = this.parentGroup.group.groups.indexOf(this.group);
if (ix >= 0) {
this.parentGroup.group.groups.splice(ix, 1);
}
this.parentGroup.removeGroup(this);
this.trigger('delete');
2015-11-08 09:59:46 +01:00
},
moveHere: function(object) {
2015-11-08 13:09:07 +01:00
if (!object || object.id === this.id || object.file !== this.file) {
2015-11-08 09:59:46 +01:00
return;
}
2015-11-19 06:55:45 +01:00
this.file.setModified();
2015-11-08 09:59:46 +01:00
if (object instanceof GroupModel) {
if (this.group.groups.indexOf(object.group) >= 0) {
return;
}
this.file.db.move(object.group, this.group);
object.parentGroup.removeGroup(object);
object.trigger('delete');
this.addGroup(object);
} else if (object instanceof EntryModel) {
if (this.group.entries.indexOf(object.entry) >= 0) {
return;
}
this.file.db.move(object.entry, this.group);
object.group.removeEntry(object);
this.addEntry(object);
2015-11-19 22:38:18 +01:00
object.group = this;
2015-11-08 09:59:46 +01:00
}
2015-10-17 23:49:24 +02:00
}
});
2015-10-31 20:09:32 +01:00
GroupModel.fromGroup = function(group, file, parentGroup) {
2015-10-17 23:49:24 +02:00
var model = new GroupModel();
model.setFromGroup(group, file);
2015-10-31 20:09:32 +01:00
if (parentGroup) {
model.parentGroup = parentGroup;
} else {
2015-11-08 09:59:46 +01:00
model.set({ top: true, drag: false }, { silent: true });
2015-10-31 20:09:32 +01:00
}
return model;
};
GroupModel.newGroup = function(group, file) {
var model = new GroupModel();
var grp = file.db.createGroup(group.group);
model.setFromGroup(grp, file);
model.group.times.update();
model.parentGroup = group;
model.unsaved = true;
model.isJustCreated = true;
group.addGroup(model);
file.setModified();
2015-10-17 23:49:24 +02:00
return model;
};
module.exports = GroupModel;