海鲜小程序

component.js 2.7KB

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