variable.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Variable = void 0;
  4. var menu_util_js_1 = require("./menu_util.js");
  5. var Variable = (function () {
  6. function Variable(_name, getter, setter) {
  7. this._name = _name;
  8. this.getter = getter;
  9. this.setter = setter;
  10. this.items = [];
  11. }
  12. Variable.fromJson = function (_factory, _a, pool) {
  13. var name = _a.name, getter = _a.getter, setter = _a.setter;
  14. var variable = new this(name, getter, setter);
  15. pool.insert(variable);
  16. };
  17. Object.defineProperty(Variable.prototype, "name", {
  18. get: function () {
  19. return this._name;
  20. },
  21. enumerable: false,
  22. configurable: true
  23. });
  24. Variable.prototype.getValue = function (node) {
  25. try {
  26. return this.getter(node);
  27. }
  28. catch (e) {
  29. menu_util_js_1.MenuUtil.error(e, 'Command of variable ' + this.name + ' failed.');
  30. return null;
  31. }
  32. };
  33. Variable.prototype.setValue = function (value, node) {
  34. try {
  35. this.setter(value, node);
  36. }
  37. catch (e) {
  38. menu_util_js_1.MenuUtil.error(e, 'Command of variable ' + this.name + ' failed.');
  39. }
  40. this.update();
  41. };
  42. Variable.prototype.register = function (item) {
  43. if (this.items.indexOf(item) === -1) {
  44. this.items.push(item);
  45. }
  46. };
  47. Variable.prototype.unregister = function (item) {
  48. var index = this.items.indexOf(item);
  49. if (index !== -1) {
  50. this.items.splice(index, 1);
  51. }
  52. };
  53. Variable.prototype.update = function () {
  54. this.items.forEach(function (x) { return x.update(); });
  55. };
  56. Variable.prototype.registerCallback = function (func) {
  57. this.items.forEach(function (x) { return x.registerCallback(func); });
  58. };
  59. Variable.prototype.unregisterCallback = function (func) {
  60. this.items.forEach(function (x) { return x.unregisterCallback(func); });
  61. };
  62. Variable.prototype.toJson = function () {
  63. return { type: 'variable',
  64. name: this.name,
  65. getter: this.getter.toString(),
  66. setter: this.setter.toString() };
  67. };
  68. return Variable;
  69. }());
  70. exports.Variable = Variable;
  71. //# sourceMappingURL=variable.js.map