Sha256: 25deb8279da80fd9326c6cd2359592aea106878afd67640ecaa979fa73fe9782

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

!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"]');
      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)

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
role-rails-1.4.0 vendor/assets/javascripts/jquery.role.js
better-role-rails-1.0.0 vendor/assets/javascripts/jquery.role.js
role-rails-1.3.1 vendor/assets/javascripts/jquery.role.js
role-rails-1.3.0 vendor/assets/javascripts/jquery.role.js