-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
397 lines (349 loc) · 11.6 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
(function() {
'use strict';
var
// main api function
ui$ = function(query) {
return pipe(query);
},
// configuration object
oConfig,
// plugin objects that are used in pipelines
// keys: { name, fnFirstPass, fnLastPass }
aPlugins = [],
// plugins that will be invoked by the "standout" plugin
// TODO: add this to config
aStandoutPlugins = ['popup', 'highlight'],
// last search results
// populated whenever a search occurs
aLastResults = [],
// for blocking
oInProgressFlags = {},
// used for init and reset
oConfigDefaults = {
// amount of time for search highlighting
standoutDelay: 3000,
// css styling for search label nodes
labelStyle: {
border: '1px solid #c2d6d6',
backgroundColor: '#FFF',
color: '#000',
fontSize: '10px',
position: 'absolute'
},
// css styling for control search hits
controlStyle: {
border: '2px solid #00FF00'
}
};
// =====
// public api
// =====
// 'search' api
function search(query) {
var aMatches = aLastResults =_search(query)
.filter(function(o) {
return o.sIdMatch !== null || o.sNameMatch !== null;
});
console.log(aLastResults.length + ' search results');
_populateSearchTargetVariables(ui$, aMatches);
return _toControl(aMatches);
}
// 'all' api
function all() {
var aMatches = aLastResults =_search();
_populateSearchTargetVariables(ui$, aMatches);
return _toControl(aMatches);
}
// 'config' api
function config(o) {
return _appendConfig(o);
}
// 'reset' api
// reset configuration object to defaults
function reset() {
oConfig = {};
oConfig = jQuery.extend({}, {}, oConfigDefaults);
aLastResults = [];
}
// 'toPipeline' api
function toPipeline(aResults) {
aLastResults = aResults.map(function(oMatch) {
return {
$element: jQuery(oMatch),
sIdMatch: undefined,
sNameMatch: undefined
};
});
return _toPipeline();
}
// 'pipe' api
function pipe(query) {
search(query, oConfig);
return _toPipeline(); // cache is accessed
}
// 'plugin' api
// fnFirstPass and unLastPass are passed the dom element match
// A scope context is shared between fnUnSet and fnUnSet.
function definePlugin(sName, fnFirstPass, fnLastPass) {
var oPlugin = {
name: sName,
fnFirstPass: fnFirstPass,
fnLastPass: fnLastPass
};
oInProgressFlags[sName] = false;
aPlugins.push(oPlugin);
ui$[sName] = function(v) {
if (jQuery.isArray(v)) {
_invokePlugin(oPlugin, aLastResults);
return v;
}
// if a new query
return pipe(v)[sName]().toArray();
};
}
// =====
// private functions api
// =====
// http://stackoverflow.com/q/3446170/2295034
function _escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}
// convert search matches to an array of control instances
function _toControl(aMatches) {
return aMatches
.map(function(o) {
return o.$element.control()[0];
});
}
// append object to config
function _appendConfig(o) {
var sKey;
if (typeof o === 'object') {
for (sKey in o) {
if (o.hasOwnProperty(sKey)) {
oConfig[sKey] = o[sKey];
}
}
}
return oConfig;
}
// gather all control instances
function _gatherElements() {
return jQuery('*[id]')
.toArray()
// use jQuery instances
.map(function(oElement) {
return jQuery(oElement);
})
.filter(function($element) {
// is at least part of a control
return $element.control().length > 0 &&
// is the root of a control
$element.attr('id') === $element.control()[0].getId();
});
}
// overlay style object onto element
function _assignStyle($node, oStyle) {
for (var key in oStyle) {
if (oStyle.hasOwnProperty(key)) {
$node.css(key, oStyle[key]);
}
}
}
// used by "search" and "all" api
function _search(query) {
var regexp;
// empty args matches everythin
if (query === undefined) {
query = /.*/;
}
// sanitize string for regexp if necessary
try {
if (query instanceof RegExp) {
regexp = query;
} else {
regexp = new RegExp(query, 'i');
}
} catch(e) {
regexp = new RegExp(_escapeRegExp(query), 'i');
}
return _gatherElements()
// perform regex search
// put in "match" object form
.map(function($element) {
return {
$element: $element,
idMatch: $element.attr('id').match(regexp),
controlNameMatch: $element.control()[0].getMetadata().getName().match(regexp)
};
})
// normalize
.map(function(o) {
return {
$element: o.$element,
sIdMatch: (o.idMatch !== null) ? o.idMatch[0] : null,
sNameMatch: (o.controlNameMatch !== null) ? o.controlNameMatch[0] : null
};
});
}
// reset search target variables
function _clearSearchTargetVariables(o) {
o.target = undefined;
delete o.target;
[0,1,2,3,4,5,6,7,8,9].forEach(function(i) {
o['target' + i] = undefined;
delete o['target' + i];
});
}
// o -> the object to attach the variables to
// aMatches -> o[]
// { $element
// sIdMatch
// sNameMatch }
function _populateSearchTargetVariables(o, aMatches) {
_clearSearchTargetVariables(o);
if (aMatches.length > 0) {
o.target = aMatches[0].$element.control()[0];
aMatches.slice(0,10).forEach(function(oMatch, i) {
o['target' + i] = oMatch.$element.control()[0];
});
}
}
// return pipeline object from aLastResults
function _toPipeline() {
var oReturn = {
toArray: function() {
return aLastResults.map(function(o) {
return o.$element.get(0);
});
},
pipe: pipe
};
// add plugin functions to pipeline object
aPlugins.forEach(function(oPlugin) {
oReturn[oPlugin.name] = function() {
_invokePlugin(oPlugin, aLastResults);
return _toPipeline();
};
});
_populateSearchTargetVariables(oReturn, aLastResults);
// borrow properties/functions from Array.prototype
// TODO: should I just copy directly from aLastResults?
Object.getOwnPropertyNames(Array.prototype).slice(0).filter(function(o) {
return jQuery.isFunction(Array.prototype[o]);
}).forEach(function(sFunctionName) {
oReturn[sFunctionName] = jQuery.proxy(Array.prototype[sFunctionName], _toControl(aLastResults));
});
oReturn.length = oReturn.toArray().length;
return oReturn;
}
// invoke plugin functionallity to results list
// call both first and last pass functions
function _invokePlugin(oPlugin, aList) {
if (oInProgressFlags[oPlugin.name] === false) {
oInProgressFlags[oPlugin.name] = true;
Promise.all(
aList.map(function(oMatch) {
var oContext = {
idMatch: oMatch.sIdMatch,
nameMatch: oMatch.sNameMatch
};
oMatch = oMatch.$element;
oPlugin.fnFirstPass.call(oContext, oMatch);
return new Promise(function(resolve) {
setTimeout(function() {
oPlugin.fnLastPass.call(oContext, oMatch);
resolve();
}, oConfig.standoutDelay);
});
})
).then(function() {
oInProgressFlags[oPlugin.name] = false;
});
}
return aList;
}
// =====
// execution starts here
// =====
// define popup plugin
definePlugin('popup',
function(oMatch) {
var
// for positioning the labels
offset = oMatch.offset(),
// custom html tag to avoid any pre-existing styling
$label = jQuery('<ui5query></ui5query>')
.addClass('ui5query_label')
.css('left', offset.left)
.css('top', offset.top);
// gather lines of text and render delimited by line breaks
_assignStyle($label, oConfig.labelStyle);
$label.html(
[
// element id
oMatch.attr('id')
.replace(
new RegExp('(' + this.idMatch + ')'),
'<strong>$1</strong>'
),
// element ui5 type
oMatch.control()[0].getMetadata().getName()
.replace(
new RegExp('(' + this.nameMatch + ')'),
'<strong>$1</strong>'
)
].reduce(function(a, b) {
return a + '<br />' + b;
})
).appendTo(document.body);
this.$label = $label;
},
function(oMatch) {
this.$label.remove();
}
);
// define highlight plugin
definePlugin('highlight',
function(oMatch) {
this.sOrigBorder = oMatch.css('border');
_assignStyle(oMatch, oConfig.controlStyle);
oMatch.addClass('ui5query_match');
},
function(oMatch) {
oMatch.css('border', this.sOrigBorder);
oMatch.removeClass('ui5query_match');
}
);
// define standout plugin
definePlugin('standout',
function(oMatch) {
var self = this;
aStandoutPlugins.forEach(function(sPluginName) {
aPlugins.filter(function(oPlugin) {
return oPlugin.name === sPluginName;
})[0].fnFirstPass.call(self, oMatch);
});
},
function(oMatch) {
var self = this;
aStandoutPlugins.forEach(function(sPluginName) {
aPlugins.filter(function(oPlugin) {
return oPlugin.name === sPluginName;
})[0].fnLastPass.call(self, oMatch);
});
}
);
// init config
reset();
// bring together main api object
ui$.pipe = pipe;
ui$.search = search;
ui$.all = all;
ui$.config = config;
ui$.reset = reset;
ui$.plugin = definePlugin;
ui$.toPipeline = toPipeline;
window.ui$ = ui$;
window.ui5Query = ui$;
}());