refactoring. created controller for set of buttons

This commit is contained in:
Maxim Kadushkin 2018-02-13 17:37:22 +03:00
parent 8be206fa33
commit 0fb072b9fa
6 changed files with 87 additions and 120 deletions

View file

@ -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 ) { %>' +
'<img src="<%= iconImg %>">' +

View file

@ -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 ) {

View file

@ -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;
}
}

View file

@ -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');

View file

@ -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;
}

View file

@ -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;
}