-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.js
339 lines (289 loc) · 9.22 KB
/
parser.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
The MIT License (MIT)
Copyright (c) 2013-2020 Rodrigo González Castillo <[email protected]>
*/
'use strict';
define(['dojo/_base/declare', 'dojo/request/xhr', 'dojo/Deferred', 'dojo/on', 'dojo/_base/lang'],
function(declare, xhr, Deferred, on, lang) {
/**
* Parse the given SLD and return the root widget within their children recursively.
* @param sld
* @param options
* @param options.tokens.TYPE default to '$type'
* @param options.tokens.CHILDREN default to '$children'
* @param options.tokens.REQUIRE default to '$require'
* @param options.tokens.REF default to '$ref'
* @param options.tokens.ON default to '$on'
* @param options.tokens.I18N default to '$i18n'
* @param options.rules array of rules
* @param options.typeAliasMap a object for the alias of the types alias=>type, as key=alias and value=type
* @param options.hydrateObject a object with members to call with the $on directive, and $ref
* @param options.i18n
* @returns {*}
*/
function parse(sld, options) {
sld = typeof sld === 'string' ? JSON.parse(sld) : sld;
options = options || {};
options.tokens = options.tokens || {};
options.typeAliasMap = options.typeAliasMap || {};
options.rules = options.rules || [];
options.i18n = options.i18n || {};
var TOKEN_TYPE = options.tokens.TYPE || '$type';
var TOKEN_CHILDREN = options.tokens.CHILDREN || '$children';
var TOKEN_REQUIRE = options.tokens.REQUIRE || '$require';
var TOKEN_REF = options.tokens.REF || '$ref';
var TOKEN_ON = options.tokens.ON || '$on';
var TOKEN_I18N = options.tokens.I18N || '$i18n';
var deferred = new Deferred();
// Call to the recursive function asynchronously
setTimeout(function() {
recursiveParse(deferred, [], [sld], {}, 0);
}, 0);
function recursiveParse(deferred, created, missing, root, i) {
if (!missing.length) {
deferred.resolve(root);
return;
}
// The SLD for this recursion
var sld = missing.shift();
// The parent widget
var widgetParent = created[sld.$__parentCreatedIndex];
sld.$__parentCreatedIndex = null;
// The index of this widget in their parent
var widgetParentIndex = sld.$__parentIndex;
sld.$__parentIndex = null;
var widgetRequire = sld[TOKEN_REQUIRE];
if(widgetRequire) {
sld[TOKEN_REQUIRE] = null;
require(['dojo/text!' + widgetRequire], function(sldRequired) {
parse(sldRequired, options).then(function(widget) {
associateWithParent(deferred, created, missing, root, i, sld, widget, {}, widgetParent,
widgetParentIndex);
}, deferred.reject, deferred.progress);
});
return;
} else {
var widgetType = sld[TOKEN_TYPE];
if(widgetType in options.typeAliasMap) {
widgetType = options.typeAliasMap[widgetType];
}
sld[TOKEN_TYPE] = null;
var widgetChildren = sld[TOKEN_CHILDREN];
var widget = null;
// The children of this widget
if(widgetChildren) {
sld[TOKEN_CHILDREN] = null;
for (var j = 0, length = widgetChildren.length; j < length; j++) {
widgetChildren[j].$__parentCreatedIndex = i;
widgetChildren[j].$__parentIndex = j;
missing.push(widgetChildren[j]);
}
}
}
// Find the children of this widget that is implicit
var eventsMap = {};
forEachKeyInSld:
for(var key in sld) {
if (!sld.hasOwnProperty(key))
continue forEachKeyInSld;
if(options.hydrateObject) {
var keyParts = key.split(':');
if(keyParts[0] === TOKEN_ON) {
eventsMap[keyParts[1]] = sld[key]
}
if(options.i18n) {
if(keyParts[0] === TOKEN_I18N) {
sld[keyParts[1]] = options.i18n[sld[key]] || sld[key];
sld[key] = null;
}
}
}
// Applies the rules for each pair key:value
forEachJInRules:
for (var k in options.rules) {
var rule = options.rules[k];
// If matches for a rule
if ((rule.keyPattern instanceof RegExp && rule.keyPattern.test(key)) || rule.keyPattern === key) {
// If must execute a handler
rule.onPreparse && rule.onPreparse(key, sld[key]);
// If this node must be deleted
if (rule.disabled) {
sld[key] = null; // previously this used `delete sld[key]`
continue forEachKeyInSld;
}
// if rule has key member
if (rule.key !== undefined) {
sld[rule.key] = sld[key];
sld[key] = null;
key = rule.key;
}
// If rule has value member
if (rule.value !== undefined) {
sld[key] = rule.value;
}
}
}
// If has a implicit children
if (sld[key] && (sld[key][TOKEN_TYPE] || sld[key][TOKEN_REQUIRE])) {
var implicitSld = sld[key];
sld[key] = null;
implicitSld.$__parentIndex = key;
implicitSld.$__parentCreatedIndex = i;
missing.push(implicitSld);
}
}
// Instantiate the SLD widget
if (typeof widgetType === 'string') {// is MID
require([widgetType], function(Type) {
try {
var widget = new Type(sld);
associateWithParent(deferred, created, missing, root, i, sld, widget, eventsMap,
widgetParent, widgetParentIndex);
} catch(e) {
deferred.reject(e);
}
});
} else if ( widgetType instanceof Array) {
var types = [], requires = [], requiresIndex = [];
for (var j in widgetType) {
if ( typeof widgetType[j] === 'string') {
requires.push(widgetType[j]);
requiresIndex.push(j);
} else// is constructor
types[j] = widgetType[j];
}
require(requires, function() {
for (var j in arguments) {
types[requiresIndex[j]] = arguments[j]
}
try {
var widget = new declare(types)(sld);
associateWithParent(deferred, created, missing, root, i, sld, widget, eventsMap,
widgetParent, widgetParentIndex);
} catch(e) {
deferred.reject(e);
}
});
} else {// is constructor
try {
var widget = new widgetType(sld);
associateWithParent(deferred, created, missing, root, i, sld, widget, eventsMap,
widgetParent, widgetParentIndex);
} catch(e) {
deferred.reject(e);
}
}
} // end recursive
function associateWithParent(deferred, created, missing, root, i,
sld, widget, eventsMap, widgetParent, widgetParentIndex) {
// Save the created widget
created[i] = widget;
// Associate the widget with their parent
if(typeof widgetParent === 'undefined')
root = widget;
else if(typeof widgetParentIndex === 'string')
widgetParent.set(widgetParentIndex, widget);
else if(typeof widgetParentIndex === 'number')
widgetParent.addChild(widget, widgetParentIndex);
else
deferred.reject(new Error("Invalid widget parent association: Index must to be a string or a number, " +
"and not " + typeof(widgetParentIndex)));
if(options.hydrateObject) {
// Hydrate the object for anchor $ref
if(sld[TOKEN_REF]) {
if(!options.hydrateObject.$ref) {
options.hydrateObject.$ref = {}
}
options.hydrateObject.$ref[sld[TOKEN_REF]] = widget;
}
// Hydrate the object for events $on:*
for(var eventKey in eventsMap) {
if(eventsMap.hasOwnProperty(eventKey)) {
if(!(eventsMap[eventKey] in options.hydrateObject)) {
return deferred.reject('Error in SLD parser: ' + eventsMap[eventKey]
+ ' method is not in hydrateObject for widget id=' + widget.id);
}
on(
widget,
eventKey,
lang.hitch(
options.hydrateObject, // scope object
lang.getObject( // get the event from the hydrateObject
eventsMap[eventKey], true, options.hydrateObject
)
)
);
}
}
}
// notify the progress
deferred.progress({
sld : sld,
widget : widget,
i : i
});
recursiveParse(deferred, created, missing, root, ++i);
} // end associateWithParent
// root
return deferred.promise;
}
return {
parse : parse,
load : function load(id, require, callback) {
var modulProps = id.split(',');
var modul = modulProps[0];
var props = modulProps[1] ? JSON.parse(modulProps[1]) : {};
require(['dojo/text!' + modul], function(sld) {
parse(sld, props).then(callback);
});
},
forEach : function forEach(sld, iterator) {
var i = -1;
return (function recursiveForEach(sld) {
if (!sld)
return;
if (!sld.$type) {
if (!(sld instanceof Array))
return;
} else
iterator(sld, ++i);
for (var key in sld) {
if(sld.hasOwnProperty(key)) {
if(sld[key] && sld[key].$type) {
recursiveForEach(sld[key].$type);
}
recursiveForEach(sld[key]);
}
}
})(sld);
},
count : function() {
var count;
this.forEach(function(item, i) {
count = i;
});
return count;
},
listDependences : function(sld, toObject) {
var dependencesMap = {};
this.forEach(sld, function(item) {
if ( typeof item.$type === 'string') {
dependencesMap[item.$type] = true;
} else if (item.$type instanceof Array) {
for (var i in item.$type) {
dependencesMap[item.$type[i]] = true;
}
}
});
if (toObject)
return dependencesMap;
if(Object.keys)
return Object.keys(dependencesMap);
var dependenceArray = [];
for (var key in dependencesMap) {
dependenceArray.push(key);
}
return dependenceArray;
}
}
});