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