kapsule.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Version 1.13.4 kapsule - https://github.com/vasturiano/kapsule
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  4. typeof define === 'function' && define.amd ? define(factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Kapsule = factory());
  6. }(this, (function () { 'use strict';
  7. function _classCallCheck(instance, Constructor) {
  8. if (!(instance instanceof Constructor)) {
  9. throw new TypeError("Cannot call a class as a function");
  10. }
  11. }
  12. function _slicedToArray(arr, i) {
  13. return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
  14. }
  15. function _arrayWithHoles(arr) {
  16. if (Array.isArray(arr)) return arr;
  17. }
  18. function _iterableToArrayLimit(arr, i) {
  19. var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]);
  20. if (_i == null) return;
  21. var _arr = [];
  22. var _n = true;
  23. var _d = false;
  24. var _s, _e;
  25. try {
  26. for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
  27. _arr.push(_s.value);
  28. if (i && _arr.length === i) break;
  29. }
  30. } catch (err) {
  31. _d = true;
  32. _e = err;
  33. } finally {
  34. try {
  35. if (!_n && _i["return"] != null) _i["return"]();
  36. } finally {
  37. if (_d) throw _e;
  38. }
  39. }
  40. return _arr;
  41. }
  42. function _unsupportedIterableToArray(o, minLen) {
  43. if (!o) return;
  44. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  45. var n = Object.prototype.toString.call(o).slice(8, -1);
  46. if (n === "Object" && o.constructor) n = o.constructor.name;
  47. if (n === "Map" || n === "Set") return Array.from(o);
  48. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  49. }
  50. function _arrayLikeToArray(arr, len) {
  51. if (len == null || len > arr.length) len = arr.length;
  52. for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  53. return arr2;
  54. }
  55. function _nonIterableRest() {
  56. throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  57. }
  58. /**
  59. * Returns a function, that, as long as it continues to be invoked, will not
  60. * be triggered. The function will be called after it stops being called for
  61. * N milliseconds. If `immediate` is passed, trigger the function on the
  62. * leading edge, instead of the trailing. The function also has a property 'clear'
  63. * that is a function which will clear the timer to prevent previously scheduled executions.
  64. *
  65. * @source underscore.js
  66. * @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
  67. * @param {Function} function to wrap
  68. * @param {Number} timeout in ms (`100`)
  69. * @param {Boolean} whether to execute at the beginning (`false`)
  70. * @api public
  71. */
  72. function debounce(func, wait, immediate){
  73. var timeout, args, context, timestamp, result;
  74. if (null == wait) wait = 100;
  75. function later() {
  76. var last = Date.now() - timestamp;
  77. if (last < wait && last >= 0) {
  78. timeout = setTimeout(later, wait - last);
  79. } else {
  80. timeout = null;
  81. if (!immediate) {
  82. result = func.apply(context, args);
  83. context = args = null;
  84. }
  85. }
  86. }
  87. var debounced = function(){
  88. context = this;
  89. args = arguments;
  90. timestamp = Date.now();
  91. var callNow = immediate && !timeout;
  92. if (!timeout) timeout = setTimeout(later, wait);
  93. if (callNow) {
  94. result = func.apply(context, args);
  95. context = args = null;
  96. }
  97. return result;
  98. };
  99. debounced.clear = function() {
  100. if (timeout) {
  101. clearTimeout(timeout);
  102. timeout = null;
  103. }
  104. };
  105. debounced.flush = function() {
  106. if (timeout) {
  107. result = func.apply(context, args);
  108. context = args = null;
  109. clearTimeout(timeout);
  110. timeout = null;
  111. }
  112. };
  113. return debounced;
  114. }
  115. // Adds compatibility for ES modules
  116. debounce.debounce = debounce;
  117. var debounce_1 = debounce;
  118. var Prop = function Prop(name, _ref) {
  119. var _ref$default = _ref["default"],
  120. defaultVal = _ref$default === void 0 ? null : _ref$default,
  121. _ref$triggerUpdate = _ref.triggerUpdate,
  122. triggerUpdate = _ref$triggerUpdate === void 0 ? true : _ref$triggerUpdate,
  123. _ref$onChange = _ref.onChange,
  124. onChange = _ref$onChange === void 0 ? function (newVal, state) {} : _ref$onChange;
  125. _classCallCheck(this, Prop);
  126. this.name = name;
  127. this.defaultVal = defaultVal;
  128. this.triggerUpdate = triggerUpdate;
  129. this.onChange = onChange;
  130. };
  131. function index (_ref2) {
  132. var _ref2$stateInit = _ref2.stateInit,
  133. stateInit = _ref2$stateInit === void 0 ? function () {
  134. return {};
  135. } : _ref2$stateInit,
  136. _ref2$props = _ref2.props,
  137. rawProps = _ref2$props === void 0 ? {} : _ref2$props,
  138. _ref2$methods = _ref2.methods,
  139. methods = _ref2$methods === void 0 ? {} : _ref2$methods,
  140. _ref2$aliases = _ref2.aliases,
  141. aliases = _ref2$aliases === void 0 ? {} : _ref2$aliases,
  142. _ref2$init = _ref2.init,
  143. initFn = _ref2$init === void 0 ? function () {} : _ref2$init,
  144. _ref2$update = _ref2.update,
  145. updateFn = _ref2$update === void 0 ? function () {} : _ref2$update;
  146. // Parse props into Prop instances
  147. var props = Object.keys(rawProps).map(function (propName) {
  148. return new Prop(propName, rawProps[propName]);
  149. });
  150. return function () {
  151. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  152. // Holds component state
  153. var state = Object.assign({}, stateInit instanceof Function ? stateInit(options) : stateInit, // Support plain objects for backwards compatibility
  154. {
  155. initialised: false
  156. }); // keeps track of which props triggered an update
  157. var changedProps = {}; // Component constructor
  158. function comp(nodeElement) {
  159. initStatic(nodeElement, options);
  160. digest();
  161. return comp;
  162. }
  163. var initStatic = function initStatic(nodeElement, options) {
  164. initFn.call(comp, nodeElement, state, options);
  165. state.initialised = true;
  166. };
  167. var digest = debounce_1(function () {
  168. if (!state.initialised) {
  169. return;
  170. }
  171. updateFn.call(comp, state, changedProps);
  172. changedProps = {};
  173. }, 1); // Getter/setter methods
  174. props.forEach(function (prop) {
  175. comp[prop.name] = getSetProp(prop);
  176. function getSetProp(_ref3) {
  177. var prop = _ref3.name,
  178. _ref3$triggerUpdate = _ref3.triggerUpdate,
  179. redigest = _ref3$triggerUpdate === void 0 ? false : _ref3$triggerUpdate,
  180. _ref3$onChange = _ref3.onChange,
  181. onChange = _ref3$onChange === void 0 ? function (newVal, state) {} : _ref3$onChange,
  182. _ref3$defaultVal = _ref3.defaultVal,
  183. defaultVal = _ref3$defaultVal === void 0 ? null : _ref3$defaultVal;
  184. return function (_) {
  185. var curVal = state[prop];
  186. if (!arguments.length) {
  187. return curVal;
  188. } // Getter mode
  189. var val = _ === undefined ? defaultVal : _; // pick default if value passed is undefined
  190. state[prop] = val;
  191. onChange.call(comp, val, state, curVal); // track changed props
  192. !changedProps.hasOwnProperty(prop) && (changedProps[prop] = curVal);
  193. if (redigest) {
  194. digest();
  195. }
  196. return comp;
  197. };
  198. }
  199. }); // Other methods
  200. Object.keys(methods).forEach(function (methodName) {
  201. comp[methodName] = function () {
  202. var _methods$methodName;
  203. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  204. args[_key] = arguments[_key];
  205. }
  206. return (_methods$methodName = methods[methodName]).call.apply(_methods$methodName, [comp, state].concat(args));
  207. };
  208. }); // Link aliases
  209. Object.entries(aliases).forEach(function (_ref4) {
  210. var _ref5 = _slicedToArray(_ref4, 2),
  211. alias = _ref5[0],
  212. target = _ref5[1];
  213. return comp[alias] = comp[target];
  214. }); // Reset all component props to their default value
  215. comp.resetProps = function () {
  216. props.forEach(function (prop) {
  217. comp[prop.name](prop.defaultVal);
  218. });
  219. return comp;
  220. }; //
  221. comp.resetProps(); // Apply all prop defaults
  222. state._rerender = digest; // Expose digest method
  223. return comp;
  224. };
  225. }
  226. return index;
  227. })));
  228. //# sourceMappingURL=kapsule.js.map