-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
70 lines (58 loc) · 1.77 KB
/
menu.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
/**
* Created by zouxuan on 4/27/17.
*/
(function (global) {
'use strict';
var menu=document.querySelector('.menu');
var script=document.querySelector('.script');
var scriptRegistry={};
var scriptDirty=false;
function runSoon() {
scriptDirty=true;
}
function run() {
if(scriptDirty){
scriptDirty=false;
Block.trigger('beforeRun',script);
var blocks = [].slice.call(
document.querySelectorAll('.script > .block'));
Block.run(blocks);
Block.trigger('afterRun', script);
}
else{
Block.trigger('everyFrame', script);
}
requestAnimationFrame(run);
}
requestAnimationFrame(run);
function runEach(evt){
var elem = evt.target;
if (!matches(elem, '.script .block')) return;
if (elem.dataset.name === 'Define block') return;
elem.classList.add('running');
scriptRegistry[elem.dataset.name](elem);
elem.classList.remove('running');
}
function repeat(block){
var count = Block.value(block);
var children = Block.contents(block);
for (var i = 0; i < count; i++){
Block.run(children);
}
}
menuItem('Repeat', repeat, 10, []);
function menuItem(name, fn, value, units){
var item = Block.create(name, value, units);
scriptRegistry[name] = fn;
menu.appendChild(item);
return item;
}
global.Menu = {
runSoon: runSoon,
item: menuItem
};
document.addEventListener('drop', runSoon, false);
script.addEventListener('run', runEach, false);
script.addEventListener('change', runSoon, false);
script.addEventListener('keyup', runSoon, false);
})(window);