/
mixins.js
139 lines (124 loc) · 3.92 KB
/
mixins.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @file mixins
* @author zhangwentao <winty2013@gmail.com>
*/
import {getPageInstance} from '../helper/instance';
import {setObjectData} from '../helper/util';
import config from '../config';
export function makePageMixin($api) {
return {
beforeCreate() {
this.$api = $api;
},
updated() {
this.$emit('vm.updated');
},
mounted() {
this.$emit('vm.mounted');
}
};
}
// function markComponentInVue(pms, vms) {
// let pm = this.$mp;
// if (!pm) {
// pm = pms.find((item, index) => {
// if (item[0].data.compId === this.compId) {
// pms.splice(index, 1);
// return true;
// }
// return false;
// });
// }
// if (!pm) {
// vms.push(this);
// }
// else {
// pm[0].$vue = this;
// pm[1]();
// }
// }
export function makeGetCompMixin($api) {
return function getCompMixin(options) {
return {
props: {
compId: String
},
beforeCreate() {
this.$api = $api;
this.$options = Object.assign(this.$options, {
pageLifetimes: options.pageLifetimes,
lifetimes: options.lifetimes
});
},
created() {
if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.lifetimes) {
console.log('[debug: Vue] created', this.compId);
}
// console.log('===vue created', this.compId);
// markComponentInVue.call(this, pms, vms);
const vms = this.$root.__vms__;
vms[this.compId] = vms[this.compId] || {cur: -1, curSwan: -1};
const curIndex = ++vms[this.compId].cur;
vms[this.compId][curIndex] = this;
},
updated() {
this.$emit('vm.updated');
}
};
};
}
export function handleProxy(event) {
if (process.env.NODE_ENV !== 'production' && config.debug && config.debug.events) {
console.log('[debug: handleProxy]', this.data.compId, event);
}
// get event dataSet
const data = event.currentTarget.dataset;
const eventType = event.type;
if (event.target.id !== event.currentTarget.id && data[`${eventType}ModifierSelf`]) {
return;
}
const realHandler = data[`${eventType}eventproxy`.toLowerCase()];
if (eventType && realHandler) {
let args = data[`${eventType}argumentsproxy`.toLowerCase()] || [event];
args = args.map(a => a === '_$event_' ? event : a);
// swan 组件的事件可能在其 created 生命周期前触发,此时 this.$vue 还没有绑定上
if (this.__isComponent__ && !this.__created__) {
// console.log('====!this.__created__', this.__isComponent__, this);
this.__cbs__ = this.__cbs__ || [];
this.__cbs__.push([realHandler, args]);
return;
}
if (!this.$vue) {
const page = getPageInstance(this);
const vms = page.$vue.__vms__;
console.warn('[swan instance mismatch]', this.data.compId, this, vms);
return;
}
if (this.$vue[realHandler]) {
this.$vue[realHandler].apply(this.$vue, args);
}
}
}
export function handleModel(event) {
const type = event.type;
let ct = event.currentTarget;
let {
dataset: {
model,
tag
}
} = ct;
if (!model) {
return;
}
if (
type === 'input'
|| (type === 'change' && tag === 'picker')
|| (type === 'change' && tag === 'radio')
) {
setObjectData(this.$vue, model, event.detail.value);
}
else if (type === 'change' && tag === 'switch') {
setObjectData(this.$vue, model, event.detail.checked);
}
}