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