@sula/ruler
Version:
183 lines (146 loc) • 5.82 kB
JavaScript
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
import * as React from 'react';
import assign from 'lodash/assign';
import isUndefined from 'lodash/isUndefined';
import { HOOK_MARK } from './RulerContext';
import { matchNameList } from './utils/lang';
var ContextStore = function ContextStore(formInstance) {
var _this = this;
_classCallCheck(this, ContextStore);
this.formInstance = void 0;
this.callbacks = {};
this.global = {};
this.fieldEntities = [];
this.getContext = function () {
return {
getRuler: _this.getRuler,
getInternalHooks: _this.getInternalHooks
};
};
this.getRuler = function () {
return _objectSpread({}, _this.formInstance, {
/**
* 扩展的方法
*/
setSourceByFieldName: _this.setSourceByFieldName,
setValueByFieldName: _this.setValueByFieldName,
setFieldSource: _this.setFieldSource
});
};
this.setFieldSource = function (source, name, row, list) {
var fieldEntitiesByName = _this.getFieldEntitiesByFieldName(name, list);
if (fieldEntitiesByName.length) {
fieldEntitiesByName.forEach(function (fieldEntity, rowIndex) {
if (row === rowIndex) {
fieldEntity.setSource(source);
}
});
}
};
this.setSourceByFieldName = function (source, name, list) {
var fieldEntitiesByName = _this.getFieldEntitiesByFieldName(name, list);
if (fieldEntitiesByName.length) {
fieldEntitiesByName.forEach(function (fieldEntity) {
fieldEntity.setSource(source);
});
}
};
this.setValueByFieldName = function (value, name, list) {
var fieldEntitiesByName = _this.getFieldEntitiesByFieldName(name, list);
if (fieldEntitiesByName.length) {
fieldEntitiesByName.forEach(function (fieldEntity) {
fieldEntity.setValue(value);
});
}
};
this.getInternalHooks = function (key) {
if (key === HOOK_MARK) {
return {
getCtx: _this.getCtx,
setCallbacks: _this.setCallbacks,
fieldValueChange: _this.fieldValueChange,
fieldDidMount: _this.fieldDidMount,
setGlobal: function setGlobal(globalConf) {
_this.global = globalConf;
},
getGlobal: function getGlobal(name) {
return _this.global[name];
},
registerField: _this.registerField,
getFieldEntities: function getFieldEntities() {
return _this.fieldEntities;
},
getFieldEntity: _this.getFieldEntity
};
}
return null;
};
this.registerField = function (entity) {
_this.fieldEntities.push(entity);
return function () {
_this.fieldEntities = _this.fieldEntities.filter(function (item) {
return item !== entity;
});
};
};
this.getFieldEntity = function (nameList) {
for (var i = 0, len = _this.fieldEntities.length; i < len; i += 1) {
var fieldEntity = _this.fieldEntities[i];
var fieldNameList = fieldEntity.getName();
if (matchNameList(fieldNameList, nameList)) {
return fieldEntity;
}
}
};
this.getFieldEntitiesByFieldName = function (name, list) {
var fieldEntities = [];
for (var i = 0, len = _this.fieldEntities.length; i < len; i += 1) {
var fieldEntity = _this.fieldEntities[i];
var fieldNameList = fieldEntity.getName(); // @ts-ignore
if (fieldNameList[fieldNameList.length - 1] === name) {
if (!isUndefined(list)) {
if (fieldNameList[0] === list) {
fieldEntities.push(fieldEntity);
}
} else {
fieldEntities.push(fieldEntity);
}
}
}
return fieldEntities;
};
this.getCtx = function () {
var extraCtx = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var form = _this.getRuler();
return assign({}, {
form: form
}, extraCtx);
};
this.setCallbacks = function (callbacks) {
_this.callbacks = callbacks;
};
this.fieldValueChange = function (ctx) {
var onFieldValueChange = _this.callbacks.onFieldValueChange;
if (onFieldValueChange) {
onFieldValueChange(ctx);
}
};
this.fieldDidMount = function (ctx) {
var onFieldDidMount = _this.callbacks.onFieldDidMount;
if (onFieldDidMount) {
onFieldDidMount(ctx);
}
};
this.formInstance = formInstance;
};
export default function useContext(formInstance) {
var contextRef = React.useRef(null);
if (!contextRef.current) {
var contextStore = new ContextStore(formInstance);
contextRef.current = contextStore.getContext();
}
return [contextRef.current];
}