123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722 |
- (function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
- typeof define === 'function' && define.amd ? define(factory) :
- (global.Vuex = factory());
- }(this, (function () { 'use strict';
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
- return typeof obj;
- } : function (obj) {
- return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
- };
- var classCallCheck = function (instance, Constructor) {
- if (!(instance instanceof Constructor)) {
- throw new TypeError("Cannot call a class as a function");
- }
- };
- var createClass = function () {
- function defineProperties(target, props) {
- for (var i = 0; i < props.length; i++) {
- var descriptor = props[i];
- descriptor.enumerable = descriptor.enumerable || false;
- descriptor.configurable = true;
- if ("value" in descriptor) descriptor.writable = true;
- Object.defineProperty(target, descriptor.key, descriptor);
- }
- }
- return function (Constructor, protoProps, staticProps) {
- if (protoProps) defineProperties(Constructor.prototype, protoProps);
- if (staticProps) defineProperties(Constructor, staticProps);
- return Constructor;
- };
- }();
- var toConsumableArray = function (arr) {
- if (Array.isArray(arr)) {
- for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
- return arr2;
- } else {
- return Array.from(arr);
- }
- };
- function mergeObjects(arr) {
- return arr.reduce(function (prev, obj) {
- Object.keys(obj).forEach(function (key) {
- var existing = prev[key];
- if (existing) {
-
-
- if (Array.isArray(existing)) {
- prev[key] = existing.concat(obj[key]);
- } else {
- prev[key] = [existing].concat(obj[key]);
- }
- } else {
- prev[key] = obj[key];
- }
- });
- return prev;
- }, {});
- }
- function isObject(obj) {
- return obj !== null && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object';
- }
- function getNestedState(state, nestedKeys) {
- return nestedKeys.reduce(function (state, key) {
- return state[key];
- }, state);
- }
- var Watcher = void 0;
- function getWatcher(vm) {
- if (!Watcher) {
- var noop = function noop() {};
- var unwatch = vm.$watch(noop, noop);
- Watcher = vm._watchers[0].constructor;
- unwatch();
- }
- return Watcher;
- }
- var Dep = void 0;
- function getDep(vm) {
- if (!Dep) {
- Dep = vm._data.__ob__.dep.constructor;
- }
- return Dep;
- }
- var hook = typeof window !== 'undefined' && window.__VUE_DEVTOOLS_GLOBAL_HOOK__;
- function devtoolPlugin(store) {
- if (!hook) return;
- hook.emit('vuex:init', store);
- hook.on('vuex:travel-to-state', function (targetState) {
- store.replaceState(targetState);
- });
- store.subscribe(function (mutation, state) {
- hook.emit('vuex:mutation', mutation, state);
- });
- }
- var override = function (Vue) {
- var version = Number(Vue.version.split('.')[0]);
- if (version >= 2) {
- var usesInit = Vue.config._lifecycleHooks.indexOf('init') > -1;
- Vue.mixin(usesInit ? { init: vuexInit } : { beforeCreate: vuexInit });
- } else {
- (function () {
-
-
- var _init = Vue.prototype._init;
- Vue.prototype._init = function () {
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- options.init = options.init ? [vuexInit].concat(options.init) : vuexInit;
- _init.call(this, options);
- };
- })();
- }
-
- function vuexInit() {
- var options = this.$options;
- var store = options.store,
- vuex = options.vuex;
-
- if (store) {
- this.$store = store;
- } else if (options.parent && options.parent.$store) {
- this.$store = options.parent.$store;
- }
-
- if (vuex) {
- if (!this.$store) {
- console.warn('[vuex] store not injected. make sure to ' + 'provide the store option in your root component.');
- }
- var state = vuex.state,
- actions = vuex.actions;
- var getters = vuex.getters;
-
- if (state && !getters) {
- console.warn('[vuex] vuex.state option will been deprecated in 1.0. ' + 'Use vuex.getters instead.');
- getters = state;
- }
-
- if (getters) {
- options.computed = options.computed || {};
- for (var key in getters) {
- defineVuexGetter(this, key, getters[key]);
- }
- }
-
- if (actions) {
- options.methods = options.methods || {};
- for (var _key in actions) {
- options.methods[_key] = makeBoundAction(this.$store, actions[_key], _key);
- }
- }
- }
- }
-
- function setter() {
- throw new Error('vuex getter properties are read-only.');
- }
-
- function defineVuexGetter(vm, key, getter) {
- if (typeof getter !== 'function') {
- console.warn('[vuex] Getter bound to key \'vuex.getters.' + key + '\' is not a function.');
- } else {
- Object.defineProperty(vm, key, {
- enumerable: true,
- configurable: true,
- get: makeComputedGetter(vm.$store, getter),
- set: setter
- });
- }
- }
-
- function makeComputedGetter(store, getter) {
- var id = store._getterCacheId;
-
- if (getter[id]) {
- return getter[id];
- }
- var vm = store._vm;
- var Watcher = getWatcher(vm);
- var Dep = getDep(vm);
- var watcher = new Watcher(vm, function (vm) {
- return getter(vm.state);
- }, null, { lazy: true });
- var computedGetter = function computedGetter() {
- if (watcher.dirty) {
- watcher.evaluate();
- }
- if (Dep.target) {
- watcher.depend();
- }
- return watcher.value;
- };
- getter[id] = computedGetter;
- return computedGetter;
- }
-
- function makeBoundAction(store, action, key) {
- if (typeof action !== 'function') {
- console.warn('[vuex] Action bound to key \'vuex.actions.' + key + '\' is not a function.');
- }
- return function vuexBoundAction() {
- for (var _len = arguments.length, args = Array(_len), _key2 = 0; _key2 < _len; _key2++) {
- args[_key2] = arguments[_key2];
- }
- return action.call.apply(action, [this, store].concat(args));
- };
- }
-
- var merge = Vue.config.optionMergeStrategies.computed;
- Vue.config.optionMergeStrategies.vuex = function (toVal, fromVal) {
- if (!toVal) return fromVal;
- if (!fromVal) return toVal;
- return {
- getters: merge(toVal.getters, fromVal.getters),
- state: merge(toVal.state, fromVal.state),
- actions: merge(toVal.actions, fromVal.actions)
- };
- };
- };
- var Vue = void 0;
- var uid = 0;
- var Store = function () {
-
- function Store() {
- var _this = this;
- var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
- _ref$state = _ref.state,
- state = _ref$state === undefined ? {} : _ref$state,
- _ref$mutations = _ref.mutations,
- mutations = _ref$mutations === undefined ? {} : _ref$mutations,
- _ref$modules = _ref.modules,
- modules = _ref$modules === undefined ? {} : _ref$modules,
- _ref$plugins = _ref.plugins,
- plugins = _ref$plugins === undefined ? [] : _ref$plugins,
- _ref$strict = _ref.strict,
- strict = _ref$strict === undefined ? false : _ref$strict;
- classCallCheck(this, Store);
- this._getterCacheId = 'vuex_store_' + uid++;
- this._dispatching = false;
- this._rootMutations = this._mutations = mutations;
- this._modules = modules;
- this._subscribers = [];
-
- var dispatch = this.dispatch;
- this.dispatch = function () {
- for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
- dispatch.apply(_this, args);
- };
-
-
-
- if (!Vue) {
- throw new Error('[vuex] must call Vue.use(Vuex) before creating a store instance.');
- }
- var silent = Vue.config.silent;
- Vue.config.silent = true;
- this._vm = new Vue({
- data: {
- state: state
- }
- });
- Vue.config.silent = silent;
- this._setupModuleState(state, modules);
- this._setupModuleMutations(modules);
-
- if (strict) {
- this._setupMutationCheck();
- }
-
- devtoolPlugin(this);
- plugins.forEach(function (plugin) {
- return plugin(_this);
- });
- }
-
- createClass(Store, [{
- key: 'replaceState',
-
- value: function replaceState(state) {
- this._dispatching = true;
- this._vm.state = state;
- this._dispatching = false;
- }
-
- }, {
- key: 'dispatch',
- value: function dispatch(type) {
- var _this2 = this;
- for (var _len2 = arguments.length, payload = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
- payload[_key2 - 1] = arguments[_key2];
- }
- var silent = false;
- var isObjectStyleDispatch = false;
-
- if ((typeof type === 'undefined' ? 'undefined' : _typeof(type)) === 'object' && type.type && arguments.length === 1) {
- isObjectStyleDispatch = true;
- payload = type;
- if (type.silent) silent = true;
- type = type.type;
- }
- var handler = this._mutations[type];
- var state = this.state;
- if (handler) {
- this._dispatching = true;
-
- if (Array.isArray(handler)) {
- handler.forEach(function (h) {
- isObjectStyleDispatch ? h(state, payload) : h.apply(undefined, [state].concat(toConsumableArray(payload)));
- });
- } else {
- isObjectStyleDispatch ? handler(state, payload) : handler.apply(undefined, [state].concat(toConsumableArray(payload)));
- }
- this._dispatching = false;
- if (!silent) {
- (function () {
- var mutation = isObjectStyleDispatch ? payload : { type: type, payload: payload };
- _this2._subscribers.forEach(function (sub) {
- return sub(mutation, state);
- });
- })();
- }
- } else {
- console.warn('[vuex] Unknown mutation: ' + type);
- }
- }
-
- }, {
- key: 'watch',
- value: function watch(fn, cb, options) {
- var _this3 = this;
- if (typeof fn !== 'function') {
- console.error('Vuex store.watch only accepts function.');
- return;
- }
- return this._vm.$watch(function () {
- return fn(_this3.state);
- }, cb, options);
- }
-
- }, {
- key: 'subscribe',
- value: function subscribe(fn) {
- var subs = this._subscribers;
- if (subs.indexOf(fn) < 0) {
- subs.push(fn);
- }
- return function () {
- var i = subs.indexOf(fn);
- if (i > -1) {
- subs.splice(i, 1);
- }
- };
- }
-
- }, {
- key: 'hotUpdate',
- value: function hotUpdate() {
- var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
- mutations = _ref2.mutations,
- modules = _ref2.modules;
- this._rootMutations = this._mutations = mutations || this._rootMutations;
- this._setupModuleMutations(modules || this._modules);
- }
-
- }, {
- key: '_setupModuleState',
- value: function _setupModuleState(state, modules) {
- var _this4 = this;
- if (!isObject(modules)) return;
- Object.keys(modules).forEach(function (key) {
- var module = modules[key];
-
- Vue.set(state, key, module.state || {});
-
- _this4._setupModuleState(state[key], module.modules);
- });
- }
-
- }, {
- key: '_setupModuleMutations',
- value: function _setupModuleMutations(updatedModules) {
- var modules = this._modules;
- Object.keys(updatedModules).forEach(function (key) {
- modules[key] = updatedModules[key];
- });
- var updatedMutations = this._createModuleMutations(modules, []);
- this._mutations = mergeObjects([this._rootMutations].concat(toConsumableArray(updatedMutations)));
- }
-
- }, {
- key: '_createModuleMutations',
- value: function _createModuleMutations(modules, nestedKeys) {
- var _this5 = this;
- if (!isObject(modules)) return [];
- return Object.keys(modules).map(function (key) {
- var module = modules[key];
- var newNestedKeys = nestedKeys.concat(key);
-
- var nestedMutations = _this5._createModuleMutations(module.modules, newNestedKeys);
- if (!module || !module.mutations) {
- return mergeObjects(nestedMutations);
- }
-
- var mutations = {};
- Object.keys(module.mutations).forEach(function (name) {
- var original = module.mutations[name];
- mutations[name] = function (state) {
- for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
- args[_key3 - 1] = arguments[_key3];
- }
- original.apply(undefined, [getNestedState(state, newNestedKeys)].concat(args));
- };
- });
-
- return mergeObjects([mutations].concat(toConsumableArray(nestedMutations)));
- });
- }
-
- }, {
- key: '_setupMutationCheck',
- value: function _setupMutationCheck() {
- var _this6 = this;
- var Watcher = getWatcher(this._vm);
-
- new Watcher(this._vm, 'state', function () {
- if (!_this6._dispatching) {
- throw new Error('[vuex] Do not mutate vuex store state outside mutation handlers.');
- }
- }, { deep: true, sync: true });
-
- }
- }, {
- key: 'state',
- get: function get$$1() {
- return this._vm.state;
- },
- set: function set$$1(v) {
- throw new Error('[vuex] Use store.replaceState() to explicit replace store state.');
- }
- }]);
- return Store;
- }();
- function install(_Vue) {
- if (Vue) {
- console.warn('[vuex] already installed. Vue.use(Vuex) should be called only once.');
- return;
- }
- Vue = _Vue;
- override(Vue);
- }
- if (typeof window !== 'undefined' && window.Vue) {
- install(window.Vue);
- }
- var index = {
- Store: Store,
- install: install
- };
- return index;
- })));
|