Skip to content

Commit 81956dd

Browse files
committed
add work with mods feature
1 parent f5b31e7 commit 81956dd

File tree

2 files changed

+168
-14
lines changed

2 files changed

+168
-14
lines changed

README.md

100644100755
File mode changed.

jquery.ui.widget.tools.js

100644100755
+168-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(function ($) {
22

3-
var W = $.Widget;
3+
var W = jQuery.Widget;
44

55
/**
66
* Shortcut for $.proxy(func,this)
@@ -22,7 +22,7 @@
2222
return this;
2323
};
2424

25-
if (typeof W.prototype['_delay'] == 'undefined') {
25+
if (typeof W.prototype['_delay'] === 'undefined') {
2626
/**
2727
* Shortcut for setTimeout($.proxy(func,this), delay || 0)
2828
* @param {Function} func
@@ -47,40 +47,194 @@
4747
return buildModClass(buildElemClass(prefix, elemName), modName, modValue);
4848
};
4949

50+
var BEM_NAME_REGEXP = '[a-z0-9\-]+';
51+
52+
function getElemNameFromNode(prefix, ctx) {
53+
var classList,
54+
classIndex,
55+
searchData,
56+
searchRegExp;
57+
58+
searchRegExp = new RegExp('^' + prefix + '__(' + BEM_NAME_REGEXP + ')$', 'i');
59+
classList = ctx.first().attr('class').split(' ');
60+
for (classIndex = 0; classIndex < classList.length; classIndex++) {
61+
searchData = $.trim(classList[classIndex]).match(searchRegExp);
62+
if (searchData) {
63+
return searchData[1];
64+
}
65+
}
66+
return false;
67+
}
68+
69+
function getModList(ctx, prefix) {
70+
var mods = {},
71+
classList, classIndex,
72+
searchRegExp,
73+
searchData;
74+
75+
searchRegExp = new RegExp('^' + prefix + '_(' + BEM_NAME_REGEXP + ')(?=_(' + BEM_NAME_REGEXP + ')$|$)', 'i');
76+
classList = (ctx.attr('class') || '').split(' ');
77+
for (classIndex = 0; classIndex < classList.length; classIndex++) {
78+
searchData = $.trim(classList[classIndex]).match(searchRegExp);
79+
if (searchData) {
80+
mods[searchData[1]] = searchData[2] || null;
81+
}
82+
}
83+
84+
return mods;
85+
}
86+
5087
/**
5188
* Find "element" of "block" in "context"
52-
* @param {String} elemName
89+
* @param {string} elemName
5390
* @param {jQuery} [ctx]
91+
* @param {string} [modName]
92+
* @param {string} [modValue]
5493
* @return {jQuery}
5594
*/
56-
W.prototype._elem = function (ctx, elemName) {
57-
if (typeof elemName == 'undefined') {
95+
W.prototype._elem = function (ctx, elemName, modName, modValue) {
96+
var searchClass;
97+
98+
if (typeof ctx === 'string') {
99+
modValue = modName;
100+
modName = elemName;
58101
elemName = ctx;
59102
ctx = this.element;
60103
}
61104

62-
return ctx.find('.' + buildElemClass(this.widgetName, elemName));
105+
searchClass = buildElemClass(this.widgetName, elemName);
106+
if (modName) {
107+
searchClass = buildModClass(searchClass, modName, modValue);
108+
}
109+
110+
return ctx.find('.' + searchClass);
63111
};
64112

65113
/**
66114
* Get class for block modName with modValue
67-
* @param {String} modName
68-
* @param {String} [modValue]
69-
* @return {String}
115+
* @param {string} modName
116+
* @param {string} [modValue]
117+
* @return {string}
70118
*/
71119
W.prototype._getModClass = function (modName, modValue) {
72120
return buildModClass(this.widgetName, modName, modValue);
73121
};
74122

75123
/**
76124
* Get class for block element modName with modValue
77-
* @param {String} elemName
78-
* @param {String} modName
79-
* @param {String} [modValue]
80-
* @return {String}
125+
* @param {string} elemName
126+
* @param {string} modName
127+
* @param {string} [modValue]
128+
* @return {string}
81129
*/
82130
W.prototype._getModElemClass = function (elemName, modName, modValue) {
83131
return buildModElemClass(this.widgetName, elemName, modName, modValue);
84-
}
132+
};
133+
134+
/**
135+
* Set mod value class to block or element
136+
* @param {string} modName
137+
* @param {jQuery} [elem]
138+
* @param {string} [modValue]
139+
* @return {jQuery.Widget}
140+
*/
141+
W.prototype._setMod = function (elem, modName, modValue) {
142+
var prefix, elemName;
143+
144+
if (typeof elem === 'string') {
145+
modValue = modName;
146+
modName = elem;
147+
elem = this.element;
148+
prefix = this.widgetName;
149+
this._delMod(modName);
150+
} else {
151+
elemName = getElemNameFromNode(this.widgetName, elem);
152+
if (!elemName) {
153+
throw "Can't find elem of " + this.widgetName + " at " + elem;
154+
}
155+
prefix = buildElemClass(this.widgetName, elemName);
156+
this._delMod(elem, modName);
157+
}
158+
159+
elem.addClass(buildModClass(prefix, modName, modValue));
160+
return this;
161+
};
162+
163+
/**
164+
* Remove mod value class from block or element
165+
* @param {string} modName
166+
* @param {jQuery} [elem]
167+
* @param {string} [modValue]
168+
* @return {jQuery.Widget}
169+
*/
170+
W.prototype._delMod = function (elem, modName) {
171+
var prefix,
172+
elemName,
173+
mods,
174+
classToRemove;
175+
176+
if (typeof elem === 'string') {
177+
modName = elem;
178+
elem = this.element;
179+
prefix = this.widgetName;
180+
} else {
181+
elemName = getElemNameFromNode(this.widgetName, elem);
182+
if (!elemName) {
183+
throw "Can't find elem of " + this.widgetName + " at " + elem;
184+
}
185+
prefix = buildElemClass(this.widgetName, elemName);
186+
}
187+
188+
mods = getModList(elem, prefix);
189+
classToRemove = buildModClass(prefix, modName, mods[modName]);
190+
elem.removeClass(classToRemove);
191+
192+
return this;
193+
};
194+
195+
/**
196+
* Check value mod class at block or element
197+
* @param {string} modName
198+
* @param {jQuery} [elem]
199+
* @param {string} [modValue]
200+
* @return {boolean}
201+
*/
202+
W.prototype._hasMod = function (elem, modName, modValue) {
203+
if (typeof elem === 'string') {
204+
modValue = modName;
205+
modName = elem;
206+
elem = this.element;
207+
}
208+
209+
if (typeof modValue === 'undefined') {
210+
modValue = null;
211+
}
212+
213+
return this._getMod(elem, modName) === modValue;
214+
};
215+
216+
/**
217+
* Get mod value of block or element
218+
* @param {string}modName
219+
* @param {jQuery} [elem]
220+
* @return {string}
221+
*/
222+
W.prototype._getMod = function (elem, modName) {
223+
var prefix, elemName;
224+
225+
if (typeof elem === 'string') {
226+
modName = elem;
227+
elem = this.element;
228+
prefix = this.widgetName;
229+
} else {
230+
elemName = getElemNameFromNode(this.widgetName, elem);
231+
if (!elemName) {
232+
throw "Can't find elem of " + this.widgetName + " at " + elem;
233+
}
234+
prefix = buildElemClass(this.widgetName, elemName);
235+
}
236+
237+
return getModList(elem, prefix)[modName];
238+
};
85239

86240
})(jQuery);

0 commit comments

Comments
 (0)