loleaflet/build/deps.js | 5 + loleaflet/dist/toolbar/toolbar.js | 89 +++++++++----------------- loleaflet/src/map/handler/Map.StateChanges.js | 44 ++++++++++++ 3 files changed, 82 insertions(+), 56 deletions(-)
New commits: commit a91bdaf082634f0d54ac53ea464dc4e0489a2c74 Author: Pranav Kant <[email protected]> Date: Wed May 3 19:31:07 2017 +0530 loleaflet: Store state change handlers in new stateChangeHandler class ... instead of having this logic in our toolbar. All the elements like menubar or toolbar can then use the map store to see the value of last state change event for any particular uno command. We will be using it when menubar starts using this store. The frivolous logic in toolbar of reading the value of last uno command should be removed. We don't expect loleaflet to change the permission dynamically after loading the document anymore. But for now, just change it accordingly and defer the task of removing it when we refactor loleaflet codebase to trim our permission model (remove the 'view' mode). Change-Id: I1be54a62e96179b7db3f61470a92e3ddc745f52d Reviewed-on: https://gerrit.libreoffice.org/37504 Reviewed-by: Jan Holesovsky <[email protected]> Tested-by: Jan Holesovsky <[email protected]> diff --git a/loleaflet/build/deps.js b/loleaflet/build/deps.js index 34575612..e671dea3 100644 --- a/loleaflet/build/deps.js +++ b/loleaflet/build/deps.js @@ -248,6 +248,11 @@ var deps = { desc: 'Handles inserting a file (image) in the document.' }, + StateChanges: { + src: ['map/handler/Map.StateChanges.js'], + desc: 'Handles state changes for the opened document' + }, + WOPI: { src: ['map/handler/Map.WOPI.js'], desc: 'Handles WOPI related logic.' diff --git a/loleaflet/dist/toolbar/toolbar.js b/loleaflet/dist/toolbar/toolbar.js index d2b2374f..b9041132 100644 --- a/loleaflet/dist/toolbar/toolbar.js +++ b/loleaflet/dist/toolbar/toolbar.js @@ -433,11 +433,6 @@ function onColorPick(e, color) { map.focus(); } -// This object is used to store enabled/disabled state of each and every eligible item -// (i.e the one having UNO command) of toolbar-up. When the permission is changed to/from -// edit/view mode, state from this object is read and then applied on corresponding buttons -var formatButtons = {}; - var stylesSelectValue; var fontsSelectValue; var fontsizesSelectValue; @@ -593,15 +588,6 @@ $(function () { } } - // Intialize the formatButtons object - if (Object.keys(formatButtons).length === 0) { - for (var itemIdx in w2ui['toolbar-up'].items) { - if (w2ui['toolbar-up'].items[itemIdx].uno) { - formatButtons[w2ui['toolbar-up'].items[itemIdx].id] = true; - } - } - } - updateCommandValues(); insertTable(); @@ -1183,41 +1169,28 @@ map.on('commandstatechanged', function (e) { } var id = unoCmdToToolbarId(commandName); - if (typeof formatButtons[id] !== 'undefined') { - if (state === 'true') { - toolbar.enable(id); - toolbar.check(id); - toolbarUpMore.check(id); - } - else if (state === 'false') { + if (state === 'true') { + toolbar.enable(id); + toolbar.check(id); + toolbarUpMore.check(id); + } + else if (state === 'false') { + toolbar.enable(id); + toolbar.uncheck(id); + toolbarUpMore.uncheck(id); + } + // Change the toolbar button states if we are in editmode + // If in non-edit mode, will be taken care of when permission is changed to 'edit' + else if (map._permission === 'edit' && (state === 'enabled' || state === 'disabled')) { + // in case some buttons are in toolbar-up-more, find + // them and en/dis-able them. + if (state === 'enabled') { toolbar.enable(id); + toolbarUpMore.enable(id); + } else { toolbar.uncheck(id); - toolbarUpMore.uncheck(id); - } - // only store the state for now; - // buttons with stored state === enabled will - // be enabled later (if we are in editmode) - // If we are in viewmode, these store states will be used - // when we get the edit access - else if (state === 'enabled') { - formatButtons[id] = true; - } - else if (state === 'disabled') { - formatButtons[id] = false; - } - - // Change the toolbar button states immediately if we are in editmode - if (map._permission === 'edit' && (state === 'enabled' || state === 'disabled')) { - // in case some buttons are in toolbar-up-more, find - // them and en/dis-able them. - if (formatButtons[id]) { - toolbar.enable(id); - toolbarUpMore.enable(id); - } else { - toolbar.uncheck(id); - toolbar.disable(id); - toolbarUpMore.disable(id); - } + toolbar.disable(id); + toolbarUpMore.disable(id); } } @@ -1440,16 +1413,20 @@ map.on('updatepermission', function (e) { var toolbar = w2ui['toolbar-up']; var toolbarUpMore = w2ui['toolbar-up-more']; - // {En,Dis}able toolbar buttons - for (var id in formatButtons) { - if (e.perm === 'edit' && formatButtons[id]) { - // restore the state from stored object (formatButtons) - toolbar.enable(id); - // some might be hidden in toolbar-up-more - toolbarUpMore.enable(id); + // copy the first array + var items = toolbar.items.slice(); + items.concat(toolbarUpMore.items); + for (var idx in items) { + var unoCmd = map.getDocType() === 'spreadsheet' ? items[idx].unosheet : items[idx].uno; + var keepDisabled = map['stateChangeHandler'].getItemValue(unoCmd) === 'disabled'; + if (e.perm === 'edit') { + if (!keepDisabled) { + toolbar.enable(items[idx].id); + toolbarUpMore.enable(items[idx].id); + } } else { - toolbar.disable(id); - toolbarUpMore.disable(id); + toolbar.disable(items[idx].id); + toolbarUpMore.disable(items[idx].id); } } diff --git a/loleaflet/src/map/handler/Map.StateChanges.js b/loleaflet/src/map/handler/Map.StateChanges.js new file mode 100644 index 00000000..6c31981e --- /dev/null +++ b/loleaflet/src/map/handler/Map.StateChanges.js @@ -0,0 +1,44 @@ +/* + * L.Map.StateChanges stores the state changes commands coming from core + * LOK_CALLBACK_STATE_CHANGED callback + */ + +L.Map.mergeOptions({ + stateChangeHandler: true +}); + +L.Map.StateChangeHandler = L.Handler.extend({ + + initialize: function (map) { + this._map = map; + // Contains the items for which state will be tracked + // Stores the last received value from core ('true', 'false', 'enabled', 'disabled') + this._items = {}; + }, + + addHooks: function () { + this._map.on('commandstatechanged', this._onStateChanged, this); + }, + + removeHooks: function () { + this._map.off('commandstatechanged', this._onStateChanged, this); + }, + + _onStateChanged: function(e) { + this._items[e.commandName] = e.state; + }, + + getItems: function() { + return this._items; + }, + + getItemValue: function(unoCmd) { + if (unoCmd && unoCmd.substring(0, 5) !== '.uno:') { + unoCmd = '.uno:' + unoCmd; + } + + return this._items[unoCmd]; + } +}); + +L.Map.addInitHook('addHandler', 'stateChangeHandler', L.Map.StateChangeHandler); _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
