complexity.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __assign = (this && this.__assign) || function () {
  18. __assign = Object.assign || function(t) {
  19. for (var s, i = 1, n = arguments.length; i < n; i++) {
  20. s = arguments[i];
  21. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  22. t[p] = s[p];
  23. }
  24. return t;
  25. };
  26. return __assign.apply(this, arguments);
  27. };
  28. var __read = (this && this.__read) || function (o, n) {
  29. var m = typeof Symbol === "function" && o[Symbol.iterator];
  30. if (!m) return o;
  31. var i = m.call(o), r, ar = [], e;
  32. try {
  33. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  34. }
  35. catch (error) { e = { error: error }; }
  36. finally {
  37. try {
  38. if (r && !r.done && (m = i["return"])) m.call(i);
  39. }
  40. finally { if (e) throw e.error; }
  41. }
  42. return ar;
  43. };
  44. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  45. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  46. if (ar || !(i in from)) {
  47. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  48. ar[i] = from[i];
  49. }
  50. }
  51. return to.concat(ar || Array.prototype.slice.call(from));
  52. };
  53. var __values = (this && this.__values) || function(o) {
  54. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  55. if (m) return m.call(o);
  56. if (o && typeof o.length === "number") return {
  57. next: function () {
  58. if (o && i >= o.length) o = void 0;
  59. return { value: o && o[i++], done: !o };
  60. }
  61. };
  62. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  63. };
  64. Object.defineProperty(exports, "__esModule", { value: true });
  65. exports.ComplexityHandler = exports.ComplexityMathDocumentMixin = exports.ComplexityMathItemMixin = void 0;
  66. var MathItem_js_1 = require("../core/MathItem.js");
  67. var semantic_enrich_js_1 = require("./semantic-enrich.js");
  68. var visitor_js_1 = require("./complexity/visitor.js");
  69. var Options_js_1 = require("../util/Options.js");
  70. (0, MathItem_js_1.newState)('COMPLEXITY', 40);
  71. function ComplexityMathItemMixin(BaseMathItem, computeComplexity) {
  72. return (function (_super) {
  73. __extends(class_1, _super);
  74. function class_1() {
  75. return _super !== null && _super.apply(this, arguments) || this;
  76. }
  77. class_1.prototype.complexity = function (document, force) {
  78. if (force === void 0) { force = false; }
  79. if (this.state() >= MathItem_js_1.STATE.COMPLEXITY)
  80. return;
  81. if (!this.isEscaped && (document.options.enableComplexity || force)) {
  82. this.enrich(document, true);
  83. computeComplexity(this.root);
  84. }
  85. this.state(MathItem_js_1.STATE.COMPLEXITY);
  86. };
  87. return class_1;
  88. }(BaseMathItem));
  89. }
  90. exports.ComplexityMathItemMixin = ComplexityMathItemMixin;
  91. function ComplexityMathDocumentMixin(BaseDocument) {
  92. var _a;
  93. return _a = (function (_super) {
  94. __extends(class_2, _super);
  95. function class_2() {
  96. var args = [];
  97. for (var _i = 0; _i < arguments.length; _i++) {
  98. args[_i] = arguments[_i];
  99. }
  100. var _this = _super.apply(this, __spreadArray([], __read(args), false)) || this;
  101. var ProcessBits = _this.constructor.ProcessBits;
  102. if (!ProcessBits.has('complexity')) {
  103. ProcessBits.allocate('complexity');
  104. }
  105. var visitorOptions = (0, Options_js_1.selectOptionsFromKeys)(_this.options, _this.options.ComplexityVisitor.OPTIONS);
  106. _this.complexityVisitor = new _this.options.ComplexityVisitor(_this.mmlFactory, visitorOptions);
  107. var computeComplexity = (function (node) { return _this.complexityVisitor.visitTree(node); });
  108. _this.options.MathItem =
  109. ComplexityMathItemMixin(_this.options.MathItem, computeComplexity);
  110. return _this;
  111. }
  112. class_2.prototype.complexity = function () {
  113. var e_1, _a;
  114. if (!this.processed.isSet('complexity')) {
  115. if (this.options.enableComplexity) {
  116. try {
  117. for (var _b = __values(this.math), _c = _b.next(); !_c.done; _c = _b.next()) {
  118. var math = _c.value;
  119. math.complexity(this);
  120. }
  121. }
  122. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  123. finally {
  124. try {
  125. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  126. }
  127. finally { if (e_1) throw e_1.error; }
  128. }
  129. }
  130. this.processed.set('complexity');
  131. }
  132. return this;
  133. };
  134. class_2.prototype.state = function (state, restore) {
  135. if (restore === void 0) { restore = false; }
  136. _super.prototype.state.call(this, state, restore);
  137. if (state < MathItem_js_1.STATE.COMPLEXITY) {
  138. this.processed.clear('complexity');
  139. }
  140. return this;
  141. };
  142. return class_2;
  143. }(BaseDocument)),
  144. _a.OPTIONS = __assign(__assign(__assign({}, BaseDocument.OPTIONS), visitor_js_1.ComplexityVisitor.OPTIONS), { enableComplexity: true, ComplexityVisitor: visitor_js_1.ComplexityVisitor, renderActions: (0, Options_js_1.expandable)(__assign(__assign({}, BaseDocument.OPTIONS.renderActions), { complexity: [MathItem_js_1.STATE.COMPLEXITY] })) }),
  145. _a;
  146. }
  147. exports.ComplexityMathDocumentMixin = ComplexityMathDocumentMixin;
  148. function ComplexityHandler(handler, MmlJax) {
  149. if (MmlJax === void 0) { MmlJax = null; }
  150. if (!handler.documentClass.prototype.enrich && MmlJax) {
  151. handler = (0, semantic_enrich_js_1.EnrichHandler)(handler, MmlJax);
  152. }
  153. handler.documentClass = ComplexityMathDocumentMixin(handler.documentClass);
  154. return handler;
  155. }
  156. exports.ComplexityHandler = ComplexityHandler;
  157. //# sourceMappingURL=complexity.js.map