From d6563ef6aaedc7dfdd97f336d5d90570ae687e00 Mon Sep 17 00:00:00 2001 From: Richard Szczerba Date: Tue, 4 Sep 2018 15:17:24 +0000 Subject: [PATCH 1/2] [erp5_notebook]: different localStorages for different notebooks --- .../erp5_notebook/gadget_notebook.html.html | 2 +- .../gadget_officejs_jio_notebook.js.js | 92 ++++++++++++++++++- 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/bt5/erp5_notebook/SkinTemplateItem/portal_skins/erp5_notebook/gadget_notebook.html.html b/bt5/erp5_notebook/SkinTemplateItem/portal_skins/erp5_notebook/gadget_notebook.html.html index c8fadb933fc..6957d0c4e4c 100644 --- a/bt5/erp5_notebook/SkinTemplateItem/portal_skins/erp5_notebook/gadget_notebook.html.html +++ b/bt5/erp5_notebook/SkinTemplateItem/portal_skins/erp5_notebook/gadget_notebook.html.html @@ -14,7 +14,7 @@ -
+
\ No newline at end of file diff --git a/bt5/erp5_notebook/SkinTemplateItem/portal_skins/erp5_notebook/gadget_officejs_jio_notebook.js.js b/bt5/erp5_notebook/SkinTemplateItem/portal_skins/erp5_notebook/gadget_officejs_jio_notebook.js.js index 13db373d42e..e7dcdc2e95b 100644 --- a/bt5/erp5_notebook/SkinTemplateItem/portal_skins/erp5_notebook/gadget_officejs_jio_notebook.js.js +++ b/bt5/erp5_notebook/SkinTemplateItem/portal_skins/erp5_notebook/gadget_officejs_jio_notebook.js.js @@ -3,6 +3,65 @@ (function (window, rJS, RSVP) { "use strict"; + function parseJsmdChunk(str) { + var chunkType, content, firstLine, + settings = {}, + firstLineBreak = str.indexOf('\n'); + if (firstLineBreak === -1) { + // a cell with only 1 line, and hence no content + firstLine = str; + content = ''; + } else { + firstLine = str.substring(0, firstLineBreak).trim(); + content = str.substring(firstLineBreak + 1).trim(); + } + // let firstLine = str.substring(0,firstLineBreak).trim() + var firstLineFirstSpace = firstLine.indexOf(' '); + + if (firstLineFirstSpace === -1) { + // if there is NO space on the first line (after trimming), there are no cell settings + chunkType = firstLine.toLowerCase(); + } else { + // if there is a space on the first line (after trimming), there must be cell settings + chunkType = firstLine.substring(0, firstLineFirstSpace).toLowerCase(); + // make sure the cell settings parse as JSON + } + if (chunkType === 'meta') { + return { chunkType: 'meta', iodideSettings: JSON.parse(content) }; + } + + return null; + } + + function getiodideSettingsFromJsmd(jsmdString) { + // returns iodideSettings of last found chunk and exits + var chunkObjects = jsmdString.split('\n%%') + .map(function (str, chunkNum) { + // if this is the first chunk, and it starts with "%%", drop those chars + var sstr; + if (chunkNum === 0 && str.substring(0, 2) === '%%') { + sstr = str.substring(2); + } else { + sstr = str; + } + return sstr; + }) + .map(function (str) { + return str.trim(); + }) + .filter(function (str) { + return str !== ''; + }); + + for (var i = (chunkObjects.length - 1); i >= 0 ; i--) { + var chunk = parseJsmdChunk(chunkObjects[i]); + if (chunk && chunk.iodideSettings) { + return chunk.iodideSettings; + } + } + return ''; + } + rJS(window) ///////////////////////////////////////////////////////////////// // Acquired methods @@ -22,11 +81,31 @@ return this.changeState({ key: options.key, value: options.value, - first_render: true + first_render: true, + timestamp: options.render_timestamp }); }) .onStateChange(function (modified_dict) { + if (!this.state.value.includes('%%')) { + this.state.value = ''; + } + var iodideSettings = getiodideSettingsFromJsmd(this.state.value); + if (iodideSettings && !iodideSettings.title) { + iodideSettings.title = 'notebook-' + modified_dict.timestamp; + var regex = /%% meta\r\n([\S\s]*?)\n%%/m; + var capturedGroup = this.state.value.match(regex)[1]; + this.state.value = this.state.value.replace(capturedGroup, JSON.stringify(iodideSettings)); + } + if (!iodideSettings) { + var jsmdTitleTemplate = '%% meta\n{\n"title": ""\n}\n', + notebookTitle = 'notebook-' + modified_dict.timestamp, + positionToInsertTitle = 20, + jsmdNotebookTitle = [jsmdTitleTemplate.slice(0, positionToInsertTitle), notebookTitle, jsmdTitleTemplate.slice(positionToInsertTitle)].join(''); + this.state.value = jsmdNotebookTitle + this.state.value; + } + this.element.querySelector('script').textContent = this.state.value; + if (!modified_dict.hasOwnProperty('first_render')) { throw new Error('Sorry, it is not possible to dynamically change the iodide content'); } @@ -36,8 +115,15 @@ }) .declareMethod("getContent", function () { - var dict = {}; - dict[this.state.key] = localStorage.getItem('AUTOSAVE: untitled'); + //prefer content since last save, fallback on autosave if it does not exist + var dict = {}, + notebookTitle = getiodideSettingsFromJsmd(this.state.value).title, + localStorageJsmd = localStorage.getItem(notebookTitle); + if (!localStorageJsmd) { + var autosaveString = 'AUTOSAVE: ' + notebookTitle; + localStorageJsmd = localStorage.getItem(autosaveString); + } + dict[this.state.key] = localStorageJsmd; return dict; }); }(window, rJS, RSVP)); \ No newline at end of file -- 2.30.9 From d92abe94b42d89f842205b5b64e6c83518d94c56 Mon Sep 17 00:00:00 2001 From: Richard Szczerba Date: Mon, 10 Sep 2018 13:14:27 +0000 Subject: [PATCH 2/2] [erp5_officejs_ui_test]: add localStorage test --- .../testOfficeJSNotebook.zpt | 5 +- .../testOfficeJSNotebookLocalStorage.xml | 58 +++++ .../testOfficeJSNotebookLocalStorage.zpt | 216 ++++++++++++++++++ 3 files changed, 278 insertions(+), 1 deletion(-) create mode 100644 bt5/erp5_officejs_ui_test/PathTemplateItem/portal_tests/officejs_ui_notebook_zuite/testOfficeJSNotebookLocalStorage.xml create mode 100644 bt5/erp5_officejs_ui_test/PathTemplateItem/portal_tests/officejs_ui_notebook_zuite/testOfficeJSNotebookLocalStorage.zpt diff --git a/bt5/erp5_officejs_ui_test/PathTemplateItem/portal_tests/officejs_ui_notebook_zuite/testOfficeJSNotebook.zpt b/bt5/erp5_officejs_ui_test/PathTemplateItem/portal_tests/officejs_ui_notebook_zuite/testOfficeJSNotebook.zpt index f54b063d184..30c5cf2471a 100644 --- a/bt5/erp5_officejs_ui_test/PathTemplateItem/portal_tests/officejs_ui_notebook_zuite/testOfficeJSNotebook.zpt +++ b/bt5/erp5_officejs_ui_test/PathTemplateItem/portal_tests/officejs_ui_notebook_zuite/testOfficeJSNotebook.zpt @@ -183,4 +183,7 @@ This has been successfully tested with 59. --> waitForElementPresent identifier=notebook-container - \ No newline at end of file + + + + \ No newline at end of file diff --git a/bt5/erp5_officejs_ui_test/PathTemplateItem/portal_tests/officejs_ui_notebook_zuite/testOfficeJSNotebookLocalStorage.xml b/bt5/erp5_officejs_ui_test/PathTemplateItem/portal_tests/officejs_ui_notebook_zuite/testOfficeJSNotebookLocalStorage.xml new file mode 100644 index 00000000000..d739ecf1ff8 --- /dev/null +++ b/bt5/erp5_officejs_ui_test/PathTemplateItem/portal_tests/officejs_ui_notebook_zuite/testOfficeJSNotebookLocalStorage.xml @@ -0,0 +1,58 @@ + + + + + + + + + + _bind_names + + + + + + + + + + _asgns + + + + name_subpath + traverse_subpath + + + + + + + + + + + content_type + text/html + + + expand + 0 + + + id + testOfficeJSNotebookLocalStorage + + + output_encoding + utf-8 + + + title + + + + + + diff --git a/bt5/erp5_officejs_ui_test/PathTemplateItem/portal_tests/officejs_ui_notebook_zuite/testOfficeJSNotebookLocalStorage.zpt b/bt5/erp5_officejs_ui_test/PathTemplateItem/portal_tests/officejs_ui_notebook_zuite/testOfficeJSNotebookLocalStorage.zpt new file mode 100644 index 00000000000..6dc1740d35a --- /dev/null +++ b/bt5/erp5_officejs_ui_test/PathTemplateItem/portal_tests/officejs_ui_notebook_zuite/testOfficeJSNotebookLocalStorage.zpt @@ -0,0 +1,216 @@ + + + +Test OfficeJS UI (expected failure) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Test OfficeJS UI (expected failure)
open${base_url}/bar_module/ListBoxZuite_reset
assertTextPresentReset Successfully.
waitForElementPresent//a[@data-i18n='Storages']
click//a[@data-i18n='Storages']
waitForElementPresentlink=Local is Enough
clicklink=Local is Enough
waitForElementPresentlink=Add
clicklink=Add
waitForElementPresent//button[@data-i18n='Save']
storeTitretitle
storeReferencereference
storeLanguagelanguage
storeVersionversion
storeDescriptiondescription
waitForElementPresentidentifier=notebook-container
type//input[@title='Title']${title}
type//input[@title='Reference']${reference}
type//input[@title='Version']${version}
type//input[@title='Language']${language}
type//textarea[@title='Description']${description}
storeNotebook contentnotebook_content
storeEval
storeEval
storeEvallocalStorage.clear();
click//*[@class="editor-mode-controls"]/button[5]
storeEvalObject.keys(localStorage)[0];localStorageKey
click//button[@data-i18n='Save']
waitForElementPresent//div[@data-gadget-scope="notification"]//button[text()='Data Updated']
click//a[@data-i18n='Document']
storeEvallocalStorage.clear();
waitForElementPresentlink=Add
waitForElementPresentlink=${title}
clicklink=${title}
waitForElementPresentidentifier=notebook-container
click//*[@class="editor-mode-controls"]/button[5]
verifyEvalObject.keys(localStorage)[0];${localStorageKey}
+ + \ No newline at end of file -- 2.30.9