forked from skanaar/nomnoml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nomnoml.app.js
221 lines (191 loc) · 6.19 KB
/
nomnoml.app.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
var nomnoml = nomnoml || {}
$(function (){
var storage = null
var jqCanvas = $('#canvas')
var viewport = $(window)
var jqBody = $('body')
var lineNumbers = $('#linenumbers')
var lineMarker = $('#linemarker')
var storageStatusElement = $('#storage-status')
var textarea = document.getElementById('textarea')
var imgLink = document.getElementById('savebutton')
var linkLink = document.getElementById('linkbutton')
var canvasElement = document.getElementById('canvas')
var canvasPanner = document.getElementById('canvas-panner')
var canvasTools = document.getElementById('canvas-tools')
var defaultSource = document.getElementById('defaultGraph').innerHTML
var zoomLevel = 0
var offset = {x:0, y:0}
var mouseDownPoint = false
var vm = skanaar.vector
var editor = CodeMirror.fromTextArea(textarea, {
lineNumbers: true,
mode: 'nomnoml',
matchBrackets: true,
theme: 'solarized light',
keyMap: 'sublime'
});
var editorElement = editor.getWrapperElement()
window.addEventListener('hashchange', reloadStorage);
window.addEventListener('resize', _.throttle(sourceChanged, 750, {leading: true}))
editor.on('changes', _.debounce(sourceChanged, 300))
canvasPanner.addEventListener('mouseenter', classToggler(jqBody, 'canvas-mode', true))
canvasPanner.addEventListener('mouseleave', classToggler(jqBody, 'canvas-mode', false))
canvasTools.addEventListener('mouseenter', classToggler(jqBody, 'canvas-mode', true))
canvasTools.addEventListener('mouseleave', classToggler(jqBody, 'canvas-mode', false))
canvasPanner.addEventListener('mousedown', mouseDown)
window.addEventListener('mousemove', _.throttle(mouseMove,50))
canvasPanner.addEventListener('mouseup', mouseUp)
canvasPanner.addEventListener('mouseleave', mouseUp)
canvasPanner.addEventListener('wheel', _.throttle(magnify, 50))
initImageDownloadLink(imgLink, canvasElement)
initToolbarTooltips()
reloadStorage()
function classToggler(element, className, state){
var jqElement = $(element)
return _.bind(jqElement.toggleClass, jqElement, className, state)
}
function mouseDown(e){
$(canvasPanner).css({width: '100%'})
mouseDownPoint = vm.diff({ x: e.pageX, y: e.pageY }, offset)
}
function mouseMove(e){
if (mouseDownPoint){
offset = vm.diff({ x: e.pageX, y: e.pageY }, mouseDownPoint)
sourceChanged()
}
}
function mouseUp(){
mouseDownPoint = false
$(canvasPanner).css({width: '33%'})
}
function magnify(e){
zoomLevel = Math.min(10, zoomLevel - (e.deltaY < 0 ? -1 : 1))
sourceChanged()
}
nomnoml.magnifyViewport = function (diff){
zoomLevel = Math.min(10, zoomLevel + diff)
sourceChanged()
}
nomnoml.resetViewport = function (){
zoomLevel = 1
offset = {x: 0, y: 0}
sourceChanged()
}
nomnoml.toggleSidebar = function (id){
var sidebars = ['reference', 'about']
_.each(sidebars, function (key){
if (id !== key) $(document.getElementById(key)).toggleClass('visible', false)
})
$(document.getElementById(id)).toggleClass('visible')
}
nomnoml.discardCurrentGraph = function (){
if (confirm('Do you want to discard current diagram and load the default example?')){
setCurrentText(defaultSource)
sourceChanged()
}
}
nomnoml.saveViewModeToStorage = function (){
var question =
'Do you want to overwrite the diagram in ' +
'localStorage with the currently viewed diagram?'
if (confirm(question)){
storage.moveToLocalStorage()
window.location = './'
}
}
nomnoml.exitViewMode = function (){
window.location = './'
}
// Adapted from http://meyerweb.com/eric/tools/dencoder/
function urlEncode(unencoded) {
return encodeURIComponent(unencoded).replace(/'/g,'%27').replace(/"/g,'%22')
}
function urlDecode(encoded) {
return decodeURIComponent(encoded.replace(/\+/g, ' '))
}
function setShareableLink(str){
var base = '#view/'
linkLink.href = base + urlEncode(str)
}
function buildStorage(locationHash){
var key = 'nomnoml.lastSource'
if (locationHash.substring(0,6) === '#view/')
return {
read: function (){ return urlDecode(locationHash.substring(6)) },
save: function (){ setShareableLink(currentText()) },
moveToLocalStorage: function (){ localStorage[key] = currentText() },
isReadonly: true
}
return {
read: function (){ return localStorage[key] || defaultSource },
save: function (source){
setShareableLink(currentText())
localStorage[key] = source
},
moveToLocalStorage: function (){},
isReadonly: false
}
}
function initImageDownloadLink(link, canvasElement){
link.addEventListener('click', downloadImage, false);
function downloadImage(){
var url = canvasElement.toDataURL('image/png')
link.href = url;
}
}
function initToolbarTooltips(){
var tooltip = $('#tooltip')[0]
$('.tools a').each(function (i, link){
link.onmouseover = function (){ tooltip.textContent = $(link).attr('title') }
link.onmouseout = function (){ tooltip.textContent = '' }
})
}
function positionCanvas(rect, superSampling, offset){
var w = rect.width / superSampling
var h = rect.height / superSampling
jqCanvas.css({
top: 300 * (1 - h/viewport.height()) + offset.y,
left: 150 + (viewport.width() - w)/2 + offset.x,
width: w,
height: h
})
}
function setFilename(filename){
imgLink.download = filename + '.png'
}
function reloadStorage(){
storage = buildStorage(location.hash)
editor.setValue(storage.read())
sourceChanged()
if (storage.isReadonly) storageStatusElement.show()
else storageStatusElement.hide()
}
function currentText(){
return editor.getValue()
}
function setCurrentText(value){
return editor.setValue(value)
}
function sourceChanged(){
try {
lineMarker.css('top', -30)
lineNumbers.toggleClass('error', false)
var superSampling = window.devicePixelRatio || 1
var scale = superSampling * Math.exp(zoomLevel/10)
var model = nomnoml.draw(canvasElement, currentText(), scale)
positionCanvas(canvasElement, superSampling, offset)
setFilename(model.config.title)
storage.save(currentText())
} catch (e){
var matches = e.message.match('line ([0-9]*)')
lineNumbers.toggleClass('error', true)
if (matches){
var lineHeight = parseFloat($(editorElement).css('line-height'))
lineMarker.css('top', 3 + lineHeight*matches[1])
} else {
throw e
}
}
}
})