Daniel@0: /** Daniel@0: * jQuery plugin for basic BEM manipulations. Daniel@0: * Daniel@0: * @author Max Shirshin Daniel@0: * @version 2.1.3 Daniel@0: * Daniel@0: */ Daniel@0: (function($, undefined) { Daniel@0: Daniel@0: var getEventPattern = function(block, elem, modName, modVal) { Daniel@0: return block + (elem !== undefined ? '__' + elem : '') + '_' + modName + '_' + (typeof modVal === 'boolean' ? '*' : (modVal || '*')); Daniel@0: }, Daniel@0: getElemClasses = function(domEl) { Daniel@0: if (domEl.classList) { Daniel@0: return $.makeArray(domEl.classList); Daniel@0: } else { Daniel@0: return $.trim(domEl.className).split(/\s+/); Daniel@0: } Daniel@0: }; Daniel@0: Daniel@0: $.extend($.fn, { Daniel@0: getMod: function(block, elem, modName) { Daniel@0: if (modName === undefined) { Daniel@0: modName = elem; Daniel@0: elem = undefined; Daniel@0: } Daniel@0: Daniel@0: if (this.length) { Daniel@0: var classPattern = block + (elem !== undefined ? '__' + elem : '') + '_' + modName, Daniel@0: modVal = false; Daniel@0: Daniel@0: $.each(getElemClasses(this.get(0)), function(i, c) { Daniel@0: if (c === classPattern) { Daniel@0: modVal = true; Daniel@0: return false; Daniel@0: } else if (c.indexOf(classPattern) === 0 && c.substr(classPattern.length, 1) === '_') { Daniel@0: modVal = c.substr(classPattern.length + 1); Daniel@0: return false; Daniel@0: } Daniel@0: }); Daniel@0: Daniel@0: return modVal; Daniel@0: Daniel@0: } else return undefined; Daniel@0: }, Daniel@0: Daniel@0: setMod: function(block, elem, modName, modVal) { Daniel@0: if (modVal === undefined) { Daniel@0: modVal = modName; Daniel@0: modName = elem; Daniel@0: elem = undefined; Daniel@0: } Daniel@0: Daniel@0: return this.each(function() { Daniel@0: var $this = $(this), Daniel@0: classPattern = block + (elem !== undefined ? '__' + elem : '') + '_' + modName; Daniel@0: Daniel@0: if (modVal === false) { Daniel@0: $this.removeClass(classPattern); Daniel@0: } else if (modVal === true) { Daniel@0: $this.addClass(classPattern); Daniel@0: } else { Daniel@0: $.each(getElemClasses(this), function(i, c) { Daniel@0: if (c.indexOf(classPattern) === 0 && c.substr(classPattern.length, 1) === '_') { Daniel@0: $this.removeClass(c); Daniel@0: } Daniel@0: }); Daniel@0: Daniel@0: $this.addClass(classPattern + '_' + modVal); Daniel@0: } Daniel@0: Daniel@0: // after the modifier is set, run the corresponding custom event Daniel@0: var args = { Daniel@0: block: block, Daniel@0: elem: elem, Daniel@0: modName: modName, Daniel@0: modVal: modVal Daniel@0: }; Daniel@0: Daniel@0: // trigger the wildcard event pattern first Daniel@0: $this.trigger('setMod:' + getEventPattern(block, elem, modName), args); Daniel@0: // for boolean modifiers, one can only use the wildcard pattern, Daniel@0: // so no need to trigger the same event twice Daniel@0: if (typeof modVal !== 'boolean') { Daniel@0: $this.trigger('setMod:' + getEventPattern(block, elem, modName, modVal), args); Daniel@0: } Daniel@0: }); Daniel@0: }, Daniel@0: Daniel@0: hasMod: function(block, elem, modName) { Daniel@0: return !! this.getMod(block, elem, modName); Daniel@0: } Daniel@0: }); Daniel@0: Daniel@0: })(jQuery);