Comparing version 0.0.1 to 0.1.0
@@ -5,2 +5,10 @@ # bemquery Changelog | ||
## 0.1.0 | ||
* Updated boilerplate. | ||
* Updated dependencies. | ||
* Added [bemquery-selector-converter](https://github.com/BEMQuery/bemquery-selector-converter) module as dependency. | ||
## 0.0.1 | ||
* First working version |
@@ -1,4 +0,2 @@ | ||
/*! BEMQuery v0.0.1 | (c) 2016 BEMQuery | MIT license (see LICENSE) */ | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):global.bemquery=factory()})(this,function(){"use strict";var babelHelpers={};babelHelpers.typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol?"symbol":typeof obj};babelHelpers.classCallCheck=function(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}};babelHelpers.createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}();babelHelpers;/*! BEMQuery v0.1.0 | (c) 2016 BEMQuery | MIT license (see LICENSE) */ | ||
var Selector=function Selector(BEM,CSS){babelHelpers.classCallCheck(this,Selector);this.BEM=BEM;this.CSS=CSS;Object.freeze(this)};function endsWithModifier(selector,bemConfig){var regex=new RegExp("[^"+bemConfig.elemSeparator+bemConfig.modifierSeparator+"]+"+bemConfig.modifierSeparator+"[^"+bemConfig.elemSeparator+bemConfig.modifierSeparator+"]+$","g");return!!selector.match(regex)}function getSelectorWithoutModifier(selector,modifierSeparator){return" "+selector.substring(selector.lastIndexOf("."),selector.lastIndexOf(modifierSeparator))}var defaultConfig={bem:{elemSeparator:"__",modifierSeparator:"_"},rules:{"default":function _default(token){return"."+token}," > ":function _(token,config){return" "+config.rules.default(token)}," ":function _(token,config,selector){if(endsWithModifier(selector,config.bem)){return""+getSelectorWithoutModifier(selector,config.bem.modifierSeparator)+config.bem.elemSeparator+token}return""+config.bem.elemSeparator+token},":":function _(token,config){return""+config.bem.modifierSeparator+token}}};function convertToken(tokens,config){var selector=arguments.length<=2||arguments[2]===undefined?"":arguments[2];var rules=config.rules;var delimeter=tokens.shift();var rule=void 0;var token=void 0;if(!delimeter){return selector}else if(!selector){token=delimeter;rule=rules.default}else{token=tokens.shift();rule=rules[delimeter]}if(typeof rule!=="function"){throw new SyntaxError("Malformed BEM rule")}selector+=rule(token,config,selector);return convertToken(tokens,config,selector)}function _convert(selector,config){var rules=Object.keys(config.rules).filter(function(rule){return rule!=="default"});var splitRule=new RegExp("("+rules.join("|")+")","g");var splittedSelector=selector.split(splitRule);selector=convertToken(splittedSelector,config);return selector}var Converter=function(){function Converter(){var config=arguments.length<=0||arguments[0]===undefined?defaultConfig:arguments[0];babelHelpers.classCallCheck(this,Converter);this.config=config}babelHelpers.createClass(Converter,[{key:"convert",value:function convert(selector){var convertedSelector=_convert(selector,this.config);return new Selector(selector,convertedSelector)}},{key:"getStateFromClass",value:function getStateFromClass(className){if(typeof className!=="string"){throw new TypeError("Class must be a string.")}var bemConfig=this.config.bem;var regex=new RegExp("[^"+bemConfig.elemSeparator+bemConfig.modifierSeparator+"]+"+bemConfig.modifierSeparator+"([^"+bemConfig.elemSeparator+bemConfig.modifierSeparator+"]+)$");var match=className.match(regex);return match?match[1]:null}}]);return Converter}();var SelectorEngine=function(){function SelectorEngine(converter){babelHelpers.classCallCheck(this,SelectorEngine);this.converter=converter}babelHelpers.createClass(SelectorEngine,[{key:"find",value:function find(selector){var context=arguments.length<=1||arguments[1]===undefined?document:arguments[1];var convertedSelector=this.converter.convert(selector);var result={selector:convertedSelector};var cssSelector=convertedSelector.CSS;var tmpId=false;if(context!==document){if(!context.id){tmpId=true;context.id="BEMQueryTMP_"+Date.now()}cssSelector="#"+context.id+" "+cssSelector}var elements=context.querySelectorAll(cssSelector);result.elements=Array.from(elements);if(tmpId){context.removeAttribute("id")}return result}}]);return SelectorEngine}();function factory$1(){var converterConfig=arguments.length<=0||arguments[0]===undefined?defaultConfig:arguments[0];var converter=new Converter(converterConfig);var selectorEngine=new SelectorEngine(converter);return selectorEngine}function checkSelectorEngine(selectorEngine){return(typeof selectorEngine==="undefined"?"undefined":babelHelpers.typeof(selectorEngine))==="object"&&typeof selectorEngine.find==="function"}function determineContext(context){if(context instanceof BEMQuery){context=context.elements[0]}if(!(context instanceof HTMLElement)&&context!==document){context=document}return context}function fetchElements(query,context,selectorEngine){if(!query){throw new TypeError("Selector must be set.")}if(typeof query==="string"){return selectorEngine.find(query,context).elements}else if(query instanceof HTMLElement){return[query]}else if(query instanceof BEMQuery){return query.elements}else if((typeof query==="undefined"?"undefined":babelHelpers.typeof(query))==="object"){return Array.from(query)}else{throw new TypeError("Selector must be a string, object, array or DOM element.")}}function defineProperties(obj,elements){Object.defineProperty(obj,"elements",{value:elements});obj.elements.forEach(function(element,index){Object.defineProperty(obj,index,{enumerable:true,get:function get(){return new BEMQuery(this.elements[index],document,this.selectorEngine)}})},obj);Object.defineProperty(obj,"length",{enumerable:true,get:function get(){return this.elements.length}})}var BEMQuery=function(){function BEMQuery(query,context,selectorEngine){babelHelpers.classCallCheck(this,BEMQuery);if(!checkSelectorEngine(selectorEngine)){throw new TypeError("SelectorEngine must be an object with find method defined.")}this.selectorEngine=selectorEngine;context=determineContext(context);defineProperties(this,fetchElements(query,context,selectorEngine))}babelHelpers.createClass(BEMQuery,[{key:"get",value:function get(index){index=Number(index);if(Number.isNaN(index)){throw new TypeError("Index must be a correct Number.")}else if(index<0){throw new RangeError("Index must be greater or equal to 0.")}else if(index>this.elements.length-1){throw new RangeError("Index cannot be greater than collection's length.")}return new BEMQuery(this.elements[index],document,this.selectorEngine)}},{key:"each",value:function each(callback){if(typeof callback!=="function"){throw new TypeError("Callback must be a function.")}var selectorEngine=this.selectorEngine;this.elements.forEach(function(element){callback(new BEMQuery(element,document,selectorEngine))});return this}},{key:Symbol.iterator,value:function value(){var i=0;var elements=this.elements;var selectorEngine=this.selectorEngine;return{next:function next(){if(i<elements.length){var element=elements[i++];return{value:new BEMQuery([element],document,selectorEngine),done:false}}return{done:true}}}}}]);return BEMQuery}();function factory(query){var context=arguments.length<=1||arguments[1]===undefined?document:arguments[1];var selectorEngine=factory$1();var bemQuery=new BEMQuery(query,context,selectorEngine);return bemQuery}var $=Object.freeze({BEMQuery:BEMQuery,"default":factory});var Batch=function(){function Batch(){babelHelpers.classCallCheck(this,Batch);this.read=[];this.write=[]}babelHelpers.createClass(Batch,[{key:"add",value:function add(type,fn){if(type!=="read"&&type!=="write"){throw new TypeError("Type must be either 'read' or 'write'.")}if(typeof fn!=="function"){throw new TypeError("Task must be a function.")}this[type].push(fn)}},{key:"run",value:function run(){var _this=this;var type=arguments.length<=0||arguments[0]===undefined?"read":arguments[0];if(type!=="read"&&type!=="write"){throw new TypeError("Type must be either 'read' or 'write'.")}return new Promise(function(resolve){requestAnimationFrame(function(){var results=[];_this[type].forEach(function(fn){results.push(fn())});_this[type]=[];return resolve(results)})})}}]);return Batch}();BEMQuery.prototype.read=function(){if(!this.batch){this.batch=new Batch}return this.batch.run("read")};BEMQuery.prototype.write=function(){if(!this.batch){this.batch=new Batch}return this.batch.run("write")};BEMQuery.prototype.html=function(newHTML){var _this2=this;if(!this.batch){this.batch=new Batch}if(typeof newHTML!=="undefined"){newHTML=String(newHTML);this.batch.add("write",function(){var elements=_this2.elements;elements.forEach(function(element){element.innerHTML=newHTML})})}else{this.batch.add("read",function(){var elements=_this2.elements;var htmls=[];elements.forEach(function(element){htmls.push(element.innerHTML)});return htmls})}return this};BEMQuery.prototype.getStates=function(){var _this3=this;if(!this.batch){this.batch=new Batch}this.batch.add("read",function(){var element=_this3.elements[0];var states=[];if(!element){return[]}[].forEach.call(element.classList,function(className){var state=_this3.selectorEngine.converter.getStateFromClass(String(className));if(state){states.push(state)}});return states});return this};var ListenersStorage=function(){function ListenersStorage(){babelHelpers.classCallCheck(this,ListenersStorage);this.storage=new WeakMap}babelHelpers.createClass(ListenersStorage,[{key:"add",value:function add(element,type,selector,fn,listener){var listeners={};if(this.storage.has(element)){listeners=this.storage.get(element)}if(typeof listeners[type]==="undefined"){listeners[type]={}}if(typeof listeners[type][selector]==="undefined"){listeners[type][selector]=[]}listeners[type][selector].push([fn,listener]);this.storage.set(element,listeners)}},{key:"get",value:function get(element,type,selector,fn){if(!this.storage.has(element)){return null}var listeners=this.storage.get(element);if(typeof listeners[type]==="undefined"||typeof listeners[type][selector]==="undefined"){return null}var _iteratorNormalCompletion=true;var _didIteratorError=false;var _iteratorError=undefined;try{for(var _iterator=listeners[type][selector][Symbol.iterator](),_step;!(_iteratorNormalCompletion=(_step=_iterator.next()).done);_iteratorNormalCompletion=true){var pair=_step.value;if(pair[0]===fn){return pair[1]}}}catch(err){_didIteratorError=true;_iteratorError=err}finally{try{if(!_iteratorNormalCompletion&&_iterator.return){_iterator.return()}}finally{if(_didIteratorError){throw _iteratorError}}}return null}},{key:"remove",value:function remove(element,type,selector,fn){if(!this.storage.has(element)){return null}var listeners=this.storage.get(element);if(typeof listeners[type]==="undefined"||typeof listeners[type][selector]==="undefined"){return null}listeners[type][selector].forEach(function(pair,i){if(pair[0]===fn){listeners[type][selector].splice(i,1)}});return null}}]);return ListenersStorage}();var storage=new ListenersStorage;BEMQuery.prototype.on=function(type,selector,callback){var listener=void 0;if(typeof type!=="string"||!type){throw new TypeError("Type of event must be a non-empty string.")}if(typeof selector!=="string"&&typeof selector!=="function"||!selector){throw new TypeError("Selector must be a non-empty string or function.")}if(typeof selector==="string"){if(typeof callback!=="function"){throw new TypeError("Callback must be a function.")}selector=this.selectorEngine.converter.convert(selector).CSS;selector=selector+", "+selector+" *";listener=function listener(evt){if(evt.target.matches(selector)){callback(evt)}}}else{listener=selector}this.elements.forEach(function(element){element.addEventListener(type,listener,false);if(typeof selector==="string"){storage.add(element,type,selector,callback,listener)}});return this};BEMQuery.prototype.off=function(type,selector,callback){var listener=void 0;if(typeof type!=="string"||!type){throw new TypeError("Type of event must be a non-empty string.")}if(typeof selector!=="string"&&typeof selector!=="function"||!selector){throw new TypeError("Selector must be a non-empty string or function.")}if(typeof selector==="string"){if(typeof callback!=="function"){throw new TypeError("Callback must be a function.")}selector=this.selectorEngine.converter.convert(selector).CSS;selector=selector+", "+selector+" *"}else{listener=selector}this.elements.forEach(function(element){if(typeof selector==="string"){listener=storage.get(element,type,selector,callback);storage.remove(element,type,selector,callback)}element.removeEventListener(type,listener,false)});return this};return $}); | ||
//# sourceMappingURL=./dist/bemquery.es5.js.map | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.bemquery=t()}(this,function(){"use strict";function e(e,t){var n=new RegExp("[^"+t.elemSeparator+t.modifierSeparator+"]+"+t.modifierSeparator+"[^"+t.elemSeparator+t.modifierSeparator+"]+$","g");return!!e.match(n)}function t(e,t){return" "+e.substring(e.lastIndexOf("."),e.lastIndexOf(t))}function n(e,t){var r=arguments.length<=2||void 0===arguments[2]?"":arguments[2],o=t.rules,i=e.shift(),u=void 0,a=void 0;if(!i)return r;if(r?(a=e.shift(),u=o[i]):(a=i,u=o["default"]),"function"!=typeof u)throw new SyntaxError("Malformed BEM rule");return r+=u(a,t,r),n(e,t,r)}function r(e,t){var r=Object.keys(t.rules).filter(function(e){return"default"!==e}),o=new RegExp("("+r.join("|")+")","g"),i=e.split(o);return e=n(i,t)}function o(){var e=arguments.length<=0||void 0===arguments[0]?p:arguments[0],t=new y(e);return t}function i(e){return"object"===("undefined"==typeof e?"undefined":h["typeof"](e))&&"function"==typeof e.convert}function u(e){return"object"===("undefined"==typeof e?"undefined":h["typeof"](e))&&"function"==typeof e.find}function a(e){return e instanceof v&&(e=e.elements[0]),e instanceof HTMLElement||e===document||(e=document),e}function f(e,t,n,r){if(!e)throw new TypeError("Selector must be set.");if("string"==typeof e)return e=n.convert(e).CSS,r.find(e,t);if(e instanceof HTMLElement)return[e];if(e instanceof v)return e.elements;if("object"===("undefined"==typeof e?"undefined":h["typeof"](e)))return Array.from(e);throw new TypeError("Selector must be a string, object, array or DOM element.")}function c(e,t){Object.defineProperty(e,"elements",{value:t}),e.elements.forEach(function(t,n){Object.defineProperty(e,n,{enumerable:!0,get:function(){return new v(this.elements[n],document,this.converter,this.selectorEngine)}})},e),Object.defineProperty(e,"length",{enumerable:!0,get:function(){return this.elements.length}})}function s(e){var t=arguments.length<=1||void 0===arguments[1]?document:arguments[1],n=o(),r=new m,i=new v(e,t,n,r);return i}function l(e,t){var n=[];return[].forEach.call(t.classList,function(t){var r=e.getStateFromClass(String(t));r&&n.push(r)}),n}var h={};h["typeof"]="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},h.classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},h.createClass=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();var d=function S(e,t){h.classCallCheck(this,S),this.BEM=e,this.CSS=t,Object.freeze(this)},p={bem:{elemSeparator:"__",modifierSeparator:"_"},rules:{"default":function(e){return"."+e}," > ":function(e,t){return" "+t.rules["default"](e)}," ":function(n,r,o){return e(o,r.bem)?""+t(o,r.bem.modifierSeparator)+r.bem.elemSeparator+n:""+r.bem.elemSeparator+n},":":function(e,t){return""+t.bem.modifierSeparator+e}}},y=function(){function e(){var t=arguments.length<=0||void 0===arguments[0]?p:arguments[0];h.classCallCheck(this,e),this.config=t}return h.createClass(e,[{key:"convert",value:function(e){var t=r(e,this.config);return new d(e,t)}},{key:"getStateFromClass",value:function(e){if("string"!=typeof e)throw new TypeError("Class must be a string.");var t=this.config.bem,n=new RegExp("[^"+t.elemSeparator+t.modifierSeparator+"]+"+t.modifierSeparator+"([^"+t.elemSeparator+t.modifierSeparator+"]+)$"),r=e.match(n);return r?r[1]:null}}]),e}(),m=function(){function e(){h.classCallCheck(this,e)}return h.createClass(e,[{key:"find",value:function(e){var t=arguments.length<=1||void 0===arguments[1]?document:arguments[1],n=!1;t!==document&&(t.id||(n=!0,t.id="BEMQueryTMP_"+Date.now()),e="#"+t.id+" "+e);var r=Array.from(t.querySelectorAll(e));return n&&t.removeAttribute("id"),r}}]),e}(),v=function(){function e(t,n,r,o){if(h.classCallCheck(this,e),!i(r))throw new TypeError("Converter must be an object with convert method defined.");if(!u(o))throw new TypeError("SelectorEngine must be an object with find method defined.");this.converter=r,this.selectorEngine=o,n=a(n),c(this,f(t,n,r,o))}return h.createClass(e,[{key:"get",value:function(t){if(t=Number(t),Number.isNaN(t))throw new TypeError("Index must be a correct Number.");if(0>t)throw new RangeError("Index must be greater or equal to 0.");if(t>this.elements.length-1)throw new RangeError("Index cannot be greater than collection's length.");return new e(this.elements[t],document,this.converter,this.selectorEngine)}},{key:"each",value:function(t){if("function"!=typeof t)throw new TypeError("Callback must be a function.");var n=this.converter,r=this.selectorEngine;return this.elements.forEach(function(o){t(new e(o,document,n,r))}),this}},{key:Symbol.iterator,value:function(){var t=0,n=this.elements,r=this.converter,o=this.selectorEngine;return{next:function(){if(t<n.length){var i=n[t++];return{value:new e([i],document,r,o),done:!1}}return{done:!0}}}}}]),e}(),b=Object.freeze({BEMQuery:v,"default":s}),g=function(){function e(){h.classCallCheck(this,e),this.read=[],this.write=[]}return h.createClass(e,[{key:"add",value:function(e,t){if("read"!==e&&"write"!==e)throw new TypeError("Type must be either 'read' or 'write'.");if("function"!=typeof t)throw new TypeError("Task must be a function.");this[e].push(t)}},{key:"run",value:function(){var e=this,t=arguments.length<=0||void 0===arguments[0]?"read":arguments[0];if("read"!==t&&"write"!==t)throw new TypeError("Type must be either 'read' or 'write'.");return new Promise(function(n){requestAnimationFrame(function(){var r=[];return e[t].forEach(function(e){r.push(e())}),e[t]=[],n(r)})})}}]),e}();v.prototype.read=function(){return this.batch||(this.batch=new g),this.batch.run("read")},v.prototype.write=function(){return this.batch||(this.batch=new g),this.batch.run("write")},v.prototype.html=function(e){var t=this;return this.batch||(this.batch=new g),"undefined"!=typeof e?(e=String(e),this.batch.add("write",function(){var n=t.elements;n.forEach(function(t){t.innerHTML=e})})):this.batch.add("read",function(){var e=t.elements,n=[];return e.forEach(function(e){n.push(e.innerHTML)}),n}),this},v.prototype.getStates=function(){var e=this;this.batch||(this.batch=new g);var t=this.elements;return this.batch.add("read",function(){var n=[];return t.forEach(function(t){n.push(l(e.converter,t))}),n}),this};var w=function(){function e(){h.classCallCheck(this,e),this.storage=new WeakMap}return h.createClass(e,[{key:"add",value:function(e,t,n,r,o){var i={};this.storage.has(e)&&(i=this.storage.get(e)),"undefined"==typeof i[t]&&(i[t]={}),"undefined"==typeof i[t][n]&&(i[t][n]=[]),i[t][n].push([r,o]),this.storage.set(e,i)}},{key:"get",value:function(e,t,n,r){if(!this.storage.has(e))return null;var o=this.storage.get(e);if("undefined"==typeof o[t]||"undefined"==typeof o[t][n])return null;var i=!0,u=!1,a=void 0;try{for(var f,c=o[t][n][Symbol.iterator]();!(i=(f=c.next()).done);i=!0){var s=f.value;if(s[0]===r)return s[1]}}catch(l){u=!0,a=l}finally{try{!i&&c["return"]&&c["return"]()}finally{if(u)throw a}}return null}},{key:"remove",value:function(e,t,n,r){if(!this.storage.has(e))return null;var o=this.storage.get(e);return"undefined"==typeof o[t]||"undefined"==typeof o[t][n]?null:(o[t][n].forEach(function(e,i){e[0]===r&&o[t][n].splice(i,1)}),null)}}]),e}(),E=new w;return v.prototype.on=function(e,t,n){var r=void 0;if("string"!=typeof e||!e)throw new TypeError("Type of event must be a non-empty string.");if("string"!=typeof t&&"function"!=typeof t||!t)throw new TypeError("Selector must be a non-empty string or function.");if("string"==typeof t){if("function"!=typeof n)throw new TypeError("Callback must be a function.");t=this.selectorEngine.converter.convert(t).CSS,t=t+", "+t+" *",r=function(e){e.target.matches(t)&&n(e)}}else r=t;return this.elements.forEach(function(o){o.addEventListener(e,r,!1),"string"==typeof t&&E.add(o,e,t,n,r)}),this},v.prototype.off=function(e,t,n){var r=void 0;if("string"!=typeof e||!e)throw new TypeError("Type of event must be a non-empty string.");if("string"!=typeof t&&"function"!=typeof t||!t)throw new TypeError("Selector must be a non-empty string or function.");if("string"==typeof t){if("function"!=typeof n)throw new TypeError("Callback must be a function.");t=this.selectorEngine.converter.convert(t).CSS,t=t+", "+t+" *"}else r=t;return this.elements.forEach(function(o){"string"==typeof t&&(r=E.get(o,e,t,n),E.remove(o,e,t,n)),o.removeEventListener(e,r,!1)}),this},b}); | ||
//# sourceMappingURL=bemquery.es5.js.map |
@@ -1,3 +0,3 @@ | ||
/*! BEMQuery v0.0.1 | (c) 2016 BEMQuery | MIT license (see LICENSE) */ | ||
/*! BEMQuery v0.1.0 | (c) 2016 BEMQuery | MIT license (see LICENSE) */ | ||
/*! bemquery v0.1.0 | (c) 2016 BEMQuery team | MIT license (see LICENSE) */ | ||
/*! bemquery-selector-converter v0.1.1 | (c) 2016 BEMQuery team | MIT license (see LICENSE) */ | ||
/** Simple class representing selector */ | ||
@@ -154,30 +154,27 @@ class Selector { | ||
/** Simple BEM selector engine. */ | ||
/** | ||
* BEM selector converter factory. | ||
* | ||
* @param {Object} [converterConfig=defaultConverterConfig] Configuration object that | ||
* should be passed to the Converter constructor. | ||
* @return {Converter} Converter's instance. | ||
*/ | ||
function factory$1( converterConfig = defaultConfig ) { | ||
const converter = new Converter( converterConfig ); | ||
return converter; | ||
} | ||
/*! bemquery-selector-engine v0.2.1 | (c) 2016 BEMQuery team | MIT license (see LICENSE) */ | ||
/** Simple selector engine. */ | ||
class SelectorEngine { | ||
/** | ||
* Create BEM selector engine's instance. | ||
* | ||
* @param {Converter} converter Converter's instance. | ||
* @class | ||
*/ | ||
constructor( converter ) { | ||
this.converter = converter; | ||
} | ||
/** | ||
* Find elements using passed selector. | ||
* | ||
* @param {String} selector BEM selector. | ||
* @param {Element|Document} context Context | ||
* @param {String} selector CSS selector. | ||
* @param {HTMLElement|Document} context Context | ||
* in which element should be found. | ||
* @returns {Object} result | ||
* @returns {Selector} result.selector Used selector. | ||
* @returns {Element[]} result.elements Found elements. | ||
* @returns {HTMLElement[]} Found elements. | ||
*/ | ||
find( selector, context = document ) { | ||
const convertedSelector = this.converter.convert( selector ); | ||
const result = { | ||
selector: convertedSelector | ||
}; | ||
let cssSelector = convertedSelector.CSS; | ||
let tmpId = false; | ||
@@ -191,9 +188,7 @@ | ||
cssSelector = `#${context.id} ${cssSelector}`; | ||
selector = `#${context.id} ${selector}`; | ||
} | ||
const elements = context.querySelectorAll( cssSelector ); | ||
const elements = Array.from( context.querySelectorAll( selector ) ); | ||
result.elements = Array.from( elements ); | ||
if ( tmpId ) { | ||
@@ -203,18 +198,8 @@ context.removeAttribute( 'id' ); | ||
return result; | ||
return elements; | ||
} | ||
} | ||
/** | ||
* BEM selector engine factory. | ||
* | ||
* @param {Object} [converterConfig=defaultConverterConfig] Configuration object that | ||
* should be passed to the Converter constructor. | ||
* @return {SelectorEngine} SelectorEngine's instance. | ||
*/ | ||
function factory$1( converterConfig = defaultConfig ) { | ||
const converter = new Converter( converterConfig ); | ||
const selectorEngine = new SelectorEngine( converter ); | ||
return selectorEngine; | ||
function checkConverter( converter ) { | ||
return typeof converter === 'object' && typeof converter.convert === 'function'; | ||
} | ||
@@ -238,3 +223,3 @@ | ||
function fetchElements( query, context, selectorEngine ) { | ||
function fetchElements( query, context, converter, selectorEngine ) { | ||
if ( !query ) { | ||
@@ -245,3 +230,4 @@ throw new TypeError( 'Selector must be set.' ); | ||
if ( typeof query === 'string' ) { | ||
return selectorEngine.find( query, context ).elements; | ||
query = converter.convert( query ).CSS; | ||
return selectorEngine.find( query, context ); | ||
} else if ( query instanceof HTMLElement ) { | ||
@@ -269,3 +255,3 @@ return [ | ||
get() { | ||
return new BEMQuery( this.elements[ index ], document, this.selectorEngine ); // eslint-disable-line no-use-before-define | ||
return new BEMQuery( this.elements[ index ], document, this.converter, this.selectorEngine ); // eslint-disable-line no-use-before-define | ||
} | ||
@@ -293,7 +279,12 @@ } ); | ||
* elements should be fetched. | ||
* @param {SelectorEngine} selectorEngine BEM selector engine to be used | ||
* @param {Converter} converter BEM selector converter to be used. | ||
* @param {SelectorEngine} selectorEngine CSS selector engine to be used | ||
* by the current and descendant `BEMQuery` instances. | ||
* @class | ||
*/ | ||
constructor( query, context, selectorEngine ) { | ||
constructor( query, context, converter, selectorEngine ) { | ||
if ( !checkConverter( converter ) ) { | ||
throw new TypeError( 'Converter must be an object with convert method defined.' ); | ||
} | ||
if ( !checkSelectorEngine( selectorEngine ) ) { | ||
@@ -303,2 +294,3 @@ throw new TypeError( 'SelectorEngine must be an object with find method defined.' ); | ||
this.converter = converter; | ||
this.selectorEngine = selectorEngine; | ||
@@ -308,3 +300,3 @@ | ||
defineProperties( this, fetchElements( query, context, selectorEngine ) ); | ||
defineProperties( this, fetchElements( query, context, converter, selectorEngine ) ); | ||
} | ||
@@ -330,3 +322,3 @@ | ||
return new BEMQuery( this.elements[ index ], document, this.selectorEngine ); | ||
return new BEMQuery( this.elements[ index ], document, this.converter, this.selectorEngine ); | ||
} | ||
@@ -345,6 +337,7 @@ | ||
const converter = this.converter; | ||
const selectorEngine = this.selectorEngine; | ||
this.elements.forEach( ( element ) => { | ||
callback( new BEMQuery( element, document, selectorEngine ) ); | ||
callback( new BEMQuery( element, document, converter, selectorEngine ) ); | ||
} ); | ||
@@ -363,2 +356,3 @@ | ||
const elements = this.elements; | ||
const converter = this.converter; | ||
const selectorEngine = this.selectorEngine; | ||
@@ -372,3 +366,3 @@ | ||
return { | ||
value: new BEMQuery( [ element ], document, selectorEngine ), | ||
value: new BEMQuery( [ element ], document, converter, selectorEngine ), | ||
done: false | ||
@@ -397,4 +391,5 @@ }; | ||
function factory( query, context = document ) { | ||
const selectorEngine = factory$1(); | ||
const bemQuery = new BEMQuery( query, context, selectorEngine ); | ||
const converter = factory$1(); | ||
const selectorEngine = new SelectorEngine(); | ||
const bemQuery = new BEMQuery( query, context, converter, selectorEngine ); | ||
@@ -537,4 +532,18 @@ return bemQuery; | ||
function processClasses( converter, element ) { | ||
const states = []; | ||
[].forEach.call( element.classList, ( className ) => { | ||
const state = converter.getStateFromClass( String( className ) ); | ||
if ( state ) { | ||
states.push( state ); | ||
} | ||
} ); | ||
return states; | ||
} | ||
/** | ||
* Method for getting states from all elements in collection | ||
* Method for getting states from all elements in collection. | ||
* | ||
@@ -549,19 +558,12 @@ * @return {BEMQuery} Current BEMQuery instance. | ||
const elements = this.elements; | ||
this.batch.add( 'read', () => { | ||
const element = this.elements[ 0 ]; | ||
const states = []; | ||
const result = []; | ||
if ( !element ) { | ||
return []; | ||
} | ||
[].forEach.call( element.classList, ( className ) => { | ||
const state = this.selectorEngine.converter.getStateFromClass( String( className ) ); | ||
if ( state ) { | ||
states.push( state ); | ||
} | ||
elements.forEach( ( element ) => { | ||
result.push( processClasses( this.converter, element ) ); | ||
} ); | ||
return states; | ||
return result; | ||
} ); | ||
@@ -568,0 +570,0 @@ |
@@ -1,2 +0,2 @@ | ||
/*! BEMQuery v0.0.1 | (c) 2016 BEMQuery | MIT license (see LICENSE) */ | ||
/*! bemquery v0.1.0 | (c) 2016 BEMQuery team | MIT license (see LICENSE) */ | ||
(function (global, factory) { | ||
@@ -8,3 +8,3 @@ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
/*! BEMQuery v0.1.0 | (c) 2016 BEMQuery | MIT license (see LICENSE) */ | ||
/*! bemquery-selector-converter v0.1.1 | (c) 2016 BEMQuery team | MIT license (see LICENSE) */ | ||
/** Simple class representing selector */ | ||
@@ -161,30 +161,27 @@ class Selector { | ||
/** Simple BEM selector engine. */ | ||
/** | ||
* BEM selector converter factory. | ||
* | ||
* @param {Object} [converterConfig=defaultConverterConfig] Configuration object that | ||
* should be passed to the Converter constructor. | ||
* @return {Converter} Converter's instance. | ||
*/ | ||
function factory$1( converterConfig = defaultConfig ) { | ||
const converter = new Converter( converterConfig ); | ||
return converter; | ||
} | ||
/*! bemquery-selector-engine v0.2.1 | (c) 2016 BEMQuery team | MIT license (see LICENSE) */ | ||
/** Simple selector engine. */ | ||
class SelectorEngine { | ||
/** | ||
* Create BEM selector engine's instance. | ||
* | ||
* @param {Converter} converter Converter's instance. | ||
* @class | ||
*/ | ||
constructor( converter ) { | ||
this.converter = converter; | ||
} | ||
/** | ||
* Find elements using passed selector. | ||
* | ||
* @param {String} selector BEM selector. | ||
* @param {Element|Document} context Context | ||
* @param {String} selector CSS selector. | ||
* @param {HTMLElement|Document} context Context | ||
* in which element should be found. | ||
* @returns {Object} result | ||
* @returns {Selector} result.selector Used selector. | ||
* @returns {Element[]} result.elements Found elements. | ||
* @returns {HTMLElement[]} Found elements. | ||
*/ | ||
find( selector, context = document ) { | ||
const convertedSelector = this.converter.convert( selector ); | ||
const result = { | ||
selector: convertedSelector | ||
}; | ||
let cssSelector = convertedSelector.CSS; | ||
let tmpId = false; | ||
@@ -198,9 +195,7 @@ | ||
cssSelector = `#${context.id} ${cssSelector}`; | ||
selector = `#${context.id} ${selector}`; | ||
} | ||
const elements = context.querySelectorAll( cssSelector ); | ||
const elements = Array.from( context.querySelectorAll( selector ) ); | ||
result.elements = Array.from( elements ); | ||
if ( tmpId ) { | ||
@@ -210,18 +205,8 @@ context.removeAttribute( 'id' ); | ||
return result; | ||
return elements; | ||
} | ||
} | ||
/** | ||
* BEM selector engine factory. | ||
* | ||
* @param {Object} [converterConfig=defaultConverterConfig] Configuration object that | ||
* should be passed to the Converter constructor. | ||
* @return {SelectorEngine} SelectorEngine's instance. | ||
*/ | ||
function factory$1( converterConfig = defaultConfig ) { | ||
const converter = new Converter( converterConfig ); | ||
const selectorEngine = new SelectorEngine( converter ); | ||
return selectorEngine; | ||
function checkConverter( converter ) { | ||
return typeof converter === 'object' && typeof converter.convert === 'function'; | ||
} | ||
@@ -245,3 +230,3 @@ | ||
function fetchElements( query, context, selectorEngine ) { | ||
function fetchElements( query, context, converter, selectorEngine ) { | ||
if ( !query ) { | ||
@@ -252,3 +237,4 @@ throw new TypeError( 'Selector must be set.' ); | ||
if ( typeof query === 'string' ) { | ||
return selectorEngine.find( query, context ).elements; | ||
query = converter.convert( query ).CSS; | ||
return selectorEngine.find( query, context ); | ||
} else if ( query instanceof HTMLElement ) { | ||
@@ -276,3 +262,3 @@ return [ | ||
get() { | ||
return new BEMQuery( this.elements[ index ], document, this.selectorEngine ); // eslint-disable-line no-use-before-define | ||
return new BEMQuery( this.elements[ index ], document, this.converter, this.selectorEngine ); // eslint-disable-line no-use-before-define | ||
} | ||
@@ -300,7 +286,12 @@ } ); | ||
* elements should be fetched. | ||
* @param {SelectorEngine} selectorEngine BEM selector engine to be used | ||
* @param {Converter} converter BEM selector converter to be used. | ||
* @param {SelectorEngine} selectorEngine CSS selector engine to be used | ||
* by the current and descendant `BEMQuery` instances. | ||
* @class | ||
*/ | ||
constructor( query, context, selectorEngine ) { | ||
constructor( query, context, converter, selectorEngine ) { | ||
if ( !checkConverter( converter ) ) { | ||
throw new TypeError( 'Converter must be an object with convert method defined.' ); | ||
} | ||
if ( !checkSelectorEngine( selectorEngine ) ) { | ||
@@ -310,2 +301,3 @@ throw new TypeError( 'SelectorEngine must be an object with find method defined.' ); | ||
this.converter = converter; | ||
this.selectorEngine = selectorEngine; | ||
@@ -315,3 +307,3 @@ | ||
defineProperties( this, fetchElements( query, context, selectorEngine ) ); | ||
defineProperties( this, fetchElements( query, context, converter, selectorEngine ) ); | ||
} | ||
@@ -337,3 +329,3 @@ | ||
return new BEMQuery( this.elements[ index ], document, this.selectorEngine ); | ||
return new BEMQuery( this.elements[ index ], document, this.converter, this.selectorEngine ); | ||
} | ||
@@ -352,6 +344,7 @@ | ||
const converter = this.converter; | ||
const selectorEngine = this.selectorEngine; | ||
this.elements.forEach( ( element ) => { | ||
callback( new BEMQuery( element, document, selectorEngine ) ); | ||
callback( new BEMQuery( element, document, converter, selectorEngine ) ); | ||
} ); | ||
@@ -370,2 +363,3 @@ | ||
const elements = this.elements; | ||
const converter = this.converter; | ||
const selectorEngine = this.selectorEngine; | ||
@@ -379,3 +373,3 @@ | ||
return { | ||
value: new BEMQuery( [ element ], document, selectorEngine ), | ||
value: new BEMQuery( [ element ], document, converter, selectorEngine ), | ||
done: false | ||
@@ -404,4 +398,5 @@ }; | ||
function factory( query, context = document ) { | ||
const selectorEngine = factory$1(); | ||
const bemQuery = new BEMQuery( query, context, selectorEngine ); | ||
const converter = factory$1(); | ||
const selectorEngine = new SelectorEngine(); | ||
const bemQuery = new BEMQuery( query, context, converter, selectorEngine ); | ||
@@ -544,4 +539,18 @@ return bemQuery; | ||
function processClasses( converter, element ) { | ||
const states = []; | ||
[].forEach.call( element.classList, ( className ) => { | ||
const state = converter.getStateFromClass( String( className ) ); | ||
if ( state ) { | ||
states.push( state ); | ||
} | ||
} ); | ||
return states; | ||
} | ||
/** | ||
* Method for getting states from all elements in collection | ||
* Method for getting states from all elements in collection. | ||
* | ||
@@ -556,19 +565,12 @@ * @return {BEMQuery} Current BEMQuery instance. | ||
const elements = this.elements; | ||
this.batch.add( 'read', () => { | ||
const element = this.elements[ 0 ]; | ||
const states = []; | ||
const result = []; | ||
if ( !element ) { | ||
return []; | ||
} | ||
[].forEach.call( element.classList, ( className ) => { | ||
const state = this.selectorEngine.converter.getStateFromClass( String( className ) ); | ||
if ( state ) { | ||
states.push( state ); | ||
} | ||
elements.forEach( ( element ) => { | ||
result.push( processClasses( this.converter, element ) ); | ||
} ); | ||
return states; | ||
return result; | ||
} ); | ||
@@ -575,0 +577,0 @@ |
{ | ||
"name": "bemquery", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "BEMQuery library", | ||
@@ -14,4 +14,3 @@ "main": "dist/bemquery.umd.js", | ||
"prebuild": "npm test", | ||
"minify": "uglifyjs ./dist/bemquery.es5.js -o ./dist/bemquery.es5.js --comments=/^/*!/ --source-map=./dist/bemquery.es5.js.map", | ||
"build": "rollup -c config/rollup/umd.js && rollup -c config/rollup/es6.js && rollup -c config/rollup/es5.js && npm run minify", | ||
"build": "rollup -c config/rollup/umd.js && rollup -c config/rollup/es6.js && rollup -c config/rollup/es5.js", | ||
"build-docs": "jsdoc -c config/jsdoc/default.json ./src", | ||
@@ -35,3 +34,6 @@ "publish-docs": "npm run build-docs && git checkout gh-pages && ncp docs/dist ./ && git add -A && git commit -m \"[ci skip] Updated docs.\" && git push origin gh-pages && git checkout master", | ||
], | ||
"author": "Comandeer", | ||
"author": { | ||
"name": "BEMQuery team", | ||
"url": "https://github.com/BEMQuery" | ||
}, | ||
"license": "MIT", | ||
@@ -45,6 +47,7 @@ "bugs": { | ||
"babel-preset-es2015-rollup": "^1.1.1", | ||
"bemquery-async-dom": "0.0.1", | ||
"bemquery-core": "0.0.2", | ||
"bemquery-dom-events": "0.0.1", | ||
"bemquery-selector-engine": "^0.1.0", | ||
"bemquery-async-dom": "^0.1.1", | ||
"bemquery-core": "^0.1.1", | ||
"bemquery-dom-events": "^0.1.0", | ||
"bemquery-selector-converter": "^0.1.1", | ||
"bemquery-selector-engine": "^0.2.1", | ||
"chai": "^3.5.0", | ||
@@ -56,9 +59,9 @@ "eslint": "^2.9.0", | ||
"karma": "^0.13.22", | ||
"karma-chrome-launcher": "^0.2.2", | ||
"karma-coverage": "^0.5.5", | ||
"karma-firefox-launcher": "^0.1.7", | ||
"karma-chrome-launcher": "^1.0.1", | ||
"karma-coverage": "^1.0.0", | ||
"karma-firefox-launcher": "^1.0.0", | ||
"karma-fixture": "^0.2.6", | ||
"karma-html2js-preprocessor": "^0.1.0", | ||
"karma-html2js-preprocessor": "^1.0.0", | ||
"karma-json-fixtures-preprocessor": "0.0.6", | ||
"karma-mocha": "^0.2.2", | ||
"karma-mocha": "^1.0.1", | ||
"karma-rollup-preprocessor": "^2.0.1", | ||
@@ -69,3 +72,3 @@ "karma-sinon-chai": "^1.2.0", | ||
"ncp": "^2.0.0", | ||
"rollup": "^0.26.1", | ||
"rollup": "^0.26.2", | ||
"rollup-plugin-babel": "^2.4.0", | ||
@@ -75,3 +78,4 @@ "rollup-plugin-commonjs": "^2.2.1", | ||
"rollup-plugin-node-resolve": "^1.5.0", | ||
"sinon": "^1.17.3", | ||
"rollup-plugin-uglify": "^0.3.1", | ||
"sinon": "^1.17.4", | ||
"sinon-chai": "^2.8.0", | ||
@@ -78,0 +82,0 @@ "uglify-js": "^2.6.2" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
155823
11
1
18
33
1314