PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.1
4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 All 140 releases
learnpress / assets / src / js / vendor / vue / vuex.js

vuex.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.2.1, at assets/src/js/vendor/vue/vuex.js

1,005 lines 33.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * vuex v3.1.0
3 * (c) 2019 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 = global || self, global.Vuex = factory());
10 }(this, function () { 'use strict';
11
12 function applyMixin (Vue) {
13 var version = Number(Vue.version.split('.')[0]);
14
15 if (version >= 2) {
16 Vue.mixin({ beforeCreate: vuexInit });
17 } else {
18 // override init and inject vuex init procedure
19 // for 1.x backwards compatibility.
20 var _init = Vue.prototype._init;
21 Vue.prototype._init = function (options) {
22 if ( options === void 0 ) options = {};
23
24 options.init = options.init
25 ? [vuexInit].concat(options.init)
26 : vuexInit;
27 _init.call(this, options);
28 };
29 }
30
31 /**
32 * Vuex init hook, injected into each instances init hooks list.
33 */
34
35 function vuexInit () {
36 var options = this.$options;
37 // store injection
38 if (options.store) {
39 this.$store = typeof options.store === 'function'
40 ? options.store()
41 : options.store;
42 } else if (options.parent && options.parent.$store) {
43 this.$store = options.parent.$store;
44 }
45 }
46 }
47
48 var devtoolHook =
49 typeof window !== 'undefined' &&
50 window.__VUE_DEVTOOLS_GLOBAL_HOOK__;
51
52 function devtoolPlugin (store) {
53 if (!devtoolHook) { return }
54
55 store._devtoolHook = devtoolHook;
56
57 devtoolHook.emit('vuex:init', store);
58
59 devtoolHook.on('vuex:travel-to-state', function (targetState) {
60 store.replaceState(targetState);
61 });
62
63 store.subscribe(function (mutation, state) {
64 devtoolHook.emit('vuex:mutation', mutation, state);
65 });
66 }
67
68 /**
69 * Get the first item that pass the test
70 * by second argument function
71 *
72 * @param {Array} list
73 * @param {Function} f
74 * @return {*}
75 */
76
77 /**
78 * forEach for object
79 */
80 function forEachValue (obj, fn) {
81 Object.keys(obj).forEach(function (key) { return fn(obj[key], key); });
82 }
83
84 function isObject (obj) {
85 return obj !== null && typeof obj === 'object'
86 }
87
88 function isPromise (val) {
89 return val && typeof val.then === 'function'
90 }
91
92 function assert (condition, msg) {
93 if (!condition) { throw new Error(("[vuex] " + msg)) }
94 }
95
96 // Base data struct for store's module, package with some attribute and method
97 var Module = function Module (rawModule, runtime) {
98 this.runtime = runtime;
99 // Store some children item
100 this._children = Object.create(null);
101 // Store the origin module object which passed by programmer
102 this._rawModule = rawModule;
103 var rawState = rawModule.state;
104
105 // Store the origin module's state
106 this.state = (typeof rawState === 'function' ? rawState() : rawState) || {};
107 };
108
109 var prototypeAccessors = { namespaced: { configurable: true } };
110
111 prototypeAccessors.namespaced.get = function () {
112 return !!this._rawModule.namespaced
113 };
114
115 Module.prototype.addChild = function addChild (key, module) {
116 this._children[key] = module;
117 };
118
119 Module.prototype.removeChild = function removeChild (key) {
120 delete this._children[key];
121 };
122
123 Module.prototype.getChild = function getChild (key) {
124 return this._children[key]
125 };
126
127 Module.prototype.update = function update (rawModule) {
128 this._rawModule.namespaced = rawModule.namespaced;
129 if (rawModule.actions) {
130 this._rawModule.actions = rawModule.actions;
131 }
132 if (rawModule.mutations) {
133 this._rawModule.mutations = rawModule.mutations;
134 }
135 if (rawModule.getters) {
136 this._rawModule.getters = rawModule.getters;
137 }
138 };
139
140 Module.prototype.forEachChild = function forEachChild (fn) {
141 forEachValue(this._children, fn);
142 };
143
144 Module.prototype.forEachGetter = function forEachGetter (fn) {
145 if (this._rawModule.getters) {
146 forEachValue(this._rawModule.getters, fn);
147 }
148 };
149
150 Module.prototype.forEachAction = function forEachAction (fn) {
151 if (this._rawModule.actions) {
152 forEachValue(this._rawModule.actions, fn);
153 }
154 };
155
156 Module.prototype.forEachMutation = function forEachMutation (fn) {
157 if (this._rawModule.mutations) {
158 forEachValue(this._rawModule.mutations, fn);
159 }
160 };
161
162 Object.defineProperties( Module.prototype, prototypeAccessors );
163
164 var ModuleCollection = function ModuleCollection (rawRootModule) {
165 // register root module (Vuex.Store options)
166 this.register([], rawRootModule, false);
167 };
168
169 ModuleCollection.prototype.get = function get (path) {
170 return path.reduce(function (module, key) {
171 return module.getChild(key)
172 }, this.root)
173 };
174
175 ModuleCollection.prototype.getNamespace = function getNamespace (path) {
176 var module = this.root;
177 return path.reduce(function (namespace, key) {
178 module = module.getChild(key);
179 return namespace + (module.namespaced ? key + '/' : '')
180 }, '')
181 };
182
183 ModuleCollection.prototype.update = function update$1 (rawRootModule) {
184 update([], this.root, rawRootModule);
185 };
186
187 ModuleCollection.prototype.register = function register (path, rawModule, runtime) {
188 var this$1 = this;
189 if ( runtime === void 0 ) runtime = true;
190
191 {
192 assertRawModule(path, rawModule);
193 }
194
195 var newModule = new Module(rawModule, runtime);
196 if (path.length === 0) {
197 this.root = newModule;
198 } else {
199 var parent = this.get(path.slice(0, -1));
200 parent.addChild(path[path.length - 1], newModule);
201 }
202
203 // register nested modules
204 if (rawModule.modules) {
205 forEachValue(rawModule.modules, function (rawChildModule, key) {
206 this$1.register(path.concat(key), rawChildModule, runtime);
207 });
208 }
209 };
210
211 ModuleCollection.prototype.unregister = function unregister (path) {
212 var parent = this.get(path.slice(0, -1));
213 var key = path[path.length - 1];
214 if (!parent.getChild(key).runtime) { return }
215
216 parent.removeChild(key);
217 };
218
219 function update (path, targetModule, newModule) {
220 {
221 assertRawModule(path, newModule);
222 }
223
224 // update target module
225 targetModule.update(newModule);
226
227 // update nested modules
228 if (newModule.modules) {
229 for (var key in newModule.modules) {
230 if (!targetModule.getChild(key)) {
231 {
232 console.warn(
233 "[vuex] trying to add a new module '" + key + "' on hot reloading, " +
234 'manual reload is needed'
235 );
236 }
237 return
238 }
239 update(
240 path.concat(key),
241 targetModule.getChild(key),
242 newModule.modules[key]
243 );
244 }
245 }
246 }
247
248 var functionAssert = {
249 assert: function (value) { return typeof value === 'function'; },
250 expected: 'function'
251 };
252
253 var objectAssert = {
254 assert: function (value) { return typeof value === 'function' ||
255 (typeof value === 'object' && typeof value.handler === 'function'); },
256 expected: 'function or object with "handler" function'
257 };
258
259 var assertTypes = {
260 getters: functionAssert,
261 mutations: functionAssert,
262 actions: objectAssert
263 };
264
265 function assertRawModule (path, rawModule) {
266 Object.keys(assertTypes).forEach(function (key) {
267 if (!rawModule[key]) { return }
268
269 var assertOptions = assertTypes[key];
270
271 forEachValue(rawModule[key], function (value, type) {
272 assert(
273 assertOptions.assert(value),
274 makeAssertionMessage(path, key, type, value, assertOptions.expected)
275 );
276 });
277 });
278 }
279
280 function makeAssertionMessage (path, key, type, value, expected) {
281 var buf = key + " should be " + expected + " but \"" + key + "." + type + "\"";
282 if (path.length > 0) {
283 buf += " in module \"" + (path.join('.')) + "\"";
284 }
285 buf += " is " + (JSON.stringify(value)) + ".";
286 return buf
287 }
288
289 var Vue; // bind on install
290
291 var Store = function Store (options) {
292 var this$1 = this;
293 if ( options === void 0 ) options = {};
294
295 // Auto install if it is not done yet and `window` has `Vue`.
296 // To allow users to avoid auto-installation in some cases,
297 // this code should be placed here. See #731
298 if (!Vue && typeof window !== 'undefined' && window.Vue) {
299 install(window.Vue);
300 }
301
302 {
303 assert(Vue, "must call Vue.use(Vuex) before creating a store instance.");
304 assert(typeof Promise !== 'undefined', "vuex requires a Promise polyfill in this browser.");
305 assert(this instanceof Store, "store must be called with the new operator.");
306 }
307
308 var plugins = options.plugins; if ( plugins === void 0 ) plugins = [];
309 var strict = options.strict; if ( strict === void 0 ) strict = false;
310
311 // store internal state
312 this._committing = false;
313 this._actions = Object.create(null);
314 this._actionSubscribers = [];
315 this._mutations = Object.create(null);
316 this._wrappedGetters = Object.create(null);
317 this._modules = new ModuleCollection(options);
318 this._modulesNamespaceMap = Object.create(null);
319 this._subscribers = [];
320 this._watcherVM = new Vue();
321
322 // bind commit and dispatch to self
323 var store = this;
324 var ref = this;
325 var dispatch = ref.dispatch;
326 var commit = ref.commit;
327 this.dispatch = function boundDispatch (type, payload) {
328 return dispatch.call(store, type, payload)
329 };
330 this.commit = function boundCommit (type, payload, options) {
331 return commit.call(store, type, payload, options)
332 };
333
334 // strict mode
335 this.strict = strict;
336
337 var state = this._modules.root.state;
338
339 // init root module.
340 // this also recursively registers all sub-modules
341 // and collects all module getters inside this._wrappedGetters
342 installModule(this, state, [], this._modules.root);
343
344 // initialize the store vm, which is responsible for the reactivity
345 // (also registers _wrappedGetters as computed properties)
346 resetStoreVM(this, state);
347
348 // apply plugins
349 plugins.forEach(function (plugin) { return plugin(this$1); });
350
351 var useDevtools = options.devtools !== undefined ? options.devtools : Vue.config.devtools;
352 if (useDevtools) {
353 devtoolPlugin(this);
354 }
355 };
356
357 var prototypeAccessors$1 = { state: { configurable: true } };
358
359 prototypeAccessors$1.state.get = function () {
360 return this._vm._data.$$state
361 };
362
363 prototypeAccessors$1.state.set = function (v) {
364 {
365 assert(false, "use store.replaceState() to explicit replace store state.");
366 }
367 };
368
369 Store.prototype.commit = function commit (_type, _payload, _options) {
370 var this$1 = this;
371
372 // check object-style commit
373 var ref = unifyObjectStyle(_type, _payload, _options);
374 var type = ref.type;
375 var payload = ref.payload;
376 var options = ref.options;
377
378 var mutation = { type: type, payload: payload };
379 var entry = this._mutations[type];
380 if (!entry) {
381 {
382 console.error(("[vuex] unknown mutation type: " + type));
383 }
384 return
385 }
386 this._withCommit(function () {
387 entry.forEach(function commitIterator (handler) {
388 handler(payload);
389 });
390 });
391 this._subscribers.forEach(function (sub) { return sub(mutation, this$1.state); });
392
393 if (
394 options && options.silent
395 ) {
396 console.warn(
397 "[vuex] mutation type: " + type + ". Silent option has been removed. " +
398 'Use the filter functionality in the vue-devtools'
399 );
400 }
401 };
402
403 Store.prototype.dispatch = function dispatch (_type, _payload) {
404 var this$1 = this;
405
406 // check object-style dispatch
407 var ref = unifyObjectStyle(_type, _payload);
408 var type = ref.type;
409 var payload = ref.payload;
410
411 var action = { type: type, payload: payload };
412 var entry = this._actions[type];
413 if (!entry) {
414 {
415 console.error(("[vuex] unknown action type: " + type));
416 }
417 return
418 }
419
420 try {
421 this._actionSubscribers
422 .filter(function (sub) { return sub.before; })
423 .forEach(function (sub) { return sub.before(action, this$1.state); });
424 } catch (e) {
425 {
426 console.warn("[vuex] error in before action subscribers: ");
427 console.error(e);
428 }
429 }
430
431 var result = entry.length > 1
432 ? Promise.all(entry.map(function (handler) { return handler(payload); }))
433 : entry[0](payload);
434
435 return result.then(function (res) {
436 try {
437 this$1._actionSubscribers
438 .filter(function (sub) { return sub.after; })
439 .forEach(function (sub) { return sub.after(action, this$1.state); });
440 } catch (e) {
441 {
442 console.warn("[vuex] error in after action subscribers: ");
443 console.error(e);
444 }
445 }
446 return res
447 })
448 };
449
450 Store.prototype.subscribe = function subscribe (fn) {
451 return genericSubscribe(fn, this._subscribers)
452 };
453
454 Store.prototype.subscribeAction = function subscribeAction (fn) {
455 var subs = typeof fn === 'function' ? { before: fn } : fn;
456 return genericSubscribe(subs, this._actionSubscribers)
457 };
458
459 Store.prototype.watch = function watch (getter, cb, options) {
460 var this$1 = this;
461
462 {
463 assert(typeof getter === 'function', "store.watch only accepts a function.");
464 }
465 return this._watcherVM.$watch(function () { return getter(this$1.state, this$1.getters); }, cb, options)
466 };
467
468 Store.prototype.replaceState = function replaceState (state) {
469 var this$1 = this;
470
471 this._withCommit(function () {
472 this$1._vm._data.$$state = state;
473 });
474 };
475
476 Store.prototype.registerModule = function registerModule (path, rawModule, options) {
477 if ( options === void 0 ) options = {};
478
479 if (typeof path === 'string') { path = [path]; }
480
481 {
482 assert(Array.isArray(path), "module path must be a string or an Array.");
483 assert(path.length > 0, 'cannot register the root module by using registerModule.');
484 }
485
486 this._modules.register(path, rawModule);
487 installModule(this, this.state, path, this._modules.get(path), options.preserveState);
488 // reset store to update getters...
489 resetStoreVM(this, this.state);
490 };
491
492 Store.prototype.unregisterModule = function unregisterModule (path) {
493 var this$1 = this;
494
495 if (typeof path === 'string') { path = [path]; }
496
497 {
498 assert(Array.isArray(path), "module path must be a string or an Array.");
499 }
500
501 this._modules.unregister(path);
502 this._withCommit(function () {
503 var parentState = getNestedState(this$1.state, path.slice(0, -1));
504 Vue.delete(parentState, path[path.length - 1]);
505 });
506 resetStore(this);
507 };
508
509 Store.prototype.hotUpdate = function hotUpdate (newOptions) {
510 this._modules.update(newOptions);
511 resetStore(this, true);
512 };
513
514 Store.prototype._withCommit = function _withCommit (fn) {
515 var committing = this._committing;
516 this._committing = true;
517 fn();
518 this._committing = committing;
519 };
520
521 Object.defineProperties( Store.prototype, prototypeAccessors$1 );
522
523 function genericSubscribe (fn, subs) {
524 if (subs.indexOf(fn) < 0) {
525 subs.push(fn);
526 }
527 return function () {
528 var i = subs.indexOf(fn);
529 if (i > -1) {
530 subs.splice(i, 1);
531 }
532 }
533 }
534
535 function resetStore (store, hot) {
536 store._actions = Object.create(null);
537 store._mutations = Object.create(null);
538 store._wrappedGetters = Object.create(null);
539 store._modulesNamespaceMap = Object.create(null);
540 var state = store.state;
541 // init all modules
542 installModule(store, state, [], store._modules.root, true);
543 // reset vm
544 resetStoreVM(store, state, hot);
545 }
546
547 function resetStoreVM (store, state, hot) {
548 var oldVm = store._vm;
549
550 // bind store public getters
551 store.getters = {};
552 var wrappedGetters = store._wrappedGetters;
553 var computed = {};
554 forEachValue(wrappedGetters, function (fn, key) {
555 // use computed to leverage its lazy-caching mechanism
556 computed[key] = function () { return fn(store); };
557 Object.defineProperty(store.getters, key, {
558 get: function () { return store._vm[key]; },
559 enumerable: true // for local getters
560 });
561 });
562
563 // use a Vue instance to store the state tree
564 // suppress warnings just in case the user has added
565 // some funky global mixins
566 var silent = Vue.config.silent;
567 Vue.config.silent = true;
568 store._vm = new Vue({
569 data: {
570 $$state: state
571 },
572 computed: computed
573 });
574 Vue.config.silent = silent;
575
576 // enable strict mode for new vm
577 if (store.strict) {
578 enableStrictMode(store);
579 }
580
581 if (oldVm) {
582 if (hot) {
583 // dispatch changes in all subscribed watchers
584 // to force getter re-evaluation for hot reloading.
585 store._withCommit(function () {
586 oldVm._data.$$state = null;
587 });
588 }
589 Vue.nextTick(function () { return oldVm.$destroy(); });
590 }
591 }
592
593 function installModule (store, rootState, path, module, hot) {
594 var isRoot = !path.length;
595 var namespace = store._modules.getNamespace(path);
596
597 // register in namespace map
598 if (module.namespaced) {
599 store._modulesNamespaceMap[namespace] = module;
600 }
601
602 // set state
603 if (!isRoot && !hot) {
604 var parentState = getNestedState(rootState, path.slice(0, -1));
605 var moduleName = path[path.length - 1];
606 store._withCommit(function () {
607 Vue.set(parentState, moduleName, module.state);
608 });
609 }
610
611 var local = module.context = makeLocalContext(store, namespace, path);
612
613 module.forEachMutation(function (mutation, key) {
614 var namespacedType = namespace + key;
615 registerMutation(store, namespacedType, mutation, local);
616 });
617
618 module.forEachAction(function (action, key) {
619 var type = action.root ? key : namespace + key;
620 var handler = action.handler || action;
621 registerAction(store, type, handler, local);
622 });
623
624 module.forEachGetter(function (getter, key) {
625 var namespacedType = namespace + key;
626 registerGetter(store, namespacedType, getter, local);
627 });
628
629 module.forEachChild(function (child, key) {
630 installModule(store, rootState, path.concat(key), child, hot);
631 });
632 }
633
634 /**
635 * make localized dispatch, commit, getters and state
636 * if there is no namespace, just use root ones
637 */
638 function makeLocalContext (store, namespace, path) {
639 var noNamespace = namespace === '';
640
641 var local = {
642 dispatch: noNamespace ? store.dispatch : function (_type, _payload, _options) {
643 var args = unifyObjectStyle(_type, _payload, _options);
644 var payload = args.payload;
645 var options = args.options;
646 var type = args.type;
647
648 if (!options || !options.root) {
649 type = namespace + type;
650 if (!store._actions[type]) {
651 console.error(("[vuex] unknown local action type: " + (args.type) + ", global type: " + type));
652 return
653 }
654 }
655
656 return store.dispatch(type, payload)
657 },
658
659 commit: noNamespace ? store.commit : function (_type, _payload, _options) {
660 var args = unifyObjectStyle(_type, _payload, _options);
661 var payload = args.payload;
662 var options = args.options;
663 var type = args.type;
664
665 if (!options || !options.root) {
666 type = namespace + type;
667 if (!store._mutations[type]) {
668 console.error(("[vuex] unknown local mutation type: " + (args.type) + ", global type: " + type));
669 return
670 }
671 }
672
673 store.commit(type, payload, options);
674 }
675 };
676
677 // getters and state object must be gotten lazily
678 // because they will be changed by vm update
679 Object.defineProperties(local, {
680 getters: {
681 get: noNamespace
682 ? function () { return store.getters; }
683 : function () { return makeLocalGetters(store, namespace); }
684 },
685 state: {
686 get: function () { return getNestedState(store.state, path); }
687 }
688 });
689
690 return local
691 }
692
693 function makeLocalGetters (store, namespace) {
694 var gettersProxy = {};
695
696 var splitPos = namespace.length;
697 Object.keys(store.getters).forEach(function (type) {
698 // skip if the target getter is not match this namespace
699 if (type.slice(0, splitPos) !== namespace) { return }
700
701 // extract local getter type
702 var localType = type.slice(splitPos);
703
704 // Add a port to the getters proxy.
705 // Define as getter property because
706 // we do not want to evaluate the getters in this time.
707 Object.defineProperty(gettersProxy, localType, {
708 get: function () { return store.getters[type]; },
709 enumerable: true
710 });
711 });
712
713 return gettersProxy
714 }
715
716 function registerMutation (store, type, handler, local) {
717 var entry = store._mutations[type] || (store._mutations[type] = []);
718 entry.push(function wrappedMutationHandler (payload) {
719 handler.call(store, local.state, payload);
720 });
721 }
722
723 function registerAction (store, type, handler, local) {
724 var entry = store._actions[type] || (store._actions[type] = []);
725 entry.push(function wrappedActionHandler (payload, cb) {
726 var res = handler.call(store, {
727 dispatch: local.dispatch,
728 commit: local.commit,
729 getters: local.getters,
730 state: local.state,
731 rootGetters: store.getters,
732 rootState: store.state
733 }, payload, cb);
734 if (!isPromise(res)) {
735 res = Promise.resolve(res);
736 }
737 if (store._devtoolHook) {
738 return res.catch(function (err) {
739 store._devtoolHook.emit('vuex:error', err);
740 throw err
741 })
742 } else {
743 return res
744 }
745 });
746 }
747
748 function registerGetter (store, type, rawGetter, local) {
749 if (store._wrappedGetters[type]) {
750 {
751 console.error(("[vuex] duplicate getter key: " + type));
752 }
753 return
754 }
755 store._wrappedGetters[type] = function wrappedGetter (store) {
756 return rawGetter(
757 local.state, // local state
758 local.getters, // local getters
759 store.state, // root state
760 store.getters // root getters
761 )
762 };
763 }
764
765 function enableStrictMode (store) {
766 store._vm.$watch(function () { return this._data.$$state }, function () {
767 {
768 assert(store._committing, "do not mutate vuex store state outside mutation handlers.");
769 }
770 }, { deep: true, sync: true });
771 }
772
773 function getNestedState (state, path) {
774 return path.length
775 ? path.reduce(function (state, key) { return state[key]; }, state)
776 : state
777 }
778
779 function unifyObjectStyle (type, payload, options) {
780 if (isObject(type) && type.type) {
781 options = payload;
782 payload = type;
783 type = type.type;
784 }
785
786 {
787 assert(typeof type === 'string', ("expects string as the type, but found " + (typeof type) + "."));
788 }
789
790 return { type: type, payload: payload, options: options }
791 }
792
793 function install (_Vue) {
794 if (Vue && _Vue === Vue) {
795 {
796 console.error(
797 '[vuex] already installed. Vue.use(Vuex) should be called only once.'
798 );
799 }
800 return
801 }
802 Vue = _Vue;
803 applyMixin(Vue);
804 }
805
806 /**
807 * Reduce the code which written in Vue.js for getting the state.
808 * @param {String} [namespace] - Module's namespace
809 * @param {Object|Array} states # Object's item can be a function which accept state and getters for param, you can do something for state and getters in it.
810 * @param {Object}
811 */
812 var mapState = normalizeNamespace(function (namespace, states) {
813 var res = {};
814 normalizeMap(states).forEach(function (ref) {
815 var key = ref.key;
816 var val = ref.val;
817
818 res[key] = function mappedState () {
819 var state = this.$store.state;
820 var getters = this.$store.getters;
821 if (namespace) {
822 var module = getModuleByNamespace(this.$store, 'mapState', namespace);
823 if (!module) {
824 return
825 }
826 state = module.context.state;
827 getters = module.context.getters;
828 }
829 return typeof val === 'function'
830 ? val.call(this, state, getters)
831 : state[val]
832 };
833 // mark vuex getter for devtools
834 res[key].vuex = true;
835 });
836 return res
837 });
838
839 /**
840 * Reduce the code which written in Vue.js for committing the mutation
841 * @param {String} [namespace] - Module's namespace
842 * @param {Object|Array} mutations # Object's item can be a function which accept `commit` function as the first param, it can accept anthor params. You can commit mutation and do any other things in this function. specially, You need to pass anthor params from the mapped function.
843 * @return {Object}
844 */
845 var mapMutations = normalizeNamespace(function (namespace, mutations) {
846 var res = {};
847 normalizeMap(mutations).forEach(function (ref) {
848 var key = ref.key;
849 var val = ref.val;
850
851 res[key] = function mappedMutation () {
852 var args = [], len = arguments.length;
853 while ( len-- ) args[ len ] = arguments[ len ];
854
855 // Get the commit method from store
856 var commit = this.$store.commit;
857 if (namespace) {
858 var module = getModuleByNamespace(this.$store, 'mapMutations', namespace);
859 if (!module) {
860 return
861 }
862 commit = module.context.commit;
863 }
864 return typeof val === 'function'
865 ? val.apply(this, [commit].concat(args))
866 : commit.apply(this.$store, [val].concat(args))
867 };
868 });
869 return res
870 });
871
872 /**
873 * Reduce the code which written in Vue.js for getting the getters
874 * @param {String} [namespace] - Module's namespace
875 * @param {Object|Array} getters
876 * @return {Object}
877 */
878 var mapGetters = normalizeNamespace(function (namespace, getters) {
879 var res = {};
880 normalizeMap(getters).forEach(function (ref) {
881 var key = ref.key;
882 var val = ref.val;
883
884 // The namespace has been mutated by normalizeNamespace
885 val = namespace + val;
886 res[key] = function mappedGetter () {
887 if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
888 return
889 }
890 if (!(val in this.$store.getters)) {
891 console.error(("[vuex] unknown getter: " + val));
892 return
893 }
894 return this.$store.getters[val]
895 };
896 // mark vuex getter for devtools
897 res[key].vuex = true;
898 });
899 return res
900 });
901
902 /**
903 * Reduce the code which written in Vue.js for dispatch the action
904 * @param {String} [namespace] - Module's namespace
905 * @param {Object|Array} actions # Object's item can be a function which accept `dispatch` function as the first param, it can accept anthor params. You can dispatch action and do any other things in this function. specially, You need to pass anthor params from the mapped function.
906 * @return {Object}
907 */
908 var mapActions = normalizeNamespace(function (namespace, actions) {
909 var res = {};
910 normalizeMap(actions).forEach(function (ref) {
911 var key = ref.key;
912 var val = ref.val;
913
914 res[key] = function mappedAction () {
915 var args = [], len = arguments.length;
916 while ( len-- ) args[ len ] = arguments[ len ];
917
918 // get dispatch function from store
919 var dispatch = this.$store.dispatch;
920 if (namespace) {
921 var module = getModuleByNamespace(this.$store, 'mapActions', namespace);
922 if (!module) {
923 return
924 }
925 dispatch = module.context.dispatch;
926 }
927 return typeof val === 'function'
928 ? val.apply(this, [dispatch].concat(args))
929 : dispatch.apply(this.$store, [val].concat(args))
930 };
931 });
932 return res
933 });
934
935 /**
936 * Rebinding namespace param for mapXXX function in special scoped, and return them by simple object
937 * @param {String} namespace
938 * @return {Object}
939 */
940 var createNamespacedHelpers = function (namespace) { return ({
941 mapState: mapState.bind(null, namespace),
942 mapGetters: mapGetters.bind(null, namespace),
943 mapMutations: mapMutations.bind(null, namespace),
944 mapActions: mapActions.bind(null, namespace)
945 }); };
946
947 /**
948 * Normalize the map
949 * normalizeMap([1, 2, 3]) => [ { key: 1, val: 1 }, { key: 2, val: 2 }, { key: 3, val: 3 } ]
950 * normalizeMap({a: 1, b: 2, c: 3}) => [ { key: 'a', val: 1 }, { key: 'b', val: 2 }, { key: 'c', val: 3 } ]
951 * @param {Array|Object} map
952 * @return {Object}
953 */
954 function normalizeMap (map) {
955 return Array.isArray(map)
956 ? map.map(function (key) { return ({ key: key, val: key }); })
957 : Object.keys(map).map(function (key) { return ({ key: key, val: map[key] }); })
958 }
959
960 /**
961 * Return a function expect two param contains namespace and map. it will normalize the namespace and then the param's function will handle the new namespace and the map.
962 * @param {Function} fn
963 * @return {Function}
964 */
965 function normalizeNamespace (fn) {
966 return function (namespace, map) {
967 if (typeof namespace !== 'string') {
968 map = namespace;
969 namespace = '';
970 } else if (namespace.charAt(namespace.length - 1) !== '/') {
971 namespace += '/';
972 }
973 return fn(namespace, map)
974 }
975 }
976
977 /**
978 * Search a special module from store by namespace. if module not exist, print error message.
979 * @param {Object} store
980 * @param {String} helper
981 * @param {String} namespace
982 * @return {Object}
983 */
984 function getModuleByNamespace (store, helper, namespace) {
985 var module = store._modulesNamespaceMap[namespace];
986 if (!module) {
987 console.error(("[vuex] module namespace not found in " + helper + "(): " + namespace));
988 }
989 return module
990 }
991
992 var index = {
993 Store: Store,
994 install: install,
995 version: '3.1.0',
996 mapState: mapState,
997 mapMutations: mapMutations,
998 mapGetters: mapGetters,
999 mapActions: mapActions,
1000 createNamespacedHelpers: createNamespacedHelpers
1001 };
1002
1003 return index;
1004
1005 }));