-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (161 loc) · 4.83 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
/**
* @author Gilles Coomans <[email protected]>
* Think about CSS. This module provides way to write real code sheets in js object format,
* that applies codes (up, bottom, etc) on part of object catched with queries (or selectors).
*
* Exactly as CSS applies style properties on part of DOM catched with selectors.
*
* Take a look to documenation on github.
*
*
* TODO :
* - protocol sheet::a_dsl_to_describe_transformation_with_string
* solution :
* - use directives parser
* - allow to pass transformation as json : "dq::./my/query":"sheet::up(bloupi) directive(arg)"
*
*
* - allow direct deep.sheeter in _backgrounds and _foregrounds
* e.g. : { _foregrounds:[ deep.compose.nodes.bottom(myLayer)] }
*
*
*
*
*
* deepjs extension :
nodes.sheet = function(s, shts) {
if (!s)
return s;
return promise.when(proto.getAll(shts))
.done(function(objects) {
if (s._deep_array_) {
var promises = [];
s.forEach(function(result) {
promises.push(sheeter.sheet.apply(sheeter, [result].concat(objects)));
});
return promise.all(promises)
.done(function() {
return s;
});
} else
return sheeter.sheet.apply(sheeter, [s].concat(objects));
});
};
//________________________________________ SHEET PROTOCOLS
sheeter.methods = {
up: function(catched, toApply, options) {
return proto.getAll(toApply).done(function(objects) {
return nodes.ups(catched, objects);
});
},
bottom: function(catched, toApply, options) {
return proto.getAll(toApply).done(function(objects) {
return nodes.bottoms(catched, objects);
});
},
map: function(catched, toApply, options) {
return nodes.map(catched, toApply);
}
};
*
*/
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(["require", "deep-protocols/index", "deep-promise/index", "deep-nodes/index", "deep-nodes/lib/dq-protocol"],
function(require, proto, promise, nodes, dqProtocol) {
proto.protocols.dq = dqProtocol;
var sheeter = {};
var applyDefault = function(catched, toApply, options) {
if (typeof toApply !== "function")
return new Error("SheetError : You try to apply sheets with something that's not a function.");
return toApply.call(this, catched, options);
};
var applySheetEntry = function(sheet, key, options) {
var parsed = proto.parseRequest(key),
toCatch = parsed.protocol + "::" + parsed.uri,
toApply = sheet[key],
method = applyDefault,
catched = null;
if (parsed.method !== "get") {
method = sheeter.methods ? sheeter.methods[parsed.method] : null;
if (!method)
throw new Error("SheetError : you try to apply sheets with unknown method : " + parsed.method);
}
return this.when(proto.get(toCatch, options))
.done(function(catched) {
if (catched)
return method.call(options.bind, catched, toApply, options);
});
};
var linearSheet = function(entry, sheet, options) {
if (typeof sheet === 'function')
return promise.when(sheet(entry, options))
.when(entry);
var keys = Object.keys(sheet);
var done = function(s) {
var key = keys.shift();
if (key == "_deep_sheet_")
key = keys.shift();
if (!key)
return s;
return applySheetEntry.call(this, sheet, key, options)
.done(done);
};
return promise.when(entry)
.done(done)
.elog("sheet application error")
.when(entry);
};
sheeter.sheet = function() {
var entry = arguments[0],
sheet;
if (arguments.length > 2) {
var args = Array.prototype.slice.call(arguments);
args.shift();
var done = function(entry) {
if (args.length) {
this.done(done);
return sheeter.sheet(entry, args.shift());
}
return entry;
};
return sheeter.sheet(entry, args.shift())
.done(done)
.elog("sheet application error");
} else
sheet = arguments[1];
var options = options || {};
options.entry = entry;
options.fullOutput = true;
options.bind = options.bind || {};
//options.allowStraightQueries = false;
return linearSheet(entry, sheet, options);
};
nodes.sheet = function(node, sheets) {
if (node.forEach) {
var promises = [];
node.forEach(function(result) {
promises.push(sheeter.sheet.apply(sheeter, [result].concat(sheets)));
});
return promise.all(promises)
.when(node);
} else
return sheeter.sheet.apply(sheeter, [node].concat(sheets));
};
sheeter.methods = {
up: function(catched, toApply, options) {
return proto.getAll(toApply)
.done(function(objects) {
return nodes.up.apply(nodes, [catched].concat(objects));
});
},
bottom: function(catched, toApply, options) {
return proto.getAll(toApply)
.done(function(objects) {
return nodes.bottom.apply(nodes, [catched].concat(objects));
});
}
};
return sheeter;
});