vendor/assets/javascripts/jquery.role.js in role-rails-1.2.1 vs vendor/assets/javascripts/jquery.role.js in role-rails-1.3.0
- old
+ new
@@ -1,19 +1,68 @@
!function($){
function rewriteSelector(context, name, argPos){
var original = context[name];
-
+
if (!original) return;
-
+
context[name] = function(){
- arguments[argPos] = arguments[argPos].replace(/@([\w\u00c0-\uFFFF\-]+)/g, '[role~="$1"]')
+ arguments[argPos] = arguments[argPos].replace(/@([\w\u00c0-\uFFFF\-]+)/g, '[role~="$1"]');
return original.apply(context, arguments);
};
-
+
$.extend(context[name], original);
}
-
+
rewriteSelector($, 'find', 0);
rewriteSelector($, 'multiFilter', 0);
rewriteSelector($.find, 'matchesSelector', 1);
rewriteSelector($.find, 'matches', 0);
+
+ function parse(roleString, without){
+ var role, result = [], roles = $.trim(roleString).split(/\s+/);
+
+ for(var i=0; i<roles.length; i++) {
+ role = roles[i];
+ if (!~$.inArray(role, result) && (!without || !~$.inArray(role, without)))
+ result.push(role);
+ }
+
+ return result;
+ };
+
+ $.extend($.fn, {
+ roles: function(){ return parse(this.attr('role')); },
+
+ hasRole: function(roleName){
+ var roles = parse(roleName);
+ for(var i=0;i<roles.length;i++)
+ if (!this.is('@'+roles[i])) return false;
+
+ return true;
+ },
+
+ addRole: function(roleName){
+ if (this.hasRole(roleName)) return this;
+
+ return this.each(function(_, element){
+ var $el = $(element);
+ $el.attr('role', parse($el.attr('role') + ' ' + roleName).join(' '));
+ });
+ },
+
+ removeRole: function(roleName){
+ if (!this.hasRole(roleName)) return this;
+
+ return this.each(function(_, element){
+ var $el = $(element);
+ $el.attr('role', parse($el.attr('role'), parse(roleName)).join(' '));
+ });
+ },
+
+ toggleRole: function(roleName){
+ var roles = parse(roleName);
+ for(var i=0;i<roles.length;i++)
+ this[this.hasRole(roles[i]) ? 'removeRole' : 'addRole'].call(this, roles[i]);
+ return this;
+ }
+ });
}(jQuery)
\ No newline at end of file