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