diff --git a/Canvas.js b/Canvas.js index 8138525d0..7bc23b8ce 100644 --- a/Canvas.js +++ b/Canvas.js @@ -100,10 +100,6 @@ function newCanvas() { function onCharSpaceInitialized(err) { viewPort.raiseEvents(); // These events will impacts on objects just initialized. - - /* We will store the last used this screen resolution. This will help us know if we can remember the las viewPort offset or not, since they depends on the screen resolution. */ - - window.localStorage.setItem('webApp.lastRun.screenResolution', browserCanvas.width + '.' + browserCanvas.height); } /* Splash Screen */ diff --git a/ChartGrid.js b/ChartGrid.js index 42323b289..11332b6c9 100644 --- a/ChartGrid.js +++ b/ChartGrid.js @@ -500,7 +500,7 @@ let fontSize = 10; - let label = ' ' + Math.round(j * 1000000) / 1000000 / 4; // TODO This 4 is to be removed. + let label = ' ' + Math.round(j * 1000000) / 1000000; let labelPoint = { x: viewPort.visibleArea.bottomRight.x + 1, diff --git a/Dashboard.js b/Dashboard.js index 2e039b20b..e1924efa4 100644 --- a/Dashboard.js +++ b/Dashboard.js @@ -128,7 +128,7 @@ const EARLIEST_DATE = new Date(2014, 0, 18, 4, 26, 8); const FONT_ASPECT_RATIO = 0.60; const CHART_ASPECT_RATIO = 1 / 10; -const INITIAL_ZOOM_LEVEL = -22; // This is the zoom level at the view port in which the APP starts. +let INITIAL_ZOOM_LEVEL = -26; // This is the zoom level at the view port in which the APP starts. let INITIAL_TIME_PERIOD = recalculatePeriod(INITIAL_ZOOM_LEVEL); // This value will be overwritten at the viewPort.initialize if the user had a prevous session with this same browser. var viewPort = newViewPort(); @@ -153,7 +153,7 @@ const PERIOD_10_MIN = "10-min"; const PERIOD_05_MIN = "05-min"; const PERIOD_01_MIN = "01-min"; -const INITIAL_DATE = new Date(); // This value will be overwritten at the viewPort.initialize if the user had a prevous session with this same browser. +let INITIAL_DATE = new Date(); // This value will be overwritten at the viewPort.initialize if the user had a prevous session with this same browser. var maxDate = new Date(); maxDate.setMilliseconds(0); diff --git a/Panels/TimeControlPanel.js b/Panels/TimeControlPanel.js index f6b3e98a5..a80248771 100644 --- a/Panels/TimeControlPanel.js +++ b/Panels/TimeControlPanel.js @@ -88,20 +88,6 @@ function newTimeControlPanel() { let datetime = INITIAL_DATE; - /* There is a chance that we could resume the last session at the place we left it. */ - - let lastRun = window.localStorage.getItem('webApp.lastRun.screenResolution'); - - if (lastRun !== null && lastRun === browserCanvas.width + '.' + browserCanvas.height) { - - let savedCurrentDatetime = window.localStorage.getItem('timeControlPanel.currentDatetime'); - - if (savedCurrentDatetime !== null) { - datetime = new Date(savedCurrentDatetime); - - } - } - var datetimeDisplay = { currentDatetime: datetime, addTime: addTime, @@ -166,8 +152,6 @@ function newTimeControlPanel() { thisObject.datetimeDisplay.currentDatetime = newDatetime; - window.localStorage.setItem('timeControlPanel.currentDatetime', newDatetime); - } diff --git a/TimelineChart.js b/TimelineChart.js index 7e317abab..8359a93a2 100644 --- a/TimelineChart.js +++ b/TimelineChart.js @@ -65,15 +65,11 @@ chartGrid = newChartGrid(); recalculateScale(); - - /* When the Web App has never run, we need to position the viewPort at the current Date. This in turn will set the current date as the date for loading the data. */ - - let lastRun = window.localStorage.getItem('webApp.lastRun.screenResolution'); - - if (lastRun === null || lastRun !== browserCanvas.width + '.' + browserCanvas.height) { - - moveViewPortToCurrentDatetime(); - } + + //moveViewPortToCurrentDatetime(); + moveToUserPosition(container, timeLineCoordinateSystem); + timePeriod = INITIAL_TIME_PERIOD; + datetime = INITIAL_DATE; /* Event Subscriptions - we need this events to be fired first here and then in active Plotters. */ @@ -504,18 +500,11 @@ if (INFO_LOG === true) { logger.write("[INFO] initializeProductPlotter -> onProductStorageInitialized -> onPlotterInizialized -> Entering function."); } if (INFO_LOG === true) { logger.write("[INFO] initializeProductPlotter -> onProductStorageInitialized -> onPlotterInizialized -> key = " + pProductCard.product.plotter.devTeam + "-" + pProductCard.product.plotter.codeName + "-" + pProductCard.product.plotter.moduleName); } - /* When the Web App has never run, we need to position the viewPort at the current Date. Otherwise it will get automatically positioned at the last thing the user was browsing. */ - - let lastRun = window.localStorage.getItem('webApp.lastRun.screenResolution'); - - if (lastRun === null || lastRun !== browserCanvas.width + '.' + browserCanvas.height) { - - try { - plotter.positionAtDatetime(INITIAL_DATE); - } catch (err) { - // If the plotter does not implement this function its ok. - } - } + try { + //plotter.positionAtDatetime(INITIAL_DATE); + } catch (err) { + // If the plotter does not implement this function its ok. + } let productPlotter = { productCard: pProductCard, @@ -724,6 +713,8 @@ recalculateCurrentDatetime(); + saveUserPosition(thisObject.container, timeLineCoordinateSystem); + } } @@ -733,6 +724,12 @@ if (initializationReady === true) { + if (thisObject.container.frame.isInViewPort()) { + + saveUserPosition(thisObject.container, timeLineCoordinateSystem); + + } + if (thisObject.container.frame.isInViewPort() && tooSmall() === false) { @@ -869,7 +866,7 @@ let maxValue = { x: MAX_PLOTABLE_DATE.valueOf(), - y: nextPorwerOf10(USDT_BTC_HTH) + y: nextPorwerOf10(USDT_BTC_HTH) / 4 }; diff --git a/Utilities.js b/Utilities.js index 910e8ccc8..d8f5fe355 100644 --- a/Utilities.js +++ b/Utilities.js @@ -50,7 +50,6 @@ function getDateFromPoint(point, container, timeLineCoordinateSystem) { return date; } - function getRateFromPoint(point, container, timeLineCoordinateSystem) { point = unTransformThisPoint(point, container); @@ -59,7 +58,6 @@ function getRateFromPoint(point, container, timeLineCoordinateSystem) { return point.y; } - function getMilisecondsFromPoint(point, container, timeLineCoordinateSystem) { point = unTransformThisPoint(point, container); @@ -68,6 +66,70 @@ function getMilisecondsFromPoint(point, container, timeLineCoordinateSystem) { return point.x; } +function saveUserPosition(container, timeLineCoordinateSystem) { + + let centerPoint = { + x: (viewPort.visibleArea.bottomRight.x - viewPort.visibleArea.topLeft.x) / 2, + y: (viewPort.visibleArea.bottomRight.y - viewPort.visibleArea.topLeft.y) / 2 + }; + + let userPosition = { + date: getDateFromPoint(centerPoint, container, timeLineCoordinateSystem), + rate: getRateFromPoint(centerPoint, container, timeLineCoordinateSystem), + market: DEFAULT_MARKET, + zoom: viewPort.zoomTargetLevel + }; + + window.localStorage.setItem('userPosition', JSON.stringify(userPosition)); +} + +function moveToUserPosition(container, timeLineCoordinateSystem) { + + let savedPosition = window.localStorage.getItem('userPosition'); + let userPosition; + + if (savedPosition === null) { + + userPosition = { + date: (new Date()).toString(), + rate: timeLineCoordinateSystem.max.y / 2, + market: DEFAULT_MARKET, + zoom: INITIAL_ZOOM_LEVEL + }; + + } else { + + userPosition = JSON.parse(savedPosition); + } + + let centerPoint = { + x: (viewPort.visibleArea.bottomRight.x - viewPort.visibleArea.topLeft.x) / 2, + y: (viewPort.visibleArea.bottomRight.y - viewPort.visibleArea.topLeft.y) / 2 + }; + + viewPort.newZoomLevel(userPosition.zoom); + INITIAL_TIME_PERIOD = recalculatePeriod(userPosition.zoom); + INITIAL_DATE = new Date (userPosition.date); + + let targetPoint = { + x: (new Date(userPosition.date)).valueOf(), + y: userPosition.rate + }; + + /* Put this point in the coordinate system of the viewPort */ + + targetPoint = timeLineCoordinateSystem.transformThisPoint(targetPoint); + targetPoint = transformThisPoint(targetPoint, container); + + /* Lets calculate the displace vector, from the point we want at the center, to the current center. */ + + let displaceVector = { + x: centerPoint.x - targetPoint.x, + y: centerPoint.y - targetPoint.y + }; + + viewPort.displace(displaceVector); +} function removeTime(datetime) { diff --git a/ViewPort.js b/ViewPort.js index 9c5d7f18d..f30d0aae3 100644 --- a/ViewPort.js +++ b/ViewPort.js @@ -12,8 +12,10 @@ function newViewPort() { let thisObject = { visibleArea: undefined, eventHandler: undefined, + zoomTargetLevel:undefined, zoomLevel: undefined, mousePosition: undefined, + newZoomLevel: newZoomLevel, applyZoom: applyZoom, zoomFontSize: zoomFontSize, zoomThisPoint: zoomThisPoint, @@ -52,8 +54,6 @@ function newViewPort() { thisObject.eventHandler = newEventHandler(); - let zoomTargetLevel; - return thisObject; function initialize() { @@ -64,46 +64,6 @@ function newViewPort() { bottomRight: { x: browserCanvas.width - RIGHT_MARGIN, y: browserCanvas.height - BOTTOM_MARGIN}, bottomLeft: { x: LEFT_MARGIN, y: browserCanvas.height - BOTTOM_MARGIN} }; - - /* There is a chance that we could resume the last session at the place we left it. */ - - let lastRun = window.localStorage.getItem('webApp.lastRun.screenResolution'); - - if (lastRun !== null && lastRun === browserCanvas.width + '.' + browserCanvas.height) { - - /* We get from the local storage the recored values of Zoom and OffSet during the last session using this app with this browser */ - - let savedZoomTargetLevel = window.localStorage.getItem('viewPort.zoomTargetLevel'); - - if (savedZoomTargetLevel !== null) { - - thisObject.zoomLevel = Number(savedZoomTargetLevel); - zoomTargetLevel = Number(savedZoomTargetLevel); - - INITIAL_TIME_PERIOD = recalculatePeriod(thisObject.zoomLevel); - } - - let savedOffset = window.localStorage.getItem('viewPort.offset'); - - if (savedOffset !== null) { - - offset = JSON.parse(savedOffset); - } - - let savedTargetOffset = window.localStorage.getItem('viewPort.targetOffset'); - - if (savedTargetOffset !== null) { - - targetOffset = JSON.parse(savedTargetOffset); - } - - } else { - - thisObject.zoomLevel = INITIAL_ZOOM_LEVEL; - zoomTargetLevel = INITIAL_ZOOM_LEVEL; - - INITIAL_TIME_PERIOD = recalculatePeriod(thisObject.zoomLevel); - } } function raiseEvents() { @@ -118,17 +78,17 @@ function newViewPort() { function animate() { - if (thisObject.zoomLevel < zoomTargetLevel) { - if (zoomTargetLevel - thisObject.zoomLevel < ANIMATION_INCREMENT) { - ANIMATION_INCREMENT = Math.abs(zoomTargetLevel - thisObject.zoomLevel); + if (thisObject.zoomLevel < thisObject.zoomTargetLevel) { + if (thisObject.zoomTargetLevel - thisObject.zoomLevel < ANIMATION_INCREMENT) { + ANIMATION_INCREMENT = Math.abs(thisObject.zoomTargetLevel - thisObject.zoomLevel); } thisObject.zoomLevel = thisObject.zoomLevel + ANIMATION_INCREMENT; changeZoom(thisObject.zoomLevel - ANIMATION_INCREMENT, thisObject.zoomLevel); } - if (thisObject.zoomLevel > zoomTargetLevel) { - if (thisObject.zoomLevel - zoomTargetLevel < ANIMATION_INCREMENT) { - ANIMATION_INCREMENT = Math.abs(zoomTargetLevel - thisObject.zoomLevel); + if (thisObject.zoomLevel > thisObject.zoomTargetLevel) { + if (thisObject.zoomLevel - thisObject.zoomTargetLevel < ANIMATION_INCREMENT) { + ANIMATION_INCREMENT = Math.abs(thisObject.zoomTargetLevel - thisObject.zoomLevel); } thisObject.zoomLevel = thisObject.zoomLevel - ANIMATION_INCREMENT; changeZoom(thisObject.zoomLevel + ANIMATION_INCREMENT, thisObject.zoomLevel); @@ -171,8 +131,6 @@ function newViewPort() { thisObject.eventHandler.raiseEvent("Offset Changed", event); - window.localStorage.setItem('viewPort.offset', JSON.stringify(offset)); - //console.log("displace produced new Offset x = " + offset.x + " y = " + offset.y); } @@ -189,12 +147,27 @@ function newViewPort() { y: (targetOffset.y - offset.y) / 10 }; - window.localStorage.setItem('viewPort.targetOffset', JSON.stringify(targetOffset)); - //console.log("displaceTarget x = " + targetOffset.x + " y = " + targetOffset.y); } - + + function newZoomLevel(level) { + + thisObject.zoomTargetLevel = level; + thisObject.zoomLevel = level; + + ANIMATION_INCREMENT = Math.abs(thisObject.zoomTargetLevel - thisObject.zoomLevel) / 3; + + let event = { + newLevel: thisObject.zoomTargetLevel, + newOffset: offset, + type: undefined + }; + + thisObject.eventHandler.raiseEvent("Zoom Changed", event); + + return true; + } function applyZoom(amount) { @@ -203,56 +176,56 @@ function newViewPort() { if (amount > 0) { - if (zoomTargetLevel > -5) { + if (thisObject.zoomTargetLevel > -5) { amount = amount * 2; } - if (zoomTargetLevel > 10) { + if (thisObject.zoomTargetLevel > 10) { amount = amount * 3; } - if (zoomTargetLevel > 15) { + if (thisObject.zoomTargetLevel > 15) { amount = amount * 4; } } if (amount < 0) { - if (zoomTargetLevel > -4) { + if (thisObject.zoomTargetLevel > -4) { amount = amount * 2; } - if (zoomTargetLevel > 13) { + if (thisObject.zoomTargetLevel > 13) { amount = amount * 3; } - if (zoomTargetLevel > 19) { + if (thisObject.zoomTargetLevel > 19) { amount = amount * 4; } } - if (zoomTargetLevel + amount > 1000) { + if (thisObject.zoomTargetLevel + amount > 1000) { return false; } - if (zoomTargetLevel <= -27 && amount < 0) { + if (thisObject.zoomTargetLevel <= -27 && amount < 0) { amount = amount / 4; } - if (zoomTargetLevel < -27 && amount > 0) { + if (thisObject.zoomTargetLevel < -27 && amount > 0) { amount = amount / 4; } - if (zoomTargetLevel + amount < -28.25) { + if (thisObject.zoomTargetLevel + amount < -28.25) { return false; } - zoomTargetLevel = zoomTargetLevel + amount; + thisObject.zoomTargetLevel = thisObject.zoomTargetLevel + amount; - ANIMATION_INCREMENT = Math.abs(zoomTargetLevel - thisObject.zoomLevel) / 3; + ANIMATION_INCREMENT = Math.abs(thisObject.zoomTargetLevel - thisObject.zoomLevel) / 3; let event = { - newLevel: zoomTargetLevel, + newLevel: thisObject.zoomTargetLevel, newOffset: offset, type: undefined }; @@ -263,12 +236,8 @@ function newViewPort() { event.type = "Zoom Out"; } - //console.log("Zoom Changed > " + event.newLevel); - thisObject.eventHandler.raiseEvent("Zoom Changed", event); - window.localStorage.setItem('viewPort.zoomTargetLevel', zoomTargetLevel); - return true; } @@ -281,15 +250,9 @@ function newViewPort() { offset.x = offset.x - newMouse.x + thisObject.mousePosition.x; offset.y = offset.y - newMouse.y + thisObject.mousePosition.y; - window.localStorage.setItem('viewPort.offset', JSON.stringify(offset)); - - //console.log("changeZoom produced new Offset x = " + offset.x + " y = " + offset.y); - targetOffset.x = offset.x targetOffset.y = offset.y ; - window.localStorage.setItem('viewPort.targetOffset', JSON.stringify(targetOffset)); - offsetIncrement = { x: 0, y: 0