UNPKG

14.9 kBJavaScriptView Raw
1/**
2 * vue-class-component v8.0.0-beta.4
3 * (c) 2015-present Evan You
4 * @license MIT
5 */
6var VueClassComponent = (function (exports, vue) {
7 'use strict';
8
9 function _classCallCheck(instance, Constructor) {
10 if (!(instance instanceof Constructor)) {
11 throw new TypeError("Cannot call a class as a function");
12 }
13 }
14
15 function _defineProperties(target, props) {
16 for (var i = 0; i < props.length; i++) {
17 var descriptor = props[i];
18 descriptor.enumerable = descriptor.enumerable || false;
19 descriptor.configurable = true;
20 if ("value" in descriptor) descriptor.writable = true;
21 Object.defineProperty(target, descriptor.key, descriptor);
22 }
23 }
24
25 function _createClass(Constructor, protoProps, staticProps) {
26 if (protoProps) _defineProperties(Constructor.prototype, protoProps);
27 if (staticProps) _defineProperties(Constructor, staticProps);
28 return Constructor;
29 }
30
31 function _defineProperty(obj, key, value) {
32 if (key in obj) {
33 Object.defineProperty(obj, key, {
34 value: value,
35 enumerable: true,
36 configurable: true,
37 writable: true
38 });
39 } else {
40 obj[key] = value;
41 }
42
43 return obj;
44 }
45
46 function ownKeys(object, enumerableOnly) {
47 var keys = Object.keys(object);
48
49 if (Object.getOwnPropertySymbols) {
50 var symbols = Object.getOwnPropertySymbols(object);
51 if (enumerableOnly) symbols = symbols.filter(function (sym) {
52 return Object.getOwnPropertyDescriptor(object, sym).enumerable;
53 });
54 keys.push.apply(keys, symbols);
55 }
56
57 return keys;
58 }
59
60 function _objectSpread2(target) {
61 for (var i = 1; i < arguments.length; i++) {
62 var source = arguments[i] != null ? arguments[i] : {};
63
64 if (i % 2) {
65 ownKeys(Object(source), true).forEach(function (key) {
66 _defineProperty(target, key, source[key]);
67 });
68 } else if (Object.getOwnPropertyDescriptors) {
69 Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
70 } else {
71 ownKeys(Object(source)).forEach(function (key) {
72 Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
73 });
74 }
75 }
76
77 return target;
78 }
79
80 function _inherits(subClass, superClass) {
81 if (typeof superClass !== "function" && superClass !== null) {
82 throw new TypeError("Super expression must either be null or a function");
83 }
84
85 subClass.prototype = Object.create(superClass && superClass.prototype, {
86 constructor: {
87 value: subClass,
88 writable: true,
89 configurable: true
90 }
91 });
92 if (superClass) _setPrototypeOf(subClass, superClass);
93 }
94
95 function _getPrototypeOf(o) {
96 _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
97 return o.__proto__ || Object.getPrototypeOf(o);
98 };
99 return _getPrototypeOf(o);
100 }
101
102 function _setPrototypeOf(o, p) {
103 _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
104 o.__proto__ = p;
105 return o;
106 };
107
108 return _setPrototypeOf(o, p);
109 }
110
111 function _isNativeReflectConstruct() {
112 if (typeof Reflect === "undefined" || !Reflect.construct) return false;
113 if (Reflect.construct.sham) return false;
114 if (typeof Proxy === "function") return true;
115
116 try {
117 Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
118 return true;
119 } catch (e) {
120 return false;
121 }
122 }
123
124 function _construct(Parent, args, Class) {
125 if (_isNativeReflectConstruct()) {
126 _construct = Reflect.construct;
127 } else {
128 _construct = function _construct(Parent, args, Class) {
129 var a = [null];
130 a.push.apply(a, args);
131 var Constructor = Function.bind.apply(Parent, a);
132 var instance = new Constructor();
133 if (Class) _setPrototypeOf(instance, Class.prototype);
134 return instance;
135 };
136 }
137
138 return _construct.apply(null, arguments);
139 }
140
141 function _assertThisInitialized(self) {
142 if (self === void 0) {
143 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
144 }
145
146 return self;
147 }
148
149 function _possibleConstructorReturn(self, call) {
150 if (call && (typeof call === "object" || typeof call === "function")) {
151 return call;
152 }
153
154 return _assertThisInitialized(self);
155 }
156
157 function _createSuper(Derived) {
158 var hasNativeReflectConstruct = _isNativeReflectConstruct();
159
160 return function _createSuperInternal() {
161 var Super = _getPrototypeOf(Derived),
162 result;
163
164 if (hasNativeReflectConstruct) {
165 var NewTarget = _getPrototypeOf(this).constructor;
166
167 result = Reflect.construct(Super, arguments, NewTarget);
168 } else {
169 result = Super.apply(this, arguments);
170 }
171
172 return _possibleConstructorReturn(this, result);
173 };
174 }
175
176 function _toConsumableArray(arr) {
177 return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
178 }
179
180 function _arrayWithoutHoles(arr) {
181 if (Array.isArray(arr)) return _arrayLikeToArray(arr);
182 }
183
184 function _iterableToArray(iter) {
185 if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
186 }
187
188 function _unsupportedIterableToArray(o, minLen) {
189 if (!o) return;
190 if (typeof o === "string") return _arrayLikeToArray(o, minLen);
191 var n = Object.prototype.toString.call(o).slice(8, -1);
192 if (n === "Object" && o.constructor) n = o.constructor.name;
193 if (n === "Map" || n === "Set") return Array.from(o);
194 if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
195 }
196
197 function _arrayLikeToArray(arr, len) {
198 if (len == null || len > arr.length) len = arr.length;
199
200 for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
201
202 return arr2;
203 }
204
205 function _nonIterableSpread() {
206 throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
207 }
208
209 function defineGetter(obj, key, getter) {
210 Object.defineProperty(obj, key, {
211 get: getter,
212 enumerable: false,
213 configurable: true
214 });
215 }
216
217 function defineProxy(proxy, key, target) {
218 Object.defineProperty(proxy, key, {
219 get: function get() {
220 return target[key].value;
221 },
222 set: function set(value) {
223 target[key].value = value;
224 },
225 enumerable: true,
226 configurable: true
227 });
228 }
229
230 function getSuper(Ctor) {
231 var superProto = Object.getPrototypeOf(Ctor.prototype);
232
233 if (!superProto) {
234 return undefined;
235 }
236
237 return superProto.constructor;
238 }
239
240 var VueImpl = /*#__PURE__*/function () {
241 function VueImpl(props, ctx) {
242 var _this = this;
243
244 _classCallCheck(this, VueImpl);
245
246 defineGetter(this, '$props', function () {
247 return props;
248 });
249 defineGetter(this, '$attrs', function () {
250 return ctx.attrs;
251 });
252 defineGetter(this, '$slots', function () {
253 return ctx.slots;
254 });
255 defineGetter(this, '$emit', function () {
256 return ctx.emit;
257 });
258 Object.keys(props).forEach(function (key) {
259 Object.defineProperty(_this, key, {
260 enumerable: false,
261 configurable: true,
262 writable: true,
263 value: props[key]
264 });
265 });
266 }
267 /** @internal */
268
269
270 _createClass(VueImpl, null, [{
271 key: "__vccExtend",
272 value: function __vccExtend(options) {
273 options.mixins = options.mixins || [];
274 options.mixins.push(this.__vccOpts);
275 }
276 /** @internal */
277
278 }, {
279 key: "registerHooks",
280 value: function registerHooks(keys) {
281 var _this$__vccHooks;
282
283 (_this$__vccHooks = this.__vccHooks).push.apply(_this$__vccHooks, _toConsumableArray(keys));
284 }
285 }, {
286 key: "props",
287 value: function props(Props) {
288 var propsMeta = new Props();
289 var props = {};
290 Object.keys(propsMeta).forEach(function (key) {
291 var meta = propsMeta[key];
292 props[key] = meta !== null && meta !== void 0 ? meta : null;
293 });
294
295 var PropsMixin = /*#__PURE__*/function (_this2) {
296 _inherits(PropsMixin, _this2);
297
298 var _super = _createSuper(PropsMixin);
299
300 function PropsMixin() {
301 _classCallCheck(this, PropsMixin);
302
303 return _super.apply(this, arguments);
304 }
305
306 _createClass(PropsMixin, null, [{
307 key: "__vccExtend",
308 value: function __vccExtend(options) {
309 options.props = props;
310 }
311 }]);
312
313 return PropsMixin;
314 }(this);
315
316 return PropsMixin;
317 }
318 }, {
319 key: "__vccOpts",
320 get: function get() {
321 // Early return if `this` is base class as it does not have any options
322 if (this === Vue) {
323 return {};
324 }
325
326 var cache = this.hasOwnProperty('__vccCache') && this.__vccCache;
327
328 if (cache) {
329 return cache;
330 }
331
332 var Ctor = this; // If the options are provided via decorator use it as a base
333
334 var options = this.__vccCache = this.hasOwnProperty('__vccBase') ? _objectSpread2({}, this.__vccBase) : {}; // Handle super class options
335
336 var Super = getSuper(Ctor);
337
338 if (Super) {
339 Super.__vccExtend(options);
340 }
341
342 options.methods = _objectSpread2({}, options.methods);
343 options.computed = _objectSpread2({}, options.computed);
344 var proto = Ctor.prototype;
345 Object.getOwnPropertyNames(proto).forEach(function (key) {
346 if (key === 'constructor') {
347 return;
348 } // hooks
349
350
351 if (Ctor.__vccHooks.indexOf(key) > -1) {
352 options[key] = proto[key];
353 return;
354 }
355
356 var descriptor = Object.getOwnPropertyDescriptor(proto, key); // methods
357
358 if (typeof descriptor.value === 'function') {
359 options.methods[key] = descriptor.value;
360 return;
361 } // computed properties
362
363
364 if (descriptor.get || descriptor.set) {
365 options.computed[key] = {
366 get: descriptor.get,
367 set: descriptor.set
368 };
369 return;
370 }
371 });
372
373 options.setup = function (props, ctx) {
374 var _promise;
375
376 var data = new Ctor(props, ctx);
377 var dataKeys = Object.keys(data);
378 var plainData = {};
379 var promise = null; // Initialize reactive data and convert constructor `this` to a proxy
380
381 dataKeys.forEach(function (key) {
382 // Skip if the value is undefined not to make it reactive.
383 // If the value has `__s`, it's a value from `setup` helper, proceed it later.
384 if (data[key] === undefined || data[key] && data[key].__s) {
385 return;
386 }
387
388 plainData[key] = vue.ref(data[key]);
389 defineProxy(data, key, plainData);
390 }); // Invoke composition functions
391
392 dataKeys.forEach(function (key) {
393 if (data[key] && data[key].__s) {
394 var setupState = data[key].__s();
395
396 if (setupState instanceof Promise) {
397 if (!promise) {
398 promise = Promise.resolve(plainData);
399 }
400
401 promise = promise.then(function () {
402 return setupState.then(function (value) {
403 plainData[key] = vue.proxyRefs(value);
404 return plainData;
405 });
406 });
407 } else {
408 plainData[key] = vue.proxyRefs(setupState);
409 }
410 }
411 });
412 return (_promise = promise) !== null && _promise !== void 0 ? _promise : plainData;
413 };
414
415 var decorators = this.hasOwnProperty('__vccDecorators') && this.__vccDecorators;
416
417 if (decorators) {
418 decorators.forEach(function (fn) {
419 return fn(options);
420 });
421 } // from Vue Loader
422
423
424 var injections = ['render', 'ssrRender', '__file', '__cssModules', '__scopeId', '__hmrId'];
425 injections.forEach(function (key) {
426 if (Ctor[key]) {
427 options[key] = Ctor[key];
428 }
429 });
430 return options;
431 }
432 }]);
433
434 return VueImpl;
435 }();
436 /** @internal */
437
438
439 VueImpl.__vccHooks = ['data', 'beforeCreate', 'created', 'beforeMount', 'mounted', 'beforeUnmount', 'unmounted', 'beforeUpdate', 'updated', 'activated', 'deactivated', 'render', 'errorCaptured', 'serverPrefetch'];
440 var Vue = VueImpl;
441
442 function Options(options) {
443 return function (Component) {
444 Component.__vccBase = options;
445 return Component;
446 };
447 }
448 function createDecorator(factory) {
449 return function (target, key, index) {
450 var Ctor = typeof target === 'function' ? target : target.constructor;
451
452 if (!Ctor.__vccDecorators) {
453 Ctor.__vccDecorators = [];
454 }
455
456 if (typeof index !== 'number') {
457 index = undefined;
458 }
459
460 Ctor.__vccDecorators.push(function (options) {
461 return factory(options, key, index);
462 });
463 };
464 }
465 function mixins() {
466 for (var _len = arguments.length, Ctors = new Array(_len), _key = 0; _key < _len; _key++) {
467 Ctors[_key] = arguments[_key];
468 }
469
470 return /*#__PURE__*/function (_Vue) {
471 _inherits(MixedVue, _Vue);
472
473 var _super = _createSuper(MixedVue);
474
475 _createClass(MixedVue, null, [{
476 key: "__vccExtend",
477 value: function __vccExtend(options) {
478 Ctors.forEach(function (Ctor) {
479 return Ctor.__vccExtend(options);
480 });
481 }
482 }]);
483
484 function MixedVue() {
485 var _this;
486
487 for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
488 args[_key2] = arguments[_key2];
489 }
490
491 _classCallCheck(this, MixedVue);
492
493 _this = _super.call.apply(_super, [this].concat(args));
494 Ctors.forEach(function (Ctor) {
495 var data = _construct(Ctor, args);
496
497 Object.keys(data).forEach(function (key) {
498 _this[key] = data[key];
499 });
500 });
501 return _this;
502 }
503
504 return MixedVue;
505 }(Vue);
506 }
507 function setup(setupFn) {
508 // Hack to delay the invocation of setup function.
509 // Will be called after dealing with class properties.
510 return {
511 __s: setupFn
512 };
513 }
514
515 // Actual implementation
516 function prop(options) {
517 return options;
518 }
519
520 exports.Options = Options;
521 exports.Vue = Vue;
522 exports.createDecorator = createDecorator;
523 exports.mixins = mixins;
524 exports.prop = prop;
525 exports.setup = setup;
526
527 return exports;
528
529}({}, Vue));