forked from jscad/OpenJSCAD.org
/
index.js
245 lines (210 loc) · 8.21 KB
/
index.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// == OpenJSCAD.org, Copyright (c) 2013-2016, Licensed under MIT License
const { setUpEditor } = require('./editor')
const { setupDragDrop } = require('./dragDrop/ui-drag-drop') // toggleAutoReload
const { detectBrowser } = require('./detectBrowser')
const { getUrlParams } = require('./urlHelpers')
const { createExamples, fetchExample, loadInitialExample } = require('./examples')
const { createOptions, getOptions } = require('./options')
const AlertUserOfUncaughtExceptions = require('./errorDispatcher')
const { version } = require('../jscad/version')
const Processor = require('../jscad/processor')
const me = document.location.toString().match(/^file:/) ? 'web-offline' : 'web-online'
const browser = detectBrowser()
const showEditor = true
const proxyUrl = './remote.pl?url='
// const proxyUrl = './remote.php?url='
var gProcessor = null
var gEditor = null
var memFs = [] // associated array, contains file content in source memFs[i].{name,source}
var currentFiles = [] // linear array, contains files (to read)
function getElementHeight (element) {
return parseInt(getComputedStyle(element).height)
}
function getElementWidth (element) {
return parseInt(getComputedStyle(element).width)
}
function setElementHeight (element, height) {
element.style.height = `${height}`
}
function setElementWidth (element, width) {
element.style.width = `${width}`
}
function addEventListenerList (list, event, callback) {
Array
.from(list)
.forEach(element => element.addEventListener(event, callback))
}
function init () {
// Show all exceptions to the user: // WARNING !! this is not practical at dev time
AlertUserOfUncaughtExceptions()
getUrlParams(document.URL)
gProcessor = new Processor(document.getElementById('viewerContext'))
gEditor = setUpEditor(undefined, gProcessor)
// FIXME: temporary hack
// major DIVs expected
let about = document.getElementById('about');
let header = document.getElementById('header');
let menu = document.getElementById('menu');
let tail = document.getElementById('tail');
let footer = document.getElementById('footer');
let editFrame = document.getElementById('editFrame');
//createOptions()
//getOptions()
let options = document.getElementById('options')
let optionsTitle = document.getElementById('optionsTitle')
let plate = document.getElementById('plate')
window.addEventListener('resize', function () {
// adjust the relevant divs
if (menu) {
setElementHeight(menu, window.innerHeight + 'px')
menuHandle.style.top = '45%'
}
if (editFrame) {
setElementHeight(editFrame, window.innerHeight + 'px')
}
})
if (menu) {
const versionText = `Version ${version}`
document.getElementById('menuVersion').innerHTML = versionText
let initialMenuHidingTimeoutID = setTimeout(function () {
initialMenuHidingTimeoutID = null
menu.style.left = '-280px'
menuHandle.src = 'imgs/menuHandleVLOut.png'
if (examples) {
setElementHeight(examples, '0px')
examples.style.display = 'none'
}
}, 3000) // -- hide slide-menu after 3secs
let examples = document.getElementById('examples');
if (examples) {
createExamples(me)
loadInitialExample(me, {memFs, gProcessor, gEditor, proxyUrl})
// -- Examples
examplesTitle.addEventListener('click', function (e) {
if (initialMenuHidingTimeoutID !== null) {
clearTimeout(initialMenuHidingTimeoutID)
initialMenuHidingTimeoutID = null
}
// When closed, examples.style.display may be '' or 'none'.
// When open, it's reliably 'inline', so test against that.
if (examples.style.display === 'inline') {
setElementHeight(examples, '0px')
examples.style.display = 'none'
} else {
setElementHeight(examples, 'auto')
examples.style.display = 'inline'
}
})
function onLoadExampleClicked (e) {
// Don't need to cancel initial menu-hiding timeout, because in order
// to get here, user must have clicked on Examples which canceled it
// already.
if (showEditor) { // FIXME test for the element
editor.style.display = 'inline'
} else {
editor.style.display = 'none'
}
const examplePath = e.currentTarget.dataset.path
fetchExample(examplePath, undefined, {memFs, gProcessor, gEditor})
}
var list = examples.querySelectorAll('.example')
for (var i = 0; i < list.length; i++) {
list[i].addEventListener('click', onLoadExampleClicked)
}
}
let menuHandle = document.getElementById('menuHandle')
menuHandle.addEventListener('click', function (e) {
if (initialMenuHidingTimeoutID !== null) {
clearTimeout(initialMenuHidingTimeoutID)
initialMenuHidingTimeoutID = null
}
// When open, left may be '' or '0' or '0px'. When closed, it's reliably
// '-280px', so test against that.
if (menu.style.left === '-280px') {
// It's closed; open it.
menu.style.left = '0'
menuHandle.src = 'imgs/menuHandleVLIn.png'
} else {
// It's open; close it, and close Examples with it.
menu.style.left = '-280px'
menuHandle.src = 'imgs/menuHandleVLOut.png'
if (examples) {
setElementHeight(examples, '0px')
examples.style.display = 'none'
}
}
})
}
if (editFrame) {
let editHandle = document.getElementById('editHandle')
editHandle.addEventListener('click', function (e) {
if (getElementWidth(editFrame) === 0) {
setElementWidth(editFrame, '40%')
editHandle.src = 'imgs/editHandleIn.png'
} else {
setElementWidth(editFrame, '0px')
editHandle.src = 'imgs/editHandleOut.png'
}
})
}
// -- Options
// FIXME: these don't exist ??? where are options handled in the first place ?
/*optionsTitle.addEventListener('click', function (e) {
setElementHeight(options, 'auto')
editor.style.display = 'inline'
setElementHeight(examples, '0px')
editor.style.display = 'none'
})
options.addEventListener('mouseleave', function (e) {
setElementHeight(options, 0)
options.style.display = 'none'
})
optionsForm.addEventListener('change', saveOptions())
plate.addEventListener('change', function () {
if (plate.value === 'custom') {
plate.style.display = 'inline'
} else {
plate.style.display = 'none'
}
})*/
// about/ licence section
if (about) {
const versionText = `Version ${version}`
document.getElementById('aboutVersion').innerHTML = versionText
addEventListenerList(document.getElementsByClassName('navlink about'), 'click', function () {
about.style.display = 'inline'
return false
})
document.querySelector('.okButton').addEventListener('click', function () {
about.style.display = 'none'
return false
})
}
// dropzone section
if (tail) {
let dropZone = document.getElementById('filedropzone')
if (dropZone) {
const dropZoneText = browser === 'chrome' && me === 'web-online' ? ', or folder with jscad files ' : ''
document.getElementById('filedropzone_empty')
.innerHTML =
`Drop one or more supported files
${dropZoneText}
here (see <a style='font-weight: normal' href='https://en.wikibooks.org/wiki/OpenJSCAD_User_Guide#Maintaining_Larger_Projects' target=_blank>details</a>)
<br>or directly edit OpenJSCAD or OpenSCAD code using the editor.`
let {toggleAutoReload, reloadAllFiles} = setupDragDrop(me, {memFs, gProcessor, gEditor})
document.getElementById('reloadAllFiles').onclick = reloadAllFiles
document.getElementById('autoreload').onclick = function (e) {
const toggle = document.getElementById('autoreload').checked
toggleAutoReload(toggle)
}
}
}
// version number displays
if (footer) {
const footerContent = `OpenJSCAD.org ${version}, MIT License, get your own copy/clone/fork from <a target=_blank href="https://github.com/Spiritdude/OpenJSCAD.org">GitHub: OpenJSCAD</a>`
document.getElementById('footer').innerHTML = footerContent
}
}
document.addEventListener('DOMContentLoaded', function (event) {
init()
})