1
0

vuex.js 24 KB

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