-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
276 lines (233 loc) · 8.2 KB
/
core.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
//Format string function
if (!String.prototype.format)
{
String.prototype.format = function ()
{
var args = arguments;
return this.replace(/{(\d+)}/g, function (match, number)
{ return typeof args[number] !== undefined ? args[number] : match; });
};
}
$(document).ready(function()
{
//fix sidebar height
$('#sb-ops').height($('#sidebar').height() - $('#sb-actions').outerHeight(true));
//remove existing ops (in case of reload)
$('#op-placer, #sizer').remove();
var $query = $('#query');
var $csr = $('#cursor');
var $sizer = $('<span>', { id: 'sizer' }); //div for auto sizing input
var $placer = $('<div>', { id: 'op-placer', 'class': 'op placer' }).html(' ');
var $op = null; //original place
$('body').append($sizer);
//operator moving
var $drag = null;
var moved = false;
var ox, oy; //operator original x,y for drag
//qbox/sidebar resizing
var qmoved = false;
var smoved = false;
var qoy; //qbox original y for resize
var sox; //sidebar original x for resize
//find div under cursor
var $hover = null;
//$(document).on('mouseleave', '.op:not(.placer)', function(ev) { $hover = null; });
$(document).on('keydown', function(ev) { if (ev.which == 27 && $drag != null) { $placer.insertBefore($op); $(document).trigger('mouseup'); } });
//resize qbox (query)
$(document).on('mousedown', '#qbox-resizer', function(ev)
{
var po = $(this).offset();
qoy = ev.pageY - po.top + $('#qbox').offset().top;
qmoved = true;
});
//resize sidebar
$(document).on('mousedown', '#sidebar-resizer', function(ev)
{
var po = $(this).offset();
sox = ev.pageX - po.left + $('#sidebar').offset().left;
smoved = true;
});
//drag operator
$(document).on('mousedown', '.op', function(ev)
{
$drag = $(this);
var po = $drag.offset();
ox = ev.pageX - po.left;
oy = ev.pageY - po.top;
});
$(document).on('mouseup', function(ev)
{
//qbox/sidebar resize reset
qmoved = false;
smoved = false;
//move operator
if ($drag !== null)
{
if (moved)
{
$drag.insertBefore($placer);
if ($drag.hasClass('newline')) //move the newline as well
$drag.data('breaker').insertAfter($drag);
$placer.detach();
moved = false;
}
$drag.css({ 'position': '', 'opacity': 1 });
$drag = null;
$placer.detach();
}
});
$(document).on('mousemove', function(ev)
{
//qbox V resize
if (qmoved)
{
$('#qbox').height(ev.pageY - qoy);
//fix sidebar height
$('#sb-ops').height($('#sidebar').height() - $('#sb-actions').outerHeight(true));
}
//sidebar H resize
if (smoved)
$('#sidebar').width(ev.pageX - sox);
//move operator in query window
if ($drag !== null)
{
//set up the dragger appearance
if (!moved)
{
$op = $drag.next();
$placer.insertBefore($drag);
$drag.css({ 'position': 'absolute', 'opacity': 0.5 });
$placer.width($drag.width()).height($drag.height());
moved = true;
}
else
{
$drag.css('display', 'none');
$hover = $(document.elementFromPoint(ev.pageX, ev.pageY));
$hp = $hover.parents('.op');
if ($hp.length) //insert before/after operator
{
//get side of div (insert before if on left half, after otherwise)
var w = $hp.outerWidth();
if (ev.pageX - $hp.offset().left <= w / 2)
$placer.insertBefore($hp[0]);
else
$placer.insertAfter($hp[0]);
}
else if ($hover.is('#cursor')) //place at end
$placer.insertBefore($csr);
//comment following to disable auto update of structure on moving newline op
if ($drag.hasClass('newline')) //move the newline as well
$drag.data('breaker').insertAfter($placer);
$drag.css('display', 'inline-block');
}
$drag.offset({ left: ev.pageX - ox, top: ev.pageY - oy });
}
});
//auto size inputs in ops on typing in query window
$(document).on('keyup', '.op input', function(ev)
{
$sizer.text($(this).val() + String.fromCharCode(ev.which));
$(this).width($sizer.width() + 10);
});
//remove ops if clicked on in query
$(document).on('dblclick', '.op:not(.placer,input)', function(ev)
{
var $this = $(this);
$this.fadeOut(100, function()
{
//newlines have to remove br
if ($this.hasClass('newline'))
$this.next().remove();
$this.remove();
});
});
//add operators to query window after clicking
$('.opcreate').on('click', function(ev)
{
//ev.preventDefault();
var $op = $('<div>', { 'from': this.id, 'class': 'op' });
//only print if exists (special operators do not)
if (parser_rules.operators[this.id])
$op.html(parser_rules.operators[this.id]['html']); //get html from parser rules
$op.addClass($(this).attr('class')).removeClass('opcreate');
$csr.before($op);
$(this).trigger('opcreate', [ $op ]);
$('#query').scrollTop($('#query').prop("scrollHeight")); //auto scroll to bottom when new div is added
});
//adding special operators
$('#op-newline').on('opcreate', function(ev, op)
{
//newline is special
op.removeClass('special').addClass('newline').html('↵')
var br = $('<br>');
op.data('breaker', br);
op.after(br);
});
$('#op-fullop, #op-comment').on('opcreate', function(ev, op) { op.append('<input class="wide">'); });
$('#clear-query').on('click', function(ev) { ev.preventDefault(); if (window.confirm('Are you sure you wish to remove your query?\nNote: you cannot undo this.')) $csr.prevAll().remove(); });
//export
$('.export-btn').on('click', function(ev)
{
ev.preventDefault();
var type = $(this).attr('export-type');
if (!type)
return;
var exporter = parser_rules.exports[type];
var date = new Date();
var dateFmt = '{0} {1} {2}'.format(date.getFullYear(), date.getMonth() + 1, date.getDate());
var content = exporter.header ? exporter.header.replace(/__date__/gmi, dateFmt) : '';
$('#query .op').each(function (idx)
{
var $this = $(this);
var opType = $this.attr('from');
//generate a list of all of the inputs in the operator
var inputs = [];
$this.find('input').each(function() { inputs.push($(this).val()); });
//add the operator from the rules with the inputs provided
//newline and fullop have custom rules
if (opType == 'op-newline')
content += String.prototype.format.apply(exporter.newline, inputs);
else if (opType == 'op-fullop')
content += String.prototype.format.apply(exporter.fullop, inputs);
else if (opType == 'op-comment')
content += String.prototype.format.apply(exporter.comment, inputs);
else if (parser_rules.operators[opType])
content += String.prototype.format.apply(parser_rules.operators[opType]['export'][type], inputs);
else
console.warn('Error: No rule for "' + opType + '"');
});
if (exporter.footer)
content += exporter.footer;
ShowInWindow(content);
});
$('#compat-view-btn').on('click', function(ev)
{
ev.preventDefault();
//convert all sidebar ops
});
//Show 'Contents' in a popup window
function ShowInWindow(Contents)
{
//create the popup
var winWid = 640;
var winHgt = 640;
var winLeft = (screen.width - winWid) / 2;
var winTop = (screen.height - winHgt) / 2;
var w = window.open('', 'export-wnd', 'width={0},height={1},left={2},top={3},scrollbar=yes,resizable=yes,toolbar=no'.format(winWid, winHgt, winLeft, winTop));
w.document.body.innerHTML = '<style type="text/css">body { padding: 4px; } pre { font-family: monospace; font-size: 16pt; height: 100%; width: 100%; height: 100%; } pre:focus { outline: none }</style><pre contenteditable="true">' + Contents + '</pre>'; //default styling and content
w.focus();
}
console.log('Loaded Simple Scripter');
});
//A simple helper function to reload the rules (Reruns core script as well but does not add new operators)
function ReloadRules(JsonFile)
{
$.getJSON(JsonFile, function(rules)
{
parser_rules = rules;
$('#parser_rules_js, #custom_js').remove();
$('head').append($('<script>', { 'id': 'parser_rules_js', 'type': 'text/javascript', 'src': 'core.js' })); //JS must be added after rules are loaded
$('head').append($('<script>', { 'id': 'custom_js', 'type': 'text/javascript', 'src': rules.scriptfile }));
});
}