UNPKG

vue-property-decorator

Version:
147 lines (141 loc) 5.23 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vue'), require('vue-class-component')) : typeof define === 'function' && define.amd ? define(['exports', 'vue', 'vue-class-component'], factory) : (factory((global['vue-property-decorator'] = {}),global.Vue,global.VueClassComponent)); }(this, (function (exports,vue,vueClassComponent) { 'use strict'; vue = vue && vue.hasOwnProperty('default') ? vue['default'] : vue; var vueClassComponent__default = 'default' in vueClassComponent ? vueClassComponent['default'] : vueClassComponent; /** vue-property-decorator verson 7.3.0 MIT LICENSE copyright 2018 kaorun343 */ 'use strict'; /** * decorator of an inject * @param from key * @return PropertyDecorator */ function Inject(options) { return vueClassComponent.createDecorator(function (componentOptions, key) { if (typeof componentOptions.inject === 'undefined') { componentOptions.inject = {}; } if (!Array.isArray(componentOptions.inject)) { componentOptions.inject[key] = options || key; } }); } /** * decorator of a provide * @param key key * @return PropertyDecorator | void */ function Provide(key) { return vueClassComponent.createDecorator(function (componentOptions, k) { var provide = componentOptions.provide; if (typeof provide !== 'function' || !provide.managed) { var original_1 = componentOptions.provide; provide = componentOptions.provide = function () { var rv = Object.create((typeof original_1 === 'function' ? original_1.call(this) : original_1) || null); for (var i in provide.managed) rv[provide.managed[i]] = this[i]; return rv; }; provide.managed = {}; } provide.managed[k] = key || k; }); } /** * decorator of model * @param event event name * @param options options * @return PropertyDecorator */ function Model(event, options) { if (options === void 0) { options = {}; } return vueClassComponent.createDecorator(function (componentOptions, k) { (componentOptions.props || (componentOptions.props = {}))[k] = options; componentOptions.model = { prop: k, event: event || k }; }); } /** * decorator of a prop * @param options the options for the prop * @return PropertyDecorator | void */ function Prop(options) { if (options === void 0) { options = {}; } return vueClassComponent.createDecorator(function (componentOptions, k) { (componentOptions.props || (componentOptions.props = {}))[k] = options; }); } /** * decorator of a watch function * @param path the path or the expression to observe * @param WatchOption * @return MethodDecorator */ function Watch(path, options) { if (options === void 0) { options = {}; } var _a = options.deep, deep = _a === void 0 ? false : _a, _b = options.immediate, immediate = _b === void 0 ? false : _b; return vueClassComponent.createDecorator(function (componentOptions, handler) { if (typeof componentOptions.watch !== 'object') { componentOptions.watch = Object.create(null); } var watch = componentOptions.watch; if (typeof watch[path] === 'object' && !Array.isArray(watch[path])) { watch[path] = [watch[path]]; } else if (typeof watch[path] === 'undefined') { watch[path] = []; } watch[path].push({ handler: handler, deep: deep, immediate: immediate }); }); } // Code copied from Vue/src/shared/util.js var hyphenateRE = /\B([A-Z])/g; var hyphenate = function (str) { return str.replace(hyphenateRE, '-$1').toLowerCase(); }; /** * decorator of an event-emitter function * @param event The name of the event * @return MethodDecorator */ function Emit(event) { return function (_target, key, descriptor) { key = hyphenate(key); var original = descriptor.value; descriptor.value = function emitter() { var _this = this; var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var emit = function (returnValue) { if (returnValue !== undefined) args.unshift(returnValue); _this.$emit.apply(_this, [event || key].concat(args)); }; var returnValue = original.apply(this, args); if (isPromise(returnValue)) { returnValue.then(function (returnValue) { emit(returnValue); }); } else { emit(returnValue); } }; }; } function isPromise(obj) { return obj instanceof Promise || (obj && typeof obj.then === 'function'); } exports.Component = vueClassComponent__default; exports.Vue = vue; exports.Mixins = vueClassComponent.mixins; exports.Inject = Inject; exports.Provide = Provide; exports.Model = Model; exports.Prop = Prop; exports.Watch = Watch; exports.Emit = Emit; Object.defineProperty(exports, '__esModule', { value: true }); })));