keeweb/app/scripts/views/grp-view.js

153 lines
4.6 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const Backbone = require('backbone');
const Scrollable = require('../mixins/scrollable');
const IconSelectView = require('./icon-select-view');
const AutoTypeHintView = require('./auto-type-hint-view');
const AutoType = require('../auto-type');
2015-10-31 20:09:32 +01:00
2017-01-31 07:50:28 +01:00
const GrpView = Backbone.View.extend({
2015-12-16 22:50:45 +01:00
template: require('templates/grp.hbs'),
2015-10-31 20:09:32 +01:00
events: {
'click .grp__icon': 'showIconsSelect',
'click .grp__buttons-trash': 'moveToTrash',
2016-08-13 21:13:16 +02:00
'click .back-button': 'returnToApp',
'input #grp__field-title': 'changeTitle',
2016-04-26 21:40:18 +02:00
'focus #grp__field-auto-type-seq': 'focusAutoTypeSeq',
2016-04-17 14:31:08 +02:00
'input #grp__field-auto-type-seq': 'changeAutoTypeSeq',
'change #grp__check-search': 'setEnableSearching',
'change #grp__check-auto-type': 'setEnableAutoType'
2015-10-31 20:09:32 +01:00
},
initialize: function() {
this.views = {};
},
render: function() {
this.removeSubView();
if (this.model) {
2019-08-16 23:05:39 +02:00
this.renderTemplate(
{
title: this.model.get('title'),
icon: this.model.get('icon') || 'folder',
customIcon: this.model.get('customIcon'),
enableSearching: this.model.getEffectiveEnableSearching(),
readonly: this.model.get('top'),
canAutoType: AutoType.enabled,
autoTypeSeq: this.model.get('autoTypeSeq'),
autoTypeEnabled: this.model.getEffectiveEnableAutoType(),
defaultAutoTypeSeq: this.model.getParentEffectiveAutoTypeSeq()
},
true
);
if (!this.model.get('title')) {
this.$el.find('#grp__field-title').focus();
}
2015-10-31 20:09:32 +01:00
}
2016-01-17 21:19:42 +01:00
this.createScroll({
2016-08-14 18:18:51 +02:00
root: this.$el.find('.grp')[0],
2015-10-31 20:09:32 +01:00
scroller: this.$el.find('.scroller')[0],
2016-01-17 21:19:42 +01:00
bar: this.$el.find('.scroller__bar')[0]
2015-10-31 20:09:32 +01:00
});
this.pageResized();
return this;
},
removeSubView: function() {
if (this.views.sub) {
this.views.sub.remove();
delete this.views.sub;
}
},
showGroup: function(group) {
this.model = group;
this.render();
},
changeTitle: function(e) {
2017-01-31 07:50:28 +01:00
const title = $.trim(e.target.value);
2015-10-31 20:09:32 +01:00
if (title) {
2016-08-13 22:19:15 +02:00
if (!this.model.get('top') && title !== this.model.get('title')) {
this.model.setName(title);
2015-10-31 20:09:32 +01:00
}
} else {
if (this.model.isJustCreated) {
this.model.removeWithoutHistory();
Backbone.trigger('edit-group');
}
}
},
2016-04-17 14:31:08 +02:00
changeAutoTypeSeq: function(e) {
2017-01-31 07:50:28 +01:00
const el = e.target;
const seq = $.trim(el.value);
2016-07-17 13:30:38 +02:00
AutoType.validate(null, seq, err => {
2016-04-26 21:40:18 +02:00
$(e.target).toggleClass('input--error', !!err);
if (!err) {
2016-07-17 13:30:38 +02:00
this.model.setAutoTypeSeq(seq);
2016-04-26 21:40:18 +02:00
}
});
},
focusAutoTypeSeq: function(e) {
if (!this.views.hint) {
2019-08-16 23:05:39 +02:00
this.views.hint = new AutoTypeHintView({ input: e.target }).render();
this.views.hint.on('remove', () => {
delete this.views.hint;
});
2016-04-26 21:40:18 +02:00
}
2016-04-17 14:31:08 +02:00
},
2015-10-31 20:09:32 +01:00
showIconsSelect: function() {
if (this.views.sub) {
this.removeSubView();
} else {
2017-01-31 07:50:28 +01:00
const subView = new IconSelectView({
2015-11-21 23:15:51 +01:00
el: this.$el.find('.grp__icons'),
model: {
iconId: this.model.get('customIconId') || this.model.get('iconId'),
file: this.model.file
}
});
2015-10-31 20:09:32 +01:00
this.listenTo(subView, 'select', this.iconSelected);
subView.render();
this.views.sub = subView;
}
this.pageResized();
},
2015-11-21 23:15:51 +01:00
iconSelected: function(sel) {
if (sel.custom) {
if (sel.id !== this.model.get('customIconId')) {
this.model.setCustomIcon(sel.id);
}
} else if (sel.id !== this.model.get('iconId')) {
2015-11-22 11:59:13 +01:00
this.model.setIcon(+sel.id);
2015-10-31 20:09:32 +01:00
}
this.render();
},
moveToTrash: function() {
this.model.moveToTrash();
Backbone.trigger('select-all');
2015-11-09 19:27:50 +01:00
},
2015-12-08 18:21:12 +01:00
setEnableSearching: function(e) {
2017-01-31 07:50:28 +01:00
const enabled = e.target.checked;
2015-12-08 18:21:12 +01:00
this.model.setEnableSearching(enabled);
},
2016-04-17 14:31:08 +02:00
setEnableAutoType: function(e) {
2017-01-31 07:50:28 +01:00
const enabled = e.target.checked;
2016-04-17 14:31:08 +02:00
this.model.setEnableAutoType(enabled);
},
2015-11-09 19:27:50 +01:00
returnToApp: function() {
Backbone.trigger('edit-group');
2015-10-31 20:09:32 +01:00
}
});
_.extend(GrpView.prototype, Scrollable);
module.exports = GrpView;