Comparing version 1.8.0 to 1.9.0
@@ -7,2 +7,17 @@ # Change Log | ||
## 1.9.0 - 2017-04-19 | ||
### Removed | ||
* Removed the functions `d.getData()` and `d.setData()`. | ||
### Added | ||
* New function `d.data()` to set/get data | ||
* Added `.data()` to the prototype | ||
### Fixed | ||
* Fixed `d.delegate()` with custom events | ||
## 1.8.0 - 2017-04-17 | ||
@@ -9,0 +24,0 @@ |
162
d.js
@@ -111,3 +111,3 @@ 'use strict'; | ||
*/ | ||
d.delegate = function (events, query, selector, callback, useCapture) { | ||
d.delegate = function (events, query, selector, callback) { | ||
d.on(events, query, function (event) { | ||
@@ -120,3 +120,3 @@ for (var target = event.target; target && target !== this; target = target.parentNode) { | ||
} | ||
}, useCapture); | ||
}, true); | ||
}; | ||
@@ -158,61 +158,2 @@ | ||
/* | ||
* 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 | ||
@@ -303,2 +244,37 @@ */ | ||
/* | ||
* Get/set data-* attributes | ||
*/ | ||
d.data = function (query, name, value) { | ||
if (arguments.length < 3 && (typeof name !== 'object')) { | ||
var element = d.get(query); | ||
if (!element || !element.dataset[name]) { | ||
return; | ||
} | ||
return dataValue(element.dataset[name]); | ||
} | ||
var values = {}; | ||
if (typeof name === 'object') { | ||
values = name; | ||
} else { | ||
values[name] = value; | ||
} | ||
d.getAll(query).forEach(function (el) { | ||
for (var name in values) { | ||
var value = values[name]; | ||
if (typeof value === 'object') { | ||
value = JSON.stringify(value); | ||
} | ||
el.dataset[name] = value; | ||
} | ||
}); | ||
}; | ||
/* | ||
* Parses a html code | ||
@@ -350,15 +326,11 @@ */ | ||
css: { | ||
value: function (prop) { | ||
var args = Array.prototype.slice.call(arguments); | ||
args.unshift(this); | ||
//getter | ||
if (args.length < 3 && (typeof prop !== 'object')) { | ||
return d.css.apply(null, args); | ||
} | ||
d.css.apply(null, args); | ||
return this; | ||
value: function () { | ||
return getSet(this, arguments, d.css); | ||
} | ||
}, | ||
data: { | ||
value: function () { | ||
return getSet(this, arguments, d.data); | ||
} | ||
}, | ||
insertBefore: { | ||
@@ -427,2 +399,15 @@ value: function (content) { | ||
function getSet(el, args, fn) { | ||
args = Array.prototype.slice.call(args); | ||
args.unshift(el); | ||
//getter | ||
if (args.length < 3 && (typeof args[1] !== 'object')) { | ||
return fn.apply(null, args); | ||
} | ||
fn.apply(null, args); | ||
return el; | ||
} | ||
function styleProp (prop) { | ||
@@ -452,2 +437,35 @@ //camelCase (ex: font-family => fontFamily) | ||
function dataValue(value) { | ||
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; | ||
} | ||
function createEvent (type, data) { | ||
@@ -454,0 +472,0 @@ var event; |
@@ -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.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}); | ||
"use strict";!function(t,e){"function"==typeof define&&define.amd?define([],function(){return e()}):"object"==typeof module&&module.exports?module.exports=e():t.d=e()}(this,function(){function t(n){if(!(this instanceof t))return new t(e(n));Array.prototype.splice.apply(this,[0,0].concat(n))}function e(e){return"string"==typeof e&&"<"===e[0]?t.parse(e,!0)||[]:t.getAll(e)}function n(t,e,n){return e=Array.prototype.slice.call(e),e.unshift(t),e.length<3&&"object"!=typeof e[1]?n.apply(null,e):(n.apply(null,e),t)}function r(t){if((t=t.replace(/(-\w)/g,function(t){return t[1].toUpperCase()}))in c.style)return t;for(var e,n=t.charAt(0).toUpperCase()+t.slice(1),r=["Moz","Webkit","O","ms"],i=0;i<r.length;i++)if((e=r[i]+n)in c.style)return e}function i(t){switch(t.toLowerCase()){case"true":return!0;case"false":return!1;case"undefined":return;case"null":return null}var e=t.substr(0,1),n=t.substr(-1);return"["===e&&"]"===n||"{"===e&&"}"===n?JSON.parse(t):/^\d+$/.test(t)?parseInt(t):/^\d+\.\d+$/.test(t)?parseFloat(t):t}function o(t,e){var n;return"on"+t in c?(n=document.createEvent("HTMLEvents"),n.initEvent(t,!0,!1),n):window.CustomEvent?new CustomEvent(t,{detail:e||{}}):(n=document.createEvent("CustomEvent"),n.initCustomEvent(t,!0,!0,e||{}),n)}var c=document.createElement("div");return t.get=function(e){if("string"==typeof e)return document.querySelector(e);if(e instanceof NodeList||e instanceof HTMLCollection||e instanceof Array||e instanceof t)return e[0];if("[object Object]"===Object.prototype.toString.call(e))for(var n in e)return e[n].querySelector(n);return e},t.getAll=function(e){if(Array.isArray(e))return e;if("string"==typeof e)e=document.querySelectorAll(e);else if("[object Object]"===Object.prototype.toString.call(e)&&!(e instanceof t))for(var n in e){e=e[n].querySelectorAll(n);break}return e instanceof NodeList||e instanceof HTMLCollection||e instanceof t?Array.prototype.slice.call(e):[e]},t.getSiblings=function(e,n){return e=t.get(e),e?t.getAll(e.parentNode.children).filter(function(r){return r!==e&&(!n||t.is(r,n))}):[]},t.is=function(t,e){return"string"==typeof e?(t.matches||t.matchesSelector||t.msMatchesSelector||t.mozMatchesSelector||t.webkitMatchesSelector||t.oMatchesSelector).call(t,e):t===e},t.on=function(e,n,r,i){t.getAll(n).forEach(function(t){e.split(" ").forEach(function(e){t.addEventListener(e,r,i||!1)})})},t.delegate=function(e,n,r,i){t.on(e,n,function(e){for(var n=e.target;n&&n!==this;n=n.parentNode)if(t.is(n,r)){i.call(n,e);break}},!0)},t.off=function(e,n,r,i){t.getAll(n).forEach(function(t){e.split(" ").forEach(function(e){t.removeEventListener(e,r,i||!1)})})},t.trigger=function(e,n,r){"string"==typeof e&&(e=o(e,r)),t.getAll(n).forEach(function(t){t.dispatchEvent(e)})},t.remove=function(e){t.getAll(e).forEach(function(t){t.parentNode.removeChild(t)})},t.insertBefore=function(n,r){var i=t.get(n);i&&e(r).forEach(function(t){i.parentNode.insertBefore(t,i)})},t.insertAfter=function(n,r){var i=t.get(n);i&&e(r).reverse().forEach(function(t){i.parentNode.insertBefore(t,i.nextSibling)})},t.prepend=function(n,r){var i=t.get(n);i&&e(r).reverse().forEach(function(t){i.insertBefore(t,i.firstChild)})},t.append=function(n,r){var i=t.get(n);i&&e(r).forEach(function(t){i.appendChild(t)})},t.css=function(e,n,i){if(arguments.length<3&&"object"!=typeof n){var o=getComputedStyle(t.get(e));return 1===arguments.length?o:o[r(n)]}var c={};"object"==typeof n?c=n:c[n]=i,t.getAll(e).forEach(function(t,e,n){for(var i in c){var o=c[i];"function"==typeof o?o=o.call(this,t,e,n):Array.isArray(o)&&(o=o[e%o.length]),t.style[r(i)]=o}})},t.data=function(e,n,r){if(arguments.length<3&&"object"!=typeof n){var o=t.get(e);if(!o||!o.dataset[n])return;return i(o.dataset[n])}var c={};"object"==typeof n?c=n:c[n]=r,t.getAll(e).forEach(function(t){for(var e in c){var n=c[e];"object"==typeof n&&(n=JSON.stringify(n)),t.dataset[e]=n}})},t.parse=function(e,n){return c.innerHTML=e,0===c.children.length?n?[]:null:1!==c.children.length||n?t.getAll(c.children):c.children[0]},t.prototype=Object.create(Array.prototype,{on:{value:function(e,n,r){return t.on(e,this,n,r),this}},delegate:{value:function(e,n,r,i){return t.delegate(e,this,n,r,i),this}},off:{value:function(e,n,r){return t.off(e,this,n,r),this}},trigger:{value:function(e,n){return t.trigger(e,this,n),this}},css:{value:function(e){return n(this,arguments,t.css)}},data:{value:function(e){return n(this,arguments,t.data)}},insertBefore:{value:function(e){return t.insertBefore(this,e),this}},insertAfter:{value:function(e){return t.insertAfter(this,e),this}},prepend:{value:function(e){return t.prepend(this,e),this}},append:{value:function(e){return t.append(this,e),this}},insertBeforeTo:{value:function(e){return t.insertBefore(e,this),this}},insertAfterTo:{value:function(e){return t.insertAfter(e,this),this}},prependTo:{value:function(e){return t.prepend(e,this),this}},appendTo:{value:function(e){return t.append(e,this),this}}}),t}); |
{ | ||
"name": "d_js", | ||
"version": "1.8.0", | ||
"version": "1.9.0", | ||
"description": "DOM manipulation micro-library", | ||
@@ -5,0 +5,0 @@ "main": "d.js", |
@@ -10,3 +10,3 @@ # d.js | ||
* Compatible with AMD, commonJS and global javascript | ||
* Only 4Kb (minified) | ||
* Only 5Kb (minified) | ||
* HTML & SVG support | ||
@@ -153,23 +153,25 @@ | ||
### d.setData(query, name, value) | ||
### d.data() | ||
Set a `data-*` attribute. If it's an array or object, is converted to json | ||
Set/get `data-*` attributes. It can detect and convert primitive types like integers, floats and booleans. 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 | ||
* **name** A string with the name of the data attribute or an object with name/values | ||
* **value** The new value of the property | ||
```js | ||
d.setData('.button', 'clicked', true); | ||
``` | ||
//Get a data value | ||
var clicked = d.data('.button', 'clicked'); | ||
### d.getData(query, name) | ||
//Set a new value | ||
d.data('.button', 'clicked', true); | ||
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 | ||
//Set several values | ||
d.data('.button', { | ||
boolean: true, | ||
array: ['blue', 'red', 'green'], | ||
object: {one: '1', two: 2}, | ||
integer: 123, | ||
float: 123.45, | ||
}); | ||
``` | ||
@@ -306,3 +308,4 @@ | ||
`.trigger(event, data)` | Trigger an event | ||
`.css(props|prop, value)` | Get/set css properties | ||
`.css(name, value)` | Get/set css properties | ||
`.data(name, value)` | Get/set data-* attributes | ||
`.insertBefore(content|query)` | Insert new elements before the element | ||
@@ -309,0 +312,0 @@ `.insertAfter(content|query)` | Insert new elements after the element |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38666
460
316