UNPKG

3.21 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _extends2 = require('babel-runtime/helpers/extends');
6
7var _extends3 = _interopRequireDefault(_extends2);
8
9var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
10
11var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
12
13var _util = require('./util');
14
15function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
17/**
18 * manage dataSource for menu list
19 */
20var DataStore = function () {
21 function DataStore(options) {
22 (0, _classCallCheck3.default)(this, DataStore);
23
24 this.options = (0, _extends3.default)({
25 filter: _util.filter,
26 key: undefined,
27 addonKey: false,
28 filterLocal: true
29 }, options);
30
31 // origin data
32 this.dataSource = [];
33 // current data for menu display
34 this.menuDataSource = [];
35 // key=>value map for menuDataSource
36 this.mapDataSource = {};
37 // current data can be select (not disabled) on menu
38 this.enabledDataSource = [];
39 }
40
41 DataStore.prototype.setOptions = function setOptions(options) {
42 (0, _extends3.default)(this.options, options);
43 };
44
45 DataStore.prototype.updateByDS = function updateByDS(dataSource) {
46 var isChildren = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
47
48 this.dataSource = isChildren ? (0, _util.parseDataSourceFromChildren)(dataSource) : (0, _util.normalizeDataSource)(dataSource);
49 return this.updateAll();
50 };
51
52 DataStore.prototype.updateByKey = function updateByKey(key) {
53 if (key === this.options.key) {
54 return this.getMenuDS();
55 }
56
57 this.options.key = key;
58 return this.updateAll();
59 };
60
61 DataStore.prototype.getOriginDS = function getOriginDS() {
62 return this.dataSource;
63 };
64
65 DataStore.prototype.getMenuDS = function getMenuDS() {
66 return this.menuDataSource;
67 };
68
69 DataStore.prototype.getFlattenDS = function getFlattenDS() {
70 return this.flattenDataSource;
71 };
72
73 DataStore.prototype.getEnableDS = function getEnableDS() {
74 return this.enabledDataSource;
75 };
76
77 DataStore.prototype.getMapDS = function getMapDS() {
78 return this.mapDataSource;
79 };
80
81 DataStore.prototype.updateAll = function updateAll() {
82 var _this = this;
83
84 var _options = this.options,
85 key = _options.key,
86 filter = _options.filter,
87 filterLocal = _options.filterLocal;
88
89 this.menuDataSource = (0, _util.filterDataSource)(this.dataSource, filterLocal ? key : '', filter, this.options.addonKey);
90
91 this.flattenDataSource = (0, _util.flattingDataSource)(this.menuDataSource);
92
93 this.mapDataSource = {};
94 this.flattenDataSource.forEach(function (item) {
95 _this.mapDataSource['' + item.value] = item;
96 });
97
98 this.enabledDataSource = this.flattenDataSource.filter(function (item) {
99 return !item.disabled;
100 });
101
102 return this.menuDataSource;
103 };
104
105 return DataStore;
106}();
107
108exports.default = DataStore;
109module.exports = exports['default'];
\No newline at end of file