UNPKG

3.44 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 showDataSourceChildren: true
30 }, options);
31
32 // origin data
33 this.dataSource = [];
34 // current data for menu display
35 this.menuDataSource = [];
36 // key=>value map for menuDataSource
37 this.mapDataSource = {};
38 // current data can be select (not disabled) on menu
39 this.enabledDataSource = [];
40 this.flattenDataSource = [];
41 }
42
43 DataStore.prototype.setOptions = function setOptions(options) {
44 (0, _extends3.default)(this.options, options);
45 };
46
47 DataStore.prototype.updateByDS = function updateByDS(dataSource) {
48 var isChildren = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
49
50 this.dataSource = isChildren ? (0, _util.parseDataSourceFromChildren)(dataSource) : (0, _util.normalizeDataSource)(dataSource, 0, this.options.showDataSourceChildren);
51 return this.updateAll();
52 };
53
54 DataStore.prototype.updateByKey = function updateByKey(key) {
55 if (key === this.options.key) {
56 return this.getMenuDS();
57 }
58
59 this.options.key = key;
60 return this.updateAll();
61 };
62
63 DataStore.prototype.getOriginDS = function getOriginDS() {
64 return this.dataSource;
65 };
66
67 DataStore.prototype.getMenuDS = function getMenuDS() {
68 return this.menuDataSource;
69 };
70
71 DataStore.prototype.getFlattenDS = function getFlattenDS() {
72 return this.flattenDataSource;
73 };
74
75 DataStore.prototype.getEnableDS = function getEnableDS() {
76 return this.enabledDataSource;
77 };
78
79 DataStore.prototype.getMapDS = function getMapDS() {
80 return this.mapDataSource;
81 };
82
83 DataStore.prototype.updateAll = function updateAll() {
84 var _this = this;
85
86 var _options = this.options,
87 key = _options.key,
88 filter = _options.filter,
89 filterLocal = _options.filterLocal,
90 showDataSourceChildren = _options.showDataSourceChildren;
91
92 this.menuDataSource = (0, _util.filterDataSource)(this.dataSource, filterLocal ? key : '', filter, this.options.addonKey);
93
94 this.flattenDataSource = showDataSourceChildren ? (0, _util.flattingDataSource)(this.menuDataSource) : this.menuDataSource;
95
96 this.mapDataSource = {};
97 this.flattenDataSource.forEach(function (item) {
98 _this.mapDataSource['' + item.value] = item;
99 });
100
101 this.enabledDataSource = this.flattenDataSource.filter(function (item) {
102 return !item.disabled;
103 });
104
105 return this.menuDataSource;
106 };
107
108 return DataStore;
109}();
110
111exports.default = DataStore;
112module.exports = exports['default'];
\No newline at end of file