keeweb/app/scripts/views/menu/menu-section-view.js

87 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const Backbone = require('backbone');
const MenuItemView = require('./menu-item-view');
const Resizable = require('../../mixins/resizable');
const Scrollable = require('../../mixins/scrollable');
const AppSettingsModel = require('../../models/app-settings-model');
2015-10-17 23:49:24 +02:00
2017-01-31 07:50:28 +01:00
const MenuSectionView = Backbone.View.extend({
2015-12-16 22:50:45 +01:00
template: require('templates/menu/menu-section.hbs'),
2015-10-17 23:49:24 +02:00
events: {},
itemViews: null,
minHeight: 55,
2019-08-16 23:05:39 +02:00
maxHeight: function() {
return this.$el.parent().height() - 116;
},
2015-10-17 23:49:24 +02:00
autoHeight: 'auto',
2019-08-16 23:05:39 +02:00
initialize: function() {
2015-10-17 23:49:24 +02:00
this.itemViews = [];
this.listenTo(this.model, 'change-items', this.itemsChanged);
2015-11-14 19:18:34 +01:00
this.listenTo(this, 'view-resize', this.viewResized);
2015-10-17 23:49:24 +02:00
},
render: function() {
if (!this.itemsEl) {
this.renderTemplate(this.model.attributes);
this.itemsEl = this.model.get('scrollable') ? this.$el.find('.scroller') : this.$el;
if (this.model.get('scrollable')) {
this.initScroll();
2016-01-17 21:19:42 +01:00
this.createScroll({
2015-10-17 23:49:24 +02:00
root: this.$el[0],
scroller: this.$el.find('.scroller')[0],
2016-01-17 21:19:42 +01:00
bar: this.$el.find('.scroller__bar')[0]
2015-10-17 23:49:24 +02:00
});
}
} else {
this.removeInnerViews();
}
this.model.get('items').forEach(function(item) {
2017-01-31 07:50:28 +01:00
const itemView = new MenuItemView({ el: this.itemsEl, model: item });
2015-10-17 23:49:24 +02:00
itemView.render();
this.itemViews.push(itemView);
}, this);
2015-11-14 19:18:34 +01:00
if (this.model.get('drag')) {
2017-01-31 07:50:28 +01:00
const height = AppSettingsModel.instance.get('tagsViewHeight');
2015-11-15 14:33:42 +01:00
if (typeof height === 'number') {
this.$el.height();
this.$el.css('flex', '0 0 ' + height + 'px');
2015-11-14 19:18:34 +01:00
}
2015-10-17 23:49:24 +02:00
}
2015-11-15 14:33:42 +01:00
this.pageResized();
2015-10-17 23:49:24 +02:00
},
2016-07-17 13:30:38 +02:00
remove: function() {
2015-10-17 23:49:24 +02:00
if (this.scroll) {
this.scroll.dispose();
}
this.removeInnerViews();
Backbone.View.prototype.remove.apply(this, arguments);
},
removeInnerViews: function() {
2016-07-17 13:30:38 +02:00
this.itemViews.forEach(itemView => itemView.remove());
2015-10-17 23:49:24 +02:00
this.itemViews = [];
},
itemsChanged: function() {
this.render();
2015-11-14 19:18:34 +01:00
},
2015-11-15 14:33:42 +01:00
viewResized: function(size) {
this.$el.css('flex', '0 0 ' + (size ? size + 'px' : 'auto'));
this.saveViewHeight(size);
},
2016-07-17 13:30:38 +02:00
saveViewHeight: _.throttle(size => {
2015-11-14 19:18:34 +01:00
AppSettingsModel.instance.set('tagsViewHeight', size);
}, 1000)
2015-10-17 23:49:24 +02:00
});
_.extend(MenuSectionView.prototype, Resizable);
_.extend(MenuSectionView.prototype, Scrollable);
module.exports = MenuSectionView;