vuex.js 22 KB

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