import Vue from "vue";
import utils from "@/hotent-ui-util.js";
import FormMath from "@/math.js";
import dialog from "@/api/dialogApi.js";
// import CustomQuery from "@/components/eipControl/bus/CustomQuery.js";
import _ from "lodash";
import req from "@/request.js"
import app from "@/main.js";
// 表单tr中根据隐藏字段动态合并单元格的指令
Vue.directive('pinyin', {
// 指令的定义
componentUpdated: function (el, binding, vnode) {
let context = vnode.context;
// 防抖
let debounceGetPinyin = _.debounce((newVal) => {
// disabled 或者 readonly 时 不需要请求
if (el.__vue__.disabled || el.__vue__.readonly) return;
req.request({
url: `${window.context.uc}/base/tools/v1/getPinyin`,
method: "GET",
params: { chinese: newVal, type: `${binding.modifiers.full ? 1 : 0}` }
}).then(res => {
if (res.data.state) {
let modelExp = vnode.data.model.expression.replace(/\[\w+.?\$\w+\]/g, "[" + binding.arg + "]")
utils.setValueByPath(context, modelExp, res.data.value);
app.$nextTick(() => {
app.$validator.validate();
})
}
}).finally(() => {
});
}, 300);
if (binding.value && binding.value != binding.oldValue) {
debounceGetPinyin(binding.value);
}
// context.$watch(watchExp(), function (newVal, oldVal) {
// if (newVal && newVal != oldVal) {
// debounceGetPinyin(newVal);
// }
// }, {
// deep: true,
// immediate: false
// });
}
});
Vue.directive('permit', {
componentUpdated: function (el, binding) {
if (el.tagName !== "TR") {
throw "v-permit指令只能用在tr元素上.";
}
if (!binding.value || binding.value.constructor !== Number) {
throw "v-permit指令的值只能是大于0的数字.";
}
el.removeAttribute("hidden");
// tr中没有子元素时,删除tr自身
if (el.cells.length == 0) {
el.setAttribute("hidden", "hidden");
}
else if (el.cells.length < binding.value) {
let colspan = binding.value - el.cells.length + 1;
// 设置colspan实现单元格合并
el.cells[el.cells.length - 1].setAttribute("colspan", colspan);
}
else if (el.cells.length === binding.value) {
for (var i = 0, c; c = el.cells[i++];) {
c.removeAttribute("colspan");
}
}
}
});
// 表单指令,会在表单的vue实例上添加一个map,用以存放数学运算的表达式
Vue.directive('form', {
bind: function (el, binding, vnode) {
const inst = vnode.context;
if (inst && !inst.hasOwnProperty("watchMap")) {
inst["watchMap"] = new Map();
}
}
});
// 判断输入框是否作为计算字段,是则添加监听
Vue.directive('express', {
componentUpdated: function (el, binding, vnode) {
const inst = vnode.context;
if (!inst.$vnode.data.model) {
return;
}
const elAttr = inst.$vnode.componentOptions.propsData.modelExpression;
if (elAttr && !inst._expressInit) {
inst._expressInit = true;
// 子表每一行数据作用域所在的dom元素
let { subScopeEl } = utils.getSubScopeElAndIndex(el);
let subname = null;
let aliasElAttr = null;
// 子表数据,需要找到配置了data-subname的元素
if (subScopeEl) {
subname = subScopeEl.dataset["subname"];
if (!subname) {
throw ("无法获取到当前子表前缀");
}
const elAttrAry = elAttr.split("."), elAttrArySize = elAttrAry.length;
if (elAttrArySize < 1) {
throw `子表中的数据绑定表达式${elAttr}错误`;
}
aliasElAttr = `${subname}.${elAttrAry[elAttrArySize - 1]}`;
}
const p = utils.getOnlineFormInstance(inst);
if (p.watchMap && (p.watchMap.has(elAttr) || p.watchMap.has(aliasElAttr))) {
if (!inst._watchers.some((m) => {
return m.expression === 'inputVal'
})) {
inst.$watch("inputVal", function (newVal, oldVal, farewell) {
if (newVal !== oldVal) {
const expList = p.watchMap.get(elAttr) || p.watchMap.get(aliasElAttr);
expList.forEach(item => {
let result = 0;
const t = item.target;
// 当在子表单行中时,而且不是对子表进行列运算时,判定为子表当前行内运算
if (subScopeEl && !/.*?\[\{.*?\}\].*?/.test(item.exp)) {
// 组件销毁时,不再计算子表单行的公式
if (farewell) {
return;
}
const index = subScopeEl.dataset["index"];
if (index === undefined) {
throw ("获取不到当前子表行的索引,无法完成计算.");
}
result = FormMath.calcSubExpValue(item.exp, p, subname, index);
p.$emit(t, { result, index });
}
else {
result = FormMath.calcExpValue(item.exp, p);
p.$emit(t, { result });
}
});
}
}, { immediate: true });
}
}
}
}
});
// 自定义对话框 v-auth-set-event
Vue.directive('auth-set-event', {
bind: function (el, bindings, vnode) {
const htAuthSetEvent = el.getAttribute("attr");
if (!htAuthSetEvent) return;
var defaultPermissionList = [];
dialog.getPermissionList(result => {
defaultPermissionList = result;
});
/**
* 设置设置权限。
* ownerNameJson 格式。
* [{type:"everyone",title:"所有人"},{type:"user",title:"用户",id:"1,2",name:"ray,tom"}]
*/
const setAuth = function () {
var conf = {
right: ownerNameJson,
permissionList: defaultPermissionList
}
//授权选择器
vnode.child.AuthSettingShow(conf);
}
var ownerNameJson = [];
var initData = bindings.value;
if (initData) {
ownerNameJson = JSON.parse(initData);;
// showLable(ownerNameJson);
}
//对话框初始化监听
el.childNodes[2].onclick = function () {
setAuth();
}
}
});
Vue.directive('parseTemplete', {
bind: function (el, binding, vnode) {
var s = JSON.stringify
el.innerHTML =
'name: ' + s(binding.name) + '
' +
'value: ' + s(binding.value) + '
' +
'expression: ' + s(binding.expression) + '
' +
'argument: ' + s(binding.arg) + '
' +
'modifiers: ' + s(binding.modifiers) + '
' +
'vnode keys: ' + Object.keys(vnode).join(', ')
}
});
Vue.directive('alsCharts', {
inserted(el, binding, vnode) {
const inst = binding.context;
function init(data) {
if (!(data.value && data.value !== true && data.value.constructor == Array && data.value[0].legend)) {
return;
}
var options = data.value[0];
var height = getDivHeight(el) - 35;
var wrap = $('
' + series[j].data[i] + ' | '; //组装表数据 } var curColor = colors[i % 2]; table += '
' + axisData[i] + ' | ' + tdBodys + '