diff --git a/src/botPage/view/View.js b/src/botPage/view/View.js index 01fbc666f8..5f1e680eee 100644 --- a/src/botPage/view/View.js +++ b/src/botPage/view/View.js @@ -40,6 +40,7 @@ import { } from '../../common/utils/storageManager'; import { isProduction } from '../../common/utils/tools'; import GTM from '../../common/gtm'; +import { saveBeforeUnload } from './blockly/utils'; let realityCheckTimeout; @@ -92,14 +93,6 @@ const chart = new Chart(api); const tradingView = new TradingView(); -const setBeforeUnload = off => { - if (off) { - window.onbeforeunload = null; - } else { - window.onbeforeunload = () => 'You have some unsaved blocks, do you want to save them before you exit?'; - } -}; - const showRealityCheck = () => { $('.blocker').show(); $('.reality-check').show(); @@ -474,7 +467,7 @@ export default class View { $('#toggleHeaderButton').click(() => this.showHeader($('#header').is(':hidden'))); $('#logout, #toolbox-logout').click(() => { - setBeforeUnload(true); + saveBeforeUnload(); logout(); hideRealityCheck(); }); @@ -612,7 +605,7 @@ export default class View { $('#login, #toolbox-login') .bind('click.login', () => { - setBeforeUnload(true); + saveBeforeUnload(); document.location = getOAuthURL(); }) .text(translate('Log in')); diff --git a/src/botPage/view/blockly/index.js b/src/botPage/view/blockly/index.js index 9bf3a102b3..b5f1a80f47 100644 --- a/src/botPage/view/blockly/index.js +++ b/src/botPage/view/blockly/index.js @@ -14,6 +14,9 @@ import { removeUnavailableMarkets, strategyHasValidTradeTypeCategory, cleanBeforeExport, + importFile, + saveBeforeUnload, + removeParam, } from './utils'; import Interpreter from '../../bot/Interpreter'; import { createErrorAndEmit } from '../../common/error'; @@ -24,14 +27,6 @@ import { showDialog } from '../../bot/tools'; import GTM from '../../../common/gtm'; import { parseQueryString } from '../../../common/utils/tools'; -const setBeforeUnload = off => { - if (off) { - window.onbeforeunload = null; - } else { - window.onbeforeunload = () => 'You have some unsaved blocks, do you want to save them before you exit?'; - } -}; - const disableStrayBlocks = () => { const topBlocks = Blockly.mainWorkspace.getTopBlocks(); topBlocks.forEach(block => { @@ -48,7 +43,7 @@ const disableStrayBlocks = () => { }; const disposeBlocksWithLoaders = () => { Blockly.mainWorkspace.addChangeListener(ev => { - setBeforeUnload(); + saveBeforeUnload(); if (ev.type === 'delete' && ev.oldXml.getAttribute('type') === 'loader' && ev.group !== 'undo') { deleteBlocksLoadedBy(ev.blockId, ev.group); } @@ -220,7 +215,6 @@ const repaintDefaultColours = () => { export default class _Blockly { constructor() { - this.blocksXmlStr = ''; this.generatedJs = ''; // eslint-disable-next-line no-underscore-dangle Blockly.WorkspaceSvg.prototype.preloadAudio_ = () => {}; // https://github.com/google/blockly/issues/299 @@ -264,22 +258,47 @@ export default class _Blockly { renderInstance(); addBlocklyTranslation().then(() => { const defaultStrat = parseQueryString().strategy; - const xmlFile = defaultStrat ? `xml/${defaultStrat}.xml` : 'xml/main.xml'; + const xmlFile = `xml/${defaultStrat}.xml`; - $.get(xmlFile, main => { + const loadDomToWorkspace = dom => { repaintDefaultColours(); overrideBlocklyDefaultShape(); - this.blocksXmlStr = Blockly.Xml.domToPrettyText(main); - Blockly.Xml.domToWorkspace(main.getElementsByTagName('xml')[0], workspace); + Blockly.Xml.domToWorkspace(dom, workspace); this.zoomOnPlusMinus(); disposeBlocksWithLoaders(); setTimeout(() => { - setBeforeUnload(true); + saveBeforeUnload(); Blockly.mainWorkspace.cleanUp(); Blockly.mainWorkspace.clearUndo(); }, 0); - resolve(); - }); + }; + + const getFile = xml => { + importFile(xml) + .then(dom => { + loadDomToWorkspace(dom.getElementsByTagName('xml')[0]); + resolve(); + }) + .catch(text => { + if (text) { + const previousStrat = Blockly.Xml.textToDom(text); + loadDomToWorkspace(previousStrat); + resolve(); + } else { + getFile('xml/main.xml'); + } + + if (defaultStrat) { + globalObserver.emit('Notify', { + className: 'warn', + message : translate('The strategy you tried to load is invalid'), + position : 'right', + }); + } + }); + }; + + getFile(xmlFile); }); }); }); @@ -294,10 +313,13 @@ export default class _Blockly { } } resetWorkspace() { - Blockly.Events.setGroup('reset'); - Blockly.mainWorkspace.clear(); - Blockly.Xml.domToWorkspace(Blockly.Xml.textToDom(this.blocksXmlStr), Blockly.mainWorkspace); - Blockly.Events.setGroup(false); + importFile('xml/main.xml').then(dom => { + Blockly.Events.setGroup('reset'); + Blockly.mainWorkspace.clear(); + Blockly.Xml.domToWorkspace(dom.getElementsByTagName('xml')[0], Blockly.mainWorkspace); + Blockly.Events.setGroup(false); + this.cleanUp(); + }); } /* eslint-disable class-methods-use-this */ cleanUp() { @@ -358,6 +380,8 @@ export default class _Blockly { } }); + removeParam('strategy'); + try { if (xml.hasAttribute('collection') && xml.getAttribute('collection') === 'true') { loadBlocks(xml, dropEvent); @@ -372,7 +396,7 @@ export default class _Blockly { save(arg) { const { filename, collection } = arg; - setBeforeUnload(true); + saveBeforeUnload(); const xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace); cleanBeforeExport(xml); diff --git a/src/botPage/view/blockly/utils.js b/src/botPage/view/blockly/utils.js index cacb8aa752..33eed2584c 100644 --- a/src/botPage/view/blockly/utils.js +++ b/src/botPage/view/blockly/utils.js @@ -506,3 +506,44 @@ export const cleanBeforeExport = xml => { } }); }; + +export const importFile = xml => + new Promise((resolve, reject) => { + $.get(xml, dom => { + resolve(dom); + }).catch(() => { + const previousWorkspaceText = localStorage.getItem('previousStrat'); + reject(previousWorkspaceText); + }); + }); + +export const saveBeforeUnload = () => { + window.onbeforeunload = () => { + const currentDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace); + localStorage.setItem('previousStrat', Blockly.Xml.domToPrettyText(currentDom)); + return null; + }; +}; + +export const removeParam = key => { + const sourceURL = window.location.href; + let rtn = sourceURL.split('?')[0]; + let paramsArr = []; + const queryString = sourceURL.indexOf('?') !== -1 ? sourceURL.split('?')[1] : ''; + if (queryString !== '') { + paramsArr = queryString.split('&'); + for (let i = paramsArr.length - 1; i >= 0; i -= 1) { + const paramPair = paramsArr[i]; + const paramKey = paramPair.split('='); + const param = paramKey[0]; + if (param === key) { + paramsArr.splice(i, 1); + } + } + if (paramsArr.length) { + rtn = `${rtn}?${paramsArr.join('&')}`; + } + } + + window.history.pushState({}, window.title, rtn); +}; diff --git a/src/indexPage/endpoint.js b/src/indexPage/endpoint.js index 872d037d90..705ae59162 100644 --- a/src/indexPage/endpoint.js +++ b/src/indexPage/endpoint.js @@ -65,7 +65,7 @@ function addEndpoint(e) { setStorage('config.server_url', serverUrl); setStorage('config.app_id', appId); - const urlReg = /^(?:http(s)?:\/\/)?[\w.-]+(?:.[\w.-]+)+[\w-._~:\/?#[\]@!$&'()*+,;=.]+$/; + const urlReg = /^(?:http(s)?:\/\/)?[\w.-]+(?:.[\w.-]+)+[\w-._~:?#[\]@!$&'()*+,;=.]+$/; if (!urlReg.test(serverUrl)) { $('#error')