Comparing version 1.7.0 to 1.8.0
@@ -7,2 +7,6 @@ # Change Log | ||
## 1.8.0 - 2017-04-17 | ||
* New functions `d.getData()` and `d.setData()`. | ||
## 1.7.0 - 2017-04-12 | ||
@@ -9,0 +13,0 @@ |
59
d.js
@@ -156,2 +156,61 @@ 'use strict'; | ||
/* | ||
* Set a data-* attribute | ||
*/ | ||
d.setData = function (query, name, value) { | ||
var element = d.get(query); | ||
if (element) { | ||
if (typeof value === 'object') { | ||
value = JSON.stringify(value); | ||
} | ||
element.dataset[name] = value; | ||
} | ||
}; | ||
/* | ||
* Get a data-* attribute | ||
*/ | ||
d.getData = function (query, name) { | ||
var element = d.get(query); | ||
if (!element || !element.dataset[name]) { | ||
return; | ||
} | ||
var value = element.dataset[name]; | ||
switch (value.toLowerCase()) { | ||
case 'true': | ||
return true; | ||
case 'false': | ||
return false; | ||
case 'undefined': | ||
return undefined; | ||
case 'null': | ||
return null; | ||
} | ||
var s = value.substr(0, 1); | ||
var e = value.substr(-1); | ||
if ((s === '[' && e === ']') || (s === '{' && e === '}')) { | ||
return JSON.parse(value); | ||
} | ||
if (/^\d+$/.test(value)) { | ||
return parseInt(value); | ||
} | ||
if (/^\d+\.\d+$/.test(value)) { | ||
return parseFloat(value); | ||
} | ||
return value; | ||
}; | ||
/* | ||
* Insert a new element before other | ||
@@ -158,0 +217,0 @@ */ |
@@ -1,1 +0,1 @@ | ||
"use strict";!function(e,t){"function"==typeof define&&define.amd?define([],function(){return t()}):"object"==typeof module&&module.exports?module.exports=t():e.d=t()}(this,function(){function e(n){if(!(this instanceof e))return new e(t(n));Array.prototype.splice.apply(this,[0,0].concat(n))}function t(t){return"string"==typeof t&&"<"===t[0]?e.parse(t,!0)||[]:e.getAll(t)}function n(e){if((e=e.replace(/(-\w)/g,function(e){return e[1].toUpperCase()}))in i.style)return e;for(var t,n=e.charAt(0).toUpperCase()+e.slice(1),r=["Moz","Webkit","O","ms"],o=0;o<r.length;o++)if((t=r[o]+n)in i.style)return t}function r(e,t){var n;return"on"+e in i?(n=document.createEvent("HTMLEvents"),n.initEvent(e,!0,!1),n):window.CustomEvent?new CustomEvent(e,{detail:t||{}}):(n=document.createEvent("CustomEvent"),n.initCustomEvent(e,!0,!0,t||{}),n)}var i=document.createElement("div");return e.get=function(t){if("string"==typeof t)return document.querySelector(t);if(t instanceof NodeList||t instanceof HTMLCollection||t instanceof Array||t instanceof e)return t[0];if("[object Object]"===Object.prototype.toString.call(t))for(var n in t)return t[n].querySelector(n);return t},e.getAll=function(t){if(Array.isArray(t))return t;if("string"==typeof t)t=document.querySelectorAll(t);else if("[object Object]"===Object.prototype.toString.call(t)&&!(t instanceof e))for(var n in t){t=t[n].querySelectorAll(n);break}return t instanceof NodeList||t instanceof HTMLCollection||t instanceof e?Array.prototype.slice.call(t):[t]},e.getSiblings=function(t,n){return t=e.get(t),t?e.getAll(t.parentNode.children).filter(function(r){return r!==t&&(!n||e.is(r,n))}):[]},e.is=function(e,t){return"string"==typeof t?(e.matches||e.matchesSelector||e.msMatchesSelector||e.mozMatchesSelector||e.webkitMatchesSelector||e.oMatchesSelector).call(e,t):e===t},e.on=function(t,n,r,i){e.getAll(n).forEach(function(e){t.split(" ").forEach(function(t){e.addEventListener(t,r,i||!1)})})},e.delegate=function(t,n,r,i,o){e.on(t,n,function(t){for(var n=t.target;n&&n!==this;n=n.parentNode)if(e.is(n,r)){i.call(n,t);break}},o)},e.off=function(t,n,r,i){e.getAll(n).forEach(function(e){t.split(" ").forEach(function(t){e.removeEventListener(t,r,i||!1)})})},e.trigger=function(t,n,i){"string"==typeof t&&(t=r(t,i)),e.getAll(n).forEach(function(e){e.dispatchEvent(t)})},e.remove=function(t){e.getAll(t).forEach(function(e){e.parentNode.removeChild(e)})},e.insertBefore=function(n,r){var i=e.get(n);i&&t(r).forEach(function(e){i.parentNode.insertBefore(e,i)})},e.insertAfter=function(n,r){var i=e.get(n);i&&t(r).reverse().forEach(function(e){i.parentNode.insertBefore(e,i.nextSibling)})},e.prepend=function(n,r){var i=e.get(n);i&&t(r).reverse().forEach(function(e){i.insertBefore(e,i.firstChild)})},e.append=function(n,r){var i=e.get(n);i&&t(r).forEach(function(e){i.appendChild(e)})},e.css=function(t,r,i){if(arguments.length<3&&"object"!=typeof r){var o=getComputedStyle(e.get(t));return 1===arguments.length?o:o[n(r)]}var c={};"object"==typeof r?c=r:c[r]=i,e.getAll(t).forEach(function(e,t,r){for(var i in c){var o=c[i];"function"==typeof o?o=o.call(this,e,t,r):Array.isArray(o)&&(o=o[t%o.length]),e.style[n(i)]=o}})},e.parse=function(t,n){return i.innerHTML=t,0===i.children.length?n?[]:null:1!==i.children.length||n?e.getAll(i.children):i.children[0]},e.prototype=Object.create(Array.prototype,{on:{value:function(t,n,r){return e.on(t,this,n,r),this}},delegate:{value:function(t,n,r,i){return e.delegate(t,this,n,r,i),this}},off:{value:function(t,n,r){return e.off(t,this,n,r),this}},trigger:{value:function(t,n){return e.trigger(t,this,n),this}},css:{value:function(t){var n=Array.prototype.slice.call(arguments);return n.unshift(this),n.length<3&&"object"!=typeof t?e.css.apply(null,n):(e.css.apply(null,n),this)}},insertBefore:{value:function(t){return e.insertBefore(this,t),this}},insertAfter:{value:function(t){return e.insertAfter(this,t),this}},prepend:{value:function(t){return e.prepend(this,t),this}},append:{value:function(t){return e.append(this,t),this}},insertBeforeTo:{value:function(t){return e.insertBefore(t,this),this}},insertAfterTo:{value:function(t){return e.insertAfter(t,this),this}},prependTo:{value:function(t){return e.prepend(t,this),this}},appendTo:{value:function(t){return e.append(t,this),this}}}),e}); | ||
"use strict";!function(e,t){"function"==typeof define&&define.amd?define([],function(){return t()}):"object"==typeof module&&module.exports?module.exports=t():e.d=t()}(this,function(){function e(n){if(!(this instanceof e))return new e(t(n));Array.prototype.splice.apply(this,[0,0].concat(n))}function t(t){return"string"==typeof t&&"<"===t[0]?e.parse(t,!0)||[]:e.getAll(t)}function n(e){if((e=e.replace(/(-\w)/g,function(e){return e[1].toUpperCase()}))in i.style)return e;for(var t,n=e.charAt(0).toUpperCase()+e.slice(1),r=["Moz","Webkit","O","ms"],o=0;o<r.length;o++)if((t=r[o]+n)in i.style)return t}function r(e,t){var n;return"on"+e in i?(n=document.createEvent("HTMLEvents"),n.initEvent(e,!0,!1),n):window.CustomEvent?new CustomEvent(e,{detail:t||{}}):(n=document.createEvent("CustomEvent"),n.initCustomEvent(e,!0,!0,t||{}),n)}var i=document.createElement("div");return e.get=function(t){if("string"==typeof t)return document.querySelector(t);if(t instanceof NodeList||t instanceof HTMLCollection||t instanceof Array||t instanceof e)return t[0];if("[object Object]"===Object.prototype.toString.call(t))for(var n in t)return t[n].querySelector(n);return t},e.getAll=function(t){if(Array.isArray(t))return t;if("string"==typeof t)t=document.querySelectorAll(t);else if("[object Object]"===Object.prototype.toString.call(t)&&!(t instanceof e))for(var n in t){t=t[n].querySelectorAll(n);break}return t instanceof NodeList||t instanceof HTMLCollection||t instanceof e?Array.prototype.slice.call(t):[t]},e.getSiblings=function(t,n){return t=e.get(t),t?e.getAll(t.parentNode.children).filter(function(r){return r!==t&&(!n||e.is(r,n))}):[]},e.is=function(e,t){return"string"==typeof t?(e.matches||e.matchesSelector||e.msMatchesSelector||e.mozMatchesSelector||e.webkitMatchesSelector||e.oMatchesSelector).call(e,t):e===t},e.on=function(t,n,r,i){e.getAll(n).forEach(function(e){t.split(" ").forEach(function(t){e.addEventListener(t,r,i||!1)})})},e.delegate=function(t,n,r,i,o){e.on(t,n,function(t){for(var n=t.target;n&&n!==this;n=n.parentNode)if(e.is(n,r)){i.call(n,t);break}},o)},e.off=function(t,n,r,i){e.getAll(n).forEach(function(e){t.split(" ").forEach(function(t){e.removeEventListener(t,r,i||!1)})})},e.trigger=function(t,n,i){"string"==typeof t&&(t=r(t,i)),e.getAll(n).forEach(function(e){e.dispatchEvent(t)})},e.remove=function(t){e.getAll(t).forEach(function(e){e.parentNode.removeChild(e)})},e.setData=function(t,n,r){var i=e.get(t);i&&("object"==typeof r&&(r=JSON.stringify(r)),i.dataset[n]=r)},e.getData=function(t,n){var r=e.get(t);if(r&&r.dataset[n]){var i=r.dataset[n];switch(i.toLowerCase()){case"true":return!0;case"false":return!1;case"undefined":return;case"null":return null}var o=i.substr(0,1),c=i.substr(-1);return"["===o&&"]"===c||"{"===o&&"}"===c?JSON.parse(i):/^\d+$/.test(i)?parseInt(i):/^\d+\.\d+$/.test(i)?parseFloat(i):i}},e.insertBefore=function(n,r){var i=e.get(n);i&&t(r).forEach(function(e){i.parentNode.insertBefore(e,i)})},e.insertAfter=function(n,r){var i=e.get(n);i&&t(r).reverse().forEach(function(e){i.parentNode.insertBefore(e,i.nextSibling)})},e.prepend=function(n,r){var i=e.get(n);i&&t(r).reverse().forEach(function(e){i.insertBefore(e,i.firstChild)})},e.append=function(n,r){var i=e.get(n);i&&t(r).forEach(function(e){i.appendChild(e)})},e.css=function(t,r,i){if(arguments.length<3&&"object"!=typeof r){var o=getComputedStyle(e.get(t));return 1===arguments.length?o:o[n(r)]}var c={};"object"==typeof r?c=r:c[r]=i,e.getAll(t).forEach(function(e,t,r){for(var i in c){var o=c[i];"function"==typeof o?o=o.call(this,e,t,r):Array.isArray(o)&&(o=o[t%o.length]),e.style[n(i)]=o}})},e.parse=function(t,n){return i.innerHTML=t,0===i.children.length?n?[]:null:1!==i.children.length||n?e.getAll(i.children):i.children[0]},e.prototype=Object.create(Array.prototype,{on:{value:function(t,n,r){return e.on(t,this,n,r),this}},delegate:{value:function(t,n,r,i){return e.delegate(t,this,n,r,i),this}},off:{value:function(t,n,r){return e.off(t,this,n,r),this}},trigger:{value:function(t,n){return e.trigger(t,this,n),this}},css:{value:function(t){var n=Array.prototype.slice.call(arguments);return n.unshift(this),n.length<3&&"object"!=typeof t?e.css.apply(null,n):(e.css.apply(null,n),this)}},insertBefore:{value:function(t){return e.insertBefore(this,t),this}},insertAfter:{value:function(t){return e.insertAfter(this,t),this}},prepend:{value:function(t){return e.prepend(this,t),this}},append:{value:function(t){return e.append(this,t),this}},insertBeforeTo:{value:function(t){return e.insertBefore(t,this),this}},insertAfterTo:{value:function(t){return e.insertAfter(t,this),this}},prependTo:{value:function(t){return e.prepend(t,this),this}},appendTo:{value:function(t){return e.append(t,this),this}}}),e}); |
{ | ||
"name": "d_js", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"description": "DOM manipulation micro-library", | ||
@@ -5,0 +5,0 @@ "main": "d.js", |
@@ -152,2 +152,25 @@ # d.js | ||
### d.setData(query, name, value) | ||
Set a `data-*` attribute. If it's an array or object, is converted to json | ||
* **query** A string with the selector, array of elements or a Node/NodeList/HTMLCollection instance | ||
* **name** The name of the data attribute | ||
* **value** The value of the data attribute | ||
```js | ||
d.setData('.button', 'clicked', true); | ||
``` | ||
### d.getData(query, name) | ||
Get a `data-*` attribute. It can detect and convert primitive types like integers, floats and booleans. Json values are automatically parsed. | ||
* **query** A string with the selector, array of elements or a Node/NodeList/HTMLCollection instance | ||
* **name** The name of the data attribute | ||
```js | ||
d.setData('.button', 'clicked'); //true | ||
``` | ||
### d.insertAfter(query, content) | ||
@@ -154,0 +177,0 @@ |
Sorry, the diff of this file is not supported yet
36626
445
313