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

224 lines
7.5 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import Backbone from 'backbone';
import { MenuSectionCollection } from 'collections/menu/menu-section-collection';
import { Colors } from 'const/colors';
import { Keys } from 'const/keys';
import { GroupsMenuModel } from 'models/menu/groups-menu-model';
import { MenuSectionModel } from 'models/menu/menu-section-model';
import { StringFormat } from 'util/formatting/string-format';
import { Locale } from 'util/locale';
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,
2019-08-18 10:17:09 +02:00
initialize() {
2015-10-17 23:49:24 +02:00
this.menus = {};
2019-08-16 23:05:39 +02:00
this.allItemsSection = new MenuSectionModel([
2019-08-18 08:05:38 +02:00
{
locTitle: 'menuAllItems',
icon: 'th-large',
active: true,
shortcut: Keys.DOM_VK_A,
filterKey: '*'
}
2019-08-16 23:05:39 +02:00
]);
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();
2019-08-16 23:05:39 +02:00
this.colorsSection = new MenuSectionModel([
{
locTitle: 'menuColors',
icon: 'bookmark',
shortcut: Keys.DOM_VK_C,
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;
2019-08-16 23:05:39 +02:00
this.trashSection = new MenuSectionModel([
{
locTitle: 'menuTrash',
icon: 'trash',
shortcut: Keys.DOM_VK_D,
filterKey: 'trash',
filterValue: true,
drop: true
}
]);
2016-07-17 13:30:38 +02:00
Colors.AllColors.forEach(color => {
2019-08-18 08:09:42 +02:00
const option = {
cls: 'fa ' + color + '-color',
value: color,
filterValue: color
};
this.colorsSection.get('items').models[0].addOption(option);
2016-07-17 13:30:38 +02:00
});
2015-10-17 23:49:24 +02:00
this.menus.app = new MenuSectionCollection([
this.allItemsSection,
this.colorsSection,
this.tagsSection,
this.groupsSection,
this.trashSection
]);
2019-08-16 23:05:39 +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' }
]);
2019-08-18 08:05:38 +02:00
this.pluginsSection = new MenuSectionModel([
{ locTitle: 'plugins', icon: 'puzzle-piece', page: 'plugins' }
]);
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
},
2019-08-18 10:17:09 +02:00
select(sel) {
2017-01-31 07:50:28 +01:00
const sections = this.get('sections');
2019-08-16 23:05:39 +02:00
sections.forEach(function(section) {
this._select(section, sel.item);
}, this);
2015-10-17 23:49:24 +02:00
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));
2019-08-18 08:05:38 +02: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) {
2019-08-18 08:05:38 +02:00
Backbone.trigger('set-page', {
page: sel.item.get('page'),
file: sel.item.get('file')
});
2015-10-17 23:49:24 +02:00
}
},
2019-08-18 10:17:09 +02:00
_selectPrevious() {
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) {
2019-08-16 23:05:39 +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));
},
2019-08-18 10:17:09 +02:00
_selectNext() {
let activeItem = null;
2018-10-22 22:03:51 +02:00
const processSection = section => {
if (section.has('visible') && !section.get('visible')) {
return true;
}
2019-08-16 23:05:39 +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));
},
2019-08-18 10:17:09 +02:00
_select(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);
}
},
2019-08-18 10:17:09 +02:00
_setLocale() {
2016-08-24 21:18:18 +02:00
[this.menus.app, this.menus.settings].forEach(menu => {
2019-08-16 23:05:39 +02:00
menu.each(section =>
section.get('items').each(item => {
if (item.get('locTitle')) {
2019-09-15 08:11:11 +02:00
item.set('title', StringFormat.capFirst(Locale[item.get('locTitle')]));
2019-08-16 23:05:39 +02:00
}
})
);
2016-08-24 21:18:18 +02:00
});
2016-08-24 23:05:08 +02:00
this.tagsSection.defaultItems[0] = this._getDefaultTagItem();
},
2019-08-18 10:17:09 +02:00
_getDefaultTagItem() {
2019-08-16 23:05:39 +02:00
return {
2019-09-15 08:11:11 +02:00
title: StringFormat.capFirst(Locale.tags),
2019-08-16 23:05:39 +02:00
icon: 'tags',
defaultItem: true,
2019-08-18 08:05:38 +02:00
disabled: {
header: Locale.menuAlertNoTags,
body: Locale.menuAlertNoTagsBody,
icon: 'tags'
}
2019-08-16 23:05:39 +02:00
};
2016-08-24 21:18:18 +02:00
},
2019-08-18 10:17:09 +02:00
setMenu(type) {
2015-10-17 23:49:24 +02:00
this.set('sections', this.menus[type]);
}
});
2019-09-15 14:16:32 +02:00
export { MenuModel };