-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.js
99 lines (86 loc) · 2.32 KB
/
plugin.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
/**
* @license MIT http://troopjs.mit-license.org/
*/
define([
"jquery",
"when/when",
"./config",
"./weave",
"./unweave",
"./woven"
], function ($, when, config, weave, unweave, woven) {
"use strict";
/**
* Extends {@link jQuery} with:
*
* - {@link $#property-woven} property
* - {@link $#method-weave}, {@link $#method-unweave} and {@link $#method-woven} methods
*
* @class widget.plugin
* @static
* @alias plugin.jquery
*/
var UNDEFINED;
var $FN = $.fn;
var $EXPR = $.expr;
var WEAVE = "weave";
var UNWEAVE = "unweave";
var WOVEN = "woven";
var ATTR_WOVEN = config.widget.woven;
/**
* Tests if element has a data-woven attribute
* @param element to test
* @return {boolean}
* @ignore
*/
function hasDataWovenAttr (element) {
return $(element).attr(ATTR_WOVEN) !== UNDEFINED;
}
/**
* @class $
*/
/**
* jQuery `:woven` expression
* @property woven
*/
$EXPR[":"][WOVEN] = $EXPR.createPseudo(function (widgets) {
// If we don't have widgets to test, quick return optimized expression
if (widgets === UNDEFINED) {
return hasDataWovenAttr;
}
// Scope `woven_re` locally since we use the `g` flag
var woven_re = /[\s,]*([\w\d_\/\.\-]+)(?:@(\d+))?/g;
var woven_res = [];
var woven_res_length = 0;
var matches;
// Iterate `widgets` (while woven_re matches)
// matches[1] : widget name - "widget/name"
// matches[2] : widget instance id - "123"
while ((matches = woven_re.exec(widgets)) !== null) {
woven_res[woven_res_length++] = "(?:^|[\\s,]+)" + matches[1] + "@" + (matches[2] || "\\d+") + "($|[\\s,]+)";
}
// Redefine `woven_re` as a combined regexp
woven_re = new RegExp(woven_res.join("|"));
// Return expression
return function (element) {
var attr_woven = $.attr(element, ATTR_WOVEN);
// Check that attr_woven is not UNDEFINED, and that widgets test against a processed attr_woven
return attr_woven !== UNDEFINED && woven_re.test(attr_woven);
};
});
/**
* @method weave
* @inheritdoc widget.weave#constructor
*/
$FN[WEAVE] = weave;
/**
* @method unweave
* @inheritdoc widget.unweave#constructor
*/
$FN[UNWEAVE] = unweave;
/**
* @method woven
* @inheritdoc widget.woven#constructor
*/
$FN[WOVEN] = woven;
});