component.js 3.1 KB

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