UNPKG

mvdom

Version:

deprecated - Moved to dom-native package

127 lines 3.92 kB
import { all } from './dom'; import { asArray, val } from './utils'; const _pushers = [ ["input[type='checkbox'], input[type='radio']", function (value) { const iptValue = this.value || "on"; if (value instanceof Array) { if (value.indexOf(iptValue) > -1) { this.checked = true; } } else if ((iptValue === "on" && value) || iptValue === value) { this.checked = true; } }], ["input", function (value) { this.value = value; }], ["select", function (value) { this.value = value; }], ["textarea", function (value) { this.value = value; }], ["*", function (value) { this.innerHTML = value; }] ]; const _pullers = [ ["input[type='checkbox'], input[type='radio']", function (existingValue) { const iptValue = this.value || "on"; let newValue; if (this.checked) { newValue = (iptValue && iptValue !== "on") ? iptValue : true; if (typeof existingValue !== "undefined") { const values = asArray(existingValue); values.push(newValue); newValue = values; } } return newValue; }], ["input, select", function (existingValue) { return this.value; }], ["textarea", function (existingValue) { return this.value; }], ["*", function (existingValue) { return this.innerHTML; }] ]; export function pusher(selector, pusherFn) { _pushers.unshift([selector, pusherFn]); } export function puller(selector, pullerFn) { _pullers.unshift([selector, pullerFn]); } export function push(el, selector_or_data, data) { let selector; if (data == null) { selector = ".dx"; data = selector_or_data; } else { selector = selector_or_data; } const dxEls = all(el, selector); dxEls.forEach(function (dxEl) { const propPath = getPropPath(dxEl); if (!propPath) { return; } const value = val(data, propPath); if (typeof value !== 'undefined') { let i = 0, pusherSelector, fun, l = _pushers.length; for (; i < l; i++) { pusherSelector = _pushers[i][0]; if (dxEl && dxEl.matches(pusherSelector)) { fun = _pushers[i][1]; fun.call(dxEl, value); break; } } } }); } export function pull(el, selector) { const obj = {}; selector = (selector) ? selector : ".dx"; const dxEls = all(el, selector); dxEls.forEach(function (dxEl) { let propPath = getPropPath(dxEl); let i = 0, pullerSelector, fun, l = _pullers.length; for (; i < l; i++) { pullerSelector = _pullers[i][0]; if (dxEl && dxEl.matches(pullerSelector)) { fun = _pullers[i][1]; const existingValue = val(obj, propPath); const value = fun.call(dxEl, existingValue); if (typeof value !== "undefined") { val(obj, propPath, value); } break; } } }); return obj; } function getPropPath(dxEl) { let path = null; let i = 0, classes = dxEl.classList, l = dxEl.classList.length, name; for (; i < l; i++) { name = classes[i]; if (name.indexOf("dx-") === 0) { path = name.split("-").slice(1).join("."); break; } } if (!path) { path = dxEl.getAttribute("data-dx"); } if (!path) { path = dxEl.getAttribute("name"); } return path; } //# sourceMappingURL=dx.js.map