UNPKG

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