-
Notifications
You must be signed in to change notification settings - Fork 25
/
uistate.js
401 lines (361 loc) · 11.2 KB
/
uistate.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
msc {
hscale="1.2";
html [label="index.html", textbgcolor="#ddf"]
, ui [label="mscgenui.js"]
, msc [label="xuparser.js"]
, render [label="mscrender.js"]
, utls [label="mscrenderutensils.js"]
, doc [label="window.document", textbgcolor="#ddf"];
html => ui [label="render"];
ui =>> doc [label="get input from textarea"];
doc >> ui [label="text"];
ui =>> msc [label="parse(text)"];
--- [label="[hunky dory]", linecolor="green"];
ui << msc [label="AST"];
ui => render [label="renderAST(AST, text)"];
render => utls [label="low level helpers"];
utls => doc [label="all kinds of dom manipulation"];
render => doc [label="all kinds of dom manipulation"];
render note render [label="move dom manipulation down?"];
--- [label="[parse error]", linecolor="red"];
ui << msc [label="exception"];
ui =>> ui [label="show error"];
ui =>> html [label="show error"];
|||;
ui note msc [label="There's a parser for mscgen and a separate one for ms genny.\nFor simplicity only showning one.",
textbgcolor="#ffe"];
}
*/
/* eslint max-statements: 0 */
var xuparser = require('mscgenjs/dist/cjs/parse/xuparser')
var msgennyparser = require('mscgenjs/dist/cjs/parse/msgennyparser')
var renderast = require('mscgenjs/dist/cjs/render/graphics/renderast')
var ast2msgenny = require('mscgenjs/dist/cjs/render/text/ast2msgenny')
var ast2xu = require('mscgenjs/dist/cjs/render/text/ast2xu')
var maps = require('../utl/maps')
var $ = require('../utl/domutl')
var exporter = require('../utl/exporter')
var populateLists = require('./populate-lists')
var state = require('./state')
var gCodeMirror = {}
var gErrorCoordinates = {
line: 0,
column: 0
}
function requestRender () {
if (state.getAutoRender()) {
render(getSource(), state.getLanguage())
}
}
function getASTBare (pSource, pLanguage) {
if (pLanguage === 'msgenny') {
return msgennyparser.parse(pSource)
} else if (pLanguage === 'json') {
return JSON.parse(pSource)
} // we use the xu parser for both mscgen and xu:
return xuparser.parse(pSource)
}
function getAST (pLanguage, pSource) {
var lLanguage = pLanguage || state.getLanguage()
var lSource = pSource || getSource()
return getASTBare(lSource, lLanguage)
}
function clear () {
if (['mscgen', 'xu'].indexOf(state.getLanguage()) > -1) {
setSource('msc{\n \n}')
setCursorInSource(1, 3)
} else {
setSource('')
}
}
function renderSource (pAST, pLanguage) {
var lTargetSource = ''
if (pLanguage === 'msgenny') {
lTargetSource = ast2msgenny.render(pAST)
} else if (pLanguage === 'json') {
lTargetSource = JSON.stringify(pAST, null, ' ')
} else { // for rendering mscgen we use the xu renderer
lTargetSource = ast2xu.render(pAST)
}
return lTargetSource
}
function getSource () {
return gCodeMirror.getValue()
}
function setSource (pSource) {
gCodeMirror.setValue(pSource)
}
function setCursorInSource (pLine, pColumn) {
gCodeMirror.setCursor(pLine, pColumn)
gCodeMirror.focus()
}
function setLanguage (pLanguage) {
state.setLanguage(pLanguage)
gCodeMirror.setOption('mode', maps.language2Mode(pLanguage))
window.__language_mscgen.checked = false
window.__language_msgenny.checked = false
window.__language_json.checked = false
if (pLanguage === 'msgenny') {
window.__language_msgenny.checked = true
$.ss(window.__btn_more_color_schemes).hide()
window.__color_panel.style.width = '0'
} else if (pLanguage === 'json') {
window.__language_json.checked = true
$.ss(window.__btn_more_color_schemes).show()
} else /* "mscgen" === pLanguage || "xu" === pLanguage */{
window.__language_mscgen.checked = true
$.ss(window.__btn_more_color_schemes).show()
}
requestRender()
}
function handleRenderException (pException, pSource) {
if (pException.location) {
gErrorCoordinates.line = pException.location.start.line
gErrorCoordinates.column = pException.location.start.column
displayError(
'Line ' + pException.location.start.line + ', column ' +
pException.location.start.column + ': ' + pException.message,
'>>> ' + pSource.split('\n')[pException.location.start.line - 1] + ' <<<'
)
} else {
gErrorCoordinates.line = 0
gErrorCoordinates.column = 0
displayError(pException.message)
}
}
function render (pSource, pLanguage) {
preRenderReset()
try {
var lAST = getASTBare(pSource, pLanguage)
if (state.getDebug()) {
try {
window.history.replaceState(
{},
'',
exporter.toLocationString(
window.location,
pSource,
maps.correctLanguage(
lAST.meta.extendedFeatures,
pLanguage
),
state.getMirrorEntities(),
state.getNamedStyle()
)
)
} catch (e) {
// on chrome window.history.replaceState barfs when
// the interpreter runs from a file:// instead of
// from a server. This try/ catch is a crude way
// to handle that without breaking the rest of the flow
}
}
renderast.render(
lAST,
window,
'__svg',
{
source: state.getIncludeSource() ? pSource : null,
mirrorEntitiesOnBottom: state.getMirrorEntities(),
regularArcTextVerticalAlignment: state.getVerticalLabelAlignment(),
additionalTemplate: state.getNamedStyle()
}
)
if (lAST.entities.length > 0) {
showRenderSuccess(lAST.meta)
}
} catch (e) {
handleRenderException(e, pSource)
}
}
function preRenderReset () {
hideError()
$.ss(window.__output_controls_area).hide()
$.ss(window.__placeholder).show('flex')
$.ss(window.__svg).hide()
renderast.clean('__svg', window)
}
function showRenderSuccess (pMeta) {
$.ss(window.__output_controls_area).show()
$.ss(window.__placeholder).hide()
$.ss(window.__svg).show()
showExtendedArcTypeFeatures(pMeta)
showExtendedFeatures(pMeta)
state.setLanguage(maps.correctLanguage(pMeta.extendedFeatures, state.getLanguage()))
}
function showExtendedArcTypeFeatures (pMeta) {
if (pMeta && pMeta.extendedArcTypes === true) {
$.ss(window.__show_anim).hide()
} else {
$.ss(window.__show_anim).show()
}
}
function showExtendedFeatures (pMeta) {
if (pMeta && pMeta.extendedFeatures === true) {
$.ss(window.__xu_notify).show()
} else {
$.ss(window.__xu_notify).hide()
}
}
function setAutoRender (pAutoRender) {
state.setAutoRender(pAutoRender)
if (pAutoRender) {
window.__autorender.checked = true
$.ss(window.__btn_render).hide()
} else {
window.__autorender.checked = false
$.ss(window.__btn_render).show()
}
}
function hideError () {
$.ss(window.__error).hide()
window.__error_output.textContent = ''
window.__error_context.textContent = ''
}
function displayError (pError, pContext) {
$.ss(window.__error).show()
$.ss(window.__placeholder).hide()
window.__error_output.textContent = pError
window.__error_context.textContent = pContext
}
module.exports = {
init: function (pCodeMirror) {
gCodeMirror = pCodeMirror
setAutoRender(state.getAutoRender())
setLanguage(state.getLanguage())
populateLists.initSamples(state.getDebug())
populateLists.initNamedStyles()
if (window.__loading) {
window.__loading.outerHTML = ''
}
},
switchLanguage: function (pLanguage) {
var lAST = {}
try {
lAST = getAST()
if (lAST !== {}) {
setSource(renderSource(lAST, pLanguage))
}
} catch (e) {
// do nothing
}
setLanguage(pLanguage)
},
manipulateSource: function (pFunction) {
var lAST = {}
try {
lAST = getAST()
if (lAST !== {}) {
setSource(
renderSource(
pFunction(lAST),
state.getLanguage()
)
)
}
} catch (e) {
// do nothing
}
},
setSample: function (pURL) {
if (pURL === 'none' || !pURL) {
clear()
} else {
$.ajax(
pURL,
function onSuccess (pEvent) {
setLanguage(maps.classifyExtension(pURL))
setSource(pEvent.target.response)
},
function onError () {
setSource("# could not find or open '" + pURL + "'")
}
)
}
},
errorOnClick: function () {
setCursorInSource(gErrorCoordinates.line - 1, gErrorCoordinates.column - 1)
},
/**
* parses + renders the given source in the given language
* @type {string} source
* @type {string} language (one of mscgen, xu, msgenny or json)
*/
render,
/**
* parse + renders the current source in the current
* language if 'autorender' is on
*/
requestRender,
getAutoRender: state.getAutoRender,
setAutoRender,
getSource,
setSource,
getLanguage: state.getLanguage,
setLanguage,
getDebug: state.getDebug,
setDebug: function (pBoolean) {
state.setDebug(pBoolean)
if (state.getDebug()) {
$.doForAllOfClass('debug', function (pDomNode) {
$.ss(pDomNode).show()
})
}
},
getAST,
getLinkToInterpeter: state.getLinkToInterpreter,
setLinkToInterpeter: function (pBoolean) {
state.setLinkToInterpreter(pBoolean)
window.__link_to_interpreter.checked = pBoolean
},
setMirrorEntities: function (pBoolean) {
state.setMirrorEntities(pBoolean)
window.__option_mirror_entities.checked = pBoolean
},
getMirrorEntities: state.getMirrorEntities,
setIncludeSource: function (pBoolean) {
state.setIncludeSource(pBoolean)
window.__option_include_source.checked = pBoolean
},
getIncludeSource: state.getIncludeSource,
setNamedStyle: function (pStyle) {
window.__option_style_basic.checked = false
window.__option_style_inverted.checked = false
window.__option_style_grayscaled.checked = false
window.__option_style_fountainpen.checked = false
window.__option_style_lazy.checked = false
window.__option_style_cygne.checked = false
window.__option_style_pegasse.checked = false
window.__option_style_classic.checked = false
window.__option_style_noentityboxes.checked = false
var lOptionToCheck = document.getElementById('__option_style_' + pStyle)
if (lOptionToCheck) {
lOptionToCheck.checked = true
state.setNamedStyle(pStyle)
} else {
window.__option_style_basic.checked = true
state.setNamedStyle('basic')
}
},
getNamedStyle: state.getNamedStyle,
setVerticalLabelAlignment: function (pVerticalLabelAlignment) {
window.__option_vertical_label_alignment.value = pVerticalLabelAlignment
state.setVerticalLabelAlignment(pVerticalLabelAlignment)
},
getVerticalLabelAlignment: state.getVerticalLabelAlignment,
preRenderReset
}
/*
This file is part of mscgen_js.
mscgen_js is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
mscgen_js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with mscgen_js. If not, see <http://www.gnu.org/licenses/>.
*/