component.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.VantComponent = VantComponent;
  6. var _basic = require("../mixins/basic");
  7. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  8. var _my = require("../../__antmove/api/index.js")(my);
  9. var wx = _my;
  10. var relationFunctions = {
  11. ancestor: {
  12. linked: function linked(parent) {
  13. // @ts-ignore
  14. this.parent = parent;
  15. },
  16. unlinked: function unlinked() {
  17. // @ts-ignore
  18. this.parent = null;
  19. }
  20. },
  21. descendant: {
  22. linked: function linked(child) {
  23. // @ts-ignore
  24. this.children = this.children || []; // @ts-ignore
  25. this.children.push(child);
  26. },
  27. unlinked: function unlinked(child) {
  28. // @ts-ignore
  29. this.children = (this.children || []).filter(function (it) {
  30. return it !== child;
  31. });
  32. }
  33. }
  34. };
  35. function mapKeys(source, target, map) {
  36. Object.keys(map).forEach(function (key) {
  37. if (source[key]) {
  38. target[map[key]] = source[key];
  39. }
  40. });
  41. }
  42. function makeRelation(options, vantOptions, relation) {
  43. var type = relation.type,
  44. name = relation.name,
  45. _linked = relation.linked,
  46. _unlinked = relation.unlinked,
  47. _linkChanged = relation.linkChanged;
  48. var beforeCreate = vantOptions.beforeCreate,
  49. destroyed = vantOptions.destroyed;
  50. if (type === "descendant") {
  51. options.created = function () {
  52. beforeCreate && beforeCreate.bind(this)();
  53. this.children = this.children || [];
  54. };
  55. options.detached = function () {
  56. this.children = [];
  57. destroyed && destroyed.bind(this)();
  58. };
  59. }
  60. options.relations = Object.assign(options.relations || {}, _defineProperty({}, "../".concat(name, "/index"), {
  61. type: type,
  62. linked: function linked(node) {
  63. relationFunctions[type].linked.bind(this)(node);
  64. _linked && _linked.bind(this)(node);
  65. },
  66. linkChanged: function linkChanged(node) {
  67. _linkChanged && _linkChanged.bind(this)(node);
  68. },
  69. unlinked: function unlinked(node) {
  70. relationFunctions[type].unlinked.bind(this)(node);
  71. _unlinked && _unlinked.bind(this)(node);
  72. }
  73. }));
  74. }
  75. function VantComponent() {
  76. var vantOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  77. var options = {};
  78. var _destroyed = vantOptions.destroyed;
  79. vantOptions.destroyed = function () {
  80. this.isDestroyed = true;
  81. _destroyed && _destroyed.bind(this)();
  82. };
  83. mapKeys(vantOptions, options, {
  84. data: "data",
  85. props: "properties",
  86. mixins: "behaviors",
  87. methods: "methods",
  88. beforeCreate: "created",
  89. created: "attached",
  90. mounted: "ready",
  91. relations: "relations",
  92. destroyed: "detached",
  93. classes: "externalClasses"
  94. });
  95. var relation = vantOptions.relation;
  96. if (relation) {
  97. makeRelation(options, vantOptions, relation);
  98. } // add default externalClasses
  99. options.externalClasses = options.externalClasses || [];
  100. options.externalClasses.push("custom-class"); // add default behaviors
  101. options.behaviors = options.behaviors || [];
  102. options.behaviors.push(_basic.basic); // map field to form-field behavior
  103. if (vantOptions.field) {
  104. options.behaviors.push("wx://form-field");
  105. }
  106. if (options.properties) {
  107. Object.keys(options.properties).forEach(function (name) {
  108. if (Array.isArray(options.properties[name])) {
  109. // miniprogram do not allow multi type
  110. options.properties[name] = null;
  111. }
  112. });
  113. } // add default options
  114. options.options = {
  115. multipleSlots: true,
  116. addGlobalClass: true
  117. };
  118. Component(options);
  119. }