Comparing version 0.5.0 to 0.6.0
@@ -8,3 +8,4 @@ module.exports = { | ||
globals: { | ||
'Promise': true | ||
'Promise': true, | ||
'WeakMap': true | ||
}, | ||
@@ -11,0 +12,0 @@ plugins: ['prettier'], |
@@ -35,3 +35,3 @@ "use strict"; | ||
if (target) { | ||
(0, _dom.find)(target).forEach(function (target) { | ||
(0, _dom.queryAll)(target).forEach(function (target) { | ||
return (0, _event.fire)(target, eventName, param); | ||
@@ -38,0 +38,0 @@ }); |
@@ -72,3 +72,3 @@ "use strict"; | ||
window.addEventListener('pageshow', function () { | ||
(0, _dom.find)("".concat(_selectors.formEnableSelector, ", ").concat(_selectors.linkDisableSelector)).filter(function (el) { | ||
(0, _dom.queryAll)("".concat(_selectors.formEnableSelector, ", ").concat(_selectors.linkDisableSelector)).filter(function (el) { | ||
return (0, _dom.getData)(el, 'ujs:disabled'); | ||
@@ -75,0 +75,0 @@ }).forEach(_disable.enableElement); |
@@ -43,3 +43,3 @@ "use strict"; | ||
if (token && param) { | ||
(0, _dom.find)("form input[name=\"".concat(param, "\"]")).forEach(function (input) { | ||
(0, _dom.queryAll)("form input[name=\"".concat(param, "\"]")).forEach(function (input) { | ||
input.value = token; | ||
@@ -46,0 +46,0 @@ }); |
@@ -11,3 +11,3 @@ "use strict"; | ||
exports.getMetaContent = getMetaContent; | ||
exports.find = find; | ||
exports.queryAll = queryAll; | ||
@@ -22,2 +22,4 @@ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
// Checks if the given native dom element matches the selector | ||
@@ -36,20 +38,32 @@ // element:: | ||
} | ||
} // get and set data on a given element using "expando properties" | ||
// See: https://developer.mozilla.org/en-US/docs/Glossary/Expando | ||
} | ||
var elementData = new WeakMap(); | ||
var expando = '_ujsData'; | ||
function getData(element, key) { | ||
var data = elementData.get(element); | ||
function getData(element, key) { | ||
return element[expando] && element[expando][key]; | ||
if (data) { | ||
return data[key]; | ||
} | ||
return; | ||
} | ||
function setData(element, key, value) { | ||
element[expando] = element[expando] || {}; | ||
element[expando][key] = value; | ||
var data = elementData.get(element); | ||
if (data) { | ||
data[key] = value; | ||
} else { | ||
elementData.set(element, _defineProperty({}, key, value)); | ||
} | ||
} | ||
function removeData(element, key) { | ||
element[expando] = element[expando] || {}; | ||
delete element[expando][key]; | ||
var data = elementData.get(element); | ||
if (data) { | ||
delete data[key]; | ||
} | ||
} | ||
@@ -62,4 +76,4 @@ | ||
function find(selector) { | ||
return _toConsumableArray(document.querySelectorAll(selector)); | ||
function queryAll(selector, element) { | ||
return _toConsumableArray((element || document).querySelectorAll(selector)); | ||
} |
@@ -72,3 +72,3 @@ "use strict"; | ||
} else { | ||
return (0, _dom.find)(selector); | ||
return (0, _dom.queryAll)(selector); | ||
} | ||
@@ -94,3 +94,3 @@ } | ||
function disableEmptyFileInputs(element) { | ||
(0, _dom.find)(element, 'input[type="file"]:not([disabled])').filter(function (input) { | ||
(0, _dom.queryAll)('input[type="file"]:not([disabled])', element).filter(function (input) { | ||
return input.files.length > 0; | ||
@@ -104,3 +104,3 @@ }).forEach(function (input) { | ||
function enableEmptyFileInputs(element) { | ||
(0, _dom.find)(element, 'input[type="file"]').filter(function (input) { | ||
(0, _dom.queryAll)('input[type="file"]', element).filter(function (input) { | ||
return (0, _dom.getData)(input, 'ujs:temp-disabled'); | ||
@@ -107,0 +107,0 @@ }).forEach(function (input) { |
{ | ||
"name": "better-ujs", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "module": "src/index.js", |
import { fire, stopEverything } from '../utils/event'; | ||
import { find } from '../utils/dom'; | ||
import { queryAll } from '../utils/dom'; | ||
@@ -25,3 +25,3 @@ export function handleTrigger(event) { | ||
if (target) { | ||
find(target).forEach(target => fire(target, eventName, param)); | ||
queryAll(target).forEach(target => fire(target, eventName, param)); | ||
} else { | ||
@@ -28,0 +28,0 @@ fire(element, eventName, param); |
import { on as delegate } from 'delegated-events'; | ||
import { getData, find } from './utils/dom'; | ||
import { getData, queryAll } from './utils/dom'; | ||
import { refreshCSRFTokens } from './utils/csrf'; | ||
@@ -51,3 +51,3 @@ import { | ||
window.addEventListener('pageshow', () => { | ||
find(`${formEnableSelector}, ${linkDisableSelector}`) | ||
queryAll(`${formEnableSelector}, ${linkDisableSelector}`) | ||
.filter(el => getData(el, 'ujs:disabled')) | ||
@@ -54,0 +54,0 @@ .forEach(enableElement); |
@@ -1,2 +0,2 @@ | ||
import { getMetaContent, find } from './dom'; | ||
import { getMetaContent, queryAll } from './dom'; | ||
@@ -32,3 +32,3 @@ const CSRFTokenHeader = 'x-csrf-token'; | ||
if (token && param) { | ||
find(`form input[name="${param}"]`).forEach(input => { | ||
queryAll(`form input[name="${param}"]`).forEach(input => { | ||
input.value = token; | ||
@@ -35,0 +35,0 @@ }); |
@@ -18,18 +18,26 @@ // Checks if the given native dom element matches the selector | ||
// get and set data on a given element using "expando properties" | ||
// See: https://developer.mozilla.org/en-US/docs/Glossary/Expando | ||
const expando = '_ujsData'; | ||
const elementData = new WeakMap(); | ||
export function getData(element, key) { | ||
return element[expando] && element[expando][key]; | ||
let data = elementData.get(element); | ||
if (data) { | ||
return data[key]; | ||
} | ||
return; | ||
} | ||
export function setData(element, key, value) { | ||
element[expando] = element[expando] || {}; | ||
element[expando][key] = value; | ||
let data = elementData.get(element); | ||
if (data) { | ||
data[key] = value; | ||
} else { | ||
elementData.set(element, { [key]: value }); | ||
} | ||
} | ||
export function removeData(element, key) { | ||
element[expando] = element[expando] || {}; | ||
delete element[expando][key]; | ||
let data = elementData.get(element); | ||
if (data) { | ||
delete data[key]; | ||
} | ||
} | ||
@@ -42,4 +50,4 @@ | ||
export function find(selector) { | ||
return [...document.querySelectorAll(selector)]; | ||
export function queryAll(selector, element) { | ||
return [...(element || document).querySelectorAll(selector)]; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { matches, find, setData, getData, removeData } from './dom'; | ||
import { matches, queryAll, setData, getData, removeData } from './dom'; | ||
@@ -53,3 +53,3 @@ export function serializeElement(element, additionalParam) { | ||
} else { | ||
return find(selector); | ||
return queryAll(selector); | ||
} | ||
@@ -73,3 +73,3 @@ } | ||
function disableEmptyFileInputs(element) { | ||
find(element, 'input[type="file"]:not([disabled])') | ||
queryAll('input[type="file"]:not([disabled])', element) | ||
.filter(input => input.files.length > 0) | ||
@@ -83,3 +83,3 @@ .forEach(input => { | ||
function enableEmptyFileInputs(element) { | ||
find(element, 'input[type="file"]') | ||
queryAll('input[type="file"]', element) | ||
.filter(input => getData(input, 'ujs:temp-disabled')) | ||
@@ -86,0 +86,0 @@ .forEach(input => { |
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
196692
1960