From 24fd04209c470f4e508afb00013dadb0e156e016 Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Wed, 16 Apr 2025 01:35:04 +0300 Subject: [PATCH 1/2] Implement class for adding content control buttons --- sdkjs-plugins/v1/plugins.dev.js | 94 ++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/sdkjs-plugins/v1/plugins.dev.js b/sdkjs-plugins/v1/plugins.dev.js index f676df40..9e5f1709 100644 --- a/sdkjs-plugins/v1/plugins.dev.js +++ b/sdkjs-plugins/v1/plugins.dev.js @@ -331,6 +331,7 @@ Asc.Buttons = {}; Asc.Buttons.ButtonsContextMenu = []; Asc.Buttons.ButtonsToolbar = []; + Asc.Buttons.ButtonsContentControl = []; Asc.Buttons.registerContextMenu = function() { @@ -383,6 +384,32 @@ window.Asc.plugin.executeMethod("AddToolbarMenuItem", [items]); }; + Asc.Buttons.registerContentControl = function() + { + window.Asc.plugin.attachEvent("onShowContentControlTrack", function(contentControls) { + + let buttons = { + guid: window.Asc.plugin.guid, + items : {} + }; + + let promises = []; + + for (let i = 0, len = Asc.Buttons.ButtonsContentControl.length; i < len; ++i) + { + promises.push(Asc.Buttons.ButtonsContentControl[i].onShowTrack(contentControls, buttons.items)); + } + + Promise.all(promises).then(function() { + for (let id in buttons.items) + { + window.Asc.plugin.executeMethod("AddContentControlButtons", [buttons]); + break; + } + }); + }); + }; + var ToolbarButtonType = { Button : "button", BigButton : "big-button" @@ -391,7 +418,8 @@ var ItemType = { None : 0, ContextMenu : 1, - Toolbar : 2 + Toolbar : 2, + ContentControl : 3 }; function Button(parent, id) @@ -628,8 +656,72 @@ } }; + function ButtonContentControl(parent, id) + { + Button.call(this, parent, id); + this.itemType = ItemType.ContentControl; + this.checker = null; + Asc.Buttons.ButtonsContentControl.push(this); + } + + ButtonContentControl.prototype = Object.create(Button.prototype); + ButtonContentControl.prototype.constructor = ButtonContentControl; + + ButtonContentControl.prototype.attachOnClick = function(handler) + { + //window.Asc.plugin.attachToolbarMenuClickEvent(this.id, handler); + }; + ButtonContentControl.prototype.addChecker = function(checker) + { + if (!checker || typeof checker !== 'function') + return; + + this.checker = checker; + }; + ButtonContentControl.prototype.onShowTrack = function(ccIds, items) + { + let fChecker = this.checker; + let promises = []; + let curItem = this.toItem(); + for (let i = 0, len = ccIds.length; i < len; ++i) + { + let ccId = ccIds[i]; + promises.push(new Promise(function(resolve) { + + if (!fChecker) + { + resolve(true) + } + else + { + let result = fChecker(ccId); + if (result instanceof Promise) + { + result.then(function(result) { + resolve(result); + }); + } + else + { + resolve(!!result) + } + } + }).then(function(result) { + if (!result) + return; + + if (!items[ccId]) + items[ccId] = []; + + items[ccId].push(curItem); + })); + } + return Promise.all(promises); + }; + Asc.ToolbarButtonType = ToolbarButtonType; Asc.ButtonContextMenu = ButtonContextMenu; Asc.ButtonToolbar = ButtonToolbar; + Asc.ButtonContentControl = ButtonContentControl; })(window); From bdc5838bb57a28b5d30b075c8e9a9f1eec3427b6 Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Wed, 16 Apr 2025 15:25:16 +0300 Subject: [PATCH 2/2] Implement attachOnClick for content control button --- sdkjs-plugins/v1/plugins.dev.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/sdkjs-plugins/v1/plugins.dev.js b/sdkjs-plugins/v1/plugins.dev.js index 9e5f1709..f12d252f 100644 --- a/sdkjs-plugins/v1/plugins.dev.js +++ b/sdkjs-plugins/v1/plugins.dev.js @@ -246,6 +246,28 @@ { this._onCustomMenuClick("toolbarMenuEvents", id); }; + window.Asc.plugin.event_onContentControlButtonClick = function(data) + { + const eventName = "ContentControlButtonEvents"; + + let buttonId = (data && data["buttonId"] ? data["buttonId"] : null); + let ccId = (data && data["contentControlId"] ? data["contentControlId"] : null); + + if (!buttonId || !ccId) + return; + + if (this[eventName] && this[eventName][buttonId]) + this[eventName][buttonId].call(this, ccId); + }; + window.Asc.plugin._attachContentControlButtonClickEvent = function(buttonId, action) + { + const eventName = "ContentControlButtonEvents"; + + if (!this[eventName]) + this[eventName] = {}; + + this[eventName][buttonId] = action; + }; window.Asc.plugin.attachEvent = function(id, action) { @@ -661,6 +683,11 @@ Button.call(this, parent, id); this.itemType = ItemType.ContentControl; this.checker = null; + + + if (0 === Asc.Buttons.ButtonsContentControl.length) + Asc.Buttons.registerContentControl(); + Asc.Buttons.ButtonsContentControl.push(this); } @@ -669,7 +696,7 @@ ButtonContentControl.prototype.attachOnClick = function(handler) { - //window.Asc.plugin.attachToolbarMenuClickEvent(this.id, handler); + window.Asc.plugin._attachContentControlButtonClickEvent(this.id, handler); }; ButtonContentControl.prototype.addChecker = function(checker) {