From 0fb072b9fab0eca92064f28ea39a2af4802aa3b5 Mon Sep 17 00:00:00 2001 From: Maxim Kadushkin Date: Tue, 13 Feb 2018 17:37:22 +0300 Subject: [PATCH] refactoring. created controller for set of buttons --- apps/common/main/lib/component/Button.js | 59 +++++++++++++++++ .../main/app/controller/Toolbar.js | 24 +++---- apps/documenteditor/main/app/view/Toolbar.js | 35 +++-------- .../main/app/controller/Toolbar.js | 6 +- .../main/app/view/Toolbar.js | 63 +++---------------- .../main/app/view/Toolbar.js | 20 ++---- 6 files changed, 87 insertions(+), 120 deletions(-) diff --git a/apps/common/main/lib/component/Button.js b/apps/common/main/lib/component/Button.js index e55b50942..013f7f78e 100644 --- a/apps/common/main/lib/component/Button.js +++ b/apps/common/main/lib/component/Button.js @@ -119,6 +119,65 @@ define([ ], function () { 'use strict'; + window.createButtonSet = function() { + function ButtonsArray(args) {}; + ButtonsArray.prototype = new Array; + ButtonsArray.prototype.constructor = ButtonsArray; + + var _disabled = false; + + ButtonsArray.prototype.add = function(button) { + button.setDisabled(_disabled); + this.push(button); + }; + + ButtonsArray.prototype.setDisabled = function(disable) { + if ( _disabled != disable ) { + _disabled = disable; + + this.forEach( function(button) { + button.setDisabled(disable); + }); + } + }; + + ButtonsArray.prototype.toggle = function(state, suppress) { + this.forEach(function(button) { + button.toggle(state, suppress); + }); + }; + + ButtonsArray.prototype.pressed = function() { + return this.some(function(button) { + return button.pressed; + }); + }; + + ButtonsArray.prototype.contains = function(id) { + return this.some(function(button) { + return button.id == id; + }); + }; + + ButtonsArray.prototype.concat = function () { + var args = Array.prototype.slice.call(arguments); + var result = Array.prototype.slice.call(this); + + args.forEach(function(sub){ + Array.prototype.push.apply(result, sub); + }); + + return result; + } + + var _out_array = Object.create(ButtonsArray.prototype); + for ( var i in arguments ) { + _out_array.add(arguments[i]); + } + + return _out_array; + }; + var templateBtnIcon = '<% if ( iconImg ) { %>' + '' + diff --git a/apps/documenteditor/main/app/controller/Toolbar.js b/apps/documenteditor/main/app/controller/Toolbar.js index 8343f7f10..b50f5e651 100644 --- a/apps/documenteditor/main/app/controller/Toolbar.js +++ b/apps/documenteditor/main/app/controller/Toolbar.js @@ -734,7 +734,7 @@ define([ toolbar.mnuInsertPageNum.setDisabled(need_disable); need_disable = paragraph_locked || header_locked || in_header || in_image || in_equation && !btn_eq_state || this.api.asc_IsCursorInFootnote() || in_control; - toolbar.btnsPageBreak.disable(need_disable); + toolbar.btnsPageBreak.setDisabled(need_disable); need_disable = paragraph_locked || header_locked || !can_add_image || in_equation || control_plain; toolbar.btnInsertImage.setDisabled(need_disable); @@ -767,10 +767,8 @@ define([ toolbar.listStylesAdditionalMenuItem.setDisabled(frame_pr===undefined); need_disable = (paragraph_locked || header_locked) && this.api.can_AddQuotedComment() || image_locked; - if (this.btnsComment && this.btnsComment.length>0 && need_disable != this.btnsComment[0].isDisabled()) - _.each (this.btnsComment, function(item){ - item.setDisabled(need_disable); - }, this); + if ( this.btnsComment && this.btnsComment.length > 0 ) + this.btnsComment.setDisabled(need_disable); this._state.in_equation = in_equation; }, @@ -922,11 +920,7 @@ define([ this.api.asc_Save(); } - toolbar.btnsSave.forEach(function(button) { - if ( button ) { - button.setDisabled(!toolbar.mode.forcesave); - } - }); + toolbar.btnsSave.setDisabled(!toolbar.mode.forcesave); Common.NotificationCenter.trigger('edit:complete', toolbar); @@ -2707,11 +2701,7 @@ define([ toolbar._state.previewmode = reviewmode && disable; if (reviewmode) { - toolbar._state.previewmode && toolbar.btnsSave.forEach(function(button) { - if ( button ) { - button.setDisabled(true); - } - }); + toolbar._state.previewmode && toolbar.btnsSave.setDisabled(true); if (toolbar.needShowSynchTip) { toolbar.needShowSynchTip = false; @@ -2797,7 +2787,7 @@ define([ var me = this; if ( config.canCoAuthoring && config.canComments ) { - this.btnsComment = []; + this.btnsComment = createButtonSet(); var slots = me.toolbar.$el.find('.slot-comment'); slots.each(function(index, el) { var _cls = 'btn-toolbar'; @@ -2810,7 +2800,7 @@ define([ caption: me.toolbar.capBtnComment }).render( slots.eq(index) ); - me.btnsComment.push(button); + me.btnsComment.add(button); }); if ( this.btnsComment.length ) { diff --git a/apps/documenteditor/main/app/view/Toolbar.js b/apps/documenteditor/main/app/view/Toolbar.js index 56b3daa6b..4fecc3cd1 100644 --- a/apps/documenteditor/main/app/view/Toolbar.js +++ b/apps/documenteditor/main/app/view/Toolbar.js @@ -123,7 +123,7 @@ define([ iconCls: 'no-mask ' + this.btnSaveCls }); this.toolbarControls.push(this.btnSave); - this.btnsSave = [this.btnSave]; + me.btnsSave = createButtonSet( me.btnSave ); this.btnUndo = new Common.UI.Button({ id: 'id-toolbar-btn-undo', @@ -1300,12 +1300,7 @@ define([ +function injectBreakButtons() { var me = this; - me.btnsPageBreak = []; - me.btnsPageBreak.disable = function(status) { - this.forEach(function(btn) { - btn.setDisabled(status); - }); - }; + me.btnsPageBreak = createButtonSet(); var $slots = $host.find('.btn-slot.btn-pagebreak'); $slots.each(function(index, el) { @@ -1320,7 +1315,7 @@ define([ menu: true }).render( $slots.eq(index) ); - me.btnsPageBreak.push(button); + me.btnsPageBreak.add(button); }); Array.prototype.push.apply(me.paragraphControls, me.btnsPageBreak); @@ -1965,16 +1960,14 @@ define([ var btnsave = DE.getController('LeftMenu').getView('LeftMenu').getMenu('file').getButton('save'); if (btnsave && this.btnsSave) { - this.btnsSave.push(btnsave); + this.btnsSave.add(btnsave); this.toolbarControls.push(btnsave); - btnsave.setDisabled(this.btnsSave[0].isDisabled()); } btnsave = DE.getController('Viewport').getView('Common.Views.Header').getButton('save'); if (btnsave && this.btnsSave) { - this.btnsSave.push(btnsave); + this.btnsSave.add(btnsave); this.toolbarControls.push(btnsave); - btnsave.setDisabled(this.btnsSave[0].isDisabled()); } }, @@ -2059,11 +2052,7 @@ define([ setMode: function (mode) { if (mode.isDisconnected) { - this.btnsSave.forEach(function(button) { - if ( button ) { - button.setDisabled(true); - } - }); + this.btnsSave.setDisabled(true); if (mode.disableDownload) this.btnPrint.setDisabled(true); } @@ -2154,11 +2143,7 @@ define([ this.btnSave.updateHint(this.tipSynchronize + Common.Utils.String.platformKey('Ctrl+S')); } - this.btnsSave.forEach(function(button) { - if ( button ) { - button.setDisabled(false); - } - }); + this.btnsSave.setDisabled(false); Common.Gateway.collaborativeChanges(); }, @@ -2189,11 +2174,7 @@ define([ if (this.synchTooltip) this.synchTooltip.hide(); this.btnSave.updateHint(this.btnSaveTip); - this.btnsSave.forEach(function(button) { - if ( button ) { - button.setDisabled(!me.mode.forcesave); - } - }); + this.btnsSave.setDisabled(!me.mode.forcesave); this._state.hasCollaborativeChanges = false; } } diff --git a/apps/presentationeditor/main/app/controller/Toolbar.js b/apps/presentationeditor/main/app/controller/Toolbar.js index 4ffa5d50f..cdd8f02e6 100644 --- a/apps/presentationeditor/main/app/controller/Toolbar.js +++ b/apps/presentationeditor/main/app/controller/Toolbar.js @@ -915,11 +915,7 @@ define([ this.api.asc_Save(); } - toolbar.btnsSave.forEach(function(button) { - if ( button ) { - button.setDisabled(!toolbar.mode.forcesave); - } - }); + toolbar.btnsSave.setDisabled(!toolbar.mode.forcesave); Common.NotificationCenter.trigger('edit:complete', this.toolbar); Common.component.Analytics.trackEvent('Save'); diff --git a/apps/presentationeditor/main/app/view/Toolbar.js b/apps/presentationeditor/main/app/view/Toolbar.js index f6abc374f..6bb2695e6 100644 --- a/apps/presentationeditor/main/app/view/Toolbar.js +++ b/apps/presentationeditor/main/app/view/Toolbar.js @@ -83,45 +83,6 @@ define([ commentLock: 'can-comment' }; - var buttonsArray = function (opts) { - var arr = []; - arr.push.apply(arr, arguments); - arr.__proto__ = buttonsArray.prototype; - return arr; - }; - - buttonsArray.prototype = new Array; - - buttonsArray.prototype.disable = function (state) { - this.forEach(function(btn) { - btn.setDisabled(state); - }); - }; - - buttonsArray.prototype.toggle = function (state, suppress) { - this.forEach(function(btn) { - btn.toggle(state, suppress); - }); - }; - - buttonsArray.prototype.pressed = function () { - return this.some(function(btn) { - return btn.pressed; - }); - }; - - buttonsArray.prototype.on = function (event, func) { - this.forEach(function(btn) { - btn.on.apply(btn, arguments); - }); - }; - - buttonsArray.prototype.contains = function (id) { - return this.some(function(btn) { - return btn.id == id; - }); - }; - PE.Views.Toolbar = Common.UI.Mixtbar.extend(_.extend((function(){ return { @@ -211,7 +172,7 @@ define([ iconCls : 'no-mask ' + me.btnSaveCls, lock : [_set.lostConnect] }); - me.btnsSave = [me.btnSave]; + me.btnsSave = createButtonSet(me.btnSave); me.btnUndo = new Common.UI.Button({ id : 'id-toolbar-btn-undo', @@ -983,7 +944,7 @@ define([ _injectComponent('#slot-btn-settings', this.btnAdvSettings); function _injectBtns(opts) { - var array = new buttonsArray; + var array = createButtonSet(); var $slots = $host.find(opts.slot); var id = opts.btnconfig.id; $slots.each(function(index, el) { @@ -992,7 +953,7 @@ define([ var button = new Common.UI.Button(opts.btnconfig); button.render( $slots.eq(index) ); - array.push(button); + array.add(button); }); return array; @@ -1381,15 +1342,13 @@ define([ var btnsave = PE.getController('LeftMenu').getView('LeftMenu').getMenu('file').getButton('save'); if (btnsave && this.btnsSave) { - this.btnsSave.push(btnsave); + this.btnsSave.add(btnsave); this.lockControls.push(btnsave); - btnsave.setDisabled(this.btnsSave[0].isDisabled()); } btnsave = PE.getController('Viewport').getView('Common.Views.Header').getButton('save'); if (btnsave && this.btnsSave) { - this.btnsSave.push(btnsave); + this.btnsSave.add(btnsave); this.lockControls.push(btnsave); - btnsave.setDisabled(this.btnsSave[0].isDisabled()); } /** coauthoring begin **/ @@ -1540,11 +1499,7 @@ define([ this.btnSave.updateHint(this.tipSynchronize + Common.Utils.String.platformKey('Ctrl+S')); } - this.btnsSave.forEach(function(button) { - if ( button ) { - button.setDisabled(false); - } - }); + this.btnsSave.setDisabled(false); Common.Gateway.collaborativeChanges(); }, @@ -1575,11 +1530,7 @@ define([ if (this.synchTooltip) this.synchTooltip.hide(); this.btnSave.updateHint(this.btnSaveTip); - this.btnsSave.forEach(function(button) { - if ( button ) { - button.setDisabled(!me.mode.forcesave); - } - }); + this.btnsSave.setDisabled(!me.mode.forcesave); this._state.hasCollaborativeChanges = false; } diff --git a/apps/spreadsheeteditor/main/app/view/Toolbar.js b/apps/spreadsheeteditor/main/app/view/Toolbar.js index 95a1dc88b..24c851321 100644 --- a/apps/spreadsheeteditor/main/app/view/Toolbar.js +++ b/apps/spreadsheeteditor/main/app/view/Toolbar.js @@ -372,7 +372,7 @@ define([ cls : 'btn-toolbar', iconCls : 'no-mask ' + me.btnSaveCls }); - me.btnsSave = [me.btnSave]; + me.btnsSave = createButtonSet(me.btnSave); me.btnIncFontSize = new Common.UI.Button({ id : 'id-toolbar-btn-incfont', @@ -1709,13 +1709,11 @@ define([ var btnsave = SSE.getController('LeftMenu').getView('LeftMenu').getMenu('file').getButton('save'); if (btnsave && this.btnsSave) { - this.btnsSave.push(btnsave); - btnsave.setDisabled(this.btnsSave[0].isDisabled()); + this.btnsSave.add(btnsave); } btnsave = SSE.getController('Viewport').getView('Common.Views.Header').getButton('save'); if (btnsave && this.btnsSave) { - this.btnsSave.push(btnsave); - btnsave.setDisabled(this.btnsSave[0].isDisabled()); + this.btnsSave.add(btnsave); } }, @@ -1853,11 +1851,7 @@ define([ this.btnSave.updateHint(this.tipSynchronize + Common.Utils.String.platformKey('Ctrl+S')); } - this.btnsSave.forEach(function(button) { - if ( button ) { - button.setDisabled(false); - } - }); + this.btnsSave.setDisabled(false); Common.Gateway.collaborativeChanges(); }, @@ -1888,11 +1882,7 @@ define([ if (this.synchTooltip) this.synchTooltip.hide(); this.btnSave.updateHint(this.btnSaveTip); - this.btnsSave.forEach(function(button) { - if ( button ) { - button.setDisabled(!me.mode.forcesave); - } - }); + this.btnsSave.setDisabled(!me.mode.forcesave); this._state.hasCollaborativeChanges = false; }