@farris/command-services-vue
Version:
Render schema to web page with farris ui.
1,499 lines (1,497 loc) • 95.8 kB
JavaScript
(function(global, factory) {
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("@farris/devkit-vue"), require("@farris/bef-vue"), require("lodash"), require("moment")) : typeof define === "function" && define.amd ? define(["exports", "@farris/devkit-vue", "@farris/bef-vue", "lodash", "moment"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.FarrisCommandServicesVue = {}, global.devkitVue, global.befVue, global.lodash, global.moment));
})(this, function(exports2, devkitVue, befVue, lodash, moment) {
"use strict";var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
const MESSAGE_BOX_SERVICE_TOKEN = new devkitVue.InjectionToken("@farris/command-services-message-box-service-token");
const NOTIFY_SERVICE_TOKEN = new devkitVue.InjectionToken("@farris/command-services-notify-service-token");
const LOADING_SERVICE_TOKEN = new devkitVue.InjectionToken("@farris/command-services-loading-service-token");
class FormNotifyService {
/**
* 构造函数
*/
constructor(injector) {
__publicField(this, "notifyService", null);
this.injector = injector;
if (this.injector) {
this.notifyService = this.injector.get(NOTIFY_SERVICE_TOKEN);
this.notifyService.globalConfig.position = "top-center";
}
}
/**
* 信息提示
* @param content 内容
*/
info(content) {
const notifyOptions = {
message: content,
timeout: 3e3
};
return this.notifyService.info(notifyOptions);
}
/**
* 成功提示
* @param content 内容
*/
success(content) {
const notifyOptions = {
message: content,
timeout: 3e3
};
this.notifyService.success(notifyOptions);
}
/**
* 警告提示
* @param content 内容
*/
warning(content) {
const notifyOptions = {
message: content,
timeout: 3e3
};
this.notifyService.warning(notifyOptions);
}
/**
* 错误提示
* @param content 内容
*/
error(content) {
const notifyOptions = {
message: content,
timeout: 3e3
};
this.notifyService.error(notifyOptions);
}
}
class BaseDataService {
/**
* 构造函数
*/
constructor(viewModel) {
/**
* 视图模型
*/
__publicField(this, "viewModel");
/**
* 数据仓库
*/
__publicField(this, "repository");
/**
* 实体状态
*/
__publicField(this, "entityState");
/**
* 表单
*/
__publicField(this, "form");
this.viewModel = viewModel;
this.repository = viewModel.respository;
this.entityState = viewModel.entityState;
this.form = viewModel.form;
}
/**
* 获取服务实例
*/
getService(token, defaultValue) {
const injector = this.viewModel.getInjector();
return injector.get(token, defaultValue);
}
/**
* 转换成功消息
* @param successMessage
* @returns
*/
parseSuccessMessage(successMessage) {
if (successMessage && successMessage.trim()) {
let showMessage = true;
if (successMessage.startsWith("{") && successMessage.endsWith("}")) {
try {
const options = JSON.parse(successMessage);
if (options.showMessage === false) {
showMessage = false;
}
} catch (e) {
}
}
return { hasMessage: true, showMessage, message: successMessage };
}
return { hasMessage: false };
}
parseBoolean(value, defaultValue = false) {
if (typeof value === "boolean") {
return value;
}
if (typeof value !== "string") {
return defaultValue;
}
if (value.toLowerCase().trim() === "true") {
return true;
} else if (value.toLowerCase().trim() === "false") {
return false;
} else {
return defaultValue;
}
}
displayMessage(parsedMessage, defaultMessage) {
const { hasMessage, showMessage = true, message = null } = parsedMessage;
const formNotifyService = this.viewModel.getInjector().get(FormNotifyService);
if (formNotifyService) {
const text = hasMessage ? message : defaultMessage;
formNotifyService.success(text);
}
}
}
class LoadDataService extends BaseDataService {
/**
* 构造函数
*/
constructor(viewModel, formLoadingService) {
super(viewModel);
this.formLoadingService = formLoadingService;
}
/**
* 加载数据
* @param filters 过滤条件
* @param sorts 排序条件
*/
load(filters, sorts, pageSize, pageIndex) {
pageSize = this.parsePageSize(pageSize);
pageIndex = this.parsePageIndex(pageIndex);
const mergedFilterConditions = this.mergeFilterConditions(filters);
const mergedSortConditions = this.mergeSortConditions(sorts);
this.formLoadingService.showLoadingWithDelay();
const loadPromise = this.repository.getEntities(mergedFilterConditions, mergedSortConditions, pageSize, pageIndex).then((entities) => {
this.entityState.loadEntities(entities);
return entities;
}).finally(() => {
this.formLoadingService.hide();
});
return loadPromise;
}
/**
*加载实体
*/
loadById(id) {
this.formLoadingService.showLoadingWithDelay();
const loadPromise = this.repository.getEntityById(id).then((entity) => {
this.entityState.loadEntities([entity]);
return entity;
}).finally(() => {
this.formLoadingService.hide();
});
return loadPromise;
}
/**
* 转换分页大小
* @param pageSize 分页大小
* @returns
*/
parsePageSize(pageSize) {
const repository = this.viewModel.respository;
if (pageSize !== 0) {
pageSize = pageSize || repository.pageSize;
}
return pageSize;
}
/**
* 转换页码
* @param pageIndex 页码
* @returns
*/
parsePageIndex(pageIndex) {
const repository = this.viewModel.respository;
if (!pageIndex) {
pageIndex = repository.pageIndex;
}
return pageIndex;
}
/**
* 合并过滤条件
* @param filters
* @returns
*/
mergeFilterConditions(filters) {
filters = !filters ? "[]" : filters;
if (typeof filters === "string") {
filters = JSON.parse(filters);
}
return filters;
}
/**
* 合并排序条件
* @param sorts
* @returns
*/
mergeSortConditions(sorts) {
sorts = !sorts ? "[]" : sorts;
if (typeof sorts === "string") {
sorts = JSON.parse(sorts);
}
return sorts;
}
}
class CreateDataService extends BaseDataService {
/**
* 构造函数
*/
constructor(viewModel, formLoadingService) {
super(viewModel);
this.formLoadingService = formLoadingService;
}
/**
* 新增数据
*/
create() {
this.formLoadingService.showLoadingWithDelay();
const createPromise = this.repository.createEntity().then((entity) => {
this.entityState.loadEntities([entity]);
return entity;
}).finally(() => {
this.formLoadingService.hide();
});
return createPromise;
}
/**
* 追加新数据
*/
append() {
this.formLoadingService.showLoadingWithDelay();
const createPromise = this.repository.createEntity().then((entity) => {
this.entityState.appendEntities([entity]);
return entity;
}).finally(() => {
this.formLoadingService.hide();
});
return createPromise;
}
}
class RemoveDataService extends BaseDataService {
/**
* 构造函数
*/
constructor(viewModel, formMessageService, formLoadingService, languageService) {
super(viewModel);
this.formMessageService = formMessageService;
this.formLoadingService = formLoadingService;
this.languageService = languageService;
}
/**
* 删除方法
* @param id 实体主键
* @param ifSave 是否保存
* @param successMessage 删除成功提示消息
*/
remove(id, ifSave, successMessage) {
if (!id) {
const currentEntity = this.entityState.getCurrentEntity();
id = currentEntity.idValue;
}
if (!id) {
return;
}
if (!this.formMessageService) {
return;
}
return this.formMessageService.confirm(this.languageService.language.confirmDeletion).then((result) => {
if (!result) {
return;
}
this.formLoadingService.showLoadingWithDelay();
const parsedMessage = this.parseSuccessMessage(successMessage);
const removePromsie = this.repository.removeAndSaveEntityById(id).then(() => {
this.entityState.removeEntityById(id);
this.displayMessage(parsedMessage, this.languageService.language.deleteSuccess);
}).finally(() => {
this.formLoadingService.hide();
});
return removePromsie;
});
}
/**
* 批量删除数据
* @param ids 数据主键数组
* @param ifSave 是否保存
* @param successMessage 保存成功提示信息
*/
removeRows(ids, ifSave, successMessage) {
if (typeof ids === "string") {
ids = ids.split(",").filter((item) => item);
}
ifSave = this.parseBoolean(ifSave, true);
const parsedMessage = this.parseSuccessMessage(successMessage);
this.formLoadingService.showLoadingWithDelay();
return this.repository.removeEntitiesByIds(ids).then(() => {
this.entityState.removeEntitiesByIds(ids);
this.displayMessage(parsedMessage, this.languageService.language.deleteSuccess);
}).finally(() => {
this.formLoadingService.hide();
});
}
}
class SaveDataService extends BaseDataService {
/**
* 构造函数
*/
constructor(viewModel, formNotifyService, formLoadingService, languageService) {
super(viewModel);
this.viewModel = viewModel;
this.formNotifyService = formNotifyService;
this.formLoadingService = formLoadingService;
this.languageService = languageService;
}
/**
* 保存成功
*/
save() {
this.formLoadingService.showLoadingWithDelay();
return this.repository.saveEntityChanges().then(() => {
this.formNotifyService.success(this.languageService.language.saveSuccess);
}).finally(() => {
this.formLoadingService.hide();
});
}
}
class UpdateDataService extends BaseDataService {
/**
* 构造函数
*/
constructor(viewModel, formLoadingService) {
super(viewModel);
this.formLoadingService = formLoadingService;
}
/**
* 加载实体
*/
update(id) {
this.formLoadingService.showLoadingWithDelay();
const loadPromise = this.repository.getEntityById(id).then((newEntity) => {
var _a;
const newEntityData = newEntity.toJSON();
(_a = this.viewModel.entityState) == null ? void 0 : _a.getCurrentEntity().loadData(newEntityData);
}).finally(() => {
this.formLoadingService.hide();
});
return loadPromise;
}
}
class CancelDataService extends BaseDataService {
/**
* 构造函数
*/
constructor(viewModel, formLoadingService, entityChangeService, formMessageService, languageService) {
super(viewModel);
this.formLoadingService = formLoadingService;
this.entityChangeService = entityChangeService;
this.formMessageService = formMessageService;
this.languageService = languageService;
}
/**
* 取消方法
*/
cancel(showConfirm = true) {
return this.entityChangeService.hasChanges().then((changed) => {
const confirm = changed && showConfirm ? this.formMessageService.confirm(this.languageService.language.confirmCancel) : Promise.resolve(true);
return confirm.then((result) => {
if (result) {
this.formLoadingService.showLoadingWithDelay();
return this.repository.cancelEntityChanges().then(() => {
const updateService = this.getService(UpdateDataService);
const currentEntity = this.entityState.getCurrentEntity();
return updateService.update(currentEntity.idValue);
}).finally(() => {
this.formLoadingService.hide();
});
}
});
});
}
}
class CardDataService extends BaseDataService {
constructor(viewModel, formLoadingService, formNotifyService, languageService, entityChangeService, formMessageService) {
super(viewModel);
this.formLoadingService = formLoadingService;
this.formNotifyService = formNotifyService;
this.languageService = languageService;
this.entityChangeService = entityChangeService;
this.formMessageService = formMessageService;
}
load(id) {
this.formLoadingService.showLoadingWithDelay();
const loadPromise = this.repository.getEntityById(id).then((newEntity) => {
var _a;
(_a = this.viewModel.entityState) == null ? void 0 : _a.loadEntities([newEntity]);
}).finally(() => {
this.formLoadingService.hide();
});
return loadPromise;
}
onLoading(transitionActionParamName) {
}
add() {
this.formLoadingService.showLoadingWithDelay();
const createPromise = this.repository.createEntity().then((entity) => {
this.entityState.loadEntities([entity]);
return entity;
}).finally(() => {
this.formLoadingService.hide();
});
return createPromise;
}
checkBeforeUpdate() {
const currentEntity = this.entityState.getCurrentEntity();
const id = currentEntity.idValue;
if (!id) {
this.formNotifyService.warning(this.languageService.language.noDataExist);
return Promise.reject();
}
return Promise.resolve();
}
edit() {
return this.update();
}
update() {
const currentEntity = this.entityState.getCurrentEntity();
const id = currentEntity.idValue;
if (!id) {
return Promise.reject();
}
this.formLoadingService.showLoadingWithDelay();
const loadPromise = this.repository.getEntityById(id).then((newEntity) => {
var _a;
const newEntityData = newEntity.toJSON();
(_a = this.viewModel.entityState) == null ? void 0 : _a.getCurrentEntity().loadData(newEntityData);
}).finally(() => {
this.formLoadingService.hide();
});
return loadPromise;
}
save(successMessage) {
this.formLoadingService.showLoadingWithDelay();
return this.repository.saveEntityChanges().then(() => {
const parsedMessage = this.parseSuccessMessage(successMessage);
this.displayMessage(parsedMessage, this.languageService.language.saveSuccess);
}).finally(() => {
this.formLoadingService.hide();
});
}
cancel(showConfirm = true) {
return this.entityChangeService.hasChanges().then((changed) => {
const confirm = changed && showConfirm ? this.formMessageService.confirm(this.languageService.language.confirmCancel) : Promise.resolve(true);
return confirm.then((result) => {
if (result) {
this.formLoadingService.showLoadingWithDelay();
return this.repository.cancelEntityChanges().then(() => {
const updateService = this.getService(UpdateDataService);
const currentEntity = this.entityState.getCurrentEntity();
return updateService.update(currentEntity.idValue);
}).finally(() => {
this.formLoadingService.hide();
});
}
});
});
}
reload() {
const currentEntity = this.entityState.getCurrentEntity();
const id = currentEntity.idValue;
return this.load(id);
}
}
class CommandService {
constructor(injector, viewModel) {
this.injector = injector;
this.viewModel = viewModel;
}
execute(commandName, viewModelId) {
if (!commandName) {
console.warn("Invalid command name!");
return;
}
if (viewModelId) {
const app = this.injector.get(devkitVue.App);
const viewModel = app.getViewModelById(viewModelId);
if (!viewModel) {
console.warn(`There is no viewmodel with id ${viewModelId}`);
return;
}
return viewModel[commandName]();
} else {
return this.viewModel[commandName]();
}
}
}
class ListDataService extends BaseDataService {
constructor(viewModel, formLoadingService, formMessageService, languageService, formNotifyService) {
super(viewModel);
this.formLoadingService = formLoadingService;
this.formMessageService = formMessageService;
this.languageService = languageService;
this.formNotifyService = formNotifyService;
}
load(filter, sort) {
const mergedFilterConditions = this.mergeFilterConditions(filter);
const mergedSortConditions = this.mergeSortConditions(sort);
this.formLoadingService.showLoadingWithDelay();
const loadPromise = this.repository.getEntities(mergedFilterConditions, mergedSortConditions).then((entities) => {
this.entityState.loadEntities(entities);
return entities;
}).finally(() => {
this.formLoadingService.hide();
});
return loadPromise;
}
filter(filter, sort) {
return this.load(filter, sort);
}
query(filter, sort, pageSize, pageIndex) {
pageSize = this.parsePageSize(pageSize);
pageIndex = this.parsePageIndex(pageIndex);
const mergedFilterConditions = this.mergeFilterConditions(filter);
const mergedSortConditions = this.mergeSortConditions(sort);
this.formLoadingService.showLoadingWithDelay();
const loadPromise = this.repository.getEntities(mergedFilterConditions, mergedSortConditions, pageSize, pageIndex).then((entities) => {
this.entityState.loadEntities(entities);
return entities;
}).finally(() => {
this.formLoadingService.hide();
});
return loadPromise;
}
removeRows(ids, ifSave, successMessage) {
if (typeof ids === "string") {
ids = ids.split(",").filter((item) => item);
}
ifSave = this.parseBoolean(ifSave, true);
const parsedMessage = this.parseSuccessMessage(successMessage);
this.formLoadingService.showLoadingWithDelay();
return this.repository.removeEntitiesByIds(ids).then(() => {
this.entityState.removeEntitiesByIds(ids);
this.displayMessage(parsedMessage, this.languageService.language.deleteSuccess);
}).finally(() => {
this.formLoadingService.hide();
});
}
remove(id, ifSave, successMessage, confirm = true, breakable = true) {
if (!id) {
this.formNotifyService.warning(this.languageService.language.plsSelectDeleteData);
return Promise.reject();
}
return this.formMessageService.confirm(this.languageService.language.confirmDeletion).then((result) => {
if (!result) {
return;
}
this.formLoadingService.showLoadingWithDelay();
const parsedMessage = this.parseSuccessMessage(successMessage);
const removePromsie = this.repository.removeAndSaveEntityById(id).then(() => {
this.entityState.removeEntityById(id);
this.displayMessage(parsedMessage, this.languageService.language.deleteSuccess);
}).finally(() => {
this.formLoadingService.hide();
});
return removePromsie;
});
}
refreshAfterRemoving(loadCmdName, loadCmdFrameId) {
if (this.viewModel && loadCmdName && loadCmdFrameId) {
const commandService = this.viewModel.getInjector().get(CommandService, void 0);
return commandService.execute(loadCmdName, loadCmdFrameId);
}
return this.load();
}
refresh(loadCmdName, loadCmdFrameId) {
if (this.viewModel && loadCmdName && loadCmdFrameId) {
const commandService = this.viewModel.getInjector().get(CommandService, void 0);
return commandService.execute(loadCmdName, loadCmdFrameId);
}
return this.load();
}
append() {
this.formLoadingService.showLoadingWithDelay();
const createPromise = this.repository.createEntity().then((entity) => {
this.entityState.appendEntities([entity]);
return entity;
}).finally(() => {
this.formLoadingService.hide();
});
return createPromise;
}
/**
* 合并过滤条件
* @param filters
* @returns
*/
mergeFilterConditions(filters) {
filters = !filters ? "[]" : filters;
if (typeof filters === "string") {
filters = JSON.parse(filters);
}
return filters;
}
/**
* 合并排序条件
* @param sorts
* @returns
*/
mergeSortConditions(sorts) {
sorts = !sorts ? "[]" : sorts;
if (typeof sorts === "string") {
sorts = JSON.parse(sorts);
}
return sorts;
}
/**
* 转换分页大小
* @param pageSize 分页大小
* @returns
*/
parsePageSize(pageSize) {
const repository = this.viewModel.respository;
if (pageSize !== 0) {
pageSize = pageSize || repository.pageSize;
}
return pageSize;
}
/**
* 转换页码
* @param pageIndex 页码
* @returns
*/
parsePageIndex(pageIndex) {
const repository = this.viewModel.respository;
if (!pageIndex) {
pageIndex = repository.pageIndex;
}
return pageIndex;
}
}
class SubListDataService extends BaseDataService {
constructor(viewModel, formLoadingService) {
super(viewModel);
this.formLoadingService = formLoadingService;
}
add() {
const path = this.getPath();
this.formLoadingService.showLoadingWithDelay();
this.repository.createEntityByPath(path, void 0).then((entity) => {
this.entityState.appendEntitesByPath(path, [entity]);
}).finally(() => {
this.formLoadingService.hide();
});
}
remove(id) {
const path = this.getPath();
this.formLoadingService.showLoadingWithDelay();
this.repository.removeEntityByPath(path, id).then(() => {
this.entityState.removeEntitiesByPath(path, [id]);
}).finally(() => {
this.formLoadingService.hide();
});
}
getPath() {
const bindingPath = this.viewModel.bindingPath;
const rid = this.viewModel.bindingData.list.currentId;
let path = "/" + rid;
const subPaths = bindingPath.split("/");
if (subPaths.length > 0) {
for (let index = 1; index < subPaths.length - 1; index++) {
const subPath = subPaths[index];
const subData = this.viewModel.bindingData[subPath];
if (!subData || !subData.currentId) {
throw Error(`获取子表完整路径出错,找不到${subData}对应的子表,或对应子表没有当前行。`);
}
path += `/${subPath}/${subData.currentId}`;
}
}
path += "/" + subPaths[subPaths.length - 1];
return path;
}
}
const AppType = {
App: "app",
Menu: "menu",
Other: "other"
};
const TAB_EVENT = {
/**
* Tab关闭后
*/
onTabClosed: "FuncClosed",
/**
* Tab关闭前
*/
onTabClosing: "beforeFuncCloseEvent",
/**
* Tab切换
*/
onTabSwitched: "funcSwitchEvent"
};
var HierarchyType = /* @__PURE__ */ ((HierarchyType2) => {
HierarchyType2["parent"] = "parent";
HierarchyType2["path"] = "path";
return HierarchyType2;
})(HierarchyType || {});
var ViewState = /* @__PURE__ */ ((ViewState2) => {
ViewState2["Initial"] = "Initial";
ViewState2["Add"] = "Add";
ViewState2["Edit"] = "Edit";
return ViewState2;
})(ViewState || {});
class ParentTreeNodeService {
getNextNodeId(treeNodesData, hierarchyInfoKey, currentId) {
const currentNodeData = treeNodesData.find((itemData) => {
return itemData["id"] === currentId;
});
const currentLayer = currentNodeData[hierarchyInfoKey]["layer"];
const fLayer = currentLayer - 1;
const fParentElement = currentNodeData[hierarchyInfoKey]["parentElement"];
const siblingtreeNodesData = this.getChildNodesData(treeNodesData, hierarchyInfoKey, fLayer, fParentElement);
if (siblingtreeNodesData.length === 1) {
const parentData = treeNodesData.find((itemData) => {
return itemData["id"] === fParentElement;
});
if (!parentData) {
return this.getFirstNodeId(treeNodesData, hierarchyInfoKey);
}
return parentData["id"];
} else {
return this.getNextSiblingNodeId(siblingtreeNodesData, currentId);
}
}
getNextSiblingNodeId(siblingtreeNodesData, currentId) {
if (siblingtreeNodesData.length <= 1) {
return "";
}
const currentIndex = siblingtreeNodesData.findIndex((itemData) => {
return itemData["id"] === currentId;
});
let nextIndex = -1;
if (currentIndex === siblingtreeNodesData.length - 1) {
nextIndex = currentIndex - 1;
} else {
nextIndex = currentIndex + 1;
}
return siblingtreeNodesData[nextIndex]["id"];
}
getChildNodesData(treeNodesData, hierarchyInfoKey, fLayer, fParentElement) {
const childtreeNodesData = treeNodesData.filter((itemData) => {
const layer = itemData[hierarchyInfoKey]["layer"];
const parentElement = itemData[hierarchyInfoKey]["parentElement"];
return layer === fLayer + 1 && fParentElement == parentElement;
});
return childtreeNodesData;
}
getFirstNodeId(treeNodesData, hierarchyInfoKey) {
let rootData = treeNodesData.find((itemData) => {
const layer = itemData[hierarchyInfoKey]["layer"];
return layer === 1;
});
if (!rootData) {
const rootLayer = this.getRootLayer(treeNodesData, hierarchyInfoKey);
rootData = treeNodesData.find((itemData) => {
const layer = itemData[hierarchyInfoKey]["layer"];
return layer === rootLayer;
});
}
return rootData ? rootData["id"] : "";
}
getRootLayer(treeNodesData, hierarchyInfoKey) {
let layer = null;
if (treeNodesData && Array.isArray(treeNodesData)) {
const layers = treeNodesData.map((item) => {
const layer2 = item[hierarchyInfoKey]["layer"];
return layer2;
});
const minLayer = Math.min.apply(Math, layers);
if (!isNaN(minLayer)) {
layer = minLayer;
}
}
return layer;
}
}
class PathTreeNodeService {
getFirstNodeId(treeNodesData, hierarchyInfoKey) {
let rootData = treeNodesData.find((itemData) => {
const layer = itemData[hierarchyInfoKey]["layer"];
return layer === 1;
});
if (!rootData) {
const rootLayer = this.getRootLayer(treeNodesData, hierarchyInfoKey);
rootData = treeNodesData.find((itemData) => {
const layer = itemData[hierarchyInfoKey]["layer"];
return layer === rootLayer;
});
}
return rootData ? rootData["id"] : "";
}
getRootLayer(treeNodesData, hierarchyInfoKey) {
let layer = null;
if (treeNodesData && Array.isArray(treeNodesData)) {
const layers = treeNodesData.map((item) => {
const layer2 = item[hierarchyInfoKey]["layer"];
return layer2;
});
const minLayer = Math.min.apply(Math, layers);
if (!isNaN(minLayer)) {
layer = minLayer;
}
}
return layer;
}
/**
* 获取下一个节点(删除后)
*/
getNextNodeId(treeNodesData, hierarchyInfoKey, currentId) {
const currentNodeData = treeNodesData.find((itemData) => {
return itemData["id"] === currentId;
});
const currentPath = currentNodeData[hierarchyInfoKey]["path"];
const currentLayer = currentNodeData[hierarchyInfoKey]["layer"];
const fLayer = currentLayer - 1;
const fPath = currentPath.substring(0, currentPath.length - 4);
const siblingtreeNodesData = this.getChildNodesData(treeNodesData, hierarchyInfoKey, fLayer, fPath);
if (siblingtreeNodesData.length === 1) {
const parentData = treeNodesData.find((itemData) => {
return itemData[hierarchyInfoKey]["path"] === fPath;
});
if (!parentData) {
return this.getFirstNodeId(treeNodesData, hierarchyInfoKey);
}
return parentData["id"];
} else {
return this.getNextSiblingNodeId(siblingtreeNodesData, currentId);
}
}
/**
* 获取下个兄弟节点的id
*/
getNextSiblingNodeId(siblingtreeNodesData, currentId) {
if (siblingtreeNodesData.length <= 1) {
return "";
}
const currentIndex = siblingtreeNodesData.findIndex((itemData) => {
return itemData["id"] === currentId;
});
let nextIndex = -1;
if (currentIndex === siblingtreeNodesData.length - 1) {
nextIndex = currentIndex - 1;
} else {
nextIndex = currentIndex + 1;
}
return siblingtreeNodesData[nextIndex]["id"];
}
/**
* 获取下级节点的BindingObjects集合
*/
getChildNodesData(treeNodesData, hierarchyInfoKey, fLayer, fPath) {
const childtreeNodesData = treeNodesData.filter((itemData) => {
const layer = itemData[hierarchyInfoKey]["layer"];
const path = itemData[hierarchyInfoKey]["path"];
return layer === fLayer + 1 && path.startsWith(fPath);
});
return childtreeNodesData;
}
/**
* 获取id获取节点数据
*/
getNodeDataById(treeNodesData, id) {
const nodeData = treeNodesData.find((itemData) => {
return itemData["id"] === id;
});
return nodeData ? nodeData : null;
}
}
class TreeDataService extends BaseDataService {
constructor(viewModel, formLoadingService, formNotifyService, formMessageService, languageService, stateService, entityChangeService) {
super(viewModel);
// 上次新增同级或子级的节点id
__publicField(this, "lastModifiedId", null);
this.viewModel = viewModel;
this.formLoadingService = formLoadingService;
this.formNotifyService = formNotifyService;
this.formMessageService = formMessageService;
this.languageService = languageService;
this.stateService = stateService;
this.entityChangeService = entityChangeService;
}
load(filters, sorts) {
const loadPromise = this.repository.getEntities([], [], 0, 1).then((entities) => {
this.entityState.loadEntities(entities);
return entities;
}).finally(() => {
this.lastModifiedId = null;
this.formLoadingService.hide();
});
return loadPromise;
}
addSibling(id) {
var _a;
id = id ? id : (_a = this.viewModel.entityState) == null ? void 0 : _a.getCurrentEntity().idValue;
if (!id) {
id = "";
}
const hierarchyType = this.getHierarchyType();
if (!hierarchyType) {
console.error(`Invalid hierarchy key!`);
return;
}
const treeRepository = this.getTreeRepository(hierarchyType);
this.formLoadingService.showLoadingWithDelay();
return treeRepository == null ? void 0 : treeRepository.addSibling(id).finally(() => {
this.lastModifiedId = id;
this.stateService.transitTo(ViewState.Add);
this.formLoadingService.hide();
});
}
/**
* 新增子级
* @param id
* @returns
*/
addChild(id) {
var _a;
id = id ? id : (_a = this.viewModel.entityState) == null ? void 0 : _a.getCurrentEntity().idValue;
if (!id) {
this.formNotifyService.warning(this.languageService.language.plsSelectParentNode);
return Promise.reject();
}
const hierarchyType = this.getHierarchyType();
if (!hierarchyType) {
console.error(`Invalid hierarchy key!`);
return;
}
const treeRepository = this.getTreeRepository(hierarchyType);
return treeRepository == null ? void 0 : treeRepository.addChild(id).finally(() => {
this.lastModifiedId = id;
this.stateService.transitTo(ViewState.Add);
this.formLoadingService.hide();
});
}
remove(id, successMessage) {
var _a;
id = id ? id : (_a = this.viewModel.entityState) == null ? void 0 : _a.getCurrentEntity().idValue;
if (!id) {
this.formNotifyService.warning(this.languageService.language.plsSelectDeleteData);
return Promise.reject();
}
return this.formMessageService.confirm(this.languageService.language.confirmDeletion).then((result) => {
if (result) {
return this.repository.removeEntityById(id).then(() => {
this.updateCurrentRow(id);
const parsedMessage = this.parseSuccessMessage(successMessage);
this.displayMessage(parsedMessage, this.languageService.language.deleteSuccess);
});
}
});
}
save() {
this.formLoadingService.showLoadingWithDelay();
return this.repository.saveEntityChanges().finally(() => {
this.stateService.transitTo(ViewState.Initial);
this.lastModifiedId = null;
this.formLoadingService.hide();
});
}
cancel() {
var _a;
const currentId = (_a = this.viewModel.entityState) == null ? void 0 : _a.getCurrentEntity().idValue;
if (!currentId) {
return Promise.resolve();
}
return this.entityChangeService.hasChanges().then((changed) => {
if (changed) {
return this.formMessageService.confirm(this.languageService.language.confirmCancel).then((result) => {
if (!result) {
return Promise.reject();
}
this.formLoadingService.showLoadingWithDelay();
return this.repository.cancelEntityChanges().then(() => {
var _a2;
if (this.stateService.is(ViewState.Add)) {
(_a2 = this.viewModel.entityState) == null ? void 0 : _a2.removeEntityById(currentId);
this.revertCurrentRow();
this.formLoadingService.hide();
} else {
return this.repository.getEntityById(currentId).then((newEntity) => {
var _a3;
const newEntityData = newEntity.toJSON();
(_a3 = this.viewModel.entityState) == null ? void 0 : _a3.getCurrentEntity().loadData(newEntityData);
return Promise.resolve(true);
}).finally(() => {
this.formLoadingService.hide();
});
}
this.stateService.transitTo(ViewState.Initial);
return Promise.resolve(true);
});
});
} else {
this.formLoadingService.showLoadingWithDelay();
return this.repository.cancelEntityChanges().finally(() => {
this.formLoadingService.hide();
});
}
});
}
/**
* 获取树数据仓库
* @param hierarchyType
* @returns
*/
getTreeRepository(hierarchyType) {
const injector = this.viewModel.getInjector();
switch (hierarchyType) {
case HierarchyType.parent:
return injector.get(befVue.BefParentTreeRepository);
case HierarchyType.path:
return injector.get(befVue.BefPathTreeRepository);
}
}
/**
* 获取分级码信息
* @returns
*/
getHierarchyType() {
var _a, _b;
const hierarchyKey = this.getHierarchyKey();
const hierarchyPaths = hierarchyKey.split("/").filter((item) => item);
const entitySchema = (_a = this.viewModel.entityState) == null ? void 0 : _a.getEntitySchema();
const entityPath = (_b = this.viewModel.entityState) == null ? void 0 : _b.createPath(hierarchyPaths);
if (!entityPath) {
return null;
}
const fieldSchema = entitySchema == null ? void 0 : entitySchema.getFieldSchemaByPath(entityPath);
const fields = fieldSchema == null ? void 0 : fieldSchema.entitySchema.getFieldSchemas();
const index = fields.findIndex((field) => field.name === "parentElement");
if (index == -1) {
return HierarchyType.path;
} else {
return HierarchyType.parent;
}
}
getHierarchyKey() {
const injector = this.viewModel.getInjector();
const app = injector.get(devkitVue.App);
return app.params.get(`hierarchy_`);
}
revertCurrentRow() {
const result = this.selectLastModifyItem();
if (!result) {
this.updateCurrentRow();
}
}
selectLastModifyItem() {
var _a, _b;
const entities = (_a = this.viewModel.entityState) == null ? void 0 : _a.getEntities();
if (entities && entities.length > 0) {
const index = entities.findIndex((entity) => entity.idValue === this.lastModifiedId);
if (this.lastModifiedId && index !== -1) {
(_b = this.viewModel.entityState) == null ? void 0 : _b.changeCurrentEntityById(this.lastModifiedId);
this.lastModifiedId = null;
return true;
}
}
this.lastModifiedId = null;
return false;
}
updateCurrentRow(previousId) {
var _a, _b;
const entities = (_a = this.viewModel.entityState) == null ? void 0 : _a.getEntities();
const hierarchyKey = this.getHierarchyKey().split("/").filter((item) => item).join("");
const treeService = this.getTreeService();
if (!entities || entities.length < 1) {
return;
}
let nodeId;
if (!previousId) {
nodeId = treeService.getFirstNodeId(entities, hierarchyKey);
} else {
nodeId = treeService.getNextNodeId(entities, hierarchyKey, previousId);
}
if (nodeId) {
(_b = this.viewModel.entityState) == null ? void 0 : _b.changeCurrentEntityById(nodeId);
}
}
getTreeService() {
const hierarchy = this.getHierarchyType();
if (hierarchy === HierarchyType.parent) {
return new ParentTreeNodeService();
} else {
return new PathTreeNodeService();
}
}
}
class EntityStateService {
/**
* 构造函数
*/
constructor(viewModel) {
/**
* 视图模型
*/
__publicField(this, "viewModel");
/**
* 实体状态
*/
__publicField(this, "entityState");
this.viewModel = viewModel;
this.entityState = viewModel.entityState;
}
/**
* 改变当前行
*/
changeCurrentEntity(id) {
this.entityState.changeCurrentEntityById(id);
}
/**
* 设置当前实体
*/
changeCurrentEntityByPath(path, id) {
this.entityState.changeCurrentEntityByPath(path, id);
}
/**
* 获取实体变更
* @returns
*/
entityChanges() {
return this.entityState.entityChangeHistory.getMergedChanges();
}
}
class QuerystringService {
/**
* 解码参数
* @param queryString search|hash
*/
parse(queryString) {
if (!queryString) {
return {};
}
const hashes = queryString.slice(queryString.indexOf("?") + 1).split("&");
return hashes.reduce((params, hash) => {
const split = hash.indexOf("=");
const key = hash.slice(0, split);
const val = hash.slice(split + 1);
return Object.assign(params, { [key]: decodeURIComponent(val) });
}, {});
}
}
class RuntimeFrameworkService {
constructor() {
__publicField(this, "rtfService");
__publicField(this, "gspFrameworkServiceInstance");
this.rtfService = this.getRuntimeFrameworkService();
}
/**
* 获取rtf服务
*/
getRuntimeFrameworkService() {
const frameworkService = this.gspFrameworkService;
return frameworkService && frameworkService.rtf || {};
}
get gspFrameworkService() {
if (this.gspFrameworkServiceInstance) {
return this.gspFrameworkServiceInstance;
}
let env = window;
while (!env["gspframeworkService"] && env !== window.top && this.isSameOrigin(env)) {
env = env.parent;
}
this.gspFrameworkServiceInstance = env["gspframeworkService"];
return this.gspFrameworkServiceInstance;
}
get common() {
return this.gspFrameworkService && this.gspFrameworkService.common;
}
// #region common服务
get userInfo() {
return this.common && this.common.userInfos && this.common.userInfos.get() || null;
}
// #endregion
// #region 导航服务
/**
* 打开菜单或应用
* @param options - options
*/
openMenu(options) {
if (this.rtfService && this.rtfService.hasOwnProperty("func") && typeof this.rtfService["func"]["openMenu"] === "function") {
this.rtfService.func.openMenu(options);
}
}
/**
* 打开菜单或应用
* @param options - options
*/
openMenu$(options) {
if (this.rtfService && this.rtfService.hasOwnProperty("func") && typeof this.rtfService["func"]["openMenuByStream"] === "function") {
return this.rtfService.func.openMenuByStream(options);
}
return Promise.reject();
}
/**
* 获取导航实体数据
* @param tabId - tabid
* @param callback - callback
* @param once - once
*/
getEntityParam(tabId, callback, once = true) {
if (this.rtfService && this.rtfService.hasOwnProperty("func") && typeof this.rtfService["func"]["getEntityParam"] === "function") {
this.rtfService.func.getEntityParam(tabId, callback, once);
}
}
/**
* 尝试关闭菜单或应用
* @param options - optins
*/
beforeCloseMenu(options) {
if (this.rtfService && this.rtfService.hasOwnProperty("func") && typeof this.rtfService["func"]["beforeClose"] === "function") {
this.rtfService.func.beforeClose(options);
}
}
/**
* 关闭菜单
* @param options - options
*/
closeMenu(options) {
if (this.rtfService && this.rtfService.hasOwnProperty("func") && typeof this.rtfService["func"]["close"] === "function") {
this.rtfService.func.close(options);
}
}
/**
* 获取菜单静态参数
* @param funcId - 菜单id
* @param callback - 回调
*/
getMenuParams(funcId, callback) {
if (this.rtfService && this.rtfService.hasOwnProperty("func") && typeof this.rtfService["func"]["getMenuParams"] === "function") {
this.rtfService.func.getMenuParams(funcId, callback);
}
}
/**
* 添加事件监听
* @param token
* @param handler
* @param options
*/
addEventListener(token, handler, options) {
if (this.rtfService && this.rtfService.hasOwnProperty("frmEvent") && typeof this.rtfService["frmEvent"]["eventListener"] === "function") {
this.rtfService.frmEvent.eventListener(token, handler, options);
}
}
// TODO: 框架菜单切换事件无法监听
// #endregion
// #region 适配层属性
get params() {
if (this.rtfService && this.rtfService.hasOwnProperty("session") && typeof this.rtfService["session"]["getCommonVariable"] === "function") {
return this.rtfService["session"]["getCommonVariable"]();
}
return null;
}
/**
* 获取tabId
*/
get tabId() {
return this.params && this.params["tabId"] || null;
}
/**
* 获取formToken
*/
get formToken() {
return this.params && this.params["formToken"] || null;
}
/**
* 获取funcId
*/
get funcId() {
return this.params && this.params["funcId"] || null;
}
// #endregion
// #region 工具函数
/**
* 是否同源
* @param environment - window
*/
isSameOrigin(environment) {
const host = window.location.host;
try {
if (environment && environment.location && typeof environment.location.host !== "undefined") {
return environment.location.host === host;
}
} catch (e) {
return false;
}
return false;
}
// #endregion
}
class NavigationService {
constructor(runtimeFrameworkService, querystringService, viewModel, injector, navigationEventService) {
/**
* 命令上下文
*/
__publicField(this, "context");
this.runtimeFrameworkService = runtimeFrameworkService;
this.querystringService = querystringService;
this.viewModel = viewModel;
this.injector = injector;
this.navigationEventService = navigationEventService;
}
get querystrings() {
let hash = window.location.hash;
const params = this.querystringService.parse(hash);
if (params) {
params.formToken = this.runtimeFrameworkService.formToken;
}
return params;
}
// #region 接口
/**
* 打开菜单
* @param tabId 根据TabId决定打开新标签页或定位之前打开的标签页
* @param funcId 菜单Id
* @param params 参数
* @param enableRefresh 启用数据刷新
* @param tabName tab标题
* @param destructuring 是否解构参数
*/
openMenu(tabId, funcId, params, enableRefresh, tabName, destructuring) {
let queryStringParams = this.buildParamMap(params);
destructuring = this.convertToBoolean(destructuring, false);
if (destructuring === true) {
queryStringParams = this.buildParam(params);
}
this.querystrings.tabId || this.querystrings.funcId || this.querystrings.appId;
const options = {
tabId,
funcId,
appType: AppType.Menu,
queryStringParams,
entityParams: queryStringParams,
appId: void 0,
appEntrance: void 0,
tabName: tabName || null
};
enableRefresh = this.convertToBoolean(enableRefresh, true);
this.runtimeFrameworkService.openMenu(options);
}
/**
* 打开菜单(流)
* @param tabId 根据TabId决定打开新标签页或定位之前打开的标签页
* @param funcId 菜单Id
* @param params 参数
* @param enableRefresh 启用数据刷新
* @param tabName 页签标题
* @param destructuring 解构参数
*/
openMenu$(tabId, funcId, params, enableRefresh, tabName, destructuring) {
let queryStringParams = this.buildParamMap(params);
destructuring = this.convertToBoolean(destructuring, false);
if (destructuring === true) {
queryStringParams = this.buildParam(params);
}
this.querystrings.tabId || this.querystrings.funcId || this.querystrings.appId;
const options = {
tabId,
funcId,
appType: AppType.Menu,
queryStringParams,
entityParams: queryStringParams,
appId: void 0,
appEntrance: void 0,
tabName: tabName || null
};
enableRefresh = this.convertToBoolean(enableRefresh, true);
return this.runtimeFrameworkService.openMenu$(options);
}
/**
* 打开菜单(带维度)
* @param tabId 根据TabId决定打开新标签页或定位之前打开的标签页
* @param funcId 菜单Id
* @param params 参数
* @param enableRefresh 启用数据刷新
* @param dim1 dim1
* @param dim2 dim2
* @param tabName 页签名称
* @param metadataId 默认元数据id
* @param destructuring 解构参数
*/
openMenuWithDimension(tabId, funcId, params, enableRefresh, dim1, dim2, tabName, metadataId, destructuring) {
if (metadataId === void 0 || metadataId === null) {
metadataId = "";
}
let queryStringParams = this.buildParamMap(params);
destructuring = this.convertToBoolean(destructuring, false);
if (destructuring === true) {
queryStringParams = this.buildParam(params);
}
queryStringParams.set("dim1", dim1 ? dim1 : "public");
queryStringParams.set("dim2", dim2 ? dim2 : "public");
queryStringParams.set("metadataId", metadataId);
queryStringParams.set("isRtc", "1");
queryStringParams.set("isRootMetadata", "true");
const options = {
tabId,
funcId,
appType: AppType.Menu,
queryStringParams,
entityParams: queryStringParams,
appId: void 0,
appEntrance: void 0,
isReload: false,
tabName: tabName || null
};
enableRefresh = this.convertToBoolean(enableRefresh, true);
this.runtimeFrameworkService.openMenu(options);
}
/**
* 打开应用
* @param tabId tabId 根据TabId决定打开新标签页或定位之前打开的标签页
* @param appId 应用Id
* @param appEntrance 应用入口
* @param params 参数
* @param tabName tab标题
* @param enableRefresh 启用数据刷新
* @param destructuring 解构参数
*/
openApp(tabId, appId, appEntrance, params, tabName, enableRefresh, destructuring) {
let queryStringParams = this.buildParamMap(params);
destructuring = this.convertToBoolean(destructuring, false);
if (destructuring === true) {
queryStringParams = this.buildParam(params);
}
const options = {
tabId,
appId,
appEntrance,
funcId: void 0,
appType: AppType.App,
queryStringParams,
entityParams: queryStringParams,
tabName: tabName || null
};
enableRefresh = this.convertToBoolean(enableRefresh, true);
this.runtimeFrameworkService.openMenu(options);
}
/**
* 打开应用(流式)
* @param tabId tabId 根据TabId决定打开新标签页或定位之前打开的标签页
* @param appId 应用Id
* @param appEntrance 应用入口
* @param params 参数
* @param tabName tab标题
* @param enableRefresh 启用数据刷新
* @param destructuring 解构参数
*/
openApp$(tabId, appId, appEntrance, params, tabName, enableRefresh, destructuring) {
let queryStringParams = this.buildParamMap(params);
destructuring = this.convertToBoolean(destructuring, false);
if (destructuring === true) {
queryStringParams = this.buildParam(params);
}
const options = {
tabId,
appId,
appEntrance,
funcId: void 0,
appType: AppType.App,
queryStringParams,