component.js 3.2 KB

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