UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

224 lines (203 loc) 8.63 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _card, _kanban, _table; import _regeneratorRuntime from "@babel/runtime/regenerator"; import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react'; import noop from 'lodash/noop'; import ConfigContext from '../../../es/config-provider/ConfigContext'; import BoardWithContext, { normalizeColumns } from './BoardWithContext'; import { ViewMode, ViewField } from './enum'; import DataSet from '../data-set'; import { FieldType } from '../data-set/enum'; export var VIEWLISTDS = '__VIEWLISTDS__'; export var DEFAULTVIEW = { card: (_card = {}, _defineProperty(_card, ViewField.viewName, '初始卡片视图'), _defineProperty(_card, ViewField.viewMode, ViewMode.card), _defineProperty(_card, ViewField.id, '__DEFAULT__'), _card), kanban: (_kanban = {}, _defineProperty(_kanban, ViewField.viewName, '初始看板视图'), _defineProperty(_kanban, ViewField.viewMode, ViewMode.kanban), _defineProperty(_kanban, ViewField.id, '__DEFAULT__'), _kanban), table: (_table = {}, _defineProperty(_table, ViewField.viewName, '初始列表视图'), _defineProperty(_table, ViewField.viewMode, ViewMode.table), _defineProperty(_table, ViewField.id, '__DEFAULT__'), _table) }; var Board = function Board(props) { var _useContext = useContext(ConfigContext), getConfig = _useContext.getConfig, getCustomizable = _useContext.getCustomizable; var defaultViewMode = props.defaultViewMode, defaultViewProps = props.defaultViewProps, tableProps = props.tableProps, viewVisible = props.viewVisible, dataSet = props.dataSet, customizedCode = props.customizedCode, _props$customizable = props.customizable, customizable = _props$customizable === void 0 ? customizedCode ? getCustomizable('Board') : undefined : _props$customizable; var $customizable = customizedCode ? customizable : false; var _useState = useState(!$customizable), _useState2 = _slicedToArray(_useState, 2), loaded = _useState2[0], setLoaded = _useState2[1]; var _useState3 = useState(), _useState4 = _slicedToArray(_useState3, 2), customized = _useState4[0], setCustomized = _useState4[1]; var customizedDS = useMemo(function () { return new DataSet({ autoLocateFirst: false, paging: false, fields: [{ name: ViewField.viewProps, type: FieldType.json }, { name: ViewField.viewName, type: FieldType.string }, { name: ViewField.viewMode, type: FieldType.string, transformResponse: function transformResponse(value) { return value.split('_VIEW')[0].toLocaleLowerCase(); }, transformRequest: function transformRequest(value) { return value.concat('_VIEW').toLocaleUpperCase(); } }, { name: ViewField.id }, { name: ViewField.activeKey }, { name: ViewField.groupField, bind: "".concat(ViewField.viewProps, ".").concat(ViewField.groupField) }, { name: ViewField.viewHeight, bind: "".concat(ViewField.viewProps, ".").concat(ViewField.viewHeight) }, { name: ViewField.cardWidth, bind: "".concat(ViewField.viewProps, ".").concat(ViewField.cardWidth) }, { name: ViewField.showLabel, bind: "".concat(ViewField.viewProps, ".").concat(ViewField.showLabel) }, { name: ViewField.displayFields, bind: "".concat(ViewField.viewProps, ".").concat(ViewField.displayFields) }, { name: ViewField.combineSort, bind: "".concat(ViewField.viewProps, ".").concat(ViewField.combineSort) }, { name: ViewField.sort, bind: "".concat(ViewField.viewProps, ".").concat(ViewField.sort) }], events: { load: function load(_ref) { var dataSet = _ref.dataSet; var activeRecord = dataSet.find(function (record) { return record.get(ViewField.activeKey); }); if (activeRecord && viewVisible) { var viewType = activeRecord.get(ViewField.viewMode); if (viewVisible[viewType] !== false) { dataSet.current = dataSet.find(function (record) { return record.get(ViewField.activeKey); }); } else { dataSet.current = dataSet.find(function (record) { return record.get(ViewField.id) === '__DEFAULT__'; }); } } else { dataSet.current = dataSet.find(function (record) { return record.get(ViewField.id) === '__DEFAULT__'; }); } } } }); }, [customizedCode]); var displayFields = useMemo(function () { if (tableProps && tableProps.columns) { return tableProps.columns; } if (tableProps && tableProps.children) { var children = tableProps.children, aggregation = tableProps.aggregation; var generatedColumns = normalizeColumns(children, aggregation); return generatedColumns[0].concat(generatedColumns[1], generatedColumns[2]); } return []; }, [tableProps]); /** * 加载当前默认个性化视图数据(单个) */ var loadCustomized = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var customizedLoad, _card2, _kanban2, res, remoteCustomized, viewProps, defaultView; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!customizedCode) { _context.next = 15; break; } setLoaded(false); customizedLoad = getConfig('customizedLoad'); _context.prev = 3; _context.next = 6; return customizedLoad(customizedCode, 'Board', { type: 'default' }); case 6: res = _context.sent; remoteCustomized = res ? [res] : []; viewProps = { card: (_card2 = {}, _defineProperty(_card2, ViewField.cardWidth, 6), _defineProperty(_card2, ViewField.displayFields, displayFields.map(function (field) { return field.name; }).filter(Boolean).slice(0, 3)), _defineProperty(_card2, ViewField.showLabel, 1), _card2), table: {}, kanban: (_kanban2 = {}, _defineProperty(_kanban2, ViewField.cardWidth, 6), _defineProperty(_kanban2, ViewField.displayFields, displayFields.map(function (field) { return field.name; }).filter(Boolean).slice(0, 3)), _defineProperty(_kanban2, ViewField.showLabel, 1), _kanban2) }; defaultView = _objectSpread(_objectSpread({ code: customizedCode }, DEFAULTVIEW[defaultViewMode]), {}, _defineProperty({}, ViewField.viewProps, _objectSpread(_objectSpread({}, viewProps[defaultViewMode]), defaultViewProps[defaultViewMode]))); if (remoteCustomized && remoteCustomized.length) { remoteCustomized.push(defaultView); customizedDS.loadData(remoteCustomized); } else { customizedDS.loadData([defaultView]); } dataSet.setState(VIEWLISTDS, customizedDS); case 12: _context.prev = 12; setLoaded(true); return _context.finish(12); case 15: case "end": return _context.stop(); } } }, _callee, null, [[3,, 12, 15]]); })), [customizedCode]); useEffect(function () { if ($customizable) { loadCustomized(); } }, [$customizable, loadCustomized]); return loaded ? /*#__PURE__*/React.createElement(BoardWithContext, _extends({}, props, { customizedDS: customizedDS, customized: customized, customizedCode: customizedCode, setCustomized: setCustomized, customizable: $customizable })) : null; }; Board.displayName = 'Board'; Board.defaultProps = { onChange: noop, viewMode: ViewMode.table, queryFields: {}, autoQuery: false, viewVisible: true, commandsLimit: 1, defaultViewMode: ViewMode.table, defaultViewProps: {} }; export default Board; //# sourceMappingURL=Board.js.map