-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwoven.js
64 lines (59 loc) · 1.82 KB
/
woven.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
/**
* @license MIT http://troopjs.mit-license.org/
*/
define([
"./config",
"when/when",
"jquery"
], function (config, when, $) {
"use strict";
/**
* @class widget.woven
* @mixin widget.config
* @mixin Function
* @static
*/
var NULL = null;
var ARRAY_MAP = Array.prototype.map;
var LENGTH = "length";
var $WEFT = config.widget.$weft;
var RE_ANY = /.*/;
var RE_WIDGET = /([\w\/\.\-]+)(?:@(\d+))?/;
/**
* Retrieve all or specific {@link widget.component widget} instances living on this element, that are
* created by {@link widget.weave}.
*
* It also lives as a jquery plugin as {@link $#method-woven}.
* @method constructor
* @param {...String} [selector] One or more widget selectors to narrow down the returned ones.
*
* * (empty string) retrieves all woven widgets
* * `module/name` retrieves widgets matching module name
* * `module/name@instance` retrieves widgets matching both module name and instance id
* @return {Promise} Promise to the completion of retrieving the woven widgets array.
*/
return function () {
var woven_re = arguments[LENGTH] > 0
? new RegExp(
ARRAY_MAP
.call(arguments, function (arg) {
var matches;
// matches[1] : widget name - "widget/name"
// matches[2] : widget instance id - "123"
return (matches = RE_WIDGET.exec(arg)) !== NULL
? "^" + matches[1] + "@" + (matches[2] || "\\d+") + "$"
: NULL;
})
.filter(function (arg) {
return arg !== NULL;
})
.join("|")
)
: RE_ANY;
return when.all(ARRAY_MAP.call(this, function (element) {
return when.filter($.data(element, $WEFT) || false, function (widget) {
return woven_re.test(widget);
});
}));
};
});