123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- import {createApp} from "vue/dist/vue.esm-bundler.js";
- import {getTokenHeader} from '~src/api/request/header';
- import {toParse, isArray} from "vue-utils-plus";
- //自定义组件或二次封装的组件
- import HcTableFormUpload from "~com/plugins/table-form/hc-form-upload.vue"
- import HcFormSelectSearch from "~com/plugins/table-form/hc-form-select-search.vue"
- import HcFormCheckboxGroup from "~com/plugins/table-form/hc-form-checkbox-group.vue"
- import ElTimePicker from "~com/plugins/table-form/hc-time-picker.vue"
- import ElDatePicker from "~com/plugins/table-form/hc-date-picker-1.vue"
- import ElRadioGroup from "~com/plugins/table-form/hc-form-radio-group.vue"
- //修改过的组件
- import {ElSelect, ElOption} from 'z-element-plus'
- import {
- ElButton, ElTooltip, ElInput, ElUpload, ElInputNumber, ElRadio, ElCheckbox, ElCheckboxGroup
- } from 'element-plus'
- const components = {
- ElButton, ElTooltip, ElInput, ElUpload, ElInputNumber, ElSelect, ElOption,
- ElRadio, ElCheckbox, ElCheckboxGroup,
- ElDatePicker, ElTimePicker, HcTableFormUpload, ElRadioGroup,
- HcFormSelectSearch,
- HcFormCheckboxGroup
- }
- //表单渲染
- export default class HTableForm {
- static createForm({template, tableForm, keys, appId, onRight, onBlur, onLeftClick}) {
- const _this = this;
- const app = createApp({
- data() {
- return {
- getTokenHeader: getTokenHeader(),
- formData: tableForm,
- }
- },
- //html标签数据
- template,
- //自定义组件,需要把饿了么的组件,或者自定义组件手动传递进来绑定,否则渲染时,自定义组件不会生效
- components,
- //监听数据,伪双向绑定(v-model)
- watch: {
- tableForm: {
- handler(obj) {
- this.formData = obj
- },
- deep: true
- },
- formData: {
- handler(obj) {
- tableForm = obj
- },
- deep: true
- },
- },
- methods: {
- //鼠标右键菜单
- contextmenuClick(a, b, c, d, e, f, event) {
- event.preventDefault();
- },
- //鼠标右键事件
- RightClick(a, b, c, d, e, f, event) {
- const KeyName = event?.target?.getAttribute('keyname') || ''
- if (onRight) {
- onRight(event, KeyName)
- }
- },
- //焦点事件
- getInformation() {
- },
- //日期选择事件
- datePickerChange(val, key) {
- this.formData[key] = val
- },
- //上传完成
- formUploadSuccess({src, key}) {
- this.formData[key] = src
- },
- //删除上传的文件
- delTableFormFile(key) {
- this.formData[key] = ''
- },
- //失去焦点事件
- getRegularExpression(event, reg, msg, a, b, leng) {
- const KeyName = event?.target?.getAttribute('keyname') || ''
- if (onBlur) {
- onBlur(event, KeyName, reg, this.formData[KeyName], msg, leng)
- }
- },
- //远程搜索处理
- formRemoteChange(data) {
- Object.keys(data).forEach(key => {
- this.formData[key] = data[key]
- })
- },
- //多选框处理
- checkboxGroupChange({key, val}) {
- this.formData[key] = val
- },
- //键盘事件 上键
- keyupShiftUp(event) {
- _this.setKeyupData(event, 'up', keys)
- },
- //键盘事件 下键
- keyupShiftDown(event) {
- _this.setKeyupData(event, 'down', keys)
- },
- //键盘事件 左键
- keyupShiftLeft(event) {
- _this.setKeyupData(event, 'left', keys)
- },
- //键盘事件 右键
- keyupShiftRight(event) {
- _this.setKeyupData(event, 'right', keys)
- },
- //日期时间框键盘事件
- dateKeydown({type, name}) {
- _this.setKeyupData(name, type, keys)
- },
- //输入左键点击事件
- inputLeftClick(event, key) {
- if (onLeftClick) {
- onLeftClick(key)
- }
- },
- }
- })
- app.mount(appId)
- return app
- }
- //处理日期范围数据
- static setPickerKey(data) {
- const pickerKey = data['pickerKey'] || ''
- if (pickerKey) {
- const pickerKeys = pickerKey.split(',')
- for (let i = 0; i < pickerKeys.length; i++) {
- const val = data[pickerKeys[i]] || ''
- if (val) {
- const dataVal = val.replace(/'/g, '"');
- data[pickerKeys[i]] = toParse(dataVal) || []
- } else {
- data[pickerKeys[i]] = []
- }
- }
- }
- return data
- }
- //处理日期时间框的切换事件
- static setByClassKeyup(keys) {
- try {
- let poppers = document.getElementsByClassName('hc-table-form-date-picker')
- for (let i = 0; i < poppers.length; i++) {
- let item = poppers[i], key = '';
- const ids = item.getAttribute('class').split('-form-id-')
- if (ids.length >= 1) {
- key = ids[1]
- }
- if (ids) {
- let panels = item.getElementsByClassName('el-picker-panel__content')
- this.setElementsEvent(panels, key, keys)
- }
- }
- } catch (e) {
- console.log(e)
- }
- }
- //设置事件
- static setElementsEvent(elements, key, keys) {
- if (elements.length > 0) {
- const _this = this;
- elements[0].addEventListener("keydown", e => {
- e.stopPropagation()
- if (e.key === 'ArrowUp') {
- _this.setKeyupData({target: {id: key}}, 'up', keys)
- } else if (e.key === 'ArrowDown') {
- _this.setKeyupData({target: {id: key}}, 'down', keys)
- } else if (e.key === 'ArrowLeft') {
- _this.setKeyupData({target: {id: key}}, 'left', keys)
- } else if (e.key === 'ArrowRight') {
- _this.setKeyupData({target: {id: key}}, 'right', keys)
- }
- }, {
- capture: true
- });
- }
- }
- //计算上下左右快捷键的
- static setKeyupData({target}, type, keys) {
- const key = target.id
- //处理快捷键数据和事件
- if (key && type && isArray(keys)) {
- //计算当前的位置
- let left = -1, top = -1;
- for (let i = 0; i < keys.length; i++) {
- if (isArray(keys[i])) {
- const index = keys[i].findIndex(id => id === key)
- if (index !== -1) {
- left = index
- top = i
- break;
- }
- }
- }
- if (type === 'up') {
- //向上移动
- if (top > 0) {
- let keyId = '';
- const tops = keys[top - 1]
- const keyLength = tops.length - 1;
- if (keyLength < left) {
- keyId = tops[keyLength]
- } else {
- keyId = tops[left]
- }
- this.setElementFocus(keyId)
- }
- } else if (type === 'down') {
- //向下移动
- const tops = keys.length - 1;
- if (tops > top) {
- let keyId = '';
- const tops = keys[top + 1]
- const keyLength = tops.length - 1;
- if (keyLength < left) {
- keyId = tops[keyLength]
- } else {
- keyId = tops[left]
- }
- this.setElementFocus(keyId)
- }
- } else if (type === 'left') {
- //向左移动
- if (left > 0) {
- const keyId = keys[top][left - 1]
- this.setElementFocus(keyId)
- }
- } else if (type === 'right') {
- //向右移动
- const lefts = keys[top]
- const leftLength = lefts.length - 1;
- if (leftLength > left) {
- const keyId = lefts[left + 1]
- this.setElementFocus(keyId)
- }
- }
- }
- }
- //设置元素焦点
- static setElementFocus(keyId) {
- if (keyId) {
- try {
- document.getElementById(keyId).focus();
- } catch {
- }
- }
- }
- //设置表单样式
- static setFormStyle(key, name = 'hc-red-border', add = false) {
- const dom = document.getElementById(key)
- let parent = dom?.parentElement ?? ''
- if (dom.tagName === 'INPUT') {
- parent = parent?.parentElement ?? ''
- this.setFormClass(parent, name, add)
- } else if (dom.tagName === 'TEXTAREA') {
- this.setFormClass(dom, name, add)
- }
- }
- static setFormClass(dom, name = 'hc-red-border', add = false) {
- const classStr = dom.getAttribute('class')
- const classArr = classStr.split(' ')
- const index = classArr.indexOf(name)
- if (index === -1 && add) {
- classArr.push(name)
- } else if (index !== -1 && add === false) {
- classArr.splice(index, 1)
- }
- dom.setAttribute('class', classArr.join(' '))
- }
- //设置选中样式
- static setCheckKeyStyle(key, remove = false) {
- if (remove) {
- this.setFormStyle(key, 'hc-green-border')
- } else {
- this.setFormStyle(key, 'hc-green-border', true)
- }
- }
- }
|