forked from skanaar/nomnoml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nomnoml.app.js
188 lines (165 loc) · 5.45 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
var nomnoml = nomnoml || {}
$(function (){
var storage = null
var jqCanvas = $('#canvas')
var viewport = $(window)
var lineNumbers = $('#linenumbers')
var lineMarker = $('#linemarker')
var jqTextarea = $('#textarea')
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 defaultSource = document.getElementById('defaultGraph').innerHTML
var zoomLevel = 0
var offset = {x:0, y:0}
var mouseDownPoint = false
var vm = skanaar.vector
window.addEventListener('hashchange', reloadStorage);
window.addEventListener('resize', _.throttle(sourceChanged, 750, {leading: true}))
textarea.addEventListener('input', _.debounce(sourceChanged, 300))
canvasPanner.addEventListener('mouseenter', classToggler(jqTextarea, 'hidden', true))
canvasPanner.addEventListener('mouseleave', classToggler(jqTextarea, 'hidden', 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)
lineNumbers.val(_.times(60, _.identity).join('\n'))
initToolbarTooltips()
reloadStorage()
function classToggler(jqElement, className, state){
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.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?')){
textarea.value = 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(textarea.value) },
moveToLocalStorage: function (){ localStorage[key] = textarea.value },
isReadonly: true
}
return {
read: function (){ return localStorage[key] || defaultSource },
save: function (source){
setShareableLink(textarea.value)
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, zoom, offset){
var w = rect.width * zoom
var h = rect.height * zoom
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)
textarea.value = storage.read()
sourceChanged()
if (storage.isReadonly) storageStatusElement.show()
else storageStatusElement.hide()
}
function sourceChanged(){
try {
lineMarker.css('top', -30)
lineNumbers.css({background:'#eee8d5', color:'#D4CEBD'})
var scale = Math.exp(zoomLevel/10)
var model = nomnoml.draw(canvasElement, textarea.value, scale)
positionCanvas(canvasElement, model.config.zoom / model.superSampling, offset)
setFilename(model.config.title)
storage.save(textarea.value)
} catch (e){
var matches = e.message.match('line ([0-9]*)')
if (matches){
var lineHeight = parseFloat(jqTextarea.css('line-height'))
lineMarker.css('top', 8 + lineHeight*matches[1])
}
else {
lineNumbers.css({background:'rgba(220,50,47,0.4)', color:'#657b83'})
throw e
}
}
}
})