cm-web-modules
Advanced tools
Comparing version 1.5.3 to 1.5.4
{ | ||
"name": "cm-web-modules", | ||
"version": "1.5.3", | ||
"version": "1.5.4", | ||
"description": "Collection of clean and small ES6 modules for the web", | ||
@@ -5,0 +5,0 @@ "main": "src/LibraryManager.js", |
@@ -6,5 +6,6 @@ // author: Remy Sharp | ||
// | ||
// ES6 Version by https://shaack.com | ||
// ES6 Version by Stefan Haack | ||
// - fixed the "SELECT" issue https://github.com/remy/bind.js/pull/35/commits/d202c2056ea7506cd152682fe5b9ffd254a51a5d | ||
// - added "export" | ||
// - added index from arrays in html.push(transform(value, target, index)) | ||
// - added index for arrays in html.push(transform(value, target, index)) | ||
export var Bind = (function Bind(global) { | ||
@@ -19,9 +20,8 @@ 'use strict'; | ||
// this is a conditional because we're also supporting node environment | ||
var $; | ||
try { $ = document.querySelectorAll.bind(document); } catch (e) {} | ||
var array = []; | ||
var isArray = Array.isArray; | ||
var o = 'object'; | ||
var pass = function (v) {return v; }; | ||
var pass = function (v) { | ||
return v; | ||
}; | ||
var tryit = function (fn) { | ||
@@ -58,16 +58,19 @@ return function () { | ||
var methods = 'pop push reverse shift sort splice unshift'.split(' '); | ||
forEach(methods, function eachArrayMethod(method) { | ||
this[method] = function augmentedMethod() { | ||
// flag that we're about to change (used later in bind) | ||
this.__dirty = true; | ||
forEach( | ||
methods, | ||
function eachArrayMethod(method) { | ||
this[method] = function augmentedMethod() { | ||
// flag that we're about to change (used later in bind) | ||
this.__dirty = true; | ||
var ret = __export({}, array[method].apply(this, arguments)); | ||
delete this.__dirty; | ||
if (callback && settings.ready) { | ||
callback(this); | ||
} | ||
var ret = __export({}, array[method].apply(this, arguments)); | ||
delete this.__dirty; | ||
if (callback && settings.ready) { | ||
callback(this); | ||
} | ||
return ret; | ||
}.bind(this); | ||
}.bind(this)); | ||
return ret; | ||
}.bind(this); | ||
}.bind(this) | ||
); | ||
@@ -85,3 +88,3 @@ var length = this.length; | ||
} | ||
var newLength = v * 1; // do a simple coersion | ||
var newLength = v * 1; // do a simple coersion | ||
// note: if `v` is a fraction, then it *should* throw an exception | ||
@@ -118,3 +121,5 @@ // "Invalid array length" but we don't support right now. | ||
function extend(target, object, settings, _path) { | ||
if (!_path) { _path = []; } | ||
if (!_path) { | ||
_path = []; | ||
} | ||
@@ -124,2 +129,17 @@ if (settings.ready && object.__callback) { | ||
} | ||
// don't rebind | ||
if (object instanceof Bind) { | ||
return object; | ||
} | ||
// this is a conditional because we're also supporting node environment | ||
var $; | ||
try { | ||
var context = settings.context || document; | ||
$ = context.querySelectorAll.bind(context); | ||
} catch (e) { | ||
// noop | ||
} | ||
// loop through each property, and make getters & setters for | ||
@@ -139,3 +159,5 @@ // each type of "regular" value. If the key/value pair is an | ||
var callback; | ||
var transform = function (v) { return safe(v); }; | ||
var transform = function (v) { | ||
return safe(v); | ||
}; | ||
var parse = pass; | ||
@@ -149,2 +171,6 @@ | ||
if (debug) { | ||
console.log('key: %s / %s', key, path.join('.'), selector); | ||
} | ||
// then we've got an advanced config - rather than 1-1 mapping | ||
@@ -166,2 +192,11 @@ if (selector && selector.toString() === '[object Object]') { | ||
var elements; | ||
if (typeof selector === 'string') { | ||
// cache the matched elements. Note the :) is because qSA won't allow an | ||
// empty (or undefined) string so I like the smilie. | ||
elements = $(selector || '☺'); | ||
} else if (global.Element && selector instanceof global.Element) { | ||
elements = [selector]; | ||
} | ||
// look for the path in the mapping arg, and if the gave | ||
@@ -171,6 +206,3 @@ // us a callback, use that, otherwise... | ||
callback = selector; | ||
} else if (typeof selector === 'string') { | ||
// cache the matched elements. Note the :) is because qSA won't allow an | ||
// empty (or undefined) string so I like the smilie. | ||
var elements = $(selector || '☺'); | ||
} else if (elements) { | ||
if (elements.length === 0) { | ||
@@ -184,3 +216,21 @@ console.warn('No elements found against "' + selector + '" selector'); | ||
// element.innerHTML to the value | ||
var valueSetters = ['SELECT', 'INPUT', 'PROGRESS']; | ||
var valueSetters = ['SELECT', 'INPUT', 'PROGRESS', 'TEXTAREA']; | ||
if (value === null || value === undefined) { | ||
if (valueSetters.indexOf(elements[0].nodeName) !== -1) { | ||
// eslint-disable-next-line no-prototype-builtins | ||
if (elements[0].hasOwnProperty('checked')) { | ||
value = parse( | ||
elements[0].value === 'on' | ||
? elements[0].checked | ||
: elements[0].value | ||
); | ||
} else { | ||
value = parse(elements[0].value); | ||
} | ||
} else { | ||
value = parse(elements[0].innerHTML); | ||
} | ||
} | ||
var oldCallback = callback; | ||
@@ -201,3 +251,3 @@ callback = function (value) { | ||
// special case for multi-select items | ||
var result = transform(value); | ||
var result = transform(value, target); | ||
if (element.type === 'checkbox') { | ||
@@ -214,2 +264,4 @@ if (value instanceof Array) { | ||
} | ||
} else if (typeof result === 'boolean') { | ||
element.checked = value; | ||
} | ||
@@ -232,8 +284,17 @@ } else if (element.type === 'radio') { | ||
} | ||
var html = ''; | ||
var index = 0 | ||
var html = []; | ||
let index = 0 | ||
forEach(value, function (value) { | ||
html += transform(value, target, index++); | ||
html.push(transform(value, target, index++)); | ||
}); | ||
element.innerHTML = html; | ||
// peek the first item, if it's a node, append | ||
// otherwise set the innerHTML | ||
if (typeof html[0] === 'object') { | ||
element.innerHTML = ''; // blow away original | ||
html.forEach(function (el) { | ||
element.appendChild(el); | ||
}); | ||
} else { | ||
element.innerHTML = html.join(''); | ||
} | ||
} | ||
@@ -250,3 +311,8 @@ }); | ||
forEach(elements, function (element) { | ||
if (element.nodeName === 'INPUT' || element.nodeName === 'SELECT') { | ||
if ( | ||
element.nodeName === 'INPUT' || | ||
element.nodeName === 'SELECT' || | ||
element.nodeName === 'TEXTAREA' | ||
) { | ||
// build up the event handler function | ||
var oninput = function () { | ||
@@ -258,3 +324,5 @@ // we set a dirty flag against this dom node to prevent a | ||
if (element.type === 'checkbox') { | ||
var inputs = (element.form || document).querySelectorAll('input[name="' + element.name + '"][type="' + element.type + '"]'); | ||
var inputs = (element.form || document).querySelectorAll( | ||
'input[name="' + element.name + '"][type="checkbox"]' | ||
); | ||
if (target[key] instanceof Array) { | ||
@@ -264,3 +332,7 @@ var results = []; | ||
if (input.checked) { | ||
results.push(parse(input.value)); | ||
results.push( | ||
parse( | ||
input.value === 'on' ? input.checked : input.value | ||
) | ||
); | ||
} | ||
@@ -270,8 +342,13 @@ }); | ||
} else { | ||
result = this.checked ? parse(this.value) : null; | ||
result = parse( | ||
this.value === 'on' ? this.checked : this.value | ||
); | ||
} | ||
} else { | ||
if (element.type === 'radio') { | ||
result = this.checked ? parse(this.value) : null; | ||
} if (typeof target[key] === 'number') { | ||
result = parse( | ||
this.value === 'on' ? this.checked : this.value | ||
); | ||
} | ||
if (typeof target[key] === 'number') { | ||
result = parse(this.value * 1); | ||
@@ -296,2 +373,3 @@ } else { | ||
var event = { | ||
// select: 'change', | ||
checkbox: 'change', | ||
@@ -319,3 +397,2 @@ radio: 'change', | ||
}, settings.callbacks); | ||
} | ||
@@ -339,2 +416,6 @@ | ||
instance = instance[curr]; | ||
if (instance === null || instance === undefined) { | ||
return prev[curr] || {}; | ||
} | ||
if (typeof prev[curr].__callback === 'function') { | ||
@@ -371,4 +452,7 @@ var v = i === path.length - 1 ? value : instance; | ||
if (dirty && always) { | ||
var instance = always.instance; | ||
always.callback.call(settings.instance, __export(instance instanceof Array ? [] : {}, instance)); | ||
instance = always.instance; | ||
always.callback.call( | ||
settings.instance, | ||
__export(instance instanceof Array ? [] : {}, instance) | ||
); | ||
} | ||
@@ -388,9 +472,24 @@ }; | ||
// if the value we're setting is an object, enumerate the properties | ||
// and apply new setter & getters, returning our bound object | ||
if (settings.ready && typeof v === o && v !== null && !isArray(v) && !v.__callback) { | ||
value = extend(target[key] ? __export({}, target[key]) : {}, v, settings, path); | ||
if ( | ||
settings.ready && | ||
typeof v === o && | ||
v !== null && | ||
!isArray(v) && | ||
!v.__callback | ||
) { | ||
value = extend( | ||
target[key] ? __export({}, target[key]) : {}, | ||
v, | ||
settings, | ||
path | ||
); | ||
} else if (isArray(v)) { | ||
value = extend(new AugmentedArray(findCallback, settings), v, settings, path); | ||
value = extend( | ||
new AugmentedArray(findCallback, settings), | ||
v, | ||
settings, | ||
path | ||
); | ||
} else { | ||
@@ -400,3 +499,10 @@ value = v; | ||
if (debug) console.log('set: key(%s): %s -> %s', key, JSON.stringify(old), JSON.stringify(v)); | ||
if (debug) { | ||
console.log( | ||
'set: key(%s): %s -> %s', | ||
key, | ||
JSON.stringify(old), | ||
JSON.stringify(v) | ||
); | ||
} | ||
@@ -416,3 +522,5 @@ // expose the callback so that child properties can call the | ||
// defer the callback until we're fully booted | ||
settings.deferred.push(findCallback.bind(target, value, old)); | ||
if (typeof settings.mapping[path.join('.')] !== 'undefined') { | ||
settings.deferred.push(findCallback.bind(target, value, old)); | ||
} | ||
} | ||
@@ -429,3 +537,5 @@ }, | ||
} catch (e) { | ||
// console.log(e.toString(), e.stack); | ||
if (debug) { | ||
console.log('failed on Object.defineProperty', e.toString(), e.stack); | ||
} | ||
} | ||
@@ -440,3 +550,8 @@ | ||
} else if (isArray(value)) { | ||
target[key] = extend(new AugmentedArray(findCallback, settings), value, settings, path); | ||
target[key] = extend( | ||
new AugmentedArray(findCallback, settings), | ||
value, | ||
settings, | ||
path | ||
); | ||
} else if (target instanceof AugmentedArray) { | ||
@@ -471,7 +586,12 @@ // do nothing | ||
target[key] = [].map.call(value, function (value) { | ||
return value instanceof Object ? | ||
__export(target[key] || {}, value) : | ||
value; | ||
return value instanceof Object | ||
? __export(target[key] || {}, value) | ||
: value; | ||
}); | ||
} else if (typeof value === o && value !== null && !isArray(value)) { | ||
} else if ( | ||
typeof value === o && | ||
value !== null && | ||
!isArray(value) && | ||
value.toString() === '[Object object]' | ||
) { | ||
target[key] = __export(target[key] || {}, value); | ||
@@ -486,9 +606,9 @@ } else { | ||
function Bind(obj, mapping) { | ||
function Bind(obj, mapping, context) { | ||
if (!this || this === global) { | ||
return new Bind(obj, mapping); | ||
return new Bind(obj, mapping, context); | ||
} | ||
var settings = { | ||
context: context || global.document, | ||
mapping: mapping || {}, | ||
@@ -530,7 +650,7 @@ callbacks: {}, | ||
return Bind; | ||
})(window); | ||
})(this); | ||
if (typeof exports !== 'undefined') { | ||
// eslint-disable-next-line no-undef | ||
module.exports = Bind; | ||
} | ||
} |
104275
1860