You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
196 lines
6.0 KiB
196 lines
6.0 KiB
import {ModelType} from '@ui/index'; |
|
import {LinkType} from '@ui/type'; |
|
import {deleteConnection, testConnection} from '@shared/crud/crud.request'; |
|
import {getCnnectionName} from './select/select.service'; |
|
import {DATA_BASE_TYPE} from '@private/constants'; |
|
import dialog from '@shared/service/dialog.service'; |
|
import {saveConnection} from './link_set.service'; |
|
|
|
const className = 'fr.model.linkset'; |
|
const Model: ModelType = { |
|
context: ['tab', 'linkList', 'linkSelected', 'linkUpdate', 'connectionNameErr'], |
|
actions: { |
|
/** |
|
* 左侧点击链接选中 |
|
* @param name |
|
*/ |
|
setLinkSelected(name: string) { |
|
this.noSaveConfirm(() => { |
|
this._setLinkSelected(name); |
|
}); |
|
}, |
|
_setLinkSelected(name: string) { |
|
this.model.linkList.forEach((item: LinkType) => { |
|
item.isSelected = item.connectionName === name; |
|
if (item.connectionName === name) { |
|
this.model.linkSelected = { |
|
...item, |
|
isSelected: false, |
|
}; |
|
} |
|
}); |
|
this.model.linkList = [...this.model.linkList]; |
|
this.model.linkUpdate = this.model.linkSelected; |
|
}, |
|
onIconClick(title: string, id: string) { |
|
switch (title) { |
|
case BI.i18nText('Dec-Dcm_Delete'): |
|
deleteConnection(id, () => { |
|
this.model.linkList = [...this.model.linkList.filter((item: LinkType) => item.connectionId !== id)]; |
|
this.model.linkSelected = {}; |
|
this.model.linkUpdate = {}; |
|
}); |
|
break; |
|
case BI.i18nText('Dec-Dcm_Test_Connection'): |
|
this._textLink(id); |
|
break; |
|
case BI.i18nText('Dec-Dcm_Copy'): |
|
this.noSaveConfirm(() => { |
|
this.copyLink(id); |
|
}); |
|
break; |
|
default: |
|
break; |
|
} |
|
}, |
|
_textLink(id: string) { |
|
const loadingId = dialog.loading(BI.i18nText('Dec-Dcm_Connection_Testing')); |
|
const link = this.model.linkList.find((item: LinkType) => item.connectionId === id); |
|
testConnection(link, (res: any) => { |
|
dialog.close(loadingId); |
|
if (res && res.errorCode) { |
|
dialog.linkFail(`${link.connectionName}${BI.i18nText('Dec-Dcm_Connection_Test_Fail')}`, res.errorMsg, () => { |
|
this._textLink(id); |
|
}); |
|
} else { |
|
dialog.success(BI.i18nText('Dec-Dcm_Connection_Test_Success')); |
|
} |
|
}); |
|
}, |
|
copyLink(id: string) { |
|
const connectionName = BI.find(this.model.linkList, (index: number, item: LinkType) => item.connectionId === id).connectionName; |
|
const name = getCnnectionName(this.model.linkList, connectionName); |
|
let data = {}; |
|
this.model.linkList.forEach((item: LinkType) => { |
|
if (item.connectionId === id) { |
|
data = item; |
|
} |
|
}); |
|
const newCopy = { |
|
...data, |
|
isSelected: true, |
|
connectionName: name, |
|
connectionId: '', |
|
}; |
|
this.model.linkList = [ |
|
newCopy, |
|
...this.model.linkList, |
|
]; |
|
this.model.linkSelected = { |
|
...newCopy, |
|
}; |
|
this.model.linkUpdate = { |
|
...newCopy, |
|
}; |
|
}, |
|
setLinkUpdate(value: any) { |
|
this.model.linkUpdate = value; |
|
}, |
|
setEdit(type: boolean) { |
|
this.model.linkSelected = { |
|
...this.model.linkSelected, |
|
isSelected: type, |
|
}; |
|
this.model.linkUpdate = this.model.linkSelected; |
|
}, |
|
setCancel() { |
|
const linkSelected: LinkType = this.model.linkSelected; |
|
const linkList: LinkType[] = this.model.linkList; |
|
if (linkSelected.connectionId) { |
|
this.setEdit(false); |
|
} else { |
|
this.model.linkList = [ |
|
...linkList.filter(item => !!item.connectionId), |
|
]; |
|
if (this.model.linkList.length > 0) { |
|
this.model.linkList[0].isSelected = true; |
|
this.model.linkSelected = { |
|
...this.model.linkList[0], |
|
isSelected: false, |
|
}; |
|
} else { |
|
this.model.linkSelected = {}; |
|
} |
|
this.model.linkUpdate = this.model.linkSelected; |
|
} |
|
}, |
|
setNewLink(value: string) { |
|
if (!DATA_BASE_TYPE.some(item => item.text === value) && !BI.Constants.getConstant(`dec.constant.database.conf.connect.form.${value.toLowerCase()}.edit`)) { |
|
dialog.error(BI.i18nText('Dec-Dcm_Connection_Option_Cannot_Find')); |
|
|
|
return; |
|
} |
|
this.noSaveConfirm(() => { |
|
this._setNewLink(value); |
|
}); |
|
}, |
|
_setNewLink(value: string) { |
|
const name = getCnnectionName(this.model.linkList, value); |
|
let data = {}; |
|
DATA_BASE_TYPE.forEach(item => { |
|
if (item.text === value) { |
|
data = item; |
|
} |
|
}); |
|
this.model.linkList = [ |
|
{ |
|
connectionName: name, |
|
isSelected: true, |
|
...data, |
|
text: value, |
|
}, |
|
...this.model.linkList, |
|
]; |
|
this.model.linkSelected = { |
|
...data, |
|
connectionName: name, |
|
isSelected: true, |
|
text: value, |
|
}; |
|
this.model.linkUpdate = { |
|
...data, |
|
connectionName: name, |
|
text: value, |
|
}; |
|
}, |
|
setConnectionNameErr(err: string) { |
|
this.model.connectionNameErr = err; |
|
}, |
|
noSaveConfirm(cb: Function) { |
|
if (this.model.linkSelected && this.model.linkSelected.isSelected) { |
|
dialog.confirm(BI.i18nText('Dec-Dcm_Connection_Config_No_Save'), (isConfirm: boolean) => { |
|
if (isConfirm) { |
|
this.saveLink(); |
|
} else { |
|
this.setCancel(); |
|
} |
|
cb(); |
|
}); |
|
} else { |
|
cb(); |
|
} |
|
}, |
|
saveLink() { |
|
const pluginData = this.model.linkUpdate.text ? BI.Constants.getConstant(`dec.constant.database.conf.connect.form.${this.model.linkUpdate.text.toLowerCase()}.value`) : {}; |
|
const update = { |
|
...this.model.linkUpdate, |
|
...pluginData, |
|
}; |
|
saveConnection(update).then(() => { |
|
this.setEdit(false); |
|
}); |
|
}, |
|
}, |
|
}; |
|
BI.model(className, BI.inherit(Fix.Model, Model)); |
|
export default className;
|
|
|