UNPKG

6.43 kBJavaScriptView Raw
1/*!
2 * vue-messenger v2.3.3
3 * (c) 2018-present fjc0k <fjc0kb@gmail.com> (https://github.com/fjc0k)
4 * Released under the MIT License.
5 */
6var isFunction = (function (value) {
7 return typeof value === 'function';
8});
9
10var isNumeric = (function (value) {
11 return !isNaN(value - parseFloat(value));
12});
13
14var isObject = (function (value) {
15 return value && typeof value === 'object';
16});
17
18var cache = Object.create(null);
19var upperCaseFirst = (function (str) {
20 if (!(str in cache)) {
21 cache[str] = str.charAt(0).toUpperCase() + str.slice(1);
22 }
23
24 return cache[str];
25});
26
27var defaultModel = {
28 prop: 'value',
29 event: 'input'
30};
31var index = {
32 beforeCreate: function beforeCreate() {
33 if (this.__MessengerBeforeCreate__) return;
34 this.__MessengerBeforeCreate__ = true;
35 var options = this.$options;
36 if (!options.localDataKeys) options.localDataKeys = [];
37 if (!options.methods) options.methods = {};
38 if (!options.watch) options.watch = {};
39 var model = options.model || defaultModel;
40 var props = options.props || {};
41
42 var _arr = Object.keys(props);
43
44 var _loop = function _loop() {
45 var prop = _arr[_i];
46 var descriptor = props[prop]; // enum
47
48 if (Array.isArray(descriptor.enum)) {
49 var nextValidator = descriptor.validator;
50
51 descriptor.validator = function (value) {
52 return descriptor.enum.indexOf(value) >= 0 && (nextValidator ? nextValidator.apply(this, arguments) : true);
53 };
54
55 if (!('default' in descriptor)) {
56 descriptor.default = descriptor.enum[0];
57 }
58 } // numeric
59
60
61 if (descriptor.numeric === true) {
62 var _nextValidator = descriptor.validator;
63 descriptor.type = [String, Number];
64
65 descriptor.validator = function (value) {
66 var infinite = descriptor.infinite,
67 range = descriptor.range;
68 return (infinite && (value === Infinity || value === -Infinity) || isNumeric(value)) && (Array.isArray(range) ? value >= range[0] && value <= range[1] : true) && (_nextValidator ? _nextValidator.apply(this, arguments) : true);
69 };
70 }
71
72 var isModelProp = prop === model.prop;
73 var event = isModelProp ? model.event : "update:" + prop;
74 var shouldEmit = isModelProp || !!descriptor.sync;
75 var shouldTransform = !!descriptor.transform;
76 var shouldListen = descriptor.on && isFunction(descriptor.on.receive || descriptor.on.change);
77 var shouldProcess = shouldEmit || shouldTransform;
78 var onReceive = void 0;
79 var onSend = void 0;
80 var onChange = void 0;
81 var on = descriptor.on;
82
83 if ((shouldListen || shouldProcess) && isObject(on)) {
84 if (isFunction(on.receive)) {
85 onReceive = on.receive;
86 }
87
88 if (isFunction(on.send)) {
89 onSend = on.send;
90 }
91
92 if (isFunction(on.change)) {
93 onChange = on.change;
94 }
95 }
96
97 if (shouldProcess) {
98 var receiveTransform;
99 var sendTransform;
100 var transform = descriptor.transform;
101
102 if (isFunction(transform)) {
103 receiveTransform = transform;
104 } else if (isObject(transform)) {
105 if (isFunction(transform.receive)) {
106 receiveTransform = transform.receive;
107 }
108
109 if (isFunction(transform.send)) {
110 sendTransform = transform.send;
111 }
112 }
113
114 var Prop = upperCaseFirst(prop);
115 var localProp = "local" + Prop;
116 var lastProp = "last" + Prop + "$$";
117 var lastLocalProp = "lastLocal" + Prop + "$$";
118 var sendProp = "send" + Prop;
119 options.localDataKeys.push(localProp);
120 options.watch[prop] = {
121 immediate: true,
122 handler: function handler(newValue, oldValue) {
123 if (newValue === oldValue || newValue === this[lastLocalProp]) {
124 this[lastProp] = newValue;
125 return;
126 }
127
128 if (receiveTransform && newValue != null) {
129 newValue = receiveTransform.call(this, newValue);
130 if (newValue === oldValue || newValue === this[lastLocalProp]) return;
131 }
132
133 if (onReceive) {
134 if (onReceive.call(this, newValue, oldValue) === false) {
135 return;
136 }
137 }
138
139 if (onChange) {
140 if (onChange.call(this, newValue, oldValue) === false) {
141 return;
142 }
143 }
144
145 this[lastProp] = newValue;
146 this[localProp] = newValue;
147 }
148 };
149 options.watch[localProp] = {
150 immediate: false,
151 handler: function handler(newValue, oldValue) {
152 if (newValue === oldValue || newValue === this[lastProp]) {
153 this[lastLocalProp] = newValue;
154 return;
155 }
156
157 if (sendTransform && newValue != null) {
158 newValue = sendTransform.call(this, newValue);
159 if (newValue === oldValue || newValue === this[lastProp]) return;
160 }
161
162 if (onSend) {
163 if (onSend.call(this, newValue, oldValue) === false) {
164 return;
165 }
166 }
167
168 if (onChange) {
169 if (onChange.call(this, newValue, oldValue) === false) {
170 return;
171 }
172 }
173
174 this[lastLocalProp] = newValue;
175
176 if (shouldEmit) {
177 this.$emit(event, newValue, oldValue);
178 }
179 }
180 };
181
182 if (shouldEmit) {
183 options.methods[sendProp] = function (newValue) {
184 this[localProp] = newValue;
185 };
186 }
187 } else if (shouldListen) {
188 options.watch[prop] = {
189 immediate: true,
190 handler: function handler(newValue, oldValue) {
191 if (newValue === oldValue) return;
192
193 if (onReceive) {
194 onReceive.call(this, newValue, oldValue);
195 }
196
197 if (onChange) {
198 onChange.call(this, newValue, oldValue);
199 }
200 }
201 };
202 }
203 };
204
205 for (var _i = 0; _i < _arr.length; _i++) {
206 _loop();
207 }
208 },
209 data: function data() {
210 if (this.__MessengerData__) return;
211 this.__MessengerData__ = true;
212 return this.$options.localDataKeys.reduce(function (data, key) {
213 data[key] = null;
214 return data;
215 }, {});
216 }
217};
218
219export default index;