123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- // Version 1.13.4 kapsule - https://github.com/vasturiano/kapsule
- (function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
- typeof define === 'function' && define.amd ? define(factory) :
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Kapsule = factory());
- }(this, (function () { 'use strict';
- function _classCallCheck(instance, Constructor) {
- if (!(instance instanceof Constructor)) {
- throw new TypeError("Cannot call a class as a function");
- }
- }
- function _slicedToArray(arr, i) {
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
- }
- function _arrayWithHoles(arr) {
- if (Array.isArray(arr)) return arr;
- }
- function _iterableToArrayLimit(arr, i) {
- var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]);
- if (_i == null) return;
- var _arr = [];
- var _n = true;
- var _d = false;
- var _s, _e;
- try {
- for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
- _arr.push(_s.value);
- if (i && _arr.length === i) break;
- }
- } catch (err) {
- _d = true;
- _e = err;
- } finally {
- try {
- if (!_n && _i["return"] != null) _i["return"]();
- } finally {
- if (_d) throw _e;
- }
- }
- return _arr;
- }
- function _unsupportedIterableToArray(o, minLen) {
- if (!o) return;
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
- var n = Object.prototype.toString.call(o).slice(8, -1);
- if (n === "Object" && o.constructor) n = o.constructor.name;
- if (n === "Map" || n === "Set") return Array.from(o);
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
- }
- function _arrayLikeToArray(arr, len) {
- if (len == null || len > arr.length) len = arr.length;
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
- return arr2;
- }
- function _nonIterableRest() {
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
- }
- /**
- * Returns a function, that, as long as it continues to be invoked, will not
- * be triggered. The function will be called after it stops being called for
- * N milliseconds. If `immediate` is passed, trigger the function on the
- * leading edge, instead of the trailing. The function also has a property 'clear'
- * that is a function which will clear the timer to prevent previously scheduled executions.
- *
- * @source underscore.js
- * @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
- * @param {Function} function to wrap
- * @param {Number} timeout in ms (`100`)
- * @param {Boolean} whether to execute at the beginning (`false`)
- * @api public
- */
- function debounce(func, wait, immediate){
- var timeout, args, context, timestamp, result;
- if (null == wait) wait = 100;
- function later() {
- var last = Date.now() - timestamp;
- if (last < wait && last >= 0) {
- timeout = setTimeout(later, wait - last);
- } else {
- timeout = null;
- if (!immediate) {
- result = func.apply(context, args);
- context = args = null;
- }
- }
- }
- var debounced = function(){
- context = this;
- args = arguments;
- timestamp = Date.now();
- var callNow = immediate && !timeout;
- if (!timeout) timeout = setTimeout(later, wait);
- if (callNow) {
- result = func.apply(context, args);
- context = args = null;
- }
- return result;
- };
- debounced.clear = function() {
- if (timeout) {
- clearTimeout(timeout);
- timeout = null;
- }
- };
-
- debounced.flush = function() {
- if (timeout) {
- result = func.apply(context, args);
- context = args = null;
-
- clearTimeout(timeout);
- timeout = null;
- }
- };
- return debounced;
- }
- // Adds compatibility for ES modules
- debounce.debounce = debounce;
- var debounce_1 = debounce;
- var Prop = function Prop(name, _ref) {
- var _ref$default = _ref["default"],
- defaultVal = _ref$default === void 0 ? null : _ref$default,
- _ref$triggerUpdate = _ref.triggerUpdate,
- triggerUpdate = _ref$triggerUpdate === void 0 ? true : _ref$triggerUpdate,
- _ref$onChange = _ref.onChange,
- onChange = _ref$onChange === void 0 ? function (newVal, state) {} : _ref$onChange;
- _classCallCheck(this, Prop);
- this.name = name;
- this.defaultVal = defaultVal;
- this.triggerUpdate = triggerUpdate;
- this.onChange = onChange;
- };
- function index (_ref2) {
- var _ref2$stateInit = _ref2.stateInit,
- stateInit = _ref2$stateInit === void 0 ? function () {
- return {};
- } : _ref2$stateInit,
- _ref2$props = _ref2.props,
- rawProps = _ref2$props === void 0 ? {} : _ref2$props,
- _ref2$methods = _ref2.methods,
- methods = _ref2$methods === void 0 ? {} : _ref2$methods,
- _ref2$aliases = _ref2.aliases,
- aliases = _ref2$aliases === void 0 ? {} : _ref2$aliases,
- _ref2$init = _ref2.init,
- initFn = _ref2$init === void 0 ? function () {} : _ref2$init,
- _ref2$update = _ref2.update,
- updateFn = _ref2$update === void 0 ? function () {} : _ref2$update;
- // Parse props into Prop instances
- var props = Object.keys(rawProps).map(function (propName) {
- return new Prop(propName, rawProps[propName]);
- });
- return function () {
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- // Holds component state
- var state = Object.assign({}, stateInit instanceof Function ? stateInit(options) : stateInit, // Support plain objects for backwards compatibility
- {
- initialised: false
- }); // keeps track of which props triggered an update
- var changedProps = {}; // Component constructor
- function comp(nodeElement) {
- initStatic(nodeElement, options);
- digest();
- return comp;
- }
- var initStatic = function initStatic(nodeElement, options) {
- initFn.call(comp, nodeElement, state, options);
- state.initialised = true;
- };
- var digest = debounce_1(function () {
- if (!state.initialised) {
- return;
- }
- updateFn.call(comp, state, changedProps);
- changedProps = {};
- }, 1); // Getter/setter methods
- props.forEach(function (prop) {
- comp[prop.name] = getSetProp(prop);
- function getSetProp(_ref3) {
- var prop = _ref3.name,
- _ref3$triggerUpdate = _ref3.triggerUpdate,
- redigest = _ref3$triggerUpdate === void 0 ? false : _ref3$triggerUpdate,
- _ref3$onChange = _ref3.onChange,
- onChange = _ref3$onChange === void 0 ? function (newVal, state) {} : _ref3$onChange,
- _ref3$defaultVal = _ref3.defaultVal,
- defaultVal = _ref3$defaultVal === void 0 ? null : _ref3$defaultVal;
- return function (_) {
- var curVal = state[prop];
- if (!arguments.length) {
- return curVal;
- } // Getter mode
- var val = _ === undefined ? defaultVal : _; // pick default if value passed is undefined
- state[prop] = val;
- onChange.call(comp, val, state, curVal); // track changed props
- !changedProps.hasOwnProperty(prop) && (changedProps[prop] = curVal);
- if (redigest) {
- digest();
- }
- return comp;
- };
- }
- }); // Other methods
- Object.keys(methods).forEach(function (methodName) {
- comp[methodName] = function () {
- var _methods$methodName;
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
- return (_methods$methodName = methods[methodName]).call.apply(_methods$methodName, [comp, state].concat(args));
- };
- }); // Link aliases
- Object.entries(aliases).forEach(function (_ref4) {
- var _ref5 = _slicedToArray(_ref4, 2),
- alias = _ref5[0],
- target = _ref5[1];
- return comp[alias] = comp[target];
- }); // Reset all component props to their default value
- comp.resetProps = function () {
- props.forEach(function (prop) {
- comp[prop.name](prop.defaultVal);
- });
- return comp;
- }; //
- comp.resetProps(); // Apply all prop defaults
- state._rerender = digest; // Expose digest method
- return comp;
- };
- }
- return index;
- })));
- //# sourceMappingURL=kapsule.js.map
|