generated from edemaine/webapp-coffee-pug-stylus
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.coffee
75 lines (68 loc) · 2.5 KB
/
gui.coffee
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
sidebarScale = 2
mappings = new svgtiler.Mappings()
tiles = {}
id = (i) -> document.getElementById i
text = (t) -> document.createTextNode t
element = (tag, className, children) ->
el = document.createElement tag
el.className = className if className?
el.appendChild child for child in children if children?
el
TODO = -> alert "Sorry, this feature isn't supported yet."
formatKey = (key) ->
key.replace(/ /g, '␣') or '(empty)'
load = (filename, filedata) ->
input = svgtiler.Input.recognize filename, filedata
if input instanceof svgtiler.Mapping
addMapping input
addMapping = (mapping) ->
mappings.push mapping
id('mappings').appendChild div = element 'div', 'mapping', [
text mapping.filename + " "
del = element 'i', 'fas fa-times delete'
]
del.addEventListener 'click', TODO
if mapping.function?
div.setAttribute 'title', 'function-defined mapping'
else
div.setAttribute 'title', 'symbols: ' +
(formatKey(key) for key of mapping.map).join ', '
id('key').style.visibility = 'visible'
addTile = (key) ->
return if key of tiles
symbol = mappings.lookup(key) ? svgtiler.unrecognizedSymbol
if symbol.use?
symbol = symbol.use new svgtiler.Context [[symbol]], 0, 0
symbolSvg = symbol.xml.documentElement.cloneNode true
symbolSvg.setAttribute 'id', symbolId = 't' + symbol.id()
svg = document.createElementNS svgtiler.SVGNS, 'svg'
#svg.setAttribute 'xmlns:xlink', svgtiler.XLINKNS
svg.setAttribute 'width', symbol.width * sidebarScale
svg.setAttribute 'height', symbol.height * sidebarScale
svg.appendChild symbolSvg
svg.setAttributeNS svgtiler.SVGNS, 'viewBox',
"0 0 #{symbol.viewBox[2]} #{symbol.viewBox[3]}"
if symbolSvg.tagName == 'symbol'
use = document.createElementNS svgtiler.SVGNS, 'use'
use.setAttribute 'href', '#' + symbolId
svg.appendChild use
tiles[key] = tile = element 'div', 'tile', [
element 'code', null, [text formatKey key]
svg
]
id('tiles').appendChild tile
window.onload = ->
id('load').addEventListener 'click', ->
id('file').click()
id('file').addEventListener 'input', ->
return unless id('file').files.length
file = id('file').files[0]
reader = new FileReader()
reader.onload = -> load file.name, reader.result
reader.readAsText file, encoding: svgtiler.Input.encoding
id('keyForm').addEventListener 'submit', (e) ->
e.preventDefault()
addTile id('key').value
id('key').value = ''
id('save').addEventListener 'click', TODO
id('border').addEventListener 'click', TODO