forked from gabrielflorit/water
/
water.js
206 lines (165 loc) · 5.97 KB
/
water.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
$(function() {
window.aceEditor = ace.edit("editor");
// set the theme
window.aceEditor.setTheme("ace/theme/twilight");
// set mode to javascript
var JavaScriptMode = require("ace/mode/javascript").Mode;
window.aceEditor.getSession().setMode(new JavaScriptMode());
function redrawSvg() {
// clear the window
$('svg').empty();
try {
// get the ide code
var thisCode = window.aceEditor.getSession().getValue();
// run it
eval(thisCode);
// save it in local storage
setLocalStorageValue('code', thisCode);
}
catch (error) {}
finally {};
}
// redraw svg when we update our code or resize the window
window.aceEditor.getSession().on('change', redrawSvg);
$(window).on('resize', redrawSvg);
d3.text('http://gabrielflor.it/static/submodule/water/data/chord.txt', function(data) {
// do we have stored code? if not, set the demo code
window.aceEditor.getSession().setValue(getLocalStorageValue('code') ? getLocalStorageValue('code') : data);
});
// local storage getter/setter
function getLocalStorageValue(key) {
var localStorageKey = 'gabrielflor.it/water1';
return localStorage.getItem([localStorageKey, key].join('/'));
}
function setLocalStorageValue(key, value) {
var localStorageKey = 'gabrielflor.it/water1';
localStorage.setItem([localStorageKey, key].join('/'), value);
}
// if we click on a numeric constant, select the token and show the slider
var chosenRow, chosenColumn;
var onNumeric = false;
window.aceEditor.on("click", function(e) {
var editor = e.editor;
var pos = editor.getCursorPosition();
var token = editor.session.getTokenAt(pos.row, pos.column);
onNumeric = false;
// did we click on a number?
if (token && /\bconstant.numeric\b/.test(token.type)) {
// stop pulsing numerics
if (pulseNumerics) {
window.clearInterval(pulse);
pulseNumerics = false;
}
// set the slider params based on the token's numeric value
// TODO: there has to be a better way of setting this up
// TODO: feels pretty silly at the moment
if (token.value == 0) {
var sliderRange = [-100, 100];
} else {
var sliderRange = [-token.value * 3, token.value * 5];
}
slider.slider('option', 'max', d3.max(sliderRange));
slider.slider('option', 'min', d3.min(sliderRange));
// slider range needs to be evenly divisible by the step
if ((d3.max(sliderRange) - d3.min(sliderRange)) > 20) {
slider.slider('option', 'step', 1);
} else {
slider.slider('option', 'step', (d3.max(sliderRange) - d3.min(sliderRange))/200);
}
slider.slider('option', 'value', token.value);
// position slider centered above the cursor
var scrollerOffset = $('.ace_scroller').offset();
var cursorOffset = editor.renderer.$cursorLayer.pixelPos;
var sliderTop = scrollerOffset.top + cursorOffset.top - Number($('#editor').css('font-size').replace('px', ''))*0.8;
var sliderLeft = scrollerOffset.left + cursorOffset.left - slider.width()/2;
// sync the slider size with the editor size
slider.css('font-size', $('#editor').css('font-size'));
slider.css('font-size', '-=4');
slider.offset({top: sliderTop, left: sliderLeft});
// allow the slider to be shown
onNumeric = true;
// make this position globally scoped
chosenRow = pos.row;
chosenColumn = token.start;
// prevent click event from bubbling up to body, which
// would then trigger an event to hide the slider
e.stopPropagation();
}
});
// turn off horizontal scrollbar
window.aceEditor.renderer.setHScrollBarAlwaysVisible(false);
// turn off print margin visibility
window.aceEditor.setShowPrintMargin(false);
// load font-size from local storage
if (getLocalStorageValue('font-size')) {
$('#editor').css('font-size', getLocalStorageValue('font-size'));
}
// increase/decrease font
$('.font-control').on('click', function(e) {
e.preventDefault();
if ($(this).attr('class').indexOf('decrease') != -1) {
$('#editor').css('font-size', '-=1');
} else {
$('#editor').css('font-size', '+=1');
}
setLocalStorageValue('font-size', $('#editor').css('font-size'));
});
// from https://github.com/ajaxorg/ace/issues/305
// this replaces the current replace functionality
// replace just replaces the current selection with the replacement text,
// and highlights the replacement text
// it does not go to the next selection (which the default version does)
window.aceEditor.replace = function(replacement) {
var range = this.getSelectionRange();
if (range !== null) {
this.$tryReplace(range, replacement);
if (range !== null)
this.selection.setSelectionRange(range);
}
}
// create slider
var slider = $('#slider');
slider.slider({
slide: function(event, ui) {
// set the cursor to desired location
var cursorPosition = window.aceEditor.getCursorPosition();
if (!(cursorPosition.row == chosenRow && cursorPosition.column == chosenColumn)) {
window.aceEditor.getSelection().moveCursorTo(chosenRow, chosenColumn);
// clear selection
window.aceEditor.clearSelection();
}
// get token
var token = window.aceEditor.session.getTokenAt(chosenRow, chosenColumn + 1);
// find and replace
window.aceEditor.find(String(token.value));
window.aceEditor.replace(String(ui.value));
}
});
// use control key on linux, alt key everywhere else
var sliderKey = navigator && navigator.platform && navigator.platform.toLowerCase().indexOf('linux') != -1
? 'ctrl' : 'alt';
// display slider key on page
$('#sliderKey').text(sliderKey);
// trigger slider on control
$('textarea').bind('keydown.' + sliderKey, function(e) {
// are we on a token?
if (onNumeric) {
slider.css('visibility', 'visible');
}
}).bind('keyup.' + sliderKey, function(e) {
slider.css('visibility', 'hidden');
});
$('#slider').bind('keyup.' + sliderKey, function(e) {
slider.css('visibility', 'hidden');
});
// we're not a numeric, by default
// if we are, the editor click will handle it
$('body').on('focus click', function(e) {
onNumeric = false;
});
// pulse numeric constants (until user clicks on them)
var pulseNumerics = true;
var pulse = setInterval(function() {
$('.ace_numeric').animate({opacity: 0.5}).animate({opacity: 1});
}, 1000);
});