-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
281 lines (204 loc) · 5.78 KB
/
index.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
277
278
279
280
281
requirejs([
"src/tiny",
"lib/prism"
], start);
function start() {
tiny.import();
tiny.output('all');
build_content_table();
// run code block in page
setTimeout(run_all_code, 100);
// full query tests
// test_all();
$('#test-info').on('click', jump_to_error);
$('.content').on('click', 'pre.collapse', function () {
expand_pre(this);
});
}
function test_all() {
// full unit tests
require([
'tinyq.test',
'tinyq.test.base',
'tinyq.test.ops',
], function () {
});
}
// ====== ui functions
function jump_to_hash(event, data) {
_log(data, event, this);
var elem = _q1(this);
var hash = elem.attr('href').substr(1);
smooth_scroll_to(hash);
return false;
}
function smooth_scroll_to(obj, offset) {
var is_hash = false;
var hash = '';
if (typeof obj == 'string') {
is_hash = true;
hash = obj;
obj = $('a[name="' + hash + '"]');
if (obj.length < 1) return;
}
if (typeof offset !== 'number') {
offset = 0;
}
obj = $(obj);
$(document.body).animate(
{ scrollTop: obj.offset().top + offset },
500,
"swing",
function () {
if (is_hash)
window.location.hash = hash;
}
);
}
function jump_to_error() {
var objs = $('.failed');
if (objs.length < 1) return;
if (objs.length == 1) {
smooth_scroll_to(objs, -100);
return;
}
var target;
objs.each(function (index, elem) {
elem = $(elem);
if (elem.prop("tagName") == 'PRE' && elem.find('.failed').length > 0) {
elem.removeClass('collapse');
if (elem.find('.failed').length > 0) {
elem.removeClass('failed');
jump_to_error();
return false;
}
} else if (elem.hasClass('current')) {
elem.removeClass('current');
index = (index + 1) < objs.length ? index + 1 : 0;
target = $(objs.get(index));
return false;
}
});
if (!target) {
target = $('.failed:first');
}
target.addClass('current');
smooth_scroll_to(target, -100);
}
function expand_pre(elem) {
var elem = $(elem);
elem.removeClass('collapse');
}
// ====== content table builder
function build_content_table() {
_timer('tinyq');
var sidebar = _q('#content-table');
var a_list = _q('.mark');
a_list.each(function (elem) {
check_and_append_link(sidebar, elem);
});
_timer('tinyq');
}
function check_and_append_link(sidebar, elem) {
var name = elem.attr('name');
var parent_elem = elem.parent();
if (parent_elem.length < 1) return;
var tag = parent_elem.prop("tagName");
var title = parent_elem.text();
var a_class = '';
if (tag == 'H2') {
a_class = 'header';
title = title.toUpperCase();
} else {
a_class = 'sub';
}
sidebar.append('<a>', {
href: '#' + name,
class: a_class,
_text: title
});
}
// ====== code runner for unit tests
var _error_count = 0;
function run_all_code() {
_timer('run code');
_error(false) // turn off error popups
_q('#test-info')
.text('RUNNING CODE TEST...')
.class('-pass -fail show');
var codes = _q('.run-code');
codes.each(function (elem) {
run_code(elem);
if (elem.class('?stop')) return false;
});
setTimeout(show_run_code_result, 500);
_error(true)
_timer('run code');
}
function run_code(elem) {
var code = elem.text();
if (elem.class('?html')) {
var html_fragment = _q(code);
_q(document.body).after(html_fragment);
return;
}
code = '\
var code_block = arguments[0];\
var assert_index = 0;\
var assert_list = code_block.q(".function").filter("@contains(ASSERT)");\
var test_result = true;\
var get_assert = function(){\
var elem = assert_list.q(assert_index);\
assert_index++;\
return elem;\
};\
var FLAT = function(obj){ return JSON.stringify(obj) };\
var ASSERT = function(txt, value){\
_info(value, " <<<< ASSERT " + txt);\
var elem = get_assert();\
if(value){\
elem.class("passed");\
}else{\
elem.class("failed");\
_error_count++;\
}\
};\
var FAIL = function(txt){ ASSERT(txt, false); };\
' + code;
tiny.output('info');
try {
var func = new Function(code);
func(elem, 'this', 'is', 'a', 'test');
} catch (e) {
_error_count++;
setTimeout(function () {
_error('=== RUN CODE ERROR ===> ', e);
}, 500);
elem.class('failed');
}
tiny.output('all');
}
function show_run_code_result() {
var error_counter = 0;
var code_block = _q(".run-code");
code_block.each(function (elem) {
var count = elem.q('.failed').count;
if (count > 0) {
elem.class("failed");
} else {
elem.class("passed");
}
});
if (_error_count == 0) {
_q('#test-info')
.class('pass')
.text('ALL CODE TEST PASSED');
setTimeout(function () {
_q('#test-info').class('-show');
}, 2000);
} else {
_q('#test-info')
.class('fail')
.text(_error_count + ' CODE TEST FAILED');
}
}