@beisen/ethos
Version:
beisencloud pc react components
402 lines (352 loc) • 12.3 kB
JavaScript
'use strict';
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _typeof2 = require('babel-runtime/helpers/typeof');
var _typeof3 = _interopRequireDefault(_typeof2);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _class, _temp; /* eslint-disable */
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactDom = require('react-dom');
var _CommonModel = require('./CommonModel');
var _CommonModel2 = _interopRequireDefault(_CommonModel);
var _MultiModel = require('./MultiModel');
var _MultiModel2 = _interopRequireDefault(_MultiModel);
require('./index.scss');
var _InitialUrls = require('./InitialUrls');
var _InitialUrls2 = _interopRequireDefault(_InitialUrls);
var _loading = require('../loading');
var _loading2 = _interopRequireDefault(_loading);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var defaultTranslation = {
advancedModeText: '高级模式',
tabItemTitleCommon: '常用',
tabItemTitleSubordinate: '直接下属',
tabItemTitleDepartment: '部门',
searchResultText: '搜索结果',
commonContactsText: '常用联系人',
commonSubordinateText: '直接下属',
clearAllSelectedText: '清空所选',
pleaseSearchText: '请搜索',
sureButtonTitle: '确定',
cancelButtonTitle: '取消',
multiHumanTitle: '已选人员',
personCount: '{0}人',
groupName: '组{0}个',
emptyText: '空 (未填写)',
emptyBgText: '这里什么都没有...'
};
var UserSelector = (_temp = _class = function (_Component) {
(0, _inherits3.default)(UserSelector, _Component);
function UserSelector(props) {
(0, _classCallCheck3.default)(this, UserSelector);
var _this = (0, _possibleConstructorReturn3.default)(this, (UserSelector.__proto__ || (0, _getPrototypeOf2.default)(UserSelector)).call(this, props));
_this._findIndex = function () {
if (!Array.prototype.findIndex) {
Array.prototype.findIndex = function (predicate) {
if (this === null) {
throw new TypeError('Array.prototype.findIndex called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return i;
}
}
return -1;
};
}
};
_this.clone = function (item) {
var self = _this;
if (!item) return item;
var types = [Number, String, Boolean],
result;
types.forEach(function (type) {
if (item instanceof type) {
result = type(item);
}
});
if (typeof result == 'undefined') {
if (Object.prototype.toString.call(item) === '[object Array]') {
result = [];
item.forEach(function (child, index, array) {
result[index] = self.clone(child);
});
} else if ((typeof item === 'undefined' ? 'undefined' : (0, _typeof3.default)(item)) == 'object') {
if (item.nodeType && typeof item.cloneNode == 'function') {
result = item.cloneNode(true);
} else if (!item.prototype) {
if (item instanceof Date) {
result = new Date(item);
} else {
result = {};
for (var i in item) {
result[i] = self.clone(item[i]);
}
}
} else {
result = item;
}
} else {
result = item;
}
}
return result;
};
_this.state = {
hidden: props.hidden || false
};
_this.translation = (0, _extends3.default)({}, defaultTranslation, props.translation);
_this.urls = new _InitialUrls2.default(props.TenantId, props.UserId);
_this.sortDepartments = _this.sortDepartments.bind(_this);
return _this;
}
(0, _createClass3.default)(UserSelector, [{
key: 'componentWillMount',
value: function componentWillMount() {}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
var _props = this.props,
isUseInitial = _props.isUseInitial,
apiType = _props.apiType,
hidden = _props.hidden;
if (hidden) return;
if (isUseInitial) {
this.props.getUsedInfo && this.props.getUsedInfo(this.urls['get' + apiType]('usedUserUrl'));
} else {
this.props.getUsedInfo && this.props.getUsedInfo();
}
this._findIndex();
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (this.props.hidden == true) {
if (nextProps.hidden == false && nextProps.hidden != this.props.hidden) {
if (nextProps.isUseInitial) {
this.props.getUsedInfo && this.props.getUsedInfo(this.urls['get' + nextProps.apiType]('usedUserUrl'));
} else {
this.props.getUsedInfo && this.props.getUsedInfo();
}
}
}
}
// 渲染普通模式
}, {
key: 'renderCommon',
value: function renderCommon() {
var _props2 = this.props,
usedusers = _props2.usedusers,
addResults = _props2.addResults,
setMulti = _props2.setMulti,
onSure = _props2.onSure,
closeComponent = _props2.closeComponent,
searchs = _props2.searchs,
getSearchInfo = _props2.getSearchInfo,
isUseInitial = _props2.isUseInitial,
apiType = _props2.apiType,
singleSelect = _props2.singleSelect,
isFetching = _props2.isFetching,
allowSearchNull = _props2.allowSearchNull,
showToMultiBtn = _props2.showToMultiBtn,
searchDataFormat = _props2.searchDataFormat,
isShowDepartmentName = _props2.isShowDepartmentName,
isSearchFetching = _props2.isSearchFetching,
_props2$searchPlaceho = _props2.searchPlaceholde,
searchPlaceholde = _props2$searchPlaceho === undefined ? this.translation.pleaseSearchText : _props2$searchPlaceho;
return _react2.default.createElement(_CommonModel2.default, {
usedusers: usedusers,
addResults: addResults,
changeToMulti: setMulti,
onSure: onSure,
showToMultiBtn: showToMultiBtn,
closeComponent: closeComponent,
searchs: searchs,
getSearchInfo: getSearchInfo,
urls: this.urls,
isUseInitial: isUseInitial,
searchPlaceholde: searchPlaceholde,
apiType: apiType,
singleSelect: singleSelect,
isFetching: isFetching,
allowSearchNull: allowSearchNull,
translation: this.translation,
isSearchFetching: isSearchFetching,
searchDataFormat: searchDataFormat,
isShowDepartmentName: isShowDepartmentName
});
}
// 渲染高级模式
}, {
key: 'renderMulti',
value: function renderMulti() {
var _props3 = this.props,
usedusers = _props3.usedusers,
results = _props3.results,
addResults = _props3.addResults,
getDepartments = _props3.getDepartments,
clearResults = _props3.clearResults,
staffs = _props3.staffs,
allowSearchNull = _props3.allowSearchNull,
getDepInfo = _props3.getDepInfo,
addAllUsers = _props3.addAllUsers,
closeComponent = _props3.closeComponent,
onSure = _props3.onSure,
onClose = _props3.onClose,
searchs = _props3.searchs,
getSearchInfo = _props3.getSearchInfo,
getStaff = _props3.getStaff,
getUsedInfo = _props3.getUsedInfo,
isUseInitial = _props3.isUseInitial,
apiType = _props3.apiType,
addDepartment = _props3.addDepartment,
hiddenTip = _props3.hiddenTip,
sideTip = _props3.sideTip,
isFetching = _props3.isFetching,
advancedModeMounted = _props3.advancedModeMounted,
hiddenTabDepartment = _props3.hiddenTabDepartment,
_props3$searchPlaceho = _props3.searchPlaceholde,
searchPlaceholde = _props3$searchPlaceho === undefined ? this.translation.pleaseSearchText : _props3$searchPlaceho,
searchDataFormat = _props3.searchDataFormat,
isShowDepartmentName = _props3.isShowDepartmentName;
var hasNoEmptyRes = results.filter(function (cmp, index) {
return cmp.Id != '#AllowSearchNull#';
});
var hasEmptyRes = results.filter(function (cmp, index) {
return cmp.Id == '#AllowSearchNull#';
});
return _react2.default.createElement(_MultiModel2.default, {
addDepartment: addDepartment,
getDepartments: getDepartments,
departments: this.sortDepartments(),
hiddenTabDepartment: hiddenTabDepartment,
usedusers: usedusers,
allowSearchNull: allowSearchNull,
results: hasNoEmptyRes,
emptyResult: hasEmptyRes,
addResults: addResults,
staffs: staffs,
clearResults: clearResults,
getDepInfo: getDepInfo,
addAllUsers: addAllUsers,
closeComponent: closeComponent,
searchPlaceholde: searchPlaceholde,
onSure: onSure,
onClose: onClose,
searchs: searchs,
getSearchInfo: getSearchInfo,
getStaff: getStaff,
getUsedInfo: getUsedInfo,
urls: this.urls,
isUseInitial: isUseInitial,
isFetching: isFetching,
apiType: apiType,
hiddenTip: hiddenTip,
sideTip: sideTip,
advancedModeMounted: advancedModeMounted,
translation: this.translation,
searchDataFormat: searchDataFormat,
isShowDepartmentName: isShowDepartmentName
});
}
// 高级模式清除所有
}, {
key: 'clearAllResults',
value: function clearAllResults() {
this.state.results.length = 0;
this.setState(this.state);
}
}, {
key: 'removeResults',
// 高级模式清除单个
value: function removeResults(id) {
var results = this.state.results;
var temp = results && results.findIndex(function (value, index, arr) {
return value.Id === id;
});
this.state.results.splice(temp, 1);
this.setState(this.state);
}
}, {
key: 'sortDepartments',
// 数据排序
value: function sortDepartments() {
var departments = this.props.departments;
var tempDep = this.clone(departments);
if (tempDep.length == 0) return [];
var sortDep = [],
hash = {},
id = 'Id',
pid = 'PId',
children = 'childDep',
i = 0,
j = 0,
len = tempDep.length;
for (; i < len; i++) {
if (tempDep[i][id] >= 0) {
hash[tempDep[i][id]] = tempDep[i];
} else {
delete tempDep[i];
}
}
for (; j < len; j++) {
var temp = tempDep[j];
if (temp) {
var hashChild = temp[pid] != temp[id] ? hash[temp[pid]] : null;
if (hashChild) {
!hashChild[children] && (hashChild[children] = []);
hashChild[children].push(temp);
} else {
sortDep.push(temp);
}
}
}
return sortDep;
}
}, {
key: 'render',
value: function render() {
var _props4 = this.props,
multi = _props4.multi,
offset = _props4.offset,
showCommonMode = _props4.showCommonMode;
var hidden = this.props.hidden;
if (!hidden) {
var renderCon = multi && !showCommonMode ? this.renderMulti() : this.renderCommon();
var isWin10 = navigator.userAgent.indexOf('Windows NT 10.0') > -1 && !!window.ActiveXObject || 'ActiveXObject' in window;
return _react2.default.createElement(
'div',
{ className: 'us-container', style: offset },
this.props.isFetching && !isWin10 ? _react2.default.createElement(_loading2.default, { style: { borderRadius: '5px' }, type: 'small' }) : _react2.default.createElement(_loading2.default, { type: 'small', hidden: 'true' }),
renderCon
);
} else {
return _react2.default.createElement('div', null);
}
}
}]);
return UserSelector;
}(_react.Component), _class.defaultProps = {
hiddenTip: false,
sideTip: false,
showToMultiBtn: true
}, _temp);
module.exports = UserSelector;