1
0
mirror of https://github.com/keeweb/keeweb.git synced 2024-06-20 06:56:40 +02:00
keeweb/app/scripts/views/menu/menu-item-view.js

131 lines
4.0 KiB
JavaScript
Raw Normal View History

2015-10-17 23:49:24 +02:00
'use strict';
var Backbone = require('backbone'),
KeyHandler = require('../../comp/key-handler'),
2015-10-17 23:49:24 +02:00
Keys = require('../../const/keys'),
Alerts = require('../../comp/alerts');
2015-10-17 23:49:24 +02:00
var MenuItemView = Backbone.View.extend({
template: require('templates/menu/menu-item.html'),
events: {
'mouseover': 'mouseover',
'mouseout': 'mouseout',
'click .menu__item-option': 'selectOption',
'click': 'selectItem',
'dblclick': 'expandItem'
},
iconEl: null,
itemViews: null,
initialize: function () {
this.itemViews = [];
this.listenTo(this.model, 'change:active', this.changeActive);
this.listenTo(this.model, 'change:expanded', this.changeExpanded);
this.listenTo(this.model, 'change:cls', this.changeCls);
var shortcut = this.model.get('shortcut');
if (shortcut) {
KeyHandler.onKey(shortcut, this.selectItem, this, KeyHandler.SHORTCUT_OPT);
if (shortcut !== Keys.DOM_VK_C) {
KeyHandler.onKey(shortcut, this.selectItem, this, KeyHandler.SHORTCUT_ACTION);
}
}
},
render: function() {
this.removeInnerViews();
this.renderTemplate(this.model.attributes);
this.iconEl = this.$el.find('i');
var items = this.model.get('items');
if (items && this.model.get('expanded')) {
items.forEach(function (item) {
if (item.get('visible')) {
var itemView = new MenuItemView({el: this.$el, model: item});
itemView.listenTo(itemView, 'select', this.itemSelected);
itemView.render();
this.itemViews.push(itemView);
}
}, this);
}
},
remove : function() {
this.removeInnerViews();
var shortcut = this.model.get('shortcut');
if (shortcut) {
KeyHandler.offKey(shortcut, this.selectItem, this, KeyHandler.SHORTCUT_OPT);
if (shortcut !== Keys.DOM_VK_C) {
KeyHandler.offKey(shortcut, this.selectItem, this, KeyHandler.SHORTCUT_ACTION);
}
}
Backbone.View.prototype.remove.apply(this, arguments);
},
removeInnerViews: function() {
this.itemViews.forEach(function(itemView) { itemView.remove(); });
this.itemViews = [];
},
changeActive: function(model, active) {
this.$el.toggleClass('menu__item--active', active);
},
changeExpanded: function(model, expanded) {
this.$el.toggleClass('menu__item--collapsed', !expanded);
},
changeCls: function(model, cls) {
var oldCls = model.previousAttributes().cls;
if (oldCls) {
this.$el.removeClass(oldCls);
}
this.$el.addClass(cls);
},
mouseover: function(e) {
this.$el.addClass('menu__item--hover');
e.stopPropagation();
},
mouseout: function(e) {
this.$el.removeClass('menu__item--hover');
e.stopPropagation();
},
selectItem: function(e) {
2015-10-23 22:12:12 +02:00
e.stopPropagation();
e.preventDefault();
2015-10-17 23:49:24 +02:00
if (this.model.get('active')) {
return;
}
if (this.model.get('disabled')) {
Alerts.info(this.model.get('disabled'));
} else {
Backbone.trigger('menu-select', { item: this.model });
}
},
selectOption: function(e) {
var options = this.model.get('options');
var value = $(e.target).data('value');
if (options && options.length) {
var option = options.find(function(op) { return op.get('value') === value; });
if (option) {
Backbone.trigger('menu-select', { item: this.model, option: option });
}
}
e.stopImmediatePropagation();
e.preventDefault();
},
expandItem: function(e) {
if (this.model.toggleExpanded) {
this.model.toggleExpanded();
}
e.stopPropagation();
}
});
module.exports = MenuItemView;