UNPKG

curvature

Version:

![avatar](https://avatars3.githubusercontent.com/u/640101?s=80&v=4)

204 lines (203 loc) 10.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RuleSet = void 0; var _Dom = require("./Dom"); var _Tag = require("./Tag"); var _View = require("./View"); function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } 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."); } function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } 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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } var RuleSet = /*#__PURE__*/function () { function RuleSet() { _classCallCheck(this, RuleSet); } _createClass(RuleSet, [{ key: "add", value: function add(selector, callback) { this.rules = this.rules || {}; this.rules[selector] = this.rules[selector] || []; this.rules[selector].push(callback); return this; } }, { key: "apply", value: function apply() { var doc = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document; var view = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; RuleSet.apply(doc, view); for (var selector in this.rules) { for (var i in this.rules[selector]) { var callback = this.rules[selector][i]; var wrapped = RuleSet.wrap(doc, callback, view); var nodes = doc.querySelectorAll(selector); var _iterator = _createForOfIteratorHelper(nodes), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var node = _step.value; wrapped(node); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } } } }, { key: "purge", value: function purge() { if (!this.rules) { return; } for (var _i = 0, _Object$entries = Object.entries(this.rules); _i < _Object$entries.length; _i++) { var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), k = _Object$entries$_i[0], v = _Object$entries$_i[1]; if (!this.rules[k]) { continue; } for (var kk in this.rules[k]) { delete this.rules[k][kk]; } } } }], [{ key: "add", value: function add(selector, callback) { this.globalRules = this.globalRules || {}; this.globalRules[selector] = this.globalRules[selector] || []; this.globalRules[selector].push(callback); return this; } }, { key: "apply", value: function apply() { var doc = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document; var view = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; for (var selector in this.globalRules) { for (var i in this.globalRules[selector]) { var callback = this.globalRules[selector][i]; var wrapped = this.wrap(doc, callback, view); var nodes = doc.querySelectorAll(selector); var _iterator2 = _createForOfIteratorHelper(nodes), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var node = _step2.value; wrapped(node); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } } } } }, { key: "wait", value: function wait() { var _this = this; var event = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'DOMContentLoaded'; var node = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document; var listener = function (event, node) { return function () { node.removeEventListener(event, listener); return _this.apply(); }; }(event, node); node.addEventListener(event, listener); } }, { key: "wrap", value: function wrap(doc, originalCallback) { var view = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; var callback = originalCallback; if (originalCallback instanceof _View.View || originalCallback && originalCallback.prototype && originalCallback.prototype instanceof _View.View) { callback = function callback() { return originalCallback; }; } return function (element) { if (typeof element.___cvApplied___ === 'undefined') { Object.defineProperty(element, '___cvApplied___', { enumerable: false, writable: false, value: new WeakSet() }); } if (element.___cvApplied___.has(originalCallback)) { return; } var direct, parentView; if (view) { direct = parentView = view; if (view.viewList) { parentView = view.viewList.parent; } } var tag = new _Tag.Tag(element, parentView, null, undefined, direct); var parent = tag.element.parentNode; var sibling = tag.element.nextSibling; var result = callback(tag); if (result !== false) { element.___cvApplied___.add(originalCallback); } if (result instanceof HTMLElement) { result = new _Tag.Tag(result); } if (result instanceof _Tag.Tag) { if (!result.element.contains(tag.element)) { while (tag.element.firstChild) { result.element.appendChild(tag.element.firstChild); } tag.remove(); } if (sibling) { parent.insertBefore(result.element, sibling); } else { parent.appendChild(result.element); } } if (result && result.prototype && result.prototype instanceof _View.View) { result = new result({}, view); } if (result instanceof _View.View) { if (view) { view.cleanup.push(function (r) { return function () { r.remove(); }; }(result)); view.cleanup.push(view.args.bindTo(function (v, k, t) { t[k] = v; result.args[k] = v; })); view.cleanup.push(result.args.bindTo(function (v, k, t, d) { t[k] = v; view.args[k] = v; })); } tag.clear(); result.render(tag.element); } }; } }]); return RuleSet; }(); exports.RuleSet = RuleSet;