From f42ba0d9b720fc2abafa054b5f40d1b43c49d0ca Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Fri, 31 May 2019 00:37:06 +0200 Subject: [PATCH] Refactor class tag view --- src/selector_manager/view/ClassTagView.js | 30 ++++++++++++---------- src/selector_manager/view/ClassTagsView.js | 24 +++++++---------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/selector_manager/view/ClassTagView.js b/src/selector_manager/view/ClassTagView.js index c29b10658..9dcb7d408 100644 --- a/src/selector_manager/view/ClassTagView.js +++ b/src/selector_manager/view/ClassTagView.js @@ -23,11 +23,12 @@ module.exports = require('backbone').View.extend({ }, initialize(o) { - this.config = o.config || {}; + const config = o.config || {}; + this.config = config; this.coll = o.coll || null; - this.pfx = this.config.stylePrefix || ''; - this.ppfx = this.config.pStylePrefix || ''; - this.em = this.config.em; + this.pfx = config.stylePrefix || ''; + this.ppfx = config.pStylePrefix || ''; + this.em = config.em; this.listenTo(this.model, 'change:active', this.updateStatus); }, @@ -84,7 +85,8 @@ module.exports = require('backbone').View.extend({ * @private */ changeStatus() { - this.model.set('active', !this.model.get('active')); + const { model } = this; + model.set('active', !model.get('active')); }, /** @@ -105,17 +107,17 @@ module.exports = require('backbone').View.extend({ * @private */ updateStatus() { - var chkOn = 'fa-check-square-o'; - var chkOff = 'fa-square-o'; + const { model, $el } = this; + const chkOn = 'fa-check-square-o'; + const chkOff = 'fa-square-o'; + const $chk = $el.find('[data-tag-status]'); - if (!this.$chk) this.$chk = this.$el.find('#' + this.pfx + 'checkbox'); - - if (this.model.get('active')) { - this.$chk.removeClass(chkOff).addClass(chkOn); - this.$el.removeClass('opac50'); + if (model.get('active')) { + $chk.removeClass(chkOff).addClass(chkOn); + $el.removeClass('opac50'); } else { - this.$chk.removeClass(chkOn).addClass(chkOff); - this.$el.addClass('opac50'); + $chk.removeClass(chkOn).addClass(chkOff); + $el.addClass('opac50'); } }, diff --git a/src/selector_manager/view/ClassTagsView.js b/src/selector_manager/view/ClassTagsView.js index 430c31df4..c95273238 100644 --- a/src/selector_manager/view/ClassTagsView.js +++ b/src/selector_manager/view/ClassTagsView.js @@ -166,7 +166,6 @@ module.exports = Backbone.View.extend({ }, getTarget() { - const targetStyle = this.getStyleEmitter().model; return this.target.getSelected(); }, @@ -252,34 +251,30 @@ module.exports = Backbone.View.extend({ * @return {Object} Object created * @private * */ - addToClasses(model, fragmentEl) { - var fragment = fragmentEl || null; - - var view = new ClassTagView({ + addToClasses(model, fragmentEl = null) { + const fragment = fragmentEl; + const classes = this.getClasses(); + const rendered = new ClassTagView({ model, config: this.config, coll: this.collection - }); - var rendered = view.render().el; + }).render().el; - if (fragment) fragment.appendChild(rendered); - else this.getClasses().append(rendered); + fragment ? fragment.appendChild(rendered) : classes.append(rendered); return rendered; }, /** * Render the collection of classes - * @return {this} * @private */ renderClasses() { const frag = document.createDocumentFragment(); const classes = this.getClasses(); + classes.empty(); this.collection.each(model => this.addToClasses(model, frag)); - classes.get(0) && classes.empty().append(frag); - - return this; + classes.append(frag); }, /** @@ -288,8 +283,7 @@ module.exports = Backbone.View.extend({ * @private */ getClasses() { - if (!this.$classes) this.$classes = this.$el.find(`#${this.pfx}tags-c`); - return this.$classes; + return this.$el.find(`#${this.pfx}tags-c`); }, /**