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 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: $.bem = { Daniel@0: generateBlock: function(tag, blockName, modifiers) { Daniel@0: if (blockName === undefined) { Daniel@0: blockName = tag; Daniel@0: tag = 'div'; Daniel@0: } else if (Array.isArray(blockName)) { Daniel@0: modifiers = blockName; Daniel@0: blockName = tag; Daniel@0: tag = 'div'; Daniel@0: } Daniel@0: Daniel@0: var $block = $('<' + tag + '/>').addClass(blockName); Daniel@0: Daniel@0: if (Array.isArray(modifiers)) { Daniel@0: for (var i = 0; i < modifiers.length; i++) { Daniel@0: $block.addClass(blockName + '_' + modifiers[i]); Daniel@0: } Daniel@0: } Daniel@0: return $block; Daniel@0: }, Daniel@0: Daniel@0: generateElement: function(tag, blockName, elementName, modifiers) { Daniel@0: if (elementName === undefined) { Daniel@0: elementName = blockName; Daniel@0: blockName = tag; Daniel@0: tag = 'div'; Daniel@0: } else if (Array.isArray(elementName)) { Daniel@0: modifiers = elementName; Daniel@0: elementName = blockName; Daniel@0: blockName = tag; Daniel@0: tag = 'div'; Daniel@0: } Daniel@0: Daniel@0: var elementClassName = blockName + '__' + elementName; Daniel@0: var $element = $('<' + tag + '/>').addClass(elementClassName); Daniel@0: Daniel@0: if (Array.isArray(modifiers)) { Daniel@0: for (var i = 0; i < modifiers.length; i++) { Daniel@0: $element.addClass(elementClassName + '_' + modifiers[i]); Daniel@0: } Daniel@0: } Daniel@0: return $element; 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: $.each(getElemClasses(this), function(i, c) { Daniel@0: if (c.indexOf(classPattern) === 0) { Daniel@0: var afterPattern = c.substr(classPattern.length, 1); Daniel@0: if (afterPattern === undefined || afterPattern === '_') { Daniel@0: $this.removeClass(c); Daniel@0: } Daniel@0: } Daniel@0: }); 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: }, 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);