UNPKG

9.96 kBJavaScriptView Raw
1/**
2 * vue-class-component v7.2.2
3 * (c) 2015-present Evan You
4 * @license MIT
5 */
6import Vue from 'vue';
7
8function _typeof(obj) {
9 if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
10 _typeof = function (obj) {
11 return typeof obj;
12 };
13 } else {
14 _typeof = function (obj) {
15 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
16 };
17 }
18
19 return _typeof(obj);
20}
21
22function _defineProperty(obj, key, value) {
23 if (key in obj) {
24 Object.defineProperty(obj, key, {
25 value: value,
26 enumerable: true,
27 configurable: true,
28 writable: true
29 });
30 } else {
31 obj[key] = value;
32 }
33
34 return obj;
35}
36
37function _toConsumableArray(arr) {
38 return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
39}
40
41function _arrayWithoutHoles(arr) {
42 if (Array.isArray(arr)) {
43 for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
44
45 return arr2;
46 }
47}
48
49function _iterableToArray(iter) {
50 if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
51}
52
53function _nonIterableSpread() {
54 throw new TypeError("Invalid attempt to spread non-iterable instance");
55}
56
57// The rational behind the verbose Reflect-feature check below is the fact that there are polyfills
58// which add an implementation for Reflect.defineMetadata but not for Reflect.getOwnMetadataKeys.
59// Without this check consumers will encounter hard to track down runtime errors.
60function reflectionIsSupported() {
61 return typeof Reflect !== 'undefined' && Reflect.defineMetadata && Reflect.getOwnMetadataKeys;
62}
63function copyReflectionMetadata(to, from) {
64 forwardMetadata(to, from);
65 Object.getOwnPropertyNames(from.prototype).forEach(function (key) {
66 forwardMetadata(to.prototype, from.prototype, key);
67 });
68 Object.getOwnPropertyNames(from).forEach(function (key) {
69 forwardMetadata(to, from, key);
70 });
71}
72
73function forwardMetadata(to, from, propertyKey) {
74 var metaKeys = propertyKey ? Reflect.getOwnMetadataKeys(from, propertyKey) : Reflect.getOwnMetadataKeys(from);
75 metaKeys.forEach(function (metaKey) {
76 var metadata = propertyKey ? Reflect.getOwnMetadata(metaKey, from, propertyKey) : Reflect.getOwnMetadata(metaKey, from);
77
78 if (propertyKey) {
79 Reflect.defineMetadata(metaKey, metadata, to, propertyKey);
80 } else {
81 Reflect.defineMetadata(metaKey, metadata, to);
82 }
83 });
84}
85
86var fakeArray = {
87 __proto__: []
88};
89var hasProto = fakeArray instanceof Array;
90function createDecorator(factory) {
91 return function (target, key, index) {
92 var Ctor = typeof target === 'function' ? target : target.constructor;
93
94 if (!Ctor.__decorators__) {
95 Ctor.__decorators__ = [];
96 }
97
98 if (typeof index !== 'number') {
99 index = undefined;
100 }
101
102 Ctor.__decorators__.push(function (options) {
103 return factory(options, key, index);
104 });
105 };
106}
107function mixins() {
108 for (var _len = arguments.length, Ctors = new Array(_len), _key = 0; _key < _len; _key++) {
109 Ctors[_key] = arguments[_key];
110 }
111
112 return Vue.extend({
113 mixins: Ctors
114 });
115}
116function isPrimitive(value) {
117 var type = _typeof(value);
118
119 return value == null || type !== 'object' && type !== 'function';
120}
121function warn(message) {
122 if (typeof console !== 'undefined') {
123 console.warn('[vue-class-component] ' + message);
124 }
125}
126
127function collectDataFromConstructor(vm, Component) {
128 // override _init to prevent to init as Vue instance
129 var originalInit = Component.prototype._init;
130
131 Component.prototype._init = function () {
132 var _this = this;
133
134 // proxy to actual vm
135 var keys = Object.getOwnPropertyNames(vm); // 2.2.0 compat (props are no longer exposed as self properties)
136
137 if (vm.$options.props) {
138 for (var key in vm.$options.props) {
139 if (!vm.hasOwnProperty(key)) {
140 keys.push(key);
141 }
142 }
143 }
144
145 keys.forEach(function (key) {
146 if (key.charAt(0) !== '_') {
147 Object.defineProperty(_this, key, {
148 get: function get() {
149 return vm[key];
150 },
151 set: function set(value) {
152 vm[key] = value;
153 },
154 configurable: true
155 });
156 }
157 });
158 }; // should be acquired class property values
159
160
161 var data = new Component(); // restore original _init to avoid memory leak (#209)
162
163 Component.prototype._init = originalInit; // create plain data object
164
165 var plainData = {};
166 Object.keys(data).forEach(function (key) {
167 if (data[key] !== undefined) {
168 plainData[key] = data[key];
169 }
170 });
171
172 if (process.env.NODE_ENV !== 'production') {
173 if (!(Component.prototype instanceof Vue) && Object.keys(plainData).length > 0) {
174 warn('Component class must inherit Vue or its descendant class ' + 'when class property is used.');
175 }
176 }
177
178 return plainData;
179}
180
181var $internalHooks = ['data', 'beforeCreate', 'created', 'beforeMount', 'mounted', 'beforeDestroy', 'destroyed', 'beforeUpdate', 'updated', 'activated', 'deactivated', 'render', 'errorCaptured', 'serverPrefetch' // 2.6
182];
183function componentFactory(Component) {
184 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
185 options.name = options.name || Component._componentTag || Component.name; // prototype props.
186
187 var proto = Component.prototype;
188 Object.getOwnPropertyNames(proto).forEach(function (key) {
189 if (key === 'constructor') {
190 return;
191 } // hooks
192
193
194 if ($internalHooks.indexOf(key) > -1) {
195 options[key] = proto[key];
196 return;
197 }
198
199 var descriptor = Object.getOwnPropertyDescriptor(proto, key);
200
201 if (descriptor.value !== void 0) {
202 // methods
203 if (typeof descriptor.value === 'function') {
204 (options.methods || (options.methods = {}))[key] = descriptor.value;
205 } else {
206 // typescript decorated data
207 (options.mixins || (options.mixins = [])).push({
208 data: function data() {
209 return _defineProperty({}, key, descriptor.value);
210 }
211 });
212 }
213 } else if (descriptor.get || descriptor.set) {
214 // computed properties
215 (options.computed || (options.computed = {}))[key] = {
216 get: descriptor.get,
217 set: descriptor.set
218 };
219 }
220 });
221 (options.mixins || (options.mixins = [])).push({
222 data: function data() {
223 return collectDataFromConstructor(this, Component);
224 }
225 }); // decorate options
226
227 var decorators = Component.__decorators__;
228
229 if (decorators) {
230 decorators.forEach(function (fn) {
231 return fn(options);
232 });
233 delete Component.__decorators__;
234 } // find super
235
236
237 var superProto = Object.getPrototypeOf(Component.prototype);
238 var Super = superProto instanceof Vue ? superProto.constructor : Vue;
239 var Extended = Super.extend(options);
240 forwardStaticMembers(Extended, Component, Super);
241
242 if (reflectionIsSupported()) {
243 copyReflectionMetadata(Extended, Component);
244 }
245
246 return Extended;
247}
248var reservedPropertyNames = [// Unique id
249'cid', // Super Vue constructor
250'super', // Component options that will be used by the component
251'options', 'superOptions', 'extendOptions', 'sealedOptions', // Private assets
252'component', 'directive', 'filter'];
253var shouldIgnore = {
254 prototype: true,
255 arguments: true,
256 callee: true,
257 caller: true
258};
259
260function forwardStaticMembers(Extended, Original, Super) {
261 // We have to use getOwnPropertyNames since Babel registers methods as non-enumerable
262 Object.getOwnPropertyNames(Original).forEach(function (key) {
263 // Skip the properties that should not be overwritten
264 if (shouldIgnore[key]) {
265 return;
266 } // Some browsers does not allow reconfigure built-in properties
267
268
269 var extendedDescriptor = Object.getOwnPropertyDescriptor(Extended, key);
270
271 if (extendedDescriptor && !extendedDescriptor.configurable) {
272 return;
273 }
274
275 var descriptor = Object.getOwnPropertyDescriptor(Original, key); // If the user agent does not support `__proto__` or its family (IE <= 10),
276 // the sub class properties may be inherited properties from the super class in TypeScript.
277 // We need to exclude such properties to prevent to overwrite
278 // the component options object which stored on the extended constructor (See #192).
279 // If the value is a referenced value (object or function),
280 // we can check equality of them and exclude it if they have the same reference.
281 // If it is a primitive value, it will be forwarded for safety.
282
283 if (!hasProto) {
284 // Only `cid` is explicitly exluded from property forwarding
285 // because we cannot detect whether it is a inherited property or not
286 // on the no `__proto__` environment even though the property is reserved.
287 if (key === 'cid') {
288 return;
289 }
290
291 var superDescriptor = Object.getOwnPropertyDescriptor(Super, key);
292
293 if (!isPrimitive(descriptor.value) && superDescriptor && superDescriptor.value === descriptor.value) {
294 return;
295 }
296 } // Warn if the users manually declare reserved properties
297
298
299 if (process.env.NODE_ENV !== 'production' && reservedPropertyNames.indexOf(key) >= 0) {
300 warn("Static property name '".concat(key, "' declared on class '").concat(Original.name, "' ") + 'conflicts with reserved property name of Vue internal. ' + 'It may cause unexpected behavior of the component. Consider renaming the property.');
301 }
302
303 Object.defineProperty(Extended, key, descriptor);
304 });
305}
306
307function Component(options) {
308 if (typeof options === 'function') {
309 return componentFactory(options);
310 }
311
312 return function (Component) {
313 return componentFactory(Component, options);
314 };
315}
316
317Component.registerHooks = function registerHooks(keys) {
318 $internalHooks.push.apply($internalHooks, _toConsumableArray(keys));
319};
320
321export default Component;
322export { createDecorator, mixins };