vuex.common.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /**
  2. * vuex v2.4.1
  3. * (c) 2017 Evan You
  4. * @license MIT
  5. */
  6. 'use strict';
  7. var applyMixin = function (Vue) {
  8. var version = Number(Vue.version.split('.')[0]);
  9. if (version >= 2) {
  10. Vue.mixin({ beforeCreate: vuexInit });
  11. } else {
  12. // override init and inject vuex init procedure
  13. // for 1.x backwards compatibility.
  14. var _init = Vue.prototype._init;
  15. Vue.prototype._init = function (options) {
  16. if ( options === void 0 ) options = {};
  17. options.init = options.init
  18. ? [vuexInit].concat(options.init)
  19. : vuexInit;
  20. _init.call(this, options);
  21. };
  22. }
  23. /**
  24. * Vuex init hook, injected into each instances init hooks list.
  25. */
  26. function vuexInit () {
  27. var options = this.$options;
  28. // store injection
  29. if (options.store) {
  30. this.$store = typeof options.store === 'function'
  31. ? options.store()
  32. : options.store;
  33. } else if (options.parent && options.parent.$store) {
  34. this.$store = options.parent.$store;
  35. }
  36. }
  37. };
  38. var devtoolHook =
  39. typeof window !== 'undefined' &&
  40. window.__VUE_DEVTOOLS_GLOBAL_HOOK__;
  41. function devtoolPlugin (store) {
  42. if (!devtoolHook) { return }
  43. store._devtoolHook = devtoolHook;
  44. devtoolHook.emit('vuex:init', store);
  45. devtoolHook.on('vuex:travel-to-state', function (targetState) {
  46. store.replaceState(targetState);
  47. });
  48. store.subscribe(function (mutation, state) {
  49. devtoolHook.emit('vuex:mutation', mutation, state);
  50. });
  51. }
  52. /**
  53. * Get the first item that pass the test
  54. * by second argument function
  55. *
  56. * @param {Array} list
  57. * @param {Function} f
  58. * @return {*}
  59. */
  60. /**
  61. * Deep copy the given object considering circular structure.
  62. * This function caches all nested objects and its copies.
  63. * If it detects circular structure, use cached copy to avoid infinite loop.
  64. *
  65. * @param {*} obj
  66. * @param {Array<Object>} cache
  67. * @return {*}
  68. */
  69. /**
  70. * forEach for object
  71. */
  72. function forEachValue (obj, fn) {
  73. Object.keys(obj).forEach(function (key) { return fn(obj[key], key); });
  74. }
  75. function isObject (obj) {
  76. return obj !== null && typeof obj === 'object'
  77. }
  78. function isPromise (val) {
  79. return val && typeof val.then === 'function'
  80. }
  81. function assert (condition, msg) {
  82. if (!condition) { throw new Error(("[vuex] " + msg)) }
  83. }
  84. var Module = function Module (rawModule, runtime) {
  85. this.runtime = runtime;
  86. this._children = Object.create(null);
  87. this._rawModule = rawModule;
  88. var rawState = rawModule.state;
  89. this.state = (typeof rawState === 'function' ? rawState() : rawState) || {};
  90. };
  91. var prototypeAccessors$1 = { namespaced: { configurable: true } };
  92. prototypeAccessors$1.namespaced.get = function () {
  93. return !!this._rawModule.namespaced
  94. };
  95. Module.prototype.addChild = function addChild (key, module) {
  96. this._children[key] = module;
  97. };
  98. Module.prototype.removeChild = function removeChild (key) {
  99. delete this._children[key];
  100. };
  101. Module.prototype.getChild = function getChild (key) {
  102. return this._children[key]
  103. };
  104. Module.prototype.update = function update (rawModule) {
  105. this._rawModule.namespaced = rawModule.namespaced;
  106. if (rawModule.actions) {
  107. this._rawModule.actions = rawModule.actions;
  108. }
  109. if (rawModule.mutations) {
  110. this._rawModule.mutations = rawModule.mutations;
  111. }
  112. if (rawModule.getters) {
  113. this._rawModule.getters = rawModule.getters;
  114. }
  115. };
  116. Module.prototype.forEachChild = function forEachChild (fn) {
  117. forEachValue(this._children, fn);
  118. };
  119. Module.prototype.forEachGetter = function forEachGetter (fn) {
  120. if (this._rawModule.getters) {
  121. forEachValue(this._rawModule.getters, fn);
  122. }
  123. };
  124. Module.prototype.forEachAction = function forEachAction (fn) {
  125. if (this._rawModule.actions) {
  126. forEachValue(this._rawModule.actions, fn);
  127. }
  128. };
  129. Module.prototype.forEachMutation = function forEachMutation (fn) {
  130. if (this._rawModule.mutations) {
  131. forEachValue(this._rawModule.mutations, fn);
  132. }
  133. };
  134. Object.defineProperties( Module.prototype, prototypeAccessors$1 );
  135. var ModuleCollection = function ModuleCollection (rawRootModule) {
  136. // register root module (Vuex.Store options)
  137. this.register([], rawRootModule, false);
  138. };
  139. ModuleCollection.prototype.get = function get (path) {
  140. return path.reduce(function (module, key) {
  141. return module.getChild(key)
  142. }, this.root)
  143. };
  144. ModuleCollection.prototype.getNamespace = function getNamespace (path) {
  145. var module = this.root;
  146. return path.reduce(function (namespace, key) {
  147. module = module.getChild(key);
  148. return namespace + (module.namespaced ? key + '/' : '')
  149. }, '')
  150. };
  151. ModuleCollection.prototype.update = function update$1 (rawRootModule) {
  152. update([], this.root, rawRootModule);
  153. };
  154. ModuleCollection.prototype.register = function register (path, rawModule, runtime) {
  155. var this$1 = this;
  156. if ( runtime === void 0 ) runtime = true;
  157. if (process.env.NODE_ENV !== 'production') {
  158. assertRawModule(path, rawModule);
  159. }
  160. var newModule = new Module(rawModule, runtime);
  161. if (path.length === 0) {
  162. this.root = newModule;
  163. } else {
  164. var parent = this.get(path.slice(0, -1));
  165. parent.addChild(path[path.length - 1], newModule);
  166. }
  167. // register nested modules
  168. if (rawModule.modules) {
  169. forEachValue(rawModule.modules, function (rawChildModule, key) {
  170. this$1.register(path.concat(key), rawChildModule, runtime);
  171. });
  172. }
  173. };
  174. ModuleCollection.prototype.unregister = function unregister (path) {
  175. var parent = this.get(path.slice(0, -1));
  176. var key = path[path.length - 1];
  177. if (!parent.getChild(key).runtime) { return }
  178. parent.removeChild(key);
  179. };
  180. function update (path, targetModule, newModule) {
  181. if (process.env.NODE_ENV !== 'production') {
  182. assertRawModule(path, newModule);
  183. }
  184. // update target module
  185. targetModule.update(newModule);
  186. // update nested modules
  187. if (newModule.modules) {
  188. for (var key in newModule.modules) {
  189. if (!targetModule.getChild(key)) {
  190. if (process.env.NODE_ENV !== 'production') {
  191. console.warn(
  192. "[vuex] trying to add a new module '" + key + "' on hot reloading, " +
  193. 'manual reload is needed'
  194. );
  195. }
  196. return
  197. }
  198. update(
  199. path.concat(key),
  200. targetModule.getChild(key),
  201. newModule.modules[key]
  202. );
  203. }
  204. }
  205. }
  206. function assertRawModule (path, rawModule) {
  207. ['getters', 'actions', 'mutations'].forEach(function (key) {
  208. if (!rawModule[key]) { return }
  209. forEachValue(rawModule[key], function (value, type) {
  210. assert(
  211. typeof value === 'function',
  212. makeAssertionMessage(path, key, type, value)
  213. );
  214. });
  215. });
  216. }
  217. function makeAssertionMessage (path, key, type, value) {
  218. var buf = key + " should be function but \"" + key + "." + type + "\"";
  219. if (path.length > 0) {
  220. buf += " in module \"" + (path.join('.')) + "\"";
  221. }
  222. buf += " is " + (JSON.stringify(value)) + ".";
  223. return buf
  224. }
  225. var Vue; // bind on install
  226. var Store = function Store (options) {
  227. var this$1 = this;
  228. if ( options === void 0 ) options = {};
  229. // Auto install if it is not done yet and `window` has `Vue`.
  230. // To allow users to avoid auto-installation in some cases,
  231. // this code should be placed here. See #731
  232. if (!Vue && typeof window !== 'undefined' && window.Vue) {
  233. install(window.Vue);
  234. }
  235. if (process.env.NODE_ENV !== 'production') {
  236. assert(Vue, "must call Vue.use(Vuex) before creating a store instance.");
  237. assert(typeof Promise !== 'undefined', "vuex requires a Promise polyfill in this browser.");
  238. assert(this instanceof Store, "Store must be called with the new operator.");
  239. }
  240. var plugins = options.plugins; if ( plugins === void 0 ) plugins = [];
  241. var strict = options.strict; if ( strict === void 0 ) strict = false;
  242. var state = options.state; if ( state === void 0 ) state = {};
  243. if (typeof state === 'function') {
  244. state = state();
  245. }
  246. // store internal state
  247. this._committing = false;
  248. this._actions = Object.create(null);
  249. this._mutations = Object.create(null);
  250. this._wrappedGetters = Object.create(null);
  251. this._modules = new ModuleCollection(options);
  252. this._modulesNamespaceMap = Object.create(null);
  253. this._subscribers = [];
  254. this._watcherVM = new Vue();
  255. // bind commit and dispatch to self
  256. var store = this;
  257. var ref = this;
  258. var dispatch = ref.dispatch;
  259. var commit = ref.commit;
  260. this.dispatch = function boundDispatch (type, payload) {
  261. return dispatch.call(store, type, payload)
  262. };
  263. this.commit = function boundCommit (type, payload, options) {
  264. return commit.call(store, type, payload, options)
  265. };
  266. // strict mode
  267. this.strict = strict;
  268. // init root module.
  269. // this also recursively registers all sub-modules
  270. // and collects all module getters inside this._wrappedGetters
  271. installModule(this, state, [], this._modules.root);
  272. // initialize the store vm, which is responsible for the reactivity
  273. // (also registers _wrappedGetters as computed properties)
  274. resetStoreVM(this, state);
  275. // apply plugins
  276. plugins.forEach(function (plugin) { return plugin(this$1); });
  277. if (Vue.config.devtools) {
  278. devtoolPlugin(this);
  279. }
  280. };
  281. var prototypeAccessors = { state: { configurable: true } };
  282. prototypeAccessors.state.get = function () {
  283. return this._vm._data.$$state
  284. };
  285. prototypeAccessors.state.set = function (v) {
  286. if (process.env.NODE_ENV !== 'production') {
  287. assert(false, "Use store.replaceState() to explicit replace store state.");
  288. }
  289. };
  290. Store.prototype.commit = function commit (_type, _payload, _options) {
  291. var this$1 = this;
  292. // check object-style commit
  293. var ref = unifyObjectStyle(_type, _payload, _options);
  294. var type = ref.type;
  295. var payload = ref.payload;
  296. var options = ref.options;
  297. var mutation = { type: type, payload: payload };
  298. var entry = this._mutations[type];
  299. if (!entry) {
  300. if (process.env.NODE_ENV !== 'production') {
  301. console.error(("[vuex] unknown mutation type: " + type));
  302. }
  303. return
  304. }
  305. this._withCommit(function () {
  306. entry.forEach(function commitIterator (handler) {
  307. handler(payload);
  308. });
  309. });
  310. this._subscribers.forEach(function (sub) { return sub(mutation, this$1.state); });
  311. if (
  312. process.env.NODE_ENV !== 'production' &&
  313. options && options.silent
  314. ) {
  315. console.warn(
  316. "[vuex] mutation type: " + type + ". Silent option has been removed. " +
  317. 'Use the filter functionality in the vue-devtools'
  318. );
  319. }
  320. };
  321. Store.prototype.dispatch = function dispatch (_type, _payload) {
  322. // check object-style dispatch
  323. var ref = unifyObjectStyle(_type, _payload);
  324. var type = ref.type;
  325. var payload = ref.payload;
  326. var entry = this._actions[type];
  327. if (!entry) {
  328. if (process.env.NODE_ENV !== 'production') {
  329. console.error(("[vuex] unknown action type: " + type));
  330. }
  331. return
  332. }
  333. return entry.length > 1
  334. ? Promise.all(entry.map(function (handler) { return handler(payload); }))
  335. : entry[0](payload)
  336. };
  337. Store.prototype.subscribe = function subscribe (fn) {
  338. var subs = this._subscribers;
  339. if (subs.indexOf(fn) < 0) {
  340. subs.push(fn);
  341. }
  342. return function () {
  343. var i = subs.indexOf(fn);
  344. if (i > -1) {
  345. subs.splice(i, 1);
  346. }
  347. }
  348. };
  349. Store.prototype.watch = function watch (getter, cb, options) {
  350. var this$1 = this;
  351. if (process.env.NODE_ENV !== 'production') {
  352. assert(typeof getter === 'function', "store.watch only accepts a function.");
  353. }
  354. return this._watcherVM.$watch(function () { return getter(this$1.state, this$1.getters); }, cb, options)
  355. };
  356. Store.prototype.replaceState = function replaceState (state) {
  357. var this$1 = this;
  358. this._withCommit(function () {
  359. this$1._vm._data.$$state = state;
  360. });
  361. };
  362. Store.prototype.registerModule = function registerModule (path, rawModule) {
  363. if (typeof path === 'string') { path = [path]; }
  364. if (process.env.NODE_ENV !== 'production') {
  365. assert(Array.isArray(path), "module path must be a string or an Array.");
  366. assert(path.length > 0, 'cannot register the root module by using registerModule.');
  367. }
  368. this._modules.register(path, rawModule);
  369. installModule(this, this.state, path, this._modules.get(path));
  370. // reset store to update getters...
  371. resetStoreVM(this, this.state);
  372. };
  373. Store.prototype.unregisterModule = function unregisterModule (path) {
  374. var this$1 = this;
  375. if (typeof path === 'string') { path = [path]; }
  376. if (process.env.NODE_ENV !== 'production') {
  377. assert(Array.isArray(path), "module path must be a string or an Array.");
  378. }
  379. this._modules.unregister(path);
  380. this._withCommit(function () {
  381. var parentState = getNestedState(this$1.state, path.slice(0, -1));
  382. Vue.delete(parentState, path[path.length - 1]);
  383. });
  384. resetStore(this);
  385. };
  386. Store.prototype.hotUpdate = function hotUpdate (newOptions) {
  387. this._modules.update(newOptions);
  388. resetStore(this, true);
  389. };
  390. Store.prototype._withCommit = function _withCommit (fn) {
  391. var committing = this._committing;
  392. this._committing = true;
  393. fn();
  394. this._committing = committing;
  395. };
  396. Object.defineProperties( Store.prototype, prototypeAccessors );
  397. function resetStore (store, hot) {
  398. store._actions = Object.create(null);
  399. store._mutations = Object.create(null);
  400. store._wrappedGetters = Object.create(null);
  401. store._modulesNamespaceMap = Object.create(null);
  402. var state = store.state;
  403. // init all modules
  404. installModule(store, state, [], store._modules.root, true);
  405. // reset vm
  406. resetStoreVM(store, state, hot);
  407. }
  408. function resetStoreVM (store, state, hot) {
  409. var oldVm = store._vm;
  410. // bind store public getters
  411. store.getters = {};
  412. var wrappedGetters = store._wrappedGetters;
  413. var computed = {};
  414. forEachValue(wrappedGetters, function (fn, key) {
  415. // use computed to leverage its lazy-caching mechanism
  416. computed[key] = function () { return fn(store); };
  417. Object.defineProperty(store.getters, key, {
  418. get: function () { return store._vm[key]; },
  419. enumerable: true // for local getters
  420. });
  421. });
  422. // use a Vue instance to store the state tree
  423. // suppress warnings just in case the user has added
  424. // some funky global mixins
  425. var silent = Vue.config.silent;
  426. Vue.config.silent = true;
  427. store._vm = new Vue({
  428. data: {
  429. $$state: state
  430. },
  431. computed: computed
  432. });
  433. Vue.config.silent = silent;
  434. // enable strict mode for new vm
  435. if (store.strict) {
  436. enableStrictMode(store);
  437. }
  438. if (oldVm) {
  439. if (hot) {
  440. // dispatch changes in all subscribed watchers
  441. // to force getter re-evaluation for hot reloading.
  442. store._withCommit(function () {
  443. oldVm._data.$$state = null;
  444. });
  445. }
  446. Vue.nextTick(function () { return oldVm.$destroy(); });
  447. }
  448. }
  449. function installModule (store, rootState, path, module, hot) {
  450. var isRoot = !path.length;
  451. var namespace = store._modules.getNamespace(path);
  452. // register in namespace map
  453. if (module.namespaced) {
  454. store._modulesNamespaceMap[namespace] = module;
  455. }
  456. // set state
  457. if (!isRoot && !hot) {
  458. var parentState = getNestedState(rootState, path.slice(0, -1));
  459. var moduleName = path[path.length - 1];
  460. store._withCommit(function () {
  461. Vue.set(parentState, moduleName, module.state);
  462. });
  463. }
  464. var local = module.context = makeLocalContext(store, namespace, path);
  465. module.forEachMutation(function (mutation, key) {
  466. var namespacedType = namespace + key;
  467. registerMutation(store, namespacedType, mutation, local);
  468. });
  469. module.forEachAction(function (action, key) {
  470. var namespacedType = namespace + key;
  471. registerAction(store, namespacedType, action, local);
  472. });
  473. module.forEachGetter(function (getter, key) {
  474. var namespacedType = namespace + key;
  475. registerGetter(store, namespacedType, getter, local);
  476. });
  477. module.forEachChild(function (child, key) {
  478. installModule(store, rootState, path.concat(key), child, hot);
  479. });
  480. }
  481. /**
  482. * make localized dispatch, commit, getters and state
  483. * if there is no namespace, just use root ones
  484. */
  485. function makeLocalContext (store, namespace, path) {
  486. var noNamespace = namespace === '';
  487. var local = {
  488. dispatch: noNamespace ? store.dispatch : function (_type, _payload, _options) {
  489. var args = unifyObjectStyle(_type, _payload, _options);
  490. var payload = args.payload;
  491. var options = args.options;
  492. var type = args.type;
  493. if (!options || !options.root) {
  494. type = namespace + type;
  495. if (process.env.NODE_ENV !== 'production' && !store._actions[type]) {
  496. console.error(("[vuex] unknown local action type: " + (args.type) + ", global type: " + type));
  497. return
  498. }
  499. }
  500. return store.dispatch(type, payload)
  501. },
  502. commit: noNamespace ? store.commit : function (_type, _payload, _options) {
  503. var args = unifyObjectStyle(_type, _payload, _options);
  504. var payload = args.payload;
  505. var options = args.options;
  506. var type = args.type;
  507. if (!options || !options.root) {
  508. type = namespace + type;
  509. if (process.env.NODE_ENV !== 'production' && !store._mutations[type]) {
  510. console.error(("[vuex] unknown local mutation type: " + (args.type) + ", global type: " + type));
  511. return
  512. }
  513. }
  514. store.commit(type, payload, options);
  515. }
  516. };
  517. // getters and state object must be gotten lazily
  518. // because they will be changed by vm update
  519. Object.defineProperties(local, {
  520. getters: {
  521. get: noNamespace
  522. ? function () { return store.getters; }
  523. : function () { return makeLocalGetters(store, namespace); }
  524. },
  525. state: {
  526. get: function () { return getNestedState(store.state, path); }
  527. }
  528. });
  529. return local
  530. }
  531. function makeLocalGetters (store, namespace) {
  532. var gettersProxy = {};
  533. var splitPos = namespace.length;
  534. Object.keys(store.getters).forEach(function (type) {
  535. // skip if the target getter is not match this namespace
  536. if (type.slice(0, splitPos) !== namespace) { return }
  537. // extract local getter type
  538. var localType = type.slice(splitPos);
  539. // Add a port to the getters proxy.
  540. // Define as getter property because
  541. // we do not want to evaluate the getters in this time.
  542. Object.defineProperty(gettersProxy, localType, {
  543. get: function () { return store.getters[type]; },
  544. enumerable: true
  545. });
  546. });
  547. return gettersProxy
  548. }
  549. function registerMutation (store, type, handler, local) {
  550. var entry = store._mutations[type] || (store._mutations[type] = []);
  551. entry.push(function wrappedMutationHandler (payload) {
  552. handler.call(store, local.state, payload);
  553. });
  554. }
  555. function registerAction (store, type, handler, local) {
  556. var entry = store._actions[type] || (store._actions[type] = []);
  557. entry.push(function wrappedActionHandler (payload, cb) {
  558. var res = handler.call(store, {
  559. dispatch: local.dispatch,
  560. commit: local.commit,
  561. getters: local.getters,
  562. state: local.state,
  563. rootGetters: store.getters,
  564. rootState: store.state
  565. }, payload, cb);
  566. if (!isPromise(res)) {
  567. res = Promise.resolve(res);
  568. }
  569. if (store._devtoolHook) {
  570. return res.catch(function (err) {
  571. store._devtoolHook.emit('vuex:error', err);
  572. throw err
  573. })
  574. } else {
  575. return res
  576. }
  577. });
  578. }
  579. function registerGetter (store, type, rawGetter, local) {
  580. if (store._wrappedGetters[type]) {
  581. if (process.env.NODE_ENV !== 'production') {
  582. console.error(("[vuex] duplicate getter key: " + type));
  583. }
  584. return
  585. }
  586. store._wrappedGetters[type] = function wrappedGetter (store) {
  587. return rawGetter(
  588. local.state, // local state
  589. local.getters, // local getters
  590. store.state, // root state
  591. store.getters // root getters
  592. )
  593. };
  594. }
  595. function enableStrictMode (store) {
  596. store._vm.$watch(function () { return this._data.$$state }, function () {
  597. if (process.env.NODE_ENV !== 'production') {
  598. assert(store._committing, "Do not mutate vuex store state outside mutation handlers.");
  599. }
  600. }, { deep: true, sync: true });
  601. }
  602. function getNestedState (state, path) {
  603. return path.length
  604. ? path.reduce(function (state, key) { return state[key]; }, state)
  605. : state
  606. }
  607. function unifyObjectStyle (type, payload, options) {
  608. if (isObject(type) && type.type) {
  609. options = payload;
  610. payload = type;
  611. type = type.type;
  612. }
  613. if (process.env.NODE_ENV !== 'production') {
  614. assert(typeof type === 'string', ("Expects string as the type, but found " + (typeof type) + "."));
  615. }
  616. return { type: type, payload: payload, options: options }
  617. }
  618. function install (_Vue) {
  619. if (Vue && _Vue === Vue) {
  620. if (process.env.NODE_ENV !== 'production') {
  621. console.error(
  622. '[vuex] already installed. Vue.use(Vuex) should be called only once.'
  623. );
  624. }
  625. return
  626. }
  627. Vue = _Vue;
  628. applyMixin(Vue);
  629. }
  630. var mapState = normalizeNamespace(function (namespace, states) {
  631. var res = {};
  632. normalizeMap(states).forEach(function (ref) {
  633. var key = ref.key;
  634. var val = ref.val;
  635. res[key] = function mappedState () {
  636. var state = this.$store.state;
  637. var getters = this.$store.getters;
  638. if (namespace) {
  639. var module = getModuleByNamespace(this.$store, 'mapState', namespace);
  640. if (!module) {
  641. return
  642. }
  643. state = module.context.state;
  644. getters = module.context.getters;
  645. }
  646. return typeof val === 'function'
  647. ? val.call(this, state, getters)
  648. : state[val]
  649. };
  650. // mark vuex getter for devtools
  651. res[key].vuex = true;
  652. });
  653. return res
  654. });
  655. var mapMutations = normalizeNamespace(function (namespace, mutations) {
  656. var res = {};
  657. normalizeMap(mutations).forEach(function (ref) {
  658. var key = ref.key;
  659. var val = ref.val;
  660. res[key] = function mappedMutation () {
  661. var args = [], len = arguments.length;
  662. while ( len-- ) args[ len ] = arguments[ len ];
  663. var commit = this.$store.commit;
  664. if (namespace) {
  665. var module = getModuleByNamespace(this.$store, 'mapMutations', namespace);
  666. if (!module) {
  667. return
  668. }
  669. commit = module.context.commit;
  670. }
  671. return typeof val === 'function'
  672. ? val.apply(this, [commit].concat(args))
  673. : commit.apply(this.$store, [val].concat(args))
  674. };
  675. });
  676. return res
  677. });
  678. var mapGetters = normalizeNamespace(function (namespace, getters) {
  679. var res = {};
  680. normalizeMap(getters).forEach(function (ref) {
  681. var key = ref.key;
  682. var val = ref.val;
  683. val = namespace + val;
  684. res[key] = function mappedGetter () {
  685. if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
  686. return
  687. }
  688. if (process.env.NODE_ENV !== 'production' && !(val in this.$store.getters)) {
  689. console.error(("[vuex] unknown getter: " + val));
  690. return
  691. }
  692. return this.$store.getters[val]
  693. };
  694. // mark vuex getter for devtools
  695. res[key].vuex = true;
  696. });
  697. return res
  698. });
  699. var mapActions = normalizeNamespace(function (namespace, actions) {
  700. var res = {};
  701. normalizeMap(actions).forEach(function (ref) {
  702. var key = ref.key;
  703. var val = ref.val;
  704. res[key] = function mappedAction () {
  705. var args = [], len = arguments.length;
  706. while ( len-- ) args[ len ] = arguments[ len ];
  707. var dispatch = this.$store.dispatch;
  708. if (namespace) {
  709. var module = getModuleByNamespace(this.$store, 'mapActions', namespace);
  710. if (!module) {
  711. return
  712. }
  713. dispatch = module.context.dispatch;
  714. }
  715. return typeof val === 'function'
  716. ? val.apply(this, [dispatch].concat(args))
  717. : dispatch.apply(this.$store, [val].concat(args))
  718. };
  719. });
  720. return res
  721. });
  722. var createNamespacedHelpers = function (namespace) { return ({
  723. mapState: mapState.bind(null, namespace),
  724. mapGetters: mapGetters.bind(null, namespace),
  725. mapMutations: mapMutations.bind(null, namespace),
  726. mapActions: mapActions.bind(null, namespace)
  727. }); };
  728. function normalizeMap (map) {
  729. return Array.isArray(map)
  730. ? map.map(function (key) { return ({ key: key, val: key }); })
  731. : Object.keys(map).map(function (key) { return ({ key: key, val: map[key] }); })
  732. }
  733. function normalizeNamespace (fn) {
  734. return function (namespace, map) {
  735. if (typeof namespace !== 'string') {
  736. map = namespace;
  737. namespace = '';
  738. } else if (namespace.charAt(namespace.length - 1) !== '/') {
  739. namespace += '/';
  740. }
  741. return fn(namespace, map)
  742. }
  743. }
  744. function getModuleByNamespace (store, helper, namespace) {
  745. var module = store._modulesNamespaceMap[namespace];
  746. if (process.env.NODE_ENV !== 'production' && !module) {
  747. console.error(("[vuex] module namespace not found in " + helper + "(): " + namespace));
  748. }
  749. return module
  750. }
  751. var index = {
  752. Store: Store,
  753. install: install,
  754. version: '2.4.1',
  755. mapState: mapState,
  756. mapMutations: mapMutations,
  757. mapGetters: mapGetters,
  758. mapActions: mapActions,
  759. createNamespacedHelpers: createNamespacedHelpers
  760. };
  761. module.exports = index;