|
Luc Didry |
c0530c |
(function (global, factory) {
|
|
Luc Didry |
c0530c |
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
Luc Didry |
c0530c |
typeof define === 'function' && define.amd ? define(factory) :
|
|
Luc Didry |
c0530c |
(global = global || self, global.Alpine = factory());
|
|
Luc Didry |
c0530c |
}(this, (function () { 'use strict';
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function _defineProperty(obj, key, value) {
|
|
Luc Didry |
c0530c |
if (key in obj) {
|
|
Luc Didry |
c0530c |
Object.defineProperty(obj, key, {
|
|
Luc Didry |
c0530c |
value: value,
|
|
Luc Didry |
c0530c |
enumerable: true,
|
|
Luc Didry |
c0530c |
configurable: true,
|
|
Luc Didry |
c0530c |
writable: true
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
obj[key] = value;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return obj;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function ownKeys(object, enumerableOnly) {
|
|
Luc Didry |
c0530c |
var keys = Object.keys(object);
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (Object.getOwnPropertySymbols) {
|
|
Luc Didry |
c0530c |
var symbols = Object.getOwnPropertySymbols(object);
|
|
Luc Didry |
c0530c |
if (enumerableOnly) symbols = symbols.filter(function (sym) {
|
|
Luc Didry |
c0530c |
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
keys.push.apply(keys, symbols);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return keys;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function _objectSpread2(target) {
|
|
Luc Didry |
c0530c |
for (var i = 1; i < arguments.length; i++) {
|
|
Luc Didry |
c0530c |
var source = arguments[i] != null ? arguments[i] : {};
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (i % 2) {
|
|
Luc Didry |
c0530c |
ownKeys(Object(source), true).forEach(function (key) {
|
|
Luc Didry |
c0530c |
_defineProperty(target, key, source[key]);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
} else if (Object.getOwnPropertyDescriptors) {
|
|
Luc Didry |
c0530c |
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
ownKeys(Object(source)).forEach(function (key) {
|
|
Luc Didry |
c0530c |
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return target;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
// Thanks @stimulus:
|
|
Luc Didry |
c0530c |
// https://github.com/stimulusjs/stimulus/blob/master/packages/%40stimulus/core/src/application.ts
|
|
Luc Didry |
c0530c |
function domReady() {
|
|
Luc Didry |
c0530c |
return new Promise(resolve => {
|
|
Luc Didry |
c0530c |
if (document.readyState == "loading") {
|
|
Luc Didry |
c0530c |
document.addEventListener("DOMContentLoaded", resolve);
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
resolve();
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function arrayUnique(array) {
|
|
Luc Didry |
c0530c |
return Array.from(new Set(array));
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function isTesting() {
|
|
Luc Didry |
c0530c |
return navigator.userAgent.includes("Node.js") || navigator.userAgent.includes("jsdom");
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function kebabCase(subject) {
|
|
Luc Didry |
c0530c |
return subject.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/[_\s]/, '-').toLowerCase();
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function walk(el, callback) {
|
|
Luc Didry |
c0530c |
if (callback(el) === false) return;
|
|
Luc Didry |
c0530c |
let node = el.firstElementChild;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
while (node) {
|
|
Luc Didry |
c0530c |
walk(node, callback);
|
|
Luc Didry |
c0530c |
node = node.nextElementSibling;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function debounce(func, wait) {
|
|
Luc Didry |
c0530c |
var timeout;
|
|
Luc Didry |
c0530c |
return function () {
|
|
Luc Didry |
c0530c |
var context = this,
|
|
Luc Didry |
c0530c |
args = arguments;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
var later = function later() {
|
|
Luc Didry |
c0530c |
timeout = null;
|
|
Luc Didry |
c0530c |
func.apply(context, args);
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
clearTimeout(timeout);
|
|
Luc Didry |
c0530c |
timeout = setTimeout(later, wait);
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function saferEval(expression, dataContext, additionalHelperVariables = {}) {
|
|
Luc Didry |
c0530c |
return new Function(['$data', ...Object.keys(additionalHelperVariables)], `var result; with($data) { result = ${expression} }; return result`)(dataContext, ...Object.values(additionalHelperVariables));
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function saferEvalNoReturn(expression, dataContext, additionalHelperVariables = {}) {
|
|
Luc Didry |
c0530c |
// For the cases when users pass only a function reference to the caller: `x-on:click="foo"`
|
|
Luc Didry |
c0530c |
// Where "foo" is a function. Also, we'll pass the function the event instance when we call it.
|
|
Luc Didry |
c0530c |
if (Object.keys(dataContext).includes(expression)) {
|
|
Luc Didry |
c0530c |
let methodReference = new Function(['dataContext', ...Object.keys(additionalHelperVariables)], `with(dataContext) { return ${expression} }`)(dataContext, ...Object.values(additionalHelperVariables));
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (typeof methodReference === 'function') {
|
|
Luc Didry |
c0530c |
return methodReference.call(dataContext, additionalHelperVariables['$event']);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return new Function(['dataContext', ...Object.keys(additionalHelperVariables)], `with(dataContext) { ${expression} }`)(dataContext, ...Object.values(additionalHelperVariables));
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
const xAttrRE = /^x-(on|bind|data|text|html|model|if|for|show|cloak|transition|ref)\b/;
|
|
Luc Didry |
c0530c |
function isXAttr(attr) {
|
|
Luc Didry |
c0530c |
const name = replaceAtAndColonWithStandardSyntax(attr.name);
|
|
Luc Didry |
c0530c |
return xAttrRE.test(name);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function getXAttrs(el, type) {
|
|
Luc Didry |
c0530c |
return Array.from(el.attributes).filter(isXAttr).map(attr => {
|
|
Luc Didry |
c0530c |
const name = replaceAtAndColonWithStandardSyntax(attr.name);
|
|
Luc Didry |
c0530c |
const typeMatch = name.match(xAttrRE);
|
|
Luc Didry |
c0530c |
const valueMatch = name.match(/:([a-zA-Z\-:]+)/);
|
|
Luc Didry |
c0530c |
const modifiers = name.match(/\.[^.\]]+(?=[^\]]*$)/g) || [];
|
|
Luc Didry |
c0530c |
return {
|
|
Luc Didry |
c0530c |
type: typeMatch ? typeMatch[1] : null,
|
|
Luc Didry |
c0530c |
value: valueMatch ? valueMatch[1] : null,
|
|
Luc Didry |
c0530c |
modifiers: modifiers.map(i => i.replace('.', '')),
|
|
Luc Didry |
c0530c |
expression: attr.value
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
}).filter(i => {
|
|
Luc Didry |
c0530c |
// If no type is passed in for filtering, bypass filter
|
|
Luc Didry |
c0530c |
if (!type) return true;
|
|
Luc Didry |
c0530c |
return i.type === type;
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function isBooleanAttr(attrName) {
|
|
Luc Didry |
c0530c |
// As per HTML spec table https://html.spec.whatwg.org/multipage/indices.html#attributes-3:boolean-attribute
|
|
Luc Didry |
c0530c |
// Array roughly ordered by estimated usage
|
|
Luc Didry |
c0530c |
const booleanAttributes = ['disabled', 'checked', 'required', 'readonly', 'hidden', 'open', 'selected', 'autofocus', 'itemscope', 'multiple', 'novalidate', 'allowfullscreen', 'allowpaymentrequest', 'formnovalidate', 'autoplay', 'controls', 'loop', 'muted', 'playsinline', 'default', 'ismap', 'reversed', 'async', 'defer', 'nomodule'];
|
|
Luc Didry |
c0530c |
return booleanAttributes.includes(attrName);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function replaceAtAndColonWithStandardSyntax(name) {
|
|
Luc Didry |
c0530c |
if (name.startsWith('@')) {
|
|
Luc Didry |
c0530c |
return name.replace('@', 'x-on:');
|
|
Luc Didry |
c0530c |
} else if (name.startsWith(':')) {
|
|
Luc Didry |
c0530c |
return name.replace(':', 'x-bind:');
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return name;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function transitionIn(el, show, forceSkip = false) {
|
|
Luc Didry |
c0530c |
// We don't want to transition on the initial page load.
|
|
Luc Didry |
c0530c |
if (forceSkip) return show();
|
|
Luc Didry |
c0530c |
const attrs = getXAttrs(el, 'transition');
|
|
Luc Didry |
c0530c |
const showAttr = getXAttrs(el, 'show')[0]; // If this is triggered by a x-show.transition.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (showAttr && showAttr.modifiers.includes('transition')) {
|
|
Luc Didry |
c0530c |
let modifiers = showAttr.modifiers; // If x-show.transition.out, we'll skip the "in" transition.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (modifiers.includes('out') && !modifiers.includes('in')) return show();
|
|
Luc Didry |
c0530c |
const settingBothSidesOfTransition = modifiers.includes('in') && modifiers.includes('out'); // If x-show.transition.in...out... only use "in" related modifiers for this transition.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
modifiers = settingBothSidesOfTransition ? modifiers.filter((i, index) => index < modifiers.indexOf('out')) : modifiers;
|
|
Luc Didry |
c0530c |
transitionHelperIn(el, modifiers, show); // Otherwise, we can assume x-transition:enter.
|
|
Luc Didry |
c0530c |
} else if (attrs.length > 0) {
|
|
Luc Didry |
c0530c |
transitionClassesIn(el, attrs, show);
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
// If neither, just show that damn thing.
|
|
Luc Didry |
c0530c |
show();
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function transitionOut(el, hide, forceSkip = false) {
|
|
Luc Didry |
c0530c |
if (forceSkip) return hide();
|
|
Luc Didry |
c0530c |
const attrs = getXAttrs(el, 'transition');
|
|
Luc Didry |
c0530c |
const showAttr = getXAttrs(el, 'show')[0];
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (showAttr && showAttr.modifiers.includes('transition')) {
|
|
Luc Didry |
c0530c |
let modifiers = showAttr.modifiers;
|
|
Luc Didry |
c0530c |
if (modifiers.includes('in') && !modifiers.includes('out')) return hide();
|
|
Luc Didry |
c0530c |
const settingBothSidesOfTransition = modifiers.includes('in') && modifiers.includes('out');
|
|
Luc Didry |
c0530c |
modifiers = settingBothSidesOfTransition ? modifiers.filter((i, index) => index > modifiers.indexOf('out')) : modifiers;
|
|
Luc Didry |
c0530c |
transitionHelperOut(el, modifiers, settingBothSidesOfTransition, hide);
|
|
Luc Didry |
c0530c |
} else if (attrs.length > 0) {
|
|
Luc Didry |
c0530c |
transitionClassesOut(el, attrs, hide);
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
hide();
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function transitionHelperIn(el, modifiers, showCallback) {
|
|
Luc Didry |
c0530c |
// Default values inspired by: https://material.io/design/motion/speed.html#duration
|
|
Luc Didry |
c0530c |
const styleValues = {
|
|
Luc Didry |
c0530c |
duration: modifierValue(modifiers, 'duration', 150),
|
|
Luc Didry |
c0530c |
origin: modifierValue(modifiers, 'origin', 'center'),
|
|
Luc Didry |
c0530c |
first: {
|
|
Luc Didry |
c0530c |
opacity: 0,
|
|
Luc Didry |
c0530c |
scale: modifierValue(modifiers, 'scale', 95)
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
second: {
|
|
Luc Didry |
c0530c |
opacity: 1,
|
|
Luc Didry |
c0530c |
scale: 100
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
transitionHelper(el, modifiers, showCallback, () => {}, styleValues);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function transitionHelperOut(el, modifiers, settingBothSidesOfTransition, hideCallback) {
|
|
Luc Didry |
c0530c |
// Make the "out" transition .5x slower than the "in". (Visually better)
|
|
Luc Didry |
c0530c |
// HOWEVER, if they explicitly set a duration for the "out" transition,
|
|
Luc Didry |
c0530c |
// use that.
|
|
Luc Didry |
c0530c |
const duration = settingBothSidesOfTransition ? modifierValue(modifiers, 'duration', 150) : modifierValue(modifiers, 'duration', 150) / 2;
|
|
Luc Didry |
c0530c |
const styleValues = {
|
|
Luc Didry |
c0530c |
duration: duration,
|
|
Luc Didry |
c0530c |
origin: modifierValue(modifiers, 'origin', 'center'),
|
|
Luc Didry |
c0530c |
first: {
|
|
Luc Didry |
c0530c |
opacity: 1,
|
|
Luc Didry |
c0530c |
scale: 100
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
second: {
|
|
Luc Didry |
c0530c |
opacity: 0,
|
|
Luc Didry |
c0530c |
scale: modifierValue(modifiers, 'scale', 95)
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
transitionHelper(el, modifiers, () => {}, hideCallback, styleValues);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function modifierValue(modifiers, key, fallback) {
|
|
Luc Didry |
c0530c |
// If the modifier isn't present, use the default.
|
|
Luc Didry |
c0530c |
if (modifiers.indexOf(key) === -1) return fallback; // If it IS present, grab the value after it: x-show.transition.duration.500ms
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
const rawValue = modifiers[modifiers.indexOf(key) + 1];
|
|
Luc Didry |
c0530c |
if (!rawValue) return fallback;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (key === 'scale') {
|
|
Luc Didry |
c0530c |
// Check if the very next value is NOT a number and return the fallback.
|
|
Luc Didry |
c0530c |
// If x-show.transition.scale, we'll use the default scale value.
|
|
Luc Didry |
c0530c |
// That is how a user opts out of the opacity transition.
|
|
Luc Didry |
c0530c |
if (!isNumeric(rawValue)) return fallback;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (key === 'duration') {
|
|
Luc Didry |
c0530c |
// Support x-show.transition.duration.500ms && duration.500
|
|
Luc Didry |
c0530c |
let match = rawValue.match(/([0-9]+)ms/);
|
|
Luc Didry |
c0530c |
if (match) return match[1];
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (key === 'origin') {
|
|
Luc Didry |
c0530c |
// Support chaining origin directions: x-show.transition.top.right
|
|
Luc Didry |
c0530c |
if (['top', 'right', 'left', 'center', 'bottom'].includes(modifiers[modifiers.indexOf(key) + 2])) {
|
|
Luc Didry |
c0530c |
return [rawValue, modifiers[modifiers.indexOf(key) + 2]].join(' ');
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return rawValue;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function transitionHelper(el, modifiers, hook1, hook2, styleValues) {
|
|
Luc Didry |
c0530c |
// If the user set these style values, we'll put them back when we're done with them.
|
|
Luc Didry |
c0530c |
const opacityCache = el.style.opacity;
|
|
Luc Didry |
c0530c |
const transformCache = el.style.transform;
|
|
Luc Didry |
c0530c |
const transformOriginCache = el.style.transformOrigin; // If no modifiers are present: x-show.transition, we'll default to both opacity and scale.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
const noModifiers = !modifiers.includes('opacity') && !modifiers.includes('scale');
|
|
Luc Didry |
c0530c |
const transitionOpacity = noModifiers || modifiers.includes('opacity');
|
|
Luc Didry |
c0530c |
const transitionScale = noModifiers || modifiers.includes('scale'); // These are the explicit stages of a transition (same stages for in and for out).
|
|
Luc Didry |
c0530c |
// This way you can get a birds eye view of the hooks, and the differences
|
|
Luc Didry |
c0530c |
// between them.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
const stages = {
|
|
Luc Didry |
c0530c |
start() {
|
|
Luc Didry |
c0530c |
if (transitionOpacity) el.style.opacity = styleValues.first.opacity;
|
|
Luc Didry |
c0530c |
if (transitionScale) el.style.transform = `scale(${styleValues.first.scale / 100})`;
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
during() {
|
|
Luc Didry |
c0530c |
if (transitionScale) el.style.transformOrigin = styleValues.origin;
|
|
Luc Didry |
c0530c |
el.style.transitionProperty = [transitionOpacity ? `opacity` : ``, transitionScale ? `transform` : ``].join(' ').trim();
|
|
Luc Didry |
c0530c |
el.style.transitionDuration = `${styleValues.duration / 1000}s`;
|
|
Luc Didry |
c0530c |
el.style.transitionTimingFunction = `cubic-bezier(0.4, 0.0, 0.2, 1)`;
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
show() {
|
|
Luc Didry |
c0530c |
hook1();
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
end() {
|
|
Luc Didry |
c0530c |
if (transitionOpacity) el.style.opacity = styleValues.second.opacity;
|
|
Luc Didry |
c0530c |
if (transitionScale) el.style.transform = `scale(${styleValues.second.scale / 100})`;
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
hide() {
|
|
Luc Didry |
c0530c |
hook2();
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
cleanup() {
|
|
Luc Didry |
c0530c |
if (transitionOpacity) el.style.opacity = opacityCache;
|
|
Luc Didry |
c0530c |
if (transitionScale) el.style.transform = transformCache;
|
|
Luc Didry |
c0530c |
if (transitionScale) el.style.transformOrigin = transformOriginCache;
|
|
Luc Didry |
c0530c |
el.style.transitionProperty = null;
|
|
Luc Didry |
c0530c |
el.style.transitionDuration = null;
|
|
Luc Didry |
c0530c |
el.style.transitionTimingFunction = null;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
transition(el, stages);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function transitionClassesIn(el, directives, showCallback) {
|
|
Luc Didry |
c0530c |
const enter = (directives.find(i => i.value === 'enter') || {
|
|
Luc Didry |
c0530c |
expression: ''
|
|
Luc Didry |
c0530c |
}).expression.split(' ').filter(i => i !== '');
|
|
Luc Didry |
c0530c |
const enterStart = (directives.find(i => i.value === 'enter-start') || {
|
|
Luc Didry |
c0530c |
expression: ''
|
|
Luc Didry |
c0530c |
}).expression.split(' ').filter(i => i !== '');
|
|
Luc Didry |
c0530c |
const enterEnd = (directives.find(i => i.value === 'enter-end') || {
|
|
Luc Didry |
c0530c |
expression: ''
|
|
Luc Didry |
c0530c |
}).expression.split(' ').filter(i => i !== '');
|
|
Luc Didry |
c0530c |
transitionClasses(el, enter, enterStart, enterEnd, showCallback, () => {});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function transitionClassesOut(el, directives, hideCallback) {
|
|
Luc Didry |
c0530c |
const leave = (directives.find(i => i.value === 'leave') || {
|
|
Luc Didry |
c0530c |
expression: ''
|
|
Luc Didry |
c0530c |
}).expression.split(' ').filter(i => i !== '');
|
|
Luc Didry |
c0530c |
const leaveStart = (directives.find(i => i.value === 'leave-start') || {
|
|
Luc Didry |
c0530c |
expression: ''
|
|
Luc Didry |
c0530c |
}).expression.split(' ').filter(i => i !== '');
|
|
Luc Didry |
c0530c |
const leaveEnd = (directives.find(i => i.value === 'leave-end') || {
|
|
Luc Didry |
c0530c |
expression: ''
|
|
Luc Didry |
c0530c |
}).expression.split(' ').filter(i => i !== '');
|
|
Luc Didry |
c0530c |
transitionClasses(el, leave, leaveStart, leaveEnd, () => {}, hideCallback);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function transitionClasses(el, classesDuring, classesStart, classesEnd, hook1, hook2) {
|
|
Luc Didry |
c0530c |
const originalClasses = el.__x_original_classes || [];
|
|
Luc Didry |
c0530c |
const stages = {
|
|
Luc Didry |
c0530c |
start() {
|
|
Luc Didry |
c0530c |
el.classList.add(...classesStart);
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
during() {
|
|
Luc Didry |
c0530c |
el.classList.add(...classesDuring);
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
show() {
|
|
Luc Didry |
c0530c |
hook1();
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
end() {
|
|
Luc Didry |
c0530c |
// Don't remove classes that were in the original class attribute.
|
|
Luc Didry |
c0530c |
el.classList.remove(...classesStart.filter(i => !originalClasses.includes(i)));
|
|
Luc Didry |
c0530c |
el.classList.add(...classesEnd);
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
hide() {
|
|
Luc Didry |
c0530c |
hook2();
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
cleanup() {
|
|
Luc Didry |
c0530c |
el.classList.remove(...classesDuring.filter(i => !originalClasses.includes(i)));
|
|
Luc Didry |
c0530c |
el.classList.remove(...classesEnd.filter(i => !originalClasses.includes(i)));
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
transition(el, stages);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function transition(el, stages) {
|
|
Luc Didry |
c0530c |
stages.start();
|
|
Luc Didry |
c0530c |
stages.during();
|
|
Luc Didry |
c0530c |
requestAnimationFrame(() => {
|
|
Luc Didry |
c0530c |
// Note: Safari's transitionDuration property will list out comma separated transition durations
|
|
Luc Didry |
c0530c |
// for every single transition property. Let's grab the first one and call it a day.
|
|
Luc Didry |
c0530c |
let duration = Number(getComputedStyle(el).transitionDuration.replace(/,.*/, '').replace('s', '')) * 1000;
|
|
Luc Didry |
c0530c |
stages.show();
|
|
Luc Didry |
c0530c |
requestAnimationFrame(() => {
|
|
Luc Didry |
c0530c |
stages.end();
|
|
Luc Didry |
c0530c |
setTimeout(() => {
|
|
Luc Didry |
c0530c |
stages.hide(); // Adding an "isConnected" check, in case the callback
|
|
Luc Didry |
c0530c |
// removed the element from the DOM.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (el.isConnected) {
|
|
Luc Didry |
c0530c |
stages.cleanup();
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}, duration);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function isNumeric(subject) {
|
|
Luc Didry |
c0530c |
return !isNaN(subject);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function handleForDirective(component, templateEl, expression, initialUpdate, extraVars) {
|
|
Luc Didry |
c0530c |
warnIfNotTemplateTag(templateEl);
|
|
Luc Didry |
c0530c |
let iteratorNames = parseForExpression(expression);
|
|
Luc Didry |
c0530c |
let items = evaluateItemsAndReturnEmptyIfXIfIsPresentAndFalseOnElement(component, templateEl, iteratorNames, extraVars); // As we walk the array, we'll also walk the DOM (updating/creating as we go).
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
let currentEl = templateEl;
|
|
Luc Didry |
c0530c |
items.forEach((item, index) => {
|
|
Luc Didry |
c0530c |
let iterationScopeVariables = getIterationScopeVariables(iteratorNames, item, index, items, extraVars());
|
|
Luc Didry |
c0530c |
let currentKey = generateKeyForIteration(component, templateEl, index, iterationScopeVariables);
|
|
Luc Didry |
c0530c |
let nextEl = currentEl.nextElementSibling; // If there's no previously x-for processed element ahead, add one.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (!nextEl || nextEl.__x_for_key === undefined) {
|
|
Luc Didry |
c0530c |
nextEl = addElementInLoopAfterCurrentEl(templateEl, currentEl); // And transition it in if it's not the first page load.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
transitionIn(nextEl, () => {}, initialUpdate);
|
|
Luc Didry |
c0530c |
nextEl.__x_for = iterationScopeVariables;
|
|
Luc Didry |
c0530c |
component.initializeElements(nextEl, () => nextEl.__x_for);
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
nextEl = lookAheadForMatchingKeyedElementAndMoveItIfFound(nextEl, currentKey); // If we haven't found a matching key, just insert the element at the current position
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (!nextEl) {
|
|
Luc Didry |
c0530c |
nextEl = addElementInLoopAfterCurrentEl(templateEl, currentEl);
|
|
Luc Didry |
c0530c |
} // Temporarily remove the key indicator to allow the normal "updateElements" to work
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
delete nextEl.__x_for_key;
|
|
Luc Didry |
c0530c |
nextEl.__x_for = iterationScopeVariables;
|
|
Luc Didry |
c0530c |
component.updateElements(nextEl, () => nextEl.__x_for);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
currentEl = nextEl;
|
|
Luc Didry |
c0530c |
currentEl.__x_for_key = currentKey;
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
removeAnyLeftOverElementsFromPreviousUpdate(currentEl);
|
|
Luc Didry |
c0530c |
} // This was taken from VueJS 2.* core. Thanks Vue!
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function parseForExpression(expression) {
|
|
Luc Didry |
c0530c |
let forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/;
|
|
Luc Didry |
c0530c |
let stripParensRE = /^\(|\)$/g;
|
|
Luc Didry |
c0530c |
let forAliasRE = /([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/;
|
|
Luc Didry |
c0530c |
let inMatch = expression.match(forAliasRE);
|
|
Luc Didry |
c0530c |
if (!inMatch) return;
|
|
Luc Didry |
c0530c |
let res = {};
|
|
Luc Didry |
c0530c |
res.items = inMatch[2].trim();
|
|
Luc Didry |
c0530c |
let item = inMatch[1].trim().replace(stripParensRE, '');
|
|
Luc Didry |
c0530c |
let iteratorMatch = item.match(forIteratorRE);
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (iteratorMatch) {
|
|
Luc Didry |
c0530c |
res.item = item.replace(forIteratorRE, '').trim();
|
|
Luc Didry |
c0530c |
res.index = iteratorMatch[1].trim();
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (iteratorMatch[2]) {
|
|
Luc Didry |
c0530c |
res.collection = iteratorMatch[2].trim();
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
res.item = item;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return res;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function getIterationScopeVariables(iteratorNames, item, index, items, extraVars) {
|
|
Luc Didry |
c0530c |
// We must create a new object, so each iteration has a new scope
|
|
Luc Didry |
c0530c |
let scopeVariables = extraVars ? _objectSpread2({}, extraVars) : {};
|
|
Luc Didry |
c0530c |
scopeVariables[iteratorNames.item] = item;
|
|
Luc Didry |
c0530c |
if (iteratorNames.index) scopeVariables[iteratorNames.index] = index;
|
|
Luc Didry |
c0530c |
if (iteratorNames.collection) scopeVariables[iteratorNames.collection] = items;
|
|
Luc Didry |
c0530c |
return scopeVariables;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function generateKeyForIteration(component, el, index, iterationScopeVariables) {
|
|
Luc Didry |
c0530c |
let bindKeyAttribute = getXAttrs(el, 'bind').filter(attr => attr.value === 'key')[0]; // If the dev hasn't specified a key, just return the index of the iteration.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (!bindKeyAttribute) return index;
|
|
Luc Didry |
c0530c |
return component.evaluateReturnExpression(el, bindKeyAttribute.expression, () => iterationScopeVariables);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function warnIfNotTemplateTag(el) {
|
|
Luc Didry |
c0530c |
if (el.tagName.toLowerCase() !== 'template') console.warn('Alpine: [x-for] directive should only be added to <template> tags.');
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function evaluateItemsAndReturnEmptyIfXIfIsPresentAndFalseOnElement(component, el, iteratorNames, extraVars) {
|
|
Luc Didry |
c0530c |
let ifAttribute = getXAttrs(el, 'if')[0];
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (ifAttribute && !component.evaluateReturnExpression(el, ifAttribute.expression)) {
|
|
Luc Didry |
c0530c |
return [];
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return component.evaluateReturnExpression(el, iteratorNames.items, extraVars);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function addElementInLoopAfterCurrentEl(templateEl, currentEl) {
|
|
Luc Didry |
c0530c |
let clone = document.importNode(templateEl.content, true);
|
|
Luc Didry |
c0530c |
if (clone.childElementCount !== 1) console.warn('Alpine: <template> tag with [x-for] encountered with multiple element roots. Make sure <template> only has a single child node.');
|
|
Luc Didry |
c0530c |
currentEl.parentElement.insertBefore(clone, currentEl.nextElementSibling);
|
|
Luc Didry |
c0530c |
return currentEl.nextElementSibling;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function lookAheadForMatchingKeyedElementAndMoveItIfFound(nextEl, currentKey) {
|
|
Luc Didry |
c0530c |
// If the the key's DO match, no need to look ahead.
|
|
Luc Didry |
c0530c |
if (nextEl.__x_for_key === currentKey) return nextEl; // If they don't, we'll look ahead for a match.
|
|
Luc Didry |
c0530c |
// If we find it, we'll move it to the current position in the loop.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
let tmpNextEl = nextEl;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
while (tmpNextEl) {
|
|
Luc Didry |
c0530c |
if (tmpNextEl.__x_for_key === currentKey) {
|
|
Luc Didry |
c0530c |
return tmpNextEl.parentElement.insertBefore(tmpNextEl, nextEl);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
tmpNextEl = tmpNextEl.nextElementSibling && tmpNextEl.nextElementSibling.__x_for_key !== undefined ? tmpNextEl.nextElementSibling : false;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function removeAnyLeftOverElementsFromPreviousUpdate(currentEl) {
|
|
Luc Didry |
c0530c |
var nextElementFromOldLoop = currentEl.nextElementSibling && currentEl.nextElementSibling.__x_for_key !== undefined ? currentEl.nextElementSibling : false;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
while (nextElementFromOldLoop) {
|
|
Luc Didry |
c0530c |
let nextElementFromOldLoopImmutable = nextElementFromOldLoop;
|
|
Luc Didry |
c0530c |
let nextSibling = nextElementFromOldLoop.nextElementSibling;
|
|
Luc Didry |
c0530c |
transitionOut(nextElementFromOldLoop, () => {
|
|
Luc Didry |
c0530c |
nextElementFromOldLoopImmutable.remove();
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
nextElementFromOldLoop = nextSibling && nextSibling.__x_for_key !== undefined ? nextSibling : false;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function handleAttributeBindingDirective(component, el, attrName, expression, extraVars, attrType) {
|
|
Luc Didry |
c0530c |
var value = component.evaluateReturnExpression(el, expression, extraVars);
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (attrName === 'value') {
|
|
Luc Didry |
c0530c |
// If nested model key is undefined, set the default value to empty string.
|
|
Luc Didry |
c0530c |
if (value === undefined && expression.match(/\./).length) {
|
|
Luc Didry |
c0530c |
value = '';
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (el.type === 'radio') {
|
|
Luc Didry |
c0530c |
// Set radio value from x-bind:value, if no "value" attribute exists.
|
|
Luc Didry |
c0530c |
// If there are any initial state values, radio will have a correct
|
|
Luc Didry |
c0530c |
// "checked" value since x-bind:value is processed before x-model.
|
|
Luc Didry |
c0530c |
if (el.attributes.value === undefined && attrType === 'bind') {
|
|
Luc Didry |
c0530c |
el.value = value;
|
|
Luc Didry |
c0530c |
} else if (attrType !== 'bind') {
|
|
Luc Didry |
c0530c |
el.checked = el.value == value;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
} else if (el.type === 'checkbox') {
|
|
Luc Didry |
c0530c |
if (Array.isArray(value)) {
|
|
Luc Didry |
c0530c |
// I'm purposely not using Array.includes here because it's
|
|
Luc Didry |
c0530c |
// strict, and because of Numeric/String mis-casting, I
|
|
Luc Didry |
c0530c |
// want the "includes" to be "fuzzy".
|
|
Luc Didry |
c0530c |
let valueFound = false;
|
|
Luc Didry |
c0530c |
value.forEach(val => {
|
|
Luc Didry |
c0530c |
if (val == el.value) {
|
|
Luc Didry |
c0530c |
valueFound = true;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
el.checked = valueFound;
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
el.checked = !!value;
|
|
Luc Didry |
c0530c |
} // If we are explicitly binding a string to the :value, set the string,
|
|
Luc Didry |
c0530c |
// If the value is a boolean, leave it alone, it will be set to "on"
|
|
Luc Didry |
c0530c |
// automatically.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (typeof value === 'string') {
|
|
Luc Didry |
c0530c |
el.value = value;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
} else if (el.tagName === 'SELECT') {
|
|
Luc Didry |
c0530c |
updateSelect(el, value);
|
|
Luc Didry |
c0530c |
} else if (el.type === 'text') {
|
|
Luc Didry |
c0530c |
// Cursor position should be restored back to origin due to a safari bug
|
|
Luc Didry |
c0530c |
const selectionStart = el.selectionStart;
|
|
Luc Didry |
c0530c |
const selectionEnd = el.selectionEnd;
|
|
Luc Didry |
c0530c |
const selectionDirection = el.selectionDirection;
|
|
Luc Didry |
c0530c |
el.value = value;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (el === document.activeElement && selectionStart !== null) {
|
|
Luc Didry |
c0530c |
el.setSelectionRange(selectionStart, selectionEnd, selectionDirection);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
el.value = value;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
} else if (attrName === 'class') {
|
|
Luc Didry |
c0530c |
if (Array.isArray(value)) {
|
|
Luc Didry |
c0530c |
const originalClasses = el.__x_original_classes || [];
|
|
Luc Didry |
c0530c |
el.setAttribute('class', arrayUnique(originalClasses.concat(value)).join(' '));
|
|
Luc Didry |
c0530c |
} else if (typeof value === 'object') {
|
|
Luc Didry |
c0530c |
// Sorting the keys / class names by their boolean value will ensure that
|
|
Luc Didry |
c0530c |
// anything that evaluates to `false` and needs to remove classes is run first.
|
|
Luc Didry |
c0530c |
const keysSortedByBooleanValue = Object.keys(value).sort((a, b) => value[a] - value[b]);
|
|
Luc Didry |
c0530c |
keysSortedByBooleanValue.forEach(classNames => {
|
|
Luc Didry |
c0530c |
if (value[classNames]) {
|
|
Luc Didry |
c0530c |
classNames.split(' ').forEach(className => el.classList.add(className));
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
classNames.split(' ').forEach(className => el.classList.remove(className));
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
const originalClasses = el.__x_original_classes || [];
|
|
Luc Didry |
c0530c |
const newClasses = value.split(' ');
|
|
Luc Didry |
c0530c |
el.setAttribute('class', arrayUnique(originalClasses.concat(newClasses)).join(' '));
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
} else if (isBooleanAttr(attrName)) {
|
|
Luc Didry |
c0530c |
// Boolean attributes have to be explicitly added and removed, not just set.
|
|
Luc Didry |
c0530c |
if (!!value) {
|
|
Luc Didry |
c0530c |
el.setAttribute(attrName, '');
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
el.removeAttribute(attrName);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
el.setAttribute(attrName, value);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function updateSelect(el, value) {
|
|
Luc Didry |
c0530c |
const arrayWrappedValue = [].concat(value).map(value => {
|
|
Luc Didry |
c0530c |
return value + '';
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
Array.from(el.options).forEach(option => {
|
|
Luc Didry |
c0530c |
option.selected = arrayWrappedValue.includes(option.value || option.text);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function handleTextDirective(el, output, expression) {
|
|
Luc Didry |
c0530c |
// If nested model key is undefined, set the default value to empty string.
|
|
Luc Didry |
c0530c |
if (output === undefined && expression.match(/\./).length) {
|
|
Luc Didry |
c0530c |
output = '';
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
el.innerText = output;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function handleHtmlDirective(component, el, expression, extraVars) {
|
|
Luc Didry |
c0530c |
el.innerHTML = component.evaluateReturnExpression(el, expression, extraVars);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function handleShowDirective(component, el, value, modifiers, initialUpdate = false) {
|
|
Luc Didry |
c0530c |
const hide = () => {
|
|
Luc Didry |
c0530c |
el.style.display = 'none';
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
const show = () => {
|
|
Luc Didry |
c0530c |
if (el.style.length === 1 && el.style.display === 'none') {
|
|
Luc Didry |
c0530c |
el.removeAttribute('style');
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
el.style.removeProperty('display');
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (initialUpdate === true) {
|
|
Luc Didry |
c0530c |
if (value) {
|
|
Luc Didry |
c0530c |
show();
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
hide();
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
const handle = resolve => {
|
|
Luc Didry |
c0530c |
if (!value) {
|
|
Luc Didry |
c0530c |
if (el.style.display !== 'none') {
|
|
Luc Didry |
c0530c |
transitionOut(el, () => {
|
|
Luc Didry |
c0530c |
resolve(() => {
|
|
Luc Didry |
c0530c |
hide();
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
resolve(() => {});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
if (el.style.display !== '') {
|
|
Luc Didry |
c0530c |
transitionIn(el, () => {
|
|
Luc Didry |
c0530c |
show();
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
} // Resolve immediately, only hold up parent `x-show`s for hidin.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
resolve(() => {});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}; // The working of x-show is a bit complex because we need to
|
|
Luc Didry |
c0530c |
// wait for any child transitions to finish before hiding
|
|
Luc Didry |
c0530c |
// some element. Also, this has to be done recursively.
|
|
Luc Didry |
c0530c |
// If x-show.immediate, foregoe the waiting.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (modifiers.includes('immediate')) {
|
|
Luc Didry |
c0530c |
handle(finish => finish());
|
|
Luc Didry |
c0530c |
return;
|
|
Luc Didry |
c0530c |
} // x-show is encountered during a DOM tree walk. If an element
|
|
Luc Didry |
c0530c |
// we encounter is NOT a child of another x-show element we
|
|
Luc Didry |
c0530c |
// can execute the previous x-show stack (if one exists).
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (component.showDirectiveLastElement && !component.showDirectiveLastElement.contains(el)) {
|
|
Luc Didry |
c0530c |
component.executeAndClearRemainingShowDirectiveStack();
|
|
Luc Didry |
c0530c |
} // We'll push the handler onto a stack to be handled later.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
component.showDirectiveStack.push(handle);
|
|
Luc Didry |
c0530c |
component.showDirectiveLastElement = el;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function handleIfDirective(component, el, expressionResult, initialUpdate, extraVars) {
|
|
Luc Didry |
c0530c |
if (el.nodeName.toLowerCase() !== 'template') console.warn(`Alpine: [x-if] directive should only be added to <template> tags. See https://github.com/alpinejs/alpine#x-if`);
|
|
Luc Didry |
c0530c |
const elementHasAlreadyBeenAdded = el.nextElementSibling && el.nextElementSibling.__x_inserted_me === true;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (expressionResult && !elementHasAlreadyBeenAdded) {
|
|
Luc Didry |
c0530c |
const clone = document.importNode(el.content, true);
|
|
Luc Didry |
c0530c |
el.parentElement.insertBefore(clone, el.nextElementSibling);
|
|
Luc Didry |
c0530c |
transitionIn(el.nextElementSibling, () => {}, initialUpdate);
|
|
Luc Didry |
c0530c |
component.initializeElements(el.nextElementSibling, extraVars);
|
|
Luc Didry |
c0530c |
el.nextElementSibling.__x_inserted_me = true;
|
|
Luc Didry |
c0530c |
} else if (!expressionResult && elementHasAlreadyBeenAdded) {
|
|
Luc Didry |
c0530c |
transitionOut(el.nextElementSibling, () => {
|
|
Luc Didry |
c0530c |
el.nextElementSibling.remove();
|
|
Luc Didry |
c0530c |
}, initialUpdate);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function registerListener(component, el, event, modifiers, expression, extraVars = {}) {
|
|
Luc Didry |
c0530c |
if (modifiers.includes('away')) {
|
|
Luc Didry |
c0530c |
let handler = e => {
|
|
Luc Didry |
c0530c |
// Don't do anything if the click came form the element or within it.
|
|
Luc Didry |
c0530c |
if (el.contains(e.target)) return; // Don't do anything if this element isn't currently visible.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (el.offsetWidth < 1 && el.offsetHeight < 1) return; // Now that we are sure the element is visible, AND the click
|
|
Luc Didry |
c0530c |
// is from outside it, let's run the expression.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
runListenerHandler(component, expression, e, extraVars);
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (modifiers.includes('once')) {
|
|
Luc Didry |
c0530c |
document.removeEventListener(event, handler);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}; // Listen for this event at the root level.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
document.addEventListener(event, handler);
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
let listenerTarget = modifiers.includes('window') ? window : modifiers.includes('document') ? document : el;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
let handler = e => {
|
|
Luc Didry |
c0530c |
// Remove this global event handler if the element that declared it
|
|
Luc Didry |
c0530c |
// has been removed. It's now stale.
|
|
Luc Didry |
c0530c |
if (listenerTarget === window || listenerTarget === document) {
|
|
Luc Didry |
c0530c |
if (!document.body.contains(el)) {
|
|
Luc Didry |
c0530c |
listenerTarget.removeEventListener(event, handler);
|
|
Luc Didry |
c0530c |
return;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (isKeyEvent(event)) {
|
|
Luc Didry |
c0530c |
if (isListeningForASpecificKeyThatHasntBeenPressed(e, modifiers)) {
|
|
Luc Didry |
c0530c |
return;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (modifiers.includes('prevent')) e.preventDefault();
|
|
Luc Didry |
c0530c |
if (modifiers.includes('stop')) e.stopPropagation(); // If the .self modifier isn't present, or if it is present and
|
|
Luc Didry |
c0530c |
// the target element matches the element we are registering the
|
|
Luc Didry |
c0530c |
// event on, run the handler
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (!modifiers.includes('self') || e.target === el) {
|
|
Luc Didry |
c0530c |
const returnValue = runListenerHandler(component, expression, e, extraVars);
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (returnValue === false) {
|
|
Luc Didry |
c0530c |
e.preventDefault();
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
if (modifiers.includes('once')) {
|
|
Luc Didry |
c0530c |
listenerTarget.removeEventListener(event, handler);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (modifiers.includes('debounce')) {
|
|
Luc Didry |
c0530c |
let nextModifier = modifiers[modifiers.indexOf('debounce') + 1] || 'invalid-wait';
|
|
Luc Didry |
c0530c |
let wait = isNumeric(nextModifier.split('ms')[0]) ? Number(nextModifier.split('ms')[0]) : 250;
|
|
Luc Didry |
c0530c |
handler = debounce(handler, wait);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
listenerTarget.addEventListener(event, handler);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function runListenerHandler(component, expression, e, extraVars) {
|
|
Luc Didry |
c0530c |
return component.evaluateCommandExpression(e.target, expression, () => {
|
|
Luc Didry |
c0530c |
return _objectSpread2({}, extraVars(), {
|
|
Luc Didry |
c0530c |
'$event': e
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function isKeyEvent(event) {
|
|
Luc Didry |
c0530c |
return ['keydown', 'keyup'].includes(event);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function isListeningForASpecificKeyThatHasntBeenPressed(e, modifiers) {
|
|
Luc Didry |
c0530c |
let keyModifiers = modifiers.filter(i => {
|
|
Luc Didry |
c0530c |
return !['window', 'document', 'prevent', 'stop'].includes(i);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (keyModifiers.includes('debounce')) {
|
|
Luc Didry |
c0530c |
let debounceIndex = keyModifiers.indexOf('debounce');
|
|
Luc Didry |
c0530c |
keyModifiers.splice(debounceIndex, isNumeric((keyModifiers[debounceIndex + 1] || 'invalid-wait').split('ms')[0]) ? 2 : 1);
|
|
Luc Didry |
c0530c |
} // If no modifier is specified, we'll call it a press.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (keyModifiers.length === 0) return false; // If one is passed, AND it matches the key pressed, we'll call it a press.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (keyModifiers.length === 1 && keyModifiers[0] === keyToModifier(e.key)) return false; // The user is listening for key combinations.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
const systemKeyModifiers = ['ctrl', 'shift', 'alt', 'meta', 'cmd', 'super'];
|
|
Luc Didry |
c0530c |
const selectedSystemKeyModifiers = systemKeyModifiers.filter(modifier => keyModifiers.includes(modifier));
|
|
Luc Didry |
c0530c |
keyModifiers = keyModifiers.filter(i => !selectedSystemKeyModifiers.includes(i));
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (selectedSystemKeyModifiers.length > 0) {
|
|
Luc Didry |
c0530c |
const activelyPressedKeyModifiers = selectedSystemKeyModifiers.filter(modifier => {
|
|
Luc Didry |
c0530c |
// Alias "cmd" and "super" to "meta"
|
|
Luc Didry |
c0530c |
if (modifier === 'cmd' || modifier === 'super') modifier = 'meta';
|
|
Luc Didry |
c0530c |
return e[`${modifier}Key`];
|
|
Luc Didry |
c0530c |
}); // If all the modifiers selected are pressed, ...
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (activelyPressedKeyModifiers.length === selectedSystemKeyModifiers.length) {
|
|
Luc Didry |
c0530c |
// AND the remaining key is pressed as well. It's a press.
|
|
Luc Didry |
c0530c |
if (keyModifiers[0] === keyToModifier(e.key)) return false;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
} // We'll call it NOT a valid keypress.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return true;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function keyToModifier(key) {
|
|
Luc Didry |
c0530c |
switch (key) {
|
|
Luc Didry |
c0530c |
case '/':
|
|
Luc Didry |
c0530c |
return 'slash';
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
case ' ':
|
|
Luc Didry |
c0530c |
case 'Spacebar':
|
|
Luc Didry |
c0530c |
return 'space';
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
default:
|
|
Luc Didry |
c0530c |
return key && kebabCase(key);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function registerModelListener(component, el, modifiers, expression, extraVars) {
|
|
Luc Didry |
c0530c |
// If the element we are binding to is a select, a radio, or checkbox
|
|
Luc Didry |
c0530c |
// we'll listen for the change event instead of the "input" event.
|
|
Luc Didry |
c0530c |
var event = el.tagName.toLowerCase() === 'select' || ['checkbox', 'radio'].includes(el.type) || modifiers.includes('lazy') ? 'change' : 'input';
|
|
Luc Didry |
c0530c |
const listenerExpression = `${expression} = rightSideOfExpression($event, ${expression})`;
|
|
Luc Didry |
c0530c |
registerListener(component, el, event, modifiers, listenerExpression, () => {
|
|
Luc Didry |
c0530c |
return _objectSpread2({}, extraVars(), {
|
|
Luc Didry |
c0530c |
rightSideOfExpression: generateModelAssignmentFunction(el, modifiers, expression)
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function generateModelAssignmentFunction(el, modifiers, expression) {
|
|
Luc Didry |
c0530c |
if (el.type === 'radio') {
|
|
Luc Didry |
c0530c |
// Radio buttons only work properly when they share a name attribute.
|
|
Luc Didry |
c0530c |
// People might assume we take care of that for them, because
|
|
Luc Didry |
c0530c |
// they already set a shared "x-model" attribute.
|
|
Luc Didry |
c0530c |
if (!el.hasAttribute('name')) el.setAttribute('name', expression);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return (event, currentValue) => {
|
|
Luc Didry |
c0530c |
// Check for event.detail due to an issue where IE11 handles other events as a CustomEvent.
|
|
Luc Didry |
c0530c |
if (event instanceof CustomEvent && event.detail) {
|
|
Luc Didry |
c0530c |
return event.detail;
|
|
Luc Didry |
c0530c |
} else if (el.type === 'checkbox') {
|
|
Luc Didry |
c0530c |
// If the data we are binding to is an array, toggle it's value inside the array.
|
|
Luc Didry |
c0530c |
if (Array.isArray(currentValue)) {
|
|
Luc Didry |
c0530c |
return event.target.checked ? currentValue.concat([event.target.value]) : currentValue.filter(i => i !== event.target.value);
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
return event.target.checked;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
} else if (el.tagName.toLowerCase() === 'select' && el.multiple) {
|
|
Luc Didry |
c0530c |
return modifiers.includes('number') ? Array.from(event.target.selectedOptions).map(option => {
|
|
Luc Didry |
c0530c |
const rawValue = option.value || option.text;
|
|
Luc Didry |
c0530c |
const number = rawValue ? parseFloat(rawValue) : null;
|
|
Luc Didry |
c0530c |
return isNaN(number) ? rawValue : number;
|
|
Luc Didry |
c0530c |
}) : Array.from(event.target.selectedOptions).map(option => {
|
|
Luc Didry |
c0530c |
return option.value || option.text;
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
const rawValue = event.target.value;
|
|
Luc Didry |
c0530c |
const number = rawValue ? parseFloat(rawValue) : null;
|
|
Luc Didry |
c0530c |
return modifiers.includes('number') ? isNaN(number) ? rawValue : number : modifiers.includes('trim') ? rawValue.trim() : rawValue;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
/**
|
|
Luc Didry |
c0530c |
* Copyright (C) 2017 salesforce.com, inc.
|
|
Luc Didry |
c0530c |
*/
|
|
Luc Didry |
c0530c |
const { isArray } = Array;
|
|
Luc Didry |
c0530c |
const { getPrototypeOf, create: ObjectCreate, defineProperty: ObjectDefineProperty, defineProperties: ObjectDefineProperties, isExtensible, getOwnPropertyDescriptor, getOwnPropertyNames, getOwnPropertySymbols, preventExtensions, hasOwnProperty, } = Object;
|
|
Luc Didry |
c0530c |
const { push: ArrayPush, concat: ArrayConcat, map: ArrayMap, } = Array.prototype;
|
|
Luc Didry |
c0530c |
function isUndefined(obj) {
|
|
Luc Didry |
c0530c |
return obj === undefined;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function isFunction(obj) {
|
|
Luc Didry |
c0530c |
return typeof obj === 'function';
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function isObject(obj) {
|
|
Luc Didry |
c0530c |
return typeof obj === 'object';
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
const proxyToValueMap = new WeakMap();
|
|
Luc Didry |
c0530c |
function registerProxy(proxy, value) {
|
|
Luc Didry |
c0530c |
proxyToValueMap.set(proxy, value);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
const unwrap = (replicaOrAny) => proxyToValueMap.get(replicaOrAny) || replicaOrAny;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function wrapValue(membrane, value) {
|
|
Luc Didry |
c0530c |
return membrane.valueIsObservable(value) ? membrane.getProxy(value) : value;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
/**
|
|
Luc Didry |
c0530c |
* Unwrap property descriptors will set value on original descriptor
|
|
Luc Didry |
c0530c |
* We only need to unwrap if value is specified
|
|
Luc Didry |
c0530c |
* @param descriptor external descrpitor provided to define new property on original value
|
|
Luc Didry |
c0530c |
*/
|
|
Luc Didry |
c0530c |
function unwrapDescriptor(descriptor) {
|
|
Luc Didry |
c0530c |
if (hasOwnProperty.call(descriptor, 'value')) {
|
|
Luc Didry |
c0530c |
descriptor.value = unwrap(descriptor.value);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
return descriptor;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function lockShadowTarget(membrane, shadowTarget, originalTarget) {
|
|
Luc Didry |
c0530c |
const targetKeys = ArrayConcat.call(getOwnPropertyNames(originalTarget), getOwnPropertySymbols(originalTarget));
|
|
Luc Didry |
c0530c |
targetKeys.forEach((key) => {
|
|
Luc Didry |
c0530c |
let descriptor = getOwnPropertyDescriptor(originalTarget, key);
|
|
Luc Didry |
c0530c |
// We do not need to wrap the descriptor if configurable
|
|
Luc Didry |
c0530c |
// Because we can deal with wrapping it when user goes through
|
|
Luc Didry |
c0530c |
// Get own property descriptor. There is also a chance that this descriptor
|
|
Luc Didry |
c0530c |
// could change sometime in the future, so we can defer wrapping
|
|
Luc Didry |
c0530c |
// until we need to
|
|
Luc Didry |
c0530c |
if (!descriptor.configurable) {
|
|
Luc Didry |
c0530c |
descriptor = wrapDescriptor(membrane, descriptor, wrapValue);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
ObjectDefineProperty(shadowTarget, key, descriptor);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
preventExtensions(shadowTarget);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
class ReactiveProxyHandler {
|
|
Luc Didry |
c0530c |
constructor(membrane, value) {
|
|
Luc Didry |
c0530c |
this.originalTarget = value;
|
|
Luc Didry |
c0530c |
this.membrane = membrane;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
get(shadowTarget, key) {
|
|
Luc Didry |
c0530c |
const { originalTarget, membrane } = this;
|
|
Luc Didry |
c0530c |
const value = originalTarget[key];
|
|
Luc Didry |
c0530c |
const { valueObserved } = membrane;
|
|
Luc Didry |
c0530c |
valueObserved(originalTarget, key);
|
|
Luc Didry |
c0530c |
return membrane.getProxy(value);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
set(shadowTarget, key, value) {
|
|
Luc Didry |
c0530c |
const { originalTarget, membrane: { valueMutated } } = this;
|
|
Luc Didry |
c0530c |
const oldValue = originalTarget[key];
|
|
Luc Didry |
c0530c |
if (oldValue !== value) {
|
|
Luc Didry |
c0530c |
originalTarget[key] = value;
|
|
Luc Didry |
c0530c |
valueMutated(originalTarget, key);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
else if (key === 'length' && isArray(originalTarget)) {
|
|
Luc Didry |
c0530c |
// fix for issue #236: push will add the new index, and by the time length
|
|
Luc Didry |
c0530c |
// is updated, the internal length is already equal to the new length value
|
|
Luc Didry |
c0530c |
// therefore, the oldValue is equal to the value. This is the forking logic
|
|
Luc Didry |
c0530c |
// to support this use case.
|
|
Luc Didry |
c0530c |
valueMutated(originalTarget, key);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
return true;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
deleteProperty(shadowTarget, key) {
|
|
Luc Didry |
c0530c |
const { originalTarget, membrane: { valueMutated } } = this;
|
|
Luc Didry |
c0530c |
delete originalTarget[key];
|
|
Luc Didry |
c0530c |
valueMutated(originalTarget, key);
|
|
Luc Didry |
c0530c |
return true;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
apply(shadowTarget, thisArg, argArray) {
|
|
Luc Didry |
c0530c |
/* No op */
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
construct(target, argArray, newTarget) {
|
|
Luc Didry |
c0530c |
/* No op */
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
has(shadowTarget, key) {
|
|
Luc Didry |
c0530c |
const { originalTarget, membrane: { valueObserved } } = this;
|
|
Luc Didry |
c0530c |
valueObserved(originalTarget, key);
|
|
Luc Didry |
c0530c |
return key in originalTarget;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
ownKeys(shadowTarget) {
|
|
Luc Didry |
c0530c |
const { originalTarget } = this;
|
|
Luc Didry |
c0530c |
return ArrayConcat.call(getOwnPropertyNames(originalTarget), getOwnPropertySymbols(originalTarget));
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
isExtensible(shadowTarget) {
|
|
Luc Didry |
c0530c |
const shadowIsExtensible = isExtensible(shadowTarget);
|
|
Luc Didry |
c0530c |
if (!shadowIsExtensible) {
|
|
Luc Didry |
c0530c |
return shadowIsExtensible;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
const { originalTarget, membrane } = this;
|
|
Luc Didry |
c0530c |
const targetIsExtensible = isExtensible(originalTarget);
|
|
Luc Didry |
c0530c |
if (!targetIsExtensible) {
|
|
Luc Didry |
c0530c |
lockShadowTarget(membrane, shadowTarget, originalTarget);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
return targetIsExtensible;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
setPrototypeOf(shadowTarget, prototype) {
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
getPrototypeOf(shadowTarget) {
|
|
Luc Didry |
c0530c |
const { originalTarget } = this;
|
|
Luc Didry |
c0530c |
return getPrototypeOf(originalTarget);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
getOwnPropertyDescriptor(shadowTarget, key) {
|
|
Luc Didry |
c0530c |
const { originalTarget, membrane } = this;
|
|
Luc Didry |
c0530c |
const { valueObserved } = this.membrane;
|
|
Luc Didry |
c0530c |
// keys looked up via hasOwnProperty need to be reactive
|
|
Luc Didry |
c0530c |
valueObserved(originalTarget, key);
|
|
Luc Didry |
c0530c |
let desc = getOwnPropertyDescriptor(originalTarget, key);
|
|
Luc Didry |
c0530c |
if (isUndefined(desc)) {
|
|
Luc Didry |
c0530c |
return desc;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
const shadowDescriptor = getOwnPropertyDescriptor(shadowTarget, key);
|
|
Luc Didry |
c0530c |
if (!isUndefined(shadowDescriptor)) {
|
|
Luc Didry |
c0530c |
return shadowDescriptor;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
// Note: by accessing the descriptor, the key is marked as observed
|
|
Luc Didry |
c0530c |
// but access to the value, setter or getter (if available) cannot observe
|
|
Luc Didry |
c0530c |
// mutations, just like regular methods, in which case we just do nothing.
|
|
Luc Didry |
c0530c |
desc = wrapDescriptor(membrane, desc, wrapValue);
|
|
Luc Didry |
c0530c |
if (!desc.configurable) {
|
|
Luc Didry |
c0530c |
// If descriptor from original target is not configurable,
|
|
Luc Didry |
c0530c |
// We must copy the wrapped descriptor over to the shadow target.
|
|
Luc Didry |
c0530c |
// Otherwise, proxy will throw an invariant error.
|
|
Luc Didry |
c0530c |
// This is our last chance to lock the value.
|
|
Luc Didry |
c0530c |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/getOwnPropertyDescriptor#Invariants
|
|
Luc Didry |
c0530c |
ObjectDefineProperty(shadowTarget, key, desc);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
return desc;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
preventExtensions(shadowTarget) {
|
|
Luc Didry |
c0530c |
const { originalTarget, membrane } = this;
|
|
Luc Didry |
c0530c |
lockShadowTarget(membrane, shadowTarget, originalTarget);
|
|
Luc Didry |
c0530c |
preventExtensions(originalTarget);
|
|
Luc Didry |
c0530c |
return true;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
defineProperty(shadowTarget, key, descriptor) {
|
|
Luc Didry |
c0530c |
const { originalTarget, membrane } = this;
|
|
Luc Didry |
c0530c |
const { valueMutated } = membrane;
|
|
Luc Didry |
c0530c |
const { configurable } = descriptor;
|
|
Luc Didry |
c0530c |
// We have to check for value in descriptor
|
|
Luc Didry |
c0530c |
// because Object.freeze(proxy) calls this method
|
|
Luc Didry |
c0530c |
// with only { configurable: false, writeable: false }
|
|
Luc Didry |
c0530c |
// Additionally, method will only be called with writeable:false
|
|
Luc Didry |
c0530c |
// if the descriptor has a value, as opposed to getter/setter
|
|
Luc Didry |
c0530c |
// So we can just check if writable is present and then see if
|
|
Luc Didry |
c0530c |
// value is present. This eliminates getter and setter descriptors
|
|
Luc Didry |
c0530c |
if (hasOwnProperty.call(descriptor, 'writable') && !hasOwnProperty.call(descriptor, 'value')) {
|
|
Luc Didry |
c0530c |
const originalDescriptor = getOwnPropertyDescriptor(originalTarget, key);
|
|
Luc Didry |
c0530c |
descriptor.value = originalDescriptor.value;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
ObjectDefineProperty(originalTarget, key, unwrapDescriptor(descriptor));
|
|
Luc Didry |
c0530c |
if (configurable === false) {
|
|
Luc Didry |
c0530c |
ObjectDefineProperty(shadowTarget, key, wrapDescriptor(membrane, descriptor, wrapValue));
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
valueMutated(originalTarget, key);
|
|
Luc Didry |
c0530c |
return true;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function wrapReadOnlyValue(membrane, value) {
|
|
Luc Didry |
c0530c |
return membrane.valueIsObservable(value) ? membrane.getReadOnlyProxy(value) : value;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
class ReadOnlyHandler {
|
|
Luc Didry |
c0530c |
constructor(membrane, value) {
|
|
Luc Didry |
c0530c |
this.originalTarget = value;
|
|
Luc Didry |
c0530c |
this.membrane = membrane;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
get(shadowTarget, key) {
|
|
Luc Didry |
c0530c |
const { membrane, originalTarget } = this;
|
|
Luc Didry |
c0530c |
const value = originalTarget[key];
|
|
Luc Didry |
c0530c |
const { valueObserved } = membrane;
|
|
Luc Didry |
c0530c |
valueObserved(originalTarget, key);
|
|
Luc Didry |
c0530c |
return membrane.getReadOnlyProxy(value);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
set(shadowTarget, key, value) {
|
|
Luc Didry |
c0530c |
return false;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
deleteProperty(shadowTarget, key) {
|
|
Luc Didry |
c0530c |
return false;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
apply(shadowTarget, thisArg, argArray) {
|
|
Luc Didry |
c0530c |
/* No op */
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
construct(target, argArray, newTarget) {
|
|
Luc Didry |
c0530c |
/* No op */
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
has(shadowTarget, key) {
|
|
Luc Didry |
c0530c |
const { originalTarget, membrane: { valueObserved } } = this;
|
|
Luc Didry |
c0530c |
valueObserved(originalTarget, key);
|
|
Luc Didry |
c0530c |
return key in originalTarget;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
ownKeys(shadowTarget) {
|
|
Luc Didry |
c0530c |
const { originalTarget } = this;
|
|
Luc Didry |
c0530c |
return ArrayConcat.call(getOwnPropertyNames(originalTarget), getOwnPropertySymbols(originalTarget));
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
setPrototypeOf(shadowTarget, prototype) {
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
getOwnPropertyDescriptor(shadowTarget, key) {
|
|
Luc Didry |
c0530c |
const { originalTarget, membrane } = this;
|
|
Luc Didry |
c0530c |
const { valueObserved } = membrane;
|
|
Luc Didry |
c0530c |
// keys looked up via hasOwnProperty need to be reactive
|
|
Luc Didry |
c0530c |
valueObserved(originalTarget, key);
|
|
Luc Didry |
c0530c |
let desc = getOwnPropertyDescriptor(originalTarget, key);
|
|
Luc Didry |
c0530c |
if (isUndefined(desc)) {
|
|
Luc Didry |
c0530c |
return desc;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
const shadowDescriptor = getOwnPropertyDescriptor(shadowTarget, key);
|
|
Luc Didry |
c0530c |
if (!isUndefined(shadowDescriptor)) {
|
|
Luc Didry |
c0530c |
return shadowDescriptor;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
// Note: by accessing the descriptor, the key is marked as observed
|
|
Luc Didry |
c0530c |
// but access to the value or getter (if available) cannot be observed,
|
|
Luc Didry |
c0530c |
// just like regular methods, in which case we just do nothing.
|
|
Luc Didry |
c0530c |
desc = wrapDescriptor(membrane, desc, wrapReadOnlyValue);
|
|
Luc Didry |
c0530c |
if (hasOwnProperty.call(desc, 'set')) {
|
|
Luc Didry |
c0530c |
desc.set = undefined; // readOnly membrane does not allow setters
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
if (!desc.configurable) {
|
|
Luc Didry |
c0530c |
// If descriptor from original target is not configurable,
|
|
Luc Didry |
c0530c |
// We must copy the wrapped descriptor over to the shadow target.
|
|
Luc Didry |
c0530c |
// Otherwise, proxy will throw an invariant error.
|
|
Luc Didry |
c0530c |
// This is our last chance to lock the value.
|
|
Luc Didry |
c0530c |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/getOwnPropertyDescriptor#Invariants
|
|
Luc Didry |
c0530c |
ObjectDefineProperty(shadowTarget, key, desc);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
return desc;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
preventExtensions(shadowTarget) {
|
|
Luc Didry |
c0530c |
return false;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
defineProperty(shadowTarget, key, descriptor) {
|
|
Luc Didry |
c0530c |
return false;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function createShadowTarget(value) {
|
|
Luc Didry |
c0530c |
let shadowTarget = undefined;
|
|
Luc Didry |
c0530c |
if (isArray(value)) {
|
|
Luc Didry |
c0530c |
shadowTarget = [];
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
else if (isObject(value)) {
|
|
Luc Didry |
c0530c |
shadowTarget = {};
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
return shadowTarget;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
const ObjectDotPrototype = Object.prototype;
|
|
Luc Didry |
c0530c |
function defaultValueIsObservable(value) {
|
|
Luc Didry |
c0530c |
// intentionally checking for null
|
|
Luc Didry |
c0530c |
if (value === null) {
|
|
Luc Didry |
c0530c |
return false;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
// treat all non-object types, including undefined, as non-observable values
|
|
Luc Didry |
c0530c |
if (typeof value !== 'object') {
|
|
Luc Didry |
c0530c |
return false;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
if (isArray(value)) {
|
|
Luc Didry |
c0530c |
return true;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
const proto = getPrototypeOf(value);
|
|
Luc Didry |
c0530c |
return (proto === ObjectDotPrototype || proto === null || getPrototypeOf(proto) === null);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
const defaultValueObserved = (obj, key) => {
|
|
Luc Didry |
c0530c |
/* do nothing */
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
const defaultValueMutated = (obj, key) => {
|
|
Luc Didry |
c0530c |
/* do nothing */
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
const defaultValueDistortion = (value) => value;
|
|
Luc Didry |
c0530c |
function wrapDescriptor(membrane, descriptor, getValue) {
|
|
Luc Didry |
c0530c |
const { set, get } = descriptor;
|
|
Luc Didry |
c0530c |
if (hasOwnProperty.call(descriptor, 'value')) {
|
|
Luc Didry |
c0530c |
descriptor.value = getValue(membrane, descriptor.value);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
else {
|
|
Luc Didry |
c0530c |
if (!isUndefined(get)) {
|
|
Luc Didry |
c0530c |
descriptor.get = function () {
|
|
Luc Didry |
c0530c |
// invoking the original getter with the original target
|
|
Luc Didry |
c0530c |
return getValue(membrane, get.call(unwrap(this)));
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
if (!isUndefined(set)) {
|
|
Luc Didry |
c0530c |
descriptor.set = function (value) {
|
|
Luc Didry |
c0530c |
// At this point we don't have a clear indication of whether
|
|
Luc Didry |
c0530c |
// or not a valid mutation will occur, we don't have the key,
|
|
Luc Didry |
c0530c |
// and we are not sure why and how they are invoking this setter.
|
|
Luc Didry |
c0530c |
// Nevertheless we preserve the original semantics by invoking the
|
|
Luc Didry |
c0530c |
// original setter with the original target and the unwrapped value
|
|
Luc Didry |
c0530c |
set.call(unwrap(this), membrane.unwrapProxy(value));
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
return descriptor;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
class ReactiveMembrane {
|
|
Luc Didry |
c0530c |
constructor(options) {
|
|
Luc Didry |
c0530c |
this.valueDistortion = defaultValueDistortion;
|
|
Luc Didry |
c0530c |
this.valueMutated = defaultValueMutated;
|
|
Luc Didry |
c0530c |
this.valueObserved = defaultValueObserved;
|
|
Luc Didry |
c0530c |
this.valueIsObservable = defaultValueIsObservable;
|
|
Luc Didry |
c0530c |
this.objectGraph = new WeakMap();
|
|
Luc Didry |
c0530c |
if (!isUndefined(options)) {
|
|
Luc Didry |
c0530c |
const { valueDistortion, valueMutated, valueObserved, valueIsObservable } = options;
|
|
Luc Didry |
c0530c |
this.valueDistortion = isFunction(valueDistortion) ? valueDistortion : defaultValueDistortion;
|
|
Luc Didry |
c0530c |
this.valueMutated = isFunction(valueMutated) ? valueMutated : defaultValueMutated;
|
|
Luc Didry |
c0530c |
this.valueObserved = isFunction(valueObserved) ? valueObserved : defaultValueObserved;
|
|
Luc Didry |
c0530c |
this.valueIsObservable = isFunction(valueIsObservable) ? valueIsObservable : defaultValueIsObservable;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
getProxy(value) {
|
|
Luc Didry |
c0530c |
const unwrappedValue = unwrap(value);
|
|
Luc Didry |
c0530c |
const distorted = this.valueDistortion(unwrappedValue);
|
|
Luc Didry |
c0530c |
if (this.valueIsObservable(distorted)) {
|
|
Luc Didry |
c0530c |
const o = this.getReactiveState(unwrappedValue, distorted);
|
|
Luc Didry |
c0530c |
// when trying to extract the writable version of a readonly
|
|
Luc Didry |
c0530c |
// we return the readonly.
|
|
Luc Didry |
c0530c |
return o.readOnly === value ? value : o.reactive;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
return distorted;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
getReadOnlyProxy(value) {
|
|
Luc Didry |
c0530c |
value = unwrap(value);
|
|
Luc Didry |
c0530c |
const distorted = this.valueDistortion(value);
|
|
Luc Didry |
c0530c |
if (this.valueIsObservable(distorted)) {
|
|
Luc Didry |
c0530c |
return this.getReactiveState(value, distorted).readOnly;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
return distorted;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
unwrapProxy(p) {
|
|
Luc Didry |
c0530c |
return unwrap(p);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
getReactiveState(value, distortedValue) {
|
|
Luc Didry |
c0530c |
const { objectGraph, } = this;
|
|
Luc Didry |
c0530c |
let reactiveState = objectGraph.get(distortedValue);
|
|
Luc Didry |
c0530c |
if (reactiveState) {
|
|
Luc Didry |
c0530c |
return reactiveState;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
const membrane = this;
|
|
Luc Didry |
c0530c |
reactiveState = {
|
|
Luc Didry |
c0530c |
get reactive() {
|
|
Luc Didry |
c0530c |
const reactiveHandler = new ReactiveProxyHandler(membrane, distortedValue);
|
|
Luc Didry |
c0530c |
// caching the reactive proxy after the first time it is accessed
|
|
Luc Didry |
c0530c |
const proxy = new Proxy(createShadowTarget(distortedValue), reactiveHandler);
|
|
Luc Didry |
c0530c |
registerProxy(proxy, value);
|
|
Luc Didry |
c0530c |
ObjectDefineProperty(this, 'reactive', { value: proxy });
|
|
Luc Didry |
c0530c |
return proxy;
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
get readOnly() {
|
|
Luc Didry |
c0530c |
const readOnlyHandler = new ReadOnlyHandler(membrane, distortedValue);
|
|
Luc Didry |
c0530c |
// caching the readOnly proxy after the first time it is accessed
|
|
Luc Didry |
c0530c |
const proxy = new Proxy(createShadowTarget(distortedValue), readOnlyHandler);
|
|
Luc Didry |
c0530c |
registerProxy(proxy, value);
|
|
Luc Didry |
c0530c |
ObjectDefineProperty(this, 'readOnly', { value: proxy });
|
|
Luc Didry |
c0530c |
return proxy;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
objectGraph.set(distortedValue, reactiveState);
|
|
Luc Didry |
c0530c |
return reactiveState;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
/** version: 0.26.0 */
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
function wrap(data, mutationCallback) {
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
let membrane = new ReactiveMembrane({
|
|
Luc Didry |
c0530c |
valueMutated(target, key) {
|
|
Luc Didry |
c0530c |
mutationCallback(target, key);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
return {
|
|
Luc Didry |
c0530c |
data: membrane.getProxy(data),
|
|
Luc Didry |
c0530c |
membrane: membrane
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
function unwrap$1(membrane, observable) {
|
|
Luc Didry |
c0530c |
let unwrappedData = membrane.unwrapProxy(observable);
|
|
Luc Didry |
c0530c |
let copy = {};
|
|
Luc Didry |
c0530c |
Object.keys(unwrappedData).forEach(key => {
|
|
Luc Didry |
c0530c |
if (['$el', '$refs', '$nextTick', '$watch'].includes(key)) return;
|
|
Luc Didry |
c0530c |
copy[key] = unwrappedData[key];
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
return copy;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
class Component {
|
|
Luc Didry |
c0530c |
constructor(el, seedDataForCloning = null) {
|
|
Luc Didry |
c0530c |
this.$el = el;
|
|
Luc Didry |
c0530c |
const dataAttr = this.$el.getAttribute('x-data');
|
|
Luc Didry |
c0530c |
const dataExpression = dataAttr === '' ? '{}' : dataAttr;
|
|
Luc Didry |
c0530c |
const initExpression = this.$el.getAttribute('x-init');
|
|
Luc Didry |
c0530c |
this.unobservedData = seedDataForCloning ? seedDataForCloning : saferEval(dataExpression, {});
|
|
Luc Didry |
c0530c |
// Construct a Proxy-based observable. This will be used to handle reactivity.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
let {
|
|
Luc Didry |
c0530c |
membrane,
|
|
Luc Didry |
c0530c |
data
|
|
Luc Didry |
c0530c |
} = this.wrapDataInObservable(this.unobservedData);
|
|
Luc Didry |
c0530c |
this.$data = data;
|
|
Luc Didry |
c0530c |
this.membrane = membrane; // After making user-supplied data methods reactive, we can now add
|
|
Luc Didry |
c0530c |
// our magic properties to the original data for access.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
this.unobservedData.$el = this.$el;
|
|
Luc Didry |
c0530c |
this.unobservedData.$refs = this.getRefsProxy();
|
|
Luc Didry |
c0530c |
this.nextTickStack = [];
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
this.unobservedData.$nextTick = callback => {
|
|
Luc Didry |
c0530c |
this.nextTickStack.push(callback);
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
this.watchers = {};
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
this.unobservedData.$watch = (property, callback) => {
|
|
Luc Didry |
c0530c |
if (!this.watchers[property]) this.watchers[property] = [];
|
|
Luc Didry |
c0530c |
this.watchers[property].push(callback);
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
this.showDirectiveStack = [];
|
|
Luc Didry |
c0530c |
this.showDirectiveLastElement;
|
|
Luc Didry |
c0530c |
var initReturnedCallback; // If x-init is present AND we aren't cloning (skip x-init on clone)
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (initExpression && !seedDataForCloning) {
|
|
Luc Didry |
c0530c |
// We want to allow data manipulation, but not trigger DOM updates just yet.
|
|
Luc Didry |
c0530c |
// We haven't even initialized the elements with their Alpine bindings. I mean c'mon.
|
|
Luc Didry |
c0530c |
this.pauseReactivity = true;
|
|
Luc Didry |
c0530c |
initReturnedCallback = this.evaluateReturnExpression(this.$el, initExpression);
|
|
Luc Didry |
c0530c |
this.pauseReactivity = false;
|
|
Luc Didry |
c0530c |
} // Register all our listeners and set all our attribute bindings.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
this.initializeElements(this.$el); // Use mutation observer to detect new elements being added within this component at run-time.
|
|
Luc Didry |
c0530c |
// Alpine's just so darn flexible amirite?
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
this.listenForNewElementsToInitialize();
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (typeof initReturnedCallback === 'function') {
|
|
Luc Didry |
c0530c |
// Run the callback returned from the "x-init" hook to allow the user to do stuff after
|
|
Luc Didry |
c0530c |
// Alpine's got it's grubby little paws all over everything.
|
|
Luc Didry |
c0530c |
initReturnedCallback.call(this.$data);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
getUnobservedData() {
|
|
Luc Didry |
c0530c |
return unwrap$1(this.membrane, this.$data);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
wrapDataInObservable(data) {
|
|
Luc Didry |
c0530c |
var self = this;
|
|
Luc Didry |
c0530c |
let updateDom = debounce(function () {
|
|
Luc Didry |
c0530c |
self.updateElements(self.$el);
|
|
Luc Didry |
c0530c |
}, 0);
|
|
Luc Didry |
c0530c |
return wrap(data, (target, key) => {
|
|
Luc Didry |
c0530c |
if (self.watchers[key]) {
|
|
Luc Didry |
c0530c |
// If there's a watcher for this specific key, run it.
|
|
Luc Didry |
c0530c |
self.watchers[key].forEach(callback => callback(target[key]));
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
// Let's walk through the watchers with "dot-notation" (foo.bar) and see
|
|
Luc Didry |
c0530c |
// if this mutation fits any of them.
|
|
Luc Didry |
c0530c |
Object.keys(self.watchers).filter(i => i.includes('.')).forEach(fullDotNotationKey => {
|
|
Luc Didry |
c0530c |
let dotNotationParts = fullDotNotationKey.split('.'); // If this dot-notation watcher's last "part" doesn't match the current
|
|
Luc Didry |
c0530c |
// key, then skip it early for performance reasons.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (key !== dotNotationParts[dotNotationParts.length - 1]) return; // Now, walk through the dot-notation "parts" recursively to find
|
|
Luc Didry |
c0530c |
// a match, and call the watcher if one's found.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
dotNotationParts.reduce((comparisonData, part) => {
|
|
Luc Didry |
c0530c |
if (Object.is(target, comparisonData)) {
|
|
Luc Didry |
c0530c |
// Run the watchers.
|
|
Luc Didry |
c0530c |
self.watchers[fullDotNotationKey].forEach(callback => callback(target[key]));
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return comparisonData[part];
|
|
Luc Didry |
c0530c |
}, self.getUnobservedData());
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
} // Don't react to data changes for cases like the `x-created` hook.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (self.pauseReactivity) return;
|
|
Luc Didry |
c0530c |
updateDom();
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
walkAndSkipNestedComponents(el, callback, initializeComponentCallback = () => {}) {
|
|
Luc Didry |
c0530c |
walk(el, el => {
|
|
Luc Didry |
c0530c |
// We've hit a component.
|
|
Luc Didry |
c0530c |
if (el.hasAttribute('x-data')) {
|
|
Luc Didry |
c0530c |
// If it's not the current one.
|
|
Luc Didry |
c0530c |
if (!el.isSameNode(this.$el)) {
|
|
Luc Didry |
c0530c |
// Initialize it if it's not.
|
|
Luc Didry |
c0530c |
if (!el.__x) initializeComponentCallback(el); // Now we'll let that sub-component deal with itself.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return false;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return callback(el);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
initializeElements(rootEl, extraVars = () => {}) {
|
|
Luc Didry |
c0530c |
this.walkAndSkipNestedComponents(rootEl, el => {
|
|
Luc Didry |
c0530c |
// Don't touch spawns from for loop
|
|
Luc Didry |
c0530c |
if (el.__x_for_key !== undefined) return false; // Don't touch spawns from if directives
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (el.__x_inserted_me !== undefined) return false;
|
|
Luc Didry |
c0530c |
this.initializeElement(el, extraVars);
|
|
Luc Didry |
c0530c |
}, el => {
|
|
Luc Didry |
c0530c |
el.__x = new Component(el);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
this.executeAndClearRemainingShowDirectiveStack();
|
|
Luc Didry |
c0530c |
this.executeAndClearNextTickStack(rootEl);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
initializeElement(el, extraVars) {
|
|
Luc Didry |
c0530c |
// To support class attribute merging, we have to know what the element's
|
|
Luc Didry |
c0530c |
// original class attribute looked like for reference.
|
|
Luc Didry |
c0530c |
if (el.hasAttribute('class') && getXAttrs(el).length > 0) {
|
|
Luc Didry |
c0530c |
el.__x_original_classes = el.getAttribute('class').split(' ');
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
this.registerListeners(el, extraVars);
|
|
Luc Didry |
c0530c |
this.resolveBoundAttributes(el, true, extraVars);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
updateElements(rootEl, extraVars = () => {}) {
|
|
Luc Didry |
c0530c |
this.walkAndSkipNestedComponents(rootEl, el => {
|
|
Luc Didry |
c0530c |
// Don't touch spawns from for loop (and check if the root is actually a for loop in a parent, don't skip it.)
|
|
Luc Didry |
c0530c |
if (el.__x_for_key !== undefined && !el.isSameNode(this.$el)) return false;
|
|
Luc Didry |
c0530c |
this.updateElement(el, extraVars);
|
|
Luc Didry |
c0530c |
}, el => {
|
|
Luc Didry |
c0530c |
el.__x = new Component(el);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
this.executeAndClearRemainingShowDirectiveStack();
|
|
Luc Didry |
c0530c |
this.executeAndClearNextTickStack(rootEl);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
executeAndClearNextTickStack(el) {
|
|
Luc Didry |
c0530c |
// Skip spawns from alpine directives
|
|
Luc Didry |
c0530c |
if (el === this.$el) {
|
|
Luc Didry |
c0530c |
// Walk through the $nextTick stack and clear it as we go.
|
|
Luc Didry |
c0530c |
while (this.nextTickStack.length > 0) {
|
|
Luc Didry |
c0530c |
this.nextTickStack.shift()();
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
executeAndClearRemainingShowDirectiveStack() {
|
|
Luc Didry |
c0530c |
// The goal here is to start all the x-show transitions
|
|
Luc Didry |
c0530c |
// and build a nested promise chain so that elements
|
|
Luc Didry |
c0530c |
// only hide when the children are finished hiding.
|
|
Luc Didry |
c0530c |
this.showDirectiveStack.reverse().map(thing => {
|
|
Luc Didry |
c0530c |
return new Promise(resolve => {
|
|
Luc Didry |
c0530c |
thing(finish => {
|
|
Luc Didry |
c0530c |
resolve(finish);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}).reduce((nestedPromise, promise) => {
|
|
Luc Didry |
c0530c |
return nestedPromise.then(() => {
|
|
Luc Didry |
c0530c |
return promise.then(finish => finish());
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}, Promise.resolve(() => {})); // We've processed the handler stack. let's clear it.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
this.showDirectiveStack = [];
|
|
Luc Didry |
c0530c |
this.showDirectiveLastElement = undefined;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
updateElement(el, extraVars) {
|
|
Luc Didry |
c0530c |
this.resolveBoundAttributes(el, false, extraVars);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
registerListeners(el, extraVars) {
|
|
Luc Didry |
c0530c |
getXAttrs(el).forEach(({
|
|
Luc Didry |
c0530c |
type,
|
|
Luc Didry |
c0530c |
value,
|
|
Luc Didry |
c0530c |
modifiers,
|
|
Luc Didry |
c0530c |
expression
|
|
Luc Didry |
c0530c |
}) => {
|
|
Luc Didry |
c0530c |
switch (type) {
|
|
Luc Didry |
c0530c |
case 'on':
|
|
Luc Didry |
c0530c |
registerListener(this, el, value, modifiers, expression, extraVars);
|
|
Luc Didry |
c0530c |
break;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
case 'model':
|
|
Luc Didry |
c0530c |
registerModelListener(this, el, modifiers, expression, extraVars);
|
|
Luc Didry |
c0530c |
break;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
resolveBoundAttributes(el, initialUpdate = false, extraVars) {
|
|
Luc Didry |
c0530c |
let attrs = getXAttrs(el);
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (el.type !== undefined && el.type === 'radio') {
|
|
Luc Didry |
c0530c |
// If there's an x-model on a radio input, move it to end of attribute list
|
|
Luc Didry |
c0530c |
// to ensure that x-bind:value (if present) is processed first.
|
|
Luc Didry |
c0530c |
const modelIdx = attrs.findIndex(attr => attr.type === 'model');
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (modelIdx > -1) {
|
|
Luc Didry |
c0530c |
attrs.push(attrs.splice(modelIdx, 1)[0]);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
attrs.forEach(({
|
|
Luc Didry |
c0530c |
type,
|
|
Luc Didry |
c0530c |
value,
|
|
Luc Didry |
c0530c |
modifiers,
|
|
Luc Didry |
c0530c |
expression
|
|
Luc Didry |
c0530c |
}) => {
|
|
Luc Didry |
c0530c |
switch (type) {
|
|
Luc Didry |
c0530c |
case 'model':
|
|
Luc Didry |
c0530c |
handleAttributeBindingDirective(this, el, 'value', expression, extraVars, type);
|
|
Luc Didry |
c0530c |
break;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
case 'bind':
|
|
Luc Didry |
c0530c |
// The :key binding on an x-for is special, ignore it.
|
|
Luc Didry |
c0530c |
if (el.tagName.toLowerCase() === 'template' && value === 'key') return;
|
|
Luc Didry |
c0530c |
handleAttributeBindingDirective(this, el, value, expression, extraVars, type);
|
|
Luc Didry |
c0530c |
break;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
case 'text':
|
|
Luc Didry |
c0530c |
var output = this.evaluateReturnExpression(el, expression, extraVars);
|
|
Luc Didry |
c0530c |
handleTextDirective(el, output, expression);
|
|
Luc Didry |
c0530c |
break;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
case 'html':
|
|
Luc Didry |
c0530c |
handleHtmlDirective(this, el, expression, extraVars);
|
|
Luc Didry |
c0530c |
break;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
case 'show':
|
|
Luc Didry |
c0530c |
var output = this.evaluateReturnExpression(el, expression, extraVars);
|
|
Luc Didry |
c0530c |
handleShowDirective(this, el, output, modifiers, initialUpdate);
|
|
Luc Didry |
c0530c |
break;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
case 'if':
|
|
Luc Didry |
c0530c |
// If this element also has x-for on it, don't process x-if.
|
|
Luc Didry |
c0530c |
// We will let the "x-for" directive handle the "if"ing.
|
|
Luc Didry |
c0530c |
if (attrs.filter(i => i.type === 'for').length > 0) return;
|
|
Luc Didry |
c0530c |
var output = this.evaluateReturnExpression(el, expression, extraVars);
|
|
Luc Didry |
c0530c |
handleIfDirective(this, el, output, initialUpdate, extraVars);
|
|
Luc Didry |
c0530c |
break;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
case 'for':
|
|
Luc Didry |
c0530c |
handleForDirective(this, el, expression, initialUpdate, extraVars);
|
|
Luc Didry |
c0530c |
break;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
case 'cloak':
|
|
Luc Didry |
c0530c |
el.removeAttribute('x-cloak');
|
|
Luc Didry |
c0530c |
break;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
evaluateReturnExpression(el, expression, extraVars = () => {}) {
|
|
Luc Didry |
c0530c |
return saferEval(expression, this.$data, _objectSpread2({}, extraVars(), {
|
|
Luc Didry |
c0530c |
$dispatch: this.getDispatchFunction(el)
|
|
Luc Didry |
c0530c |
}));
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
evaluateCommandExpression(el, expression, extraVars = () => {}) {
|
|
Luc Didry |
c0530c |
return saferEvalNoReturn(expression, this.$data, _objectSpread2({}, extraVars(), {
|
|
Luc Didry |
c0530c |
$dispatch: this.getDispatchFunction(el)
|
|
Luc Didry |
c0530c |
}));
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
getDispatchFunction(el) {
|
|
Luc Didry |
c0530c |
return (event, detail = {}) => {
|
|
Luc Didry |
c0530c |
el.dispatchEvent(new CustomEvent(event, {
|
|
Luc Didry |
c0530c |
detail,
|
|
Luc Didry |
c0530c |
bubbles: true
|
|
Luc Didry |
c0530c |
}));
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
listenForNewElementsToInitialize() {
|
|
Luc Didry |
c0530c |
const targetNode = this.$el;
|
|
Luc Didry |
c0530c |
const observerOptions = {
|
|
Luc Didry |
c0530c |
childList: true,
|
|
Luc Didry |
c0530c |
attributes: true,
|
|
Luc Didry |
c0530c |
subtree: true
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
const observer = new MutationObserver(mutations => {
|
|
Luc Didry |
c0530c |
for (let i = 0; i < mutations.length; i++) {
|
|
Luc Didry |
c0530c |
// Filter out mutations triggered from child components.
|
|
Luc Didry |
c0530c |
const closestParentComponent = mutations[i].target.closest('[x-data]');
|
|
Luc Didry |
c0530c |
if (!(closestParentComponent && closestParentComponent.isSameNode(this.$el))) continue;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (mutations[i].type === 'attributes' && mutations[i].attributeName === 'x-data') {
|
|
Luc Didry |
c0530c |
const rawData = saferEval(mutations[i].target.getAttribute('x-data'), {});
|
|
Luc Didry |
c0530c |
Object.keys(rawData).forEach(key => {
|
|
Luc Didry |
c0530c |
if (this.$data[key] !== rawData[key]) {
|
|
Luc Didry |
c0530c |
this.$data[key] = rawData[key];
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (mutations[i].addedNodes.length > 0) {
|
|
Luc Didry |
c0530c |
mutations[i].addedNodes.forEach(node => {
|
|
Luc Didry |
c0530c |
if (node.nodeType !== 1 || node.__x_inserted_me) return;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (node.matches('[x-data]')) {
|
|
Luc Didry |
c0530c |
node.__x = new Component(node);
|
|
Luc Didry |
c0530c |
return;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
this.initializeElements(node);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
observer.observe(targetNode, observerOptions);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
getRefsProxy() {
|
|
Luc Didry |
c0530c |
var self = this;
|
|
Luc Didry |
c0530c |
var refObj = {};
|
|
Luc Didry |
c0530c |
// One of the goals of this is to not hold elements in memory, but rather re-evaluate
|
|
Luc Didry |
c0530c |
// the DOM when the system needs something from it. This way, the framework is flexible and
|
|
Luc Didry |
c0530c |
// friendly to outside DOM changes from libraries like Vue/Livewire.
|
|
Luc Didry |
c0530c |
// For this reason, I'm using an "on-demand" proxy to fake a "$refs" object.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return new Proxy(refObj, {
|
|
Luc Didry |
c0530c |
get(object, property) {
|
|
Luc Didry |
c0530c |
if (property === '$isAlpineProxy') return true;
|
|
Luc Didry |
c0530c |
var ref; // We can't just query the DOM because it's hard to filter out refs in
|
|
Luc Didry |
c0530c |
// nested components.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
self.walkAndSkipNestedComponents(self.$el, el => {
|
|
Luc Didry |
c0530c |
if (el.hasAttribute('x-ref') && el.getAttribute('x-ref') === property) {
|
|
Luc Didry |
c0530c |
ref = el;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
return ref;
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
const Alpine = {
|
|
Luc Didry |
c0530c |
version: "2.3.3",
|
|
Luc Didry |
c0530c |
start: async function start() {
|
|
Luc Didry |
c0530c |
if (!isTesting()) {
|
|
Luc Didry |
c0530c |
await domReady();
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
this.discoverComponents(el => {
|
|
Luc Didry |
c0530c |
this.initializeComponent(el);
|
|
Luc Didry |
c0530c |
}); // It's easier and more performant to just support Turbolinks than listen
|
|
Luc Didry |
c0530c |
// to MutationObserver mutations at the document level.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
document.addEventListener("turbolinks:load", () => {
|
|
Luc Didry |
c0530c |
this.discoverUninitializedComponents(el => {
|
|
Luc Didry |
c0530c |
this.initializeComponent(el);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
this.listenForNewUninitializedComponentsAtRunTime(el => {
|
|
Luc Didry |
c0530c |
this.initializeComponent(el);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
discoverComponents: function discoverComponents(callback) {
|
|
Luc Didry |
c0530c |
const rootEls = document.querySelectorAll('[x-data]');
|
|
Luc Didry |
c0530c |
rootEls.forEach(rootEl => {
|
|
Luc Didry |
c0530c |
callback(rootEl);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
discoverUninitializedComponents: function discoverUninitializedComponents(callback, el = null) {
|
|
Luc Didry |
c0530c |
const rootEls = (el || document).querySelectorAll('[x-data]');
|
|
Luc Didry |
c0530c |
Array.from(rootEls).filter(el => el.__x === undefined).forEach(rootEl => {
|
|
Luc Didry |
c0530c |
callback(rootEl);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
listenForNewUninitializedComponentsAtRunTime: function listenForNewUninitializedComponentsAtRunTime(callback) {
|
|
Luc Didry |
c0530c |
const targetNode = document.querySelector('body');
|
|
Luc Didry |
c0530c |
const observerOptions = {
|
|
Luc Didry |
c0530c |
childList: true,
|
|
Luc Didry |
c0530c |
attributes: true,
|
|
Luc Didry |
c0530c |
subtree: true
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
const observer = new MutationObserver(mutations => {
|
|
Luc Didry |
c0530c |
for (let i = 0; i < mutations.length; i++) {
|
|
Luc Didry |
c0530c |
if (mutations[i].addedNodes.length > 0) {
|
|
Luc Didry |
c0530c |
mutations[i].addedNodes.forEach(node => {
|
|
Luc Didry |
c0530c |
// Discard non-element nodes (like line-breaks)
|
|
Luc Didry |
c0530c |
if (node.nodeType !== 1) return; // Discard any changes happening within an existing component.
|
|
Luc Didry |
c0530c |
// They will take care of themselves.
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (node.parentElement && node.parentElement.closest('[x-data]')) return;
|
|
Luc Didry |
c0530c |
this.discoverUninitializedComponents(el => {
|
|
Luc Didry |
c0530c |
this.initializeComponent(el);
|
|
Luc Didry |
c0530c |
}, node.parentElement);
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
observer.observe(targetNode, observerOptions);
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
initializeComponent: function initializeComponent(el) {
|
|
Luc Didry |
c0530c |
if (!el.__x) {
|
|
Luc Didry |
c0530c |
el.__x = new Component(el);
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
},
|
|
Luc Didry |
c0530c |
clone: function clone(component, newEl) {
|
|
Luc Didry |
c0530c |
if (!newEl.__x) {
|
|
Luc Didry |
c0530c |
newEl.__x = new Component(newEl, component.getUnobservedData());
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
};
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (!isTesting()) {
|
|
Luc Didry |
c0530c |
window.Alpine = Alpine;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
if (window.deferLoadingAlpine) {
|
|
Luc Didry |
c0530c |
window.deferLoadingAlpine(function () {
|
|
Luc Didry |
c0530c |
window.Alpine.start();
|
|
Luc Didry |
c0530c |
});
|
|
Luc Didry |
c0530c |
} else {
|
|
Luc Didry |
c0530c |
window.Alpine.start();
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
}
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
return Alpine;
|
|
Luc Didry |
c0530c |
|
|
Luc Didry |
c0530c |
})));
|