|
@@ -1,277 +0,0 @@
|
|
|
-class moveServer {
|
|
|
|
|
- constructor() {
|
|
|
|
|
- this.movedom = null
|
|
|
|
|
- this.showdom = null
|
|
|
|
|
- this.movedomPos = null
|
|
|
|
|
- this.rightDom = null
|
|
|
|
|
- this.callBack = null
|
|
|
|
|
- this.num = 0
|
|
|
|
|
- this.tranList = []
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 鼠标按下
|
|
|
|
|
- * @param {Object} event
|
|
|
|
|
- */
|
|
|
|
|
- mousdown(event) {
|
|
|
|
|
- try {
|
|
|
|
|
- const e = event || window.event
|
|
|
|
|
- this.movedomPos = this.movedom.getBoundingClientRect()
|
|
|
|
|
- const x = e.pageX - this.movedomPos.left
|
|
|
|
|
- const y = e.pageY - this.movedomPos.top
|
|
|
|
|
- const dom = e.target
|
|
|
|
|
- const type = dom.getAttribute('type')
|
|
|
|
|
- // 区分左右键
|
|
|
|
|
- if (e.button !== 2) {
|
|
|
|
|
-
|
|
|
|
|
- // 传感器点击
|
|
|
|
|
- if (type === 'transducer' || type === 'transItem') {
|
|
|
|
|
- if (type === 'transducer') {
|
|
|
|
|
-
|
|
|
|
|
- // 新增移动样式可修改成元器件样式
|
|
|
|
|
- const txt = document.createElementNS('http://www.w3.org/2000/svg', 'text')
|
|
|
|
|
- txt.innerHTML = dom.innerHTML
|
|
|
|
|
-
|
|
|
|
|
- // 当前元素的宽
|
|
|
|
|
- const t = txt.getBoundingClientRect()
|
|
|
|
|
- const tw = x - t.width / 2
|
|
|
|
|
- const th = y + t.height / 2
|
|
|
|
|
- const transform = `translate(${tw},${th}) rotate(0)`
|
|
|
|
|
- txt.setAttributeNS(null, 'transform', transform)
|
|
|
|
|
- txt.setAttributeNS(null, 'type', 'transItem')
|
|
|
|
|
- txt.setAttributeNS(null, '_id', this.num) //唯一ID
|
|
|
|
|
- this.movedom.appendChild(txt)
|
|
|
|
|
- this.showdom = txt
|
|
|
|
|
-
|
|
|
|
|
- // 传感器信息
|
|
|
|
|
- const obj = {
|
|
|
|
|
- name: dom.innerHTML,
|
|
|
|
|
- transform: transform,
|
|
|
|
|
- _id: this.num
|
|
|
|
|
- }
|
|
|
|
|
- this.tranList.push(obj)
|
|
|
|
|
- this.num += 1
|
|
|
|
|
-
|
|
|
|
|
- } else if (type === 'transItem') {
|
|
|
|
|
- // 切换showdom
|
|
|
|
|
- this.showdom = dom
|
|
|
|
|
- const transform = dom.getAttribute('transform')
|
|
|
|
|
- const arr = transform.split(' ')
|
|
|
|
|
-
|
|
|
|
|
- const newtransform = `translate(${x},${y}) ${arr[1]}`
|
|
|
|
|
- this.showdom.setAttributeNS(null, 'transform', newtransform)
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
- // 监听移动
|
|
|
|
|
- this.movedom.onmousemove = this.mousmove.bind(this)
|
|
|
|
|
- this.movedom.onmouseup = this.mousup.bind(this)
|
|
|
|
|
-
|
|
|
|
|
- } else if (type === 'RmenuButton') {
|
|
|
|
|
-
|
|
|
|
|
- // 右键确认点击
|
|
|
|
|
- const id = +this.showdom.getAttribute('_id')
|
|
|
|
|
- const rotate = this.rightDom.getElementsByTagName("input")[0].value
|
|
|
|
|
- const transform = this.showdom.getAttribute('transform')
|
|
|
|
|
- const arr = transform.split(' ')
|
|
|
|
|
-
|
|
|
|
|
- let newStr = arr[0]
|
|
|
|
|
- newStr = newStr.replace('translate(', '').replace(')', '')
|
|
|
|
|
- const newArr = newStr.split(',')
|
|
|
|
|
-
|
|
|
|
|
- const txtW = +(this.showdom.getBoundingClientRect().width) / 2
|
|
|
|
|
- const h1 = txtW * Math.cos(rotate / 180 * Math.PI)
|
|
|
|
|
- const h2 = txtW * Math.sin(rotate / 180 * Math.PI)
|
|
|
|
|
- const newX = txtW - h1 + +newArr[0]
|
|
|
|
|
- const newY = +newArr[1] - h2
|
|
|
|
|
-
|
|
|
|
|
- const newtransform = `translate(${newX},${newY}) rotate(${rotate})`
|
|
|
|
|
- this.showdom.setAttributeNS(null, 'transform', newtransform)
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- this.tranList.forEach(el => {
|
|
|
|
|
- if (el._id === id) {
|
|
|
|
|
- el.transform = newtransform
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- // 清除
|
|
|
|
|
- this.movedom.parentNode.removeChild(this.rightDom)
|
|
|
|
|
- this.rightDom.onmousedown = null
|
|
|
|
|
- this.rightDom.oncontextmenu = null
|
|
|
|
|
- this.rightDom = null
|
|
|
|
|
-
|
|
|
|
|
- // 回调
|
|
|
|
|
- this.callBack(this.tranList)
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- } else if (type === 'Rmenu') {
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- this.movedom.parentNode.removeChild(this.rightDom)
|
|
|
|
|
- this.rightDom.onmousedown = null
|
|
|
|
|
- this.rightDom.oncontextmenu = null
|
|
|
|
|
- this.rightDom = null
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- } else {
|
|
|
|
|
- // 右键菜单
|
|
|
|
|
- if (type === 'transItem') {
|
|
|
|
|
- if (this.rightDom) {
|
|
|
|
|
- this.movedom.parentNode.removeChild(this.rightDom)
|
|
|
|
|
- this.rightDom = null
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- this.rightDom = document.createElement('div')
|
|
|
|
|
- this.rightDom.style.position = 'absolute'
|
|
|
|
|
- this.rightDom.style.background = 'black'
|
|
|
|
|
- this.rightDom.style.borderRadius = '5px'
|
|
|
|
|
- this.rightDom.style.padding = '5px'
|
|
|
|
|
- this.rightDom.style.color = 'red'
|
|
|
|
|
- this.rightDom.innerText = '旋转角度:'
|
|
|
|
|
- this.rightDom.style.left = e.pageX + 'px'
|
|
|
|
|
- this.rightDom.style.top = e.pageY + 'px'
|
|
|
|
|
- this.rightDom.setAttribute('type', 'Rmenu')
|
|
|
|
|
- this.rightDom.style.zIndex = 9
|
|
|
|
|
-
|
|
|
|
|
- const input = document.createElement('input')
|
|
|
|
|
- input.style.width = '80px'
|
|
|
|
|
- input.setAttribute('type', 'number')
|
|
|
|
|
- input.setAttribute('id', 'inputNumber')
|
|
|
|
|
- this.rightDom.appendChild(input)
|
|
|
|
|
-
|
|
|
|
|
- const button = document.createElement('button')
|
|
|
|
|
- button.innerText = '确定'
|
|
|
|
|
- button.style.margin = '0 0 0 10px'
|
|
|
|
|
- button.style.color = 'white'
|
|
|
|
|
- button.style.background = '#2db7f5'
|
|
|
|
|
- button.style.border = 'none'
|
|
|
|
|
- button.style.borderRadius = '5px'
|
|
|
|
|
-
|
|
|
|
|
- button.setAttribute('type', 'RmenuButton')
|
|
|
|
|
- this.rightDom.appendChild(button)
|
|
|
|
|
- // 单独增加点击事件
|
|
|
|
|
- this.rightDom.onmousedown = this.mousdown.bind(this)
|
|
|
|
|
- this.rightDom.oncontextmenu = function (e) {
|
|
|
|
|
- e.preventDefault()
|
|
|
|
|
- };
|
|
|
|
|
- this.showdom = dom
|
|
|
|
|
- // 父级DIV添加
|
|
|
|
|
- this.movedom.parentNode.appendChild(this.rightDom)
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error('鼠标按下', error);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 移动
|
|
|
|
|
- * 实时获取dom
|
|
|
|
|
- * @param event
|
|
|
|
|
- */
|
|
|
|
|
- mousmove(event) {
|
|
|
|
|
- const e = event || window.event
|
|
|
|
|
- const x = e.pageX - this.movedomPos.left
|
|
|
|
|
- const y = e.pageY - this.movedomPos.top
|
|
|
|
|
- const id = +this.showdom.getAttribute('_id')
|
|
|
|
|
-
|
|
|
|
|
- // 移除
|
|
|
|
|
- if (x >= this.movedomPos.width - 10 || y >= this.movedomPos.height - 10) {
|
|
|
|
|
-
|
|
|
|
|
- let newarr = []
|
|
|
|
|
- this.tranList.forEach(el => {
|
|
|
|
|
- if (el._id !== id) {
|
|
|
|
|
- newarr.push(el)
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- this.callBack(newarr)
|
|
|
|
|
- this.tranList = newarr
|
|
|
|
|
- this.movedom.removeChild(this.showdom)
|
|
|
|
|
- this.movedom.onmousemove = null
|
|
|
|
|
- this.movedom.onmouseup = null
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 位置
|
|
|
|
|
- const transform = this.showdom.getAttribute('transform')
|
|
|
|
|
- const arr = transform.split(' ')
|
|
|
|
|
-
|
|
|
|
|
- const newtransform = `translate(${x},${y}) ${arr[1]}`
|
|
|
|
|
- this.showdom.setAttributeNS(null, 'transform', newtransform)
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 鼠标松开
|
|
|
|
|
- * @param event
|
|
|
|
|
- */
|
|
|
|
|
- mousup(event) {
|
|
|
|
|
- try {
|
|
|
|
|
- const e = event || window.event
|
|
|
|
|
- const id = +(this.showdom.getAttribute('_id'))
|
|
|
|
|
- const transform = this.showdom.getAttribute('transform')
|
|
|
|
|
-
|
|
|
|
|
- this.tranList.forEach(el => {
|
|
|
|
|
- if (el._id === id) {
|
|
|
|
|
- el.transform = transform
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- this.movedom.onmousemove = null
|
|
|
|
|
- this.movedom.onmouseup = null
|
|
|
|
|
- // 回调
|
|
|
|
|
- this.callBack(this.tranList)
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error('鼠标松开', error);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 监听dom
|
|
|
|
|
- * @param {HTMLElement} dom 监听元素
|
|
|
|
|
- * @param {Function} callBack 回调函数
|
|
|
|
|
- */
|
|
|
|
|
- on(dom, callBack) {
|
|
|
|
|
- const isdom = this.isElement(dom)
|
|
|
|
|
- if (isdom) {
|
|
|
|
|
- this.callBack = callBack
|
|
|
|
|
- this.movedom = dom
|
|
|
|
|
- this.movedomPos = dom.getBoundingClientRect()
|
|
|
|
|
- // 去掉右键
|
|
|
|
|
- this.movedom.oncontextmenu = function (e) {
|
|
|
|
|
- e.preventDefault()
|
|
|
|
|
- };
|
|
|
|
|
- this.movedom.onmousedown = this.mousdown.bind(this)
|
|
|
|
|
- } else {
|
|
|
|
|
- alert('无效的dom元素')
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 清除
|
|
|
|
|
- */
|
|
|
|
|
- off() {
|
|
|
|
|
- this.movedom.onmousedown = null
|
|
|
|
|
- this.movedom.onmousemove = null
|
|
|
|
|
- this.movedom.onmouseup = null
|
|
|
|
|
- this.movedom.oncontextmenu = null
|
|
|
|
|
- this.movedom = null
|
|
|
|
|
- this.showdom = null
|
|
|
|
|
- this.movedomPos = null
|
|
|
|
|
- this.rightDom = null
|
|
|
|
|
- this.callBack = null
|
|
|
|
|
- this.num = 0
|
|
|
|
|
- this.tranList = []
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 判断是否是DOM
|
|
|
|
|
- */
|
|
|
|
|
- isElement(obj) {
|
|
|
|
|
- return (typeof HTMLElement === 'object')
|
|
|
|
|
- ? (obj instanceof HTMLElement)
|
|
|
|
|
- : !!(obj && typeof obj === 'object' && (obj.nodeType === 1 || obj.nodeType === 9) && typeof obj.nodeName === 'string');
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-export default new moveServer()
|
|
|