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

169 lines
6.8 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const Backbone = require('backbone');
const MenuSectionCollection = require('../../collections/menu/menu-section-collection');
const MenuSectionModel = require('./menu-section-model');
const GroupsMenuModel = require('./groups-menu-model');
const Locale = require('../../util/locale');
const Format = require('../../util/format');
const Keys = require('../../const/keys');
const Colors = require('../../const/colors');
2015-10-17 23:49:24 +02:00
2017-01-31 07:50:28 +01:00
const MenuModel = Backbone.Model.extend({
2015-10-17 23:49:24 +02:00
defaults: {
sections: null
},
menus: null,
initialize: function() {
this.menus = {};
2016-08-24 21:18:18 +02:00
this.allItemsSection = new MenuSectionModel([{ locTitle: 'menuAllItems', icon: 'th-large', active: true,
2015-12-17 19:25:25 +01:00
shortcut: Keys.DOM_VK_A, filterKey: '*' }]);
2016-07-16 12:01:19 +02:00
this.allItemsItem = this.allItemsSection.get('items').models[0];
2015-10-17 23:49:24 +02:00
this.groupsSection = new GroupsMenuModel();
2016-08-24 21:18:18 +02:00
this.colorsSection = new MenuSectionModel([{ locTitle: 'menuColors', icon: 'bookmark', shortcut: Keys.DOM_VK_C,
2015-12-17 19:25:25 +01:00
cls: 'menu__item-colors', filterKey: 'color', filterValue: true }]);
2015-10-17 23:49:24 +02:00
this.colorsItem = this.colorsSection.get('items').models[0];
2017-01-31 07:50:28 +01:00
const defTags = [this._getDefaultTagItem()];
2015-10-17 23:49:24 +02:00
this.tagsSection = new MenuSectionModel(defTags);
this.tagsSection.set({ scrollable: true, drag: true });
this.tagsSection.defaultItems = defTags;
2016-08-24 21:18:18 +02:00
this.trashSection = new MenuSectionModel([{ locTitle: 'menuTrash', icon: 'trash', shortcut: Keys.DOM_VK_D,
2015-11-15 21:26:37 +01:00
filterKey: 'trash', filterValue: true, drop: true }]);
2016-07-17 13:30:38 +02:00
Colors.AllColors.forEach(color => {
this.colorsSection.get('items').models[0]
.addOption({ cls: 'fa ' + color + '-color', value: color, filterValue: color });
});
2015-10-17 23:49:24 +02:00
this.menus.app = new MenuSectionCollection([
this.allItemsSection,
this.colorsSection,
this.tagsSection,
this.groupsSection,
this.trashSection
]);
2016-08-24 21:18:18 +02:00
this.generalSection = new MenuSectionModel([{ locTitle: 'menuSetGeneral', icon: 'cog', page: 'general', active: true }]);
this.shortcutsSection = new MenuSectionModel([{ locTitle: 'shortcuts', icon: 'keyboard-o', page: 'shortcuts' }]);
2017-02-18 23:46:59 +01:00
this.pluginsSection = new MenuSectionModel([{ locTitle: 'plugins', icon: 'puzzle-piece', page: 'plugins' }]);
2016-08-24 21:18:18 +02:00
this.aboutSection = new MenuSectionModel([{ locTitle: 'menuSetAbout', icon: 'info', page: 'about' }]);
this.helpSection = new MenuSectionModel([{ locTitle: 'help', icon: 'question', page: 'help' }]);
2015-10-17 23:49:24 +02:00
this.filesSection = new MenuSectionModel();
this.filesSection.set({ scrollable: true, grow: true });
this.menus.settings = new MenuSectionCollection([
this.generalSection,
this.shortcutsSection,
2017-02-18 23:46:59 +01:00
this.pluginsSection,
2015-10-17 23:49:24 +02:00
this.aboutSection,
2015-10-22 20:03:44 +02:00
this.helpSection,
2015-10-17 23:49:24 +02:00
this.filesSection
]);
this.set('sections', this.menus.app);
2016-08-24 21:18:18 +02:00
this.listenTo(Backbone, 'set-locale', this._setLocale);
this.listenTo(Backbone, 'select-next-menu-item', this._selectNext);
this.listenTo(Backbone, 'select-previous-menu-item', this._selectPrevious);
2016-08-24 21:18:18 +02:00
this._setLocale();
2015-10-17 23:49:24 +02:00
},
select: function(sel) {
2017-01-31 07:50:28 +01:00
const sections = this.get('sections');
2015-10-17 23:49:24 +02:00
sections.forEach(function(section) { this._select(section, sel.item); }, this);
if (sections === this.menus.app) {
2016-07-17 13:30:38 +02:00
this.colorsItem.get('options').forEach(opt => opt.set('active', opt === sel.option));
2017-01-31 07:50:28 +01:00
const selColor = sel.item === this.colorsItem && sel.option ? sel.option.get('value') + '-color' : '';
2015-10-17 23:49:24 +02:00
this.colorsItem.set('cls', 'menu__item-colors ' + selColor);
2017-01-31 07:50:28 +01:00
const filterKey = sel.item.get('filterKey');
const filterValue = (sel.option || sel.item).get('filterValue');
const filter = {};
2015-10-17 23:49:24 +02:00
filter[filterKey] = filterValue;
Backbone.trigger('set-filter', filter);
} else if (sections === this.menus.settings) {
Backbone.trigger('set-page', { page: sel.item.get('page'), file: sel.item.get('file') });
}
},
_selectPrevious: function() {
let previousItem = null;
2018-10-22 22:03:51 +02:00
const processSection = section => {
if (section.has('visible') && !section.get('visible')) {
return true;
}
2018-10-22 22:03:51 +02:00
if (section.has('active')) {
previousItem = section;
}
const items = section.get('items');
if (items) {
2018-10-22 22:03:51 +02:00
items.forEach(it => {
if (it.get('active') && previousItem) {
2018-10-22 22:03:51 +02:00
this.select({item: previousItem});
return false;
}
return processSection(it);
2018-10-22 22:03:51 +02:00
});
}
2018-10-22 22:03:51 +02:00
};
const sections = this.get('sections');
2018-10-22 22:03:51 +02:00
sections.forEach(section => processSection(section));
},
_selectNext: function() {
let activeItem = null;
2018-10-22 22:03:51 +02:00
const processSection = section => {
if (section.has('visible') && !section.get('visible')) {
return true;
}
2018-10-22 22:03:51 +02:00
if (section.has('active') && activeItem && (section !== activeItem)) {
this.select({item: section});
activeItem = null;
return false;
}
const items = section.get('items');
if (items) {
2018-10-22 22:03:51 +02:00
items.forEach(it => {
if (it.get('active')) {
activeItem = it;
}
return processSection(it);
2018-10-22 22:03:51 +02:00
});
}
2018-10-22 22:03:51 +02:00
};
const sections = this.get('sections');
2018-10-22 22:03:51 +02:00
sections.forEach(section => processSection(section));
},
2015-10-17 23:49:24 +02:00
_select: function(item, selectedItem) {
2017-01-31 07:50:28 +01:00
const items = item.get('items');
2015-10-17 23:49:24 +02:00
if (items) {
items.forEach(function(it) {
it.set('active', it === selectedItem);
this._select(it, selectedItem);
}, this);
}
},
2016-08-24 21:18:18 +02:00
_setLocale: function() {
[this.menus.app, this.menus.settings].forEach(menu => {
menu.each(section => section.get('items').each(item => {
if (item.get('locTitle')) {
2016-08-24 21:19:41 +02:00
item.set('title', Format.capFirst(Locale[item.get('locTitle')]));
2016-08-24 21:18:18 +02:00
}
}));
});
2016-08-24 23:05:08 +02:00
this.tagsSection.defaultItems[0] = this._getDefaultTagItem();
},
_getDefaultTagItem: function() {
return { title: Format.capFirst(Locale.tags), icon: 'tags', defaultItem: true,
disabled: { header: Locale.menuAlertNoTags, body: Locale.menuAlertNoTagsBody, icon: 'tags' } };
2016-08-24 21:18:18 +02:00
},
2015-10-17 23:49:24 +02:00
setMenu: function(type) {
this.set('sections', this.menus[type]);
}
});
module.exports = MenuModel;