annotate src/DML/VendorAssetsBundle/Resources/assets/jquery.bemhelpers/2.1.3-modif/jquery.bemhelpers.js @ 1:f38015048f48 tip

Added GPL
author Daniel Wolff
date Sat, 13 Feb 2016 20:43:38 +0100
parents 493bcb69166c
children
rev   line source
Daniel@0 1 /**
Daniel@0 2 * jQuery plugin for basic BEM manipulations.
Daniel@0 3 *
Daniel@0 4 * @author Max Shirshin
Daniel@0 5 * @version 2.1.3
Daniel@0 6 *
Daniel@0 7 */
Daniel@0 8 (function($, undefined) {
Daniel@0 9
Daniel@0 10 var getElemClasses = function(domEl) {
Daniel@0 11 if (domEl.classList) {
Daniel@0 12 return $.makeArray(domEl.classList);
Daniel@0 13 } else {
Daniel@0 14 return $.trim(domEl.className).split(/\s+/);
Daniel@0 15 }
Daniel@0 16 };
Daniel@0 17
Daniel@0 18 $.bem = {
Daniel@0 19 generateBlock: function(tag, blockName, modifiers) {
Daniel@0 20 if (blockName === undefined) {
Daniel@0 21 blockName = tag;
Daniel@0 22 tag = 'div';
Daniel@0 23 } else if (Array.isArray(blockName)) {
Daniel@0 24 modifiers = blockName;
Daniel@0 25 blockName = tag;
Daniel@0 26 tag = 'div';
Daniel@0 27 }
Daniel@0 28
Daniel@0 29 var $block = $('<' + tag + '/>').addClass(blockName);
Daniel@0 30
Daniel@0 31 if (Array.isArray(modifiers)) {
Daniel@0 32 for (var i = 0; i < modifiers.length; i++) {
Daniel@0 33 $block.addClass(blockName + '_' + modifiers[i]);
Daniel@0 34 }
Daniel@0 35 }
Daniel@0 36 return $block;
Daniel@0 37 },
Daniel@0 38
Daniel@0 39 generateElement: function(tag, blockName, elementName, modifiers) {
Daniel@0 40 if (elementName === undefined) {
Daniel@0 41 elementName = blockName;
Daniel@0 42 blockName = tag;
Daniel@0 43 tag = 'div';
Daniel@0 44 } else if (Array.isArray(elementName)) {
Daniel@0 45 modifiers = elementName;
Daniel@0 46 elementName = blockName;
Daniel@0 47 blockName = tag;
Daniel@0 48 tag = 'div';
Daniel@0 49 }
Daniel@0 50
Daniel@0 51 var elementClassName = blockName + '__' + elementName;
Daniel@0 52 var $element = $('<' + tag + '/>').addClass(elementClassName);
Daniel@0 53
Daniel@0 54 if (Array.isArray(modifiers)) {
Daniel@0 55 for (var i = 0; i < modifiers.length; i++) {
Daniel@0 56 $element.addClass(elementClassName + '_' + modifiers[i]);
Daniel@0 57 }
Daniel@0 58 }
Daniel@0 59 return $element;
Daniel@0 60 }
Daniel@0 61 };
Daniel@0 62
Daniel@0 63 $.extend($.fn, {
Daniel@0 64 getMod: function(block, elem, modName) {
Daniel@0 65 if (modName === undefined) {
Daniel@0 66 modName = elem;
Daniel@0 67 elem = undefined;
Daniel@0 68 }
Daniel@0 69
Daniel@0 70 if (this.length) {
Daniel@0 71 var classPattern = block + (elem !== undefined ? '__' + elem : '') + '_' + modName,
Daniel@0 72 modVal = false;
Daniel@0 73
Daniel@0 74 $.each(getElemClasses(this.get(0)), function(i, c) {
Daniel@0 75 if (c === classPattern) {
Daniel@0 76 modVal = true;
Daniel@0 77 return false;
Daniel@0 78 } else if (c.indexOf(classPattern) === 0 && c.substr(classPattern.length, 1) === '_') {
Daniel@0 79 modVal = c.substr(classPattern.length + 1);
Daniel@0 80 return false;
Daniel@0 81 }
Daniel@0 82 });
Daniel@0 83
Daniel@0 84 return modVal;
Daniel@0 85
Daniel@0 86 } else return undefined;
Daniel@0 87 },
Daniel@0 88
Daniel@0 89 setMod: function(block, elem, modName, modVal) {
Daniel@0 90 if (modVal === undefined) {
Daniel@0 91 modVal = modName;
Daniel@0 92 modName = elem;
Daniel@0 93 elem = undefined;
Daniel@0 94 }
Daniel@0 95
Daniel@0 96 return this.each(function() {
Daniel@0 97 var $this = $(this),
Daniel@0 98 classPattern = block + (elem !== undefined ? '__' + elem : '') + '_' + modName;
Daniel@0 99
Daniel@0 100 if (modVal === false) {
Daniel@0 101 $.each(getElemClasses(this), function(i, c) {
Daniel@0 102 if (c.indexOf(classPattern) === 0) {
Daniel@0 103 var afterPattern = c.substr(classPattern.length, 1);
Daniel@0 104 if (afterPattern === undefined || afterPattern === '_') {
Daniel@0 105 $this.removeClass(c);
Daniel@0 106 }
Daniel@0 107 }
Daniel@0 108 });
Daniel@0 109 } else if (modVal === true) {
Daniel@0 110 $this.addClass(classPattern);
Daniel@0 111 } else {
Daniel@0 112 $.each(getElemClasses(this), function(i, c) {
Daniel@0 113 if (c.indexOf(classPattern) === 0 && c.substr(classPattern.length, 1) === '_') {
Daniel@0 114 $this.removeClass(c);
Daniel@0 115 }
Daniel@0 116 });
Daniel@0 117
Daniel@0 118 $this.addClass(classPattern + '_' + modVal);
Daniel@0 119 }
Daniel@0 120 });
Daniel@0 121 },
Daniel@0 122
Daniel@0 123 hasMod: function(block, elem, modName) {
Daniel@0 124 return !! this.getMod(block, elem, modName);
Daniel@0 125 }
Daniel@0 126 });
Daniel@0 127
Daniel@0 128 })(jQuery);