forked from AAMasters/UI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utilities.js
208 lines (157 loc) · 5.68 KB
/
Utilities.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
function getSimulationParams () {
let dateAtScreenCorner = new Date(window.localStorage.getItem('Date @ Screen Corner'))
let currentTimePeriod = JSON.parse(window.localStorage.getItem('Current Time Period'))
let timePeriodsMasterArray = [marketFilesPeriods, dailyFilePeriods]
let timePeriodArray = timePeriodsMasterArray[currentTimePeriod.filePeriodIndex]
let timePeriod = timePeriodArray[currentTimePeriod.timePeriodIndex][1]
let timePeriodDailyArray = []
let timePeriodMarketArray = []
switch (currentTimePeriod.filePeriodIndex) {
case 0: {
timePeriodMarketArray.push(timePeriod)
break
}
case 1: {
timePeriodDailyArray.push(timePeriod)
break
}
}
let simulationParams = {
beginDatetime: dateAtScreenCorner.valueOf(),
resumeExecution: false,
timePeriodDailyArray: timePeriodDailyArray,
timePeriodMarketArray: timePeriodMarketArray,
timestamp: (new Date()).valueOf()
}
return simulationParams
}
function transformThisPoint (point, container) {
/* We make the point relative to the current frame */
point = container.frame.frameThisPoint(point)
/* We add the possible displacement */
point = container.displacement.displaceThisPoint(point)
/* We viewport Transformation. */
point = viewPort.zoomThisPoint(point)
return point
}
function unTransformThisPoint (point, container) {
point = viewPort.unzoomThisPoint(point)
point = container.displacement.undisplaceThisPoint(point)
point = container.frame.unframeThisPoint(point)
return point
}
function nextPorwerOf10 (number) {
for (let i = -10; i <= 10; i++) {
if (number < Math.pow(10, i)) {
return Math.pow(10, i)
}
}
}
function pad (str, max) {
str = str.toString()
return str.length < max ? pad('0' + str, max) : str
}
function getDateFromPoint (point, container, timeLineCoordinateSystem) {
point = unTransformThisPoint(point, container)
point = timeLineCoordinateSystem.unInverseTransform(point, container.frame.height)
let date = new Date(point.x)
return date
}
function getRateFromPoint (point, container, timeLineCoordinateSystem) {
point = unTransformThisPoint(point, container)
point = timeLineCoordinateSystem.unInverseTransform(point, container.frame.height)
return point.y
}
function getMilisecondsFromPoint (point, container, timeLineCoordinateSystem) {
point = unTransformThisPoint(point, container)
point = timeLineCoordinateSystem.unInverseTransform(point, container.frame.height)
return point.x
}
function saveUserPosition (container, timeLineCoordinateSystem, position) {
if (position === undefined) {
position = {
x: (viewPort.visibleArea.bottomRight.x - viewPort.visibleArea.topLeft.x) / 2,
y: (viewPort.visibleArea.bottomRight.y - viewPort.visibleArea.topLeft.y) / 2
}
}
let userPosition = {
date: getDateFromPoint(position, container, timeLineCoordinateSystem),
rate: getRateFromPoint(position, container, timeLineCoordinateSystem),
market: DEFAULT_MARKET,
zoom: viewPort.zoomTargetLevel
}
window.localStorage.setItem('userPosition', JSON.stringify(userPosition))
}
function getUserPosition (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)
}
userPosition.point = {
x: (new Date(userPosition.date)).valueOf(),
y: userPosition.rate
}
return userPosition
}
function moveToUserPosition (container, timeLineCoordinateSystem, ignoreX, ignoreY, center, considerZoom) {
let userPosition = getUserPosition(timeLineCoordinateSystem)
if (considerZoom === true) {
viewPort.newZoomLevel(userPosition.zoom)
}
INITIAL_TIME_PERIOD = recalculatePeriod(userPosition.zoom)
NEW_SESSION_INITIAL_DATE = new Date(userPosition.date)
let targetPoint = {
x: (new Date(userPosition.date)).valueOf(),
y: userPosition.rate
}
/* Put this po int in the coordinate system of the viewPort */
targetPoint = timeLineCoordinateSystem.transformThisPoint(targetPoint)
targetPoint = transformThisPoint(targetPoint, container)
let centerPoint
if (center !== undefined) {
centerPoint = {
x: center.x,
y: center.y
}
} else {
centerPoint = {
x: (viewPort.visibleArea.bottomRight.x - viewPort.visibleArea.topLeft.x) / 2,
y: (viewPort.visibleArea.bottomRight.y - viewPort.visibleArea.topLeft.y) / 2
}
}
/* 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
}
if (ignoreX) { displaceVector.x = 0 }
if (ignoreY) { displaceVector.y = 0 }
viewPort.displace(displaceVector)
}
function removeTime (datetime) {
if (datetime === undefined) { return }
let dateOnly = new Date(Math.trunc(datetime.valueOf() / ONE_DAY_IN_MILISECONDS) * ONE_DAY_IN_MILISECONDS)
return dateOnly
}
function printLabel (labelToPrint, x, y, opacity, fontSize) {
let labelPoint
browserCanvasContext.font = fontSize + 'px ' + UI_FONT.PRIMARY
let label = '' + labelToPrint
let xOffset = label.length / 2 * fontSize * FONT_ASPECT_RATIO
browserCanvasContext.fillStyle = 'rgba(' + UI_COLOR.DARK + ', ' + opacity + ')'
browserCanvasContext.fillText(label, x, y)
}
function newUniqueId () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}