component.js
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
62
63
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.VantComponent = void 0;
var basic_1 = require('../mixins/basic');
var relationFunctions = {
ancestor: {
linked: function (parent) {
this.parent = parent;
},
unlinked: function () {
this.parent = null;
},
},
descendant: {
linked: function (child) {
this.children = this.children || [];
this.children.push(child);
},
unlinked: function (child) {
this.children = (this.children || []).filter(function (it) {
return it !== child;
});
},
},
};
function mapKeys(source, target, map) {
Object.keys(map).forEach(function (key) {
if (source[key]) {
target[map[key]] = source[key];
}
});
}
function makeRelation(options, vantOptions, relation) {
var _a;
var type = relation.type,
name = relation.name,
linked = relation.linked,
unlinked = relation.unlinked,
linkChanged = relation.linkChanged;
var beforeCreate = vantOptions.beforeCreate,
destroyed = vantOptions.destroyed;
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 || {},
((_a = {}),
(_a['../' + name + '/index'] = {
type: type,
linked: function (node) {
relationFunctions[type].linked.bind(this)(node);
linked && linked.bind(this)(node);
},
linkChanged: function (node) {
linkChanged && linkChanged.bind(this)(node);
},
unlinked: function (node) {
relationFunctions[type].unlinked.bind(this)(node);
unlinked && unlinked.bind(this)(node);
},
}),
_a)
);
}
function VantComponent(vantOptions) {
if (vantOptions === void 0) {
vantOptions = {};
}
var options = {};
mapKeys(vantOptions, options, {
data: 'data',
props: 'properties',
mixins: 'behaviors',
methods: 'methods',
beforeCreate: 'created',
created: 'attached',
mounted: 'ready',
relations: 'relations',
destroyed: 'detached',
classes: 'externalClasses',
});
var relation = vantOptions.relation;
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_1.basic);
// map field to form-field behavior
if (vantOptions.field) {
options.behaviors.push('wx://form-field');
}
if (options.properties) {
Object.keys(options.properties).forEach(function (name) {
if (Array.isArray(options.properties[name])) {
// miniprogram do not allow multi type
options.properties[name] = null;
}
});
}
// add default options
options.options = {
multipleSlots: true,
addGlobalClass: true,
};
Component(options);
}
exports.VantComponent = VantComponent;