component.js 3.1 KB

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