UNPKG

nscomponentsreact

Version:

React Wrapper Components for NSComponents

779 lines (744 loc) 25.6 kB
var nsModuleExport = function(root,name,prototype) { if(typeof exports === 'object' && typeof module === 'object') { module.exports[name] = prototype; } else if (typeof define === "function" && define.amd) { define(name,[], function () {return prototype;}); } else if(typeof exports === 'object') { exports[name] = prototype; } else { root[name] = prototype; } };var nsIsWeb = function(root) { if(typeof exports === 'object' && typeof module === 'object') { return false; } else if (typeof define === "function" && define.amd) { return false; } else if(typeof exports === 'object') { return false; } else { return true; } };if(!nsIsWeb()) { var nsutilRef = require('./nsUtil.min.js'); var NSUtil = nsutilRef.NSUtil; var nscontainerbaseRef = require('./nsContainerBase.min.js'); var nsExtendPrototype = nscontainerbaseRef.nsExtendPrototype; var NSContainerBase = nscontainerbaseRef.NSContainerBase; var svgRef = require('./nsSVG.min.js'); var NSSvg = svgRef.NSSvg; var NSSvgShapes = svgRef.NSSvgShapes; var plugginsRef = require('./nsPluggins.min.js'); var nsTextEditor = plugginsRef.nsTextEditor; var nsTextAreaEditor = plugginsRef.nsTextAreaEditor; var NSCellSelection = plugginsRef.NSCellSelection; var NSTableCellNavigator = plugginsRef.NSTableCellNavigator; var dateutilRef = require('./nsDateUtil.min.js'); var NSDateUtil = dateutilRef.NSDateUtil; } "use strict"; var NSTableRowMover = (function() { function NSTableRowMover(setting) { this.util = new NSUtil(); this.__id = null; this.__setting = setting; this.__config = null; this.__currentRow = null; this.__targetRow = null; this.__rowMouseOffset = null; this.__lastYPos = 0; this.__oldIndex = -1; this.__touchSupport = false; this.__touchID = null; this.__divGhost = null; this.__verticalOffset = 0; this.__rowIndex = 0; this.__mapRowIndex = {}; this.__divMovable = null; this.__lastVisitedRow = null; this.__mouseMoveRef = null; this.__documentMouseUpRef = null; this.__initialize(); } NSTableRowMover.prototype.remove = function() { this.__removeDocumentEvents(); }; NSTableRowMover.prototype.processRows = function() { this.__processRows(); }; NSTableRowMover.prototype.__initialize = function() { if(this.__setting) { this.__config = { table: this.__setting["table"] || null, isSameTableMove: (this.util.isUndefined(this.__setting["isSameTableMove"]) || this.__setting["isSameTableMove"] === null) ? true : Boolean.parse(this.__setting["isSameTableMove"]), droppableTable: this.__setting["droppableTable"] || null, handler: this.__setting["handler"] || null, dragStartHandler: (this.__setting["dragStartHandler"] ? this.util.getFunction(this.__setting["dragStartHandler"]) : null), dragHandler: (this.__setting["dropHandler"] ? this.util.getFunction(this.__setting["dropHandler"]) : null), dragEndHandler: (this.__setting["dragEndHandler"] ? this.util.getFunction(this.__setting["dragEndHandler"]) : null), customClass: this.__setting["customClass"] || {table: null,dragRow: null}, enableAnimation: Boolean.parse(this.__setting["enableAnimation"]), enableMovingText: Boolean.parse(this.__setting["enableMovingText"]), movingTextCallback: (this.__setting["movingTextCallback"] ? this.util.getFunction(this.__setting["movingTextCallback"]) : null), }; if(this.__config.table) { this.__processTableForAnimation(); this.__touchSupport = !!("ontouchstart" in document); this.__processRows(); } } }; NSTableRowMover.prototype.__processRows = function() { if(this.__config.enableAnimation) { this.__verticalOffset = 0; this.__rowIndex = 0; } if(this.__config.table.tHead && this.__config.table.tHead.rows) { var rows = this.__config.table.tHead.rows; for (var count = 0; count < rows.length; count++) { var row = rows[count]; row.setAttribute("nsDropAllowed",false); this.__makeRowAbsolute(row); } } var rows = this.__config.table.tBodies[0].rows; for (var count = 0; count < rows.length; count++) { var row = rows[count]; var dragAllowed = true; if(this.util.hasAttribute(row,"nsDragAllowed")) { dragAllowed = Boolean.parse(this.util.getAttribute(row,"nsDragAllowed")); } if (dragAllowed) { this.__makeRowDraggable(row); } } }; NSTableRowMover.prototype.__processTableForAnimation = function() { if(this.__config.enableAnimation) { this.__config.table.style.position = "relative"; } }; NSTableRowMover.prototype.__rowMouseDownHandler= function(event) { event = this.util.getEvent(event); var target = this.util.getTarget(event); target = this.util.findParent(target,"TR"); var offset = this.util.getMouseOffset(event,target); var posEvent = this.util.getEventPosition(event); this.__mouseStartEvent = event; if(this.__dragStart(target,offset,posEvent)) { event.preventDefault(); } }; NSTableRowMover.prototype.__rowTouchStartHandler= function(event) { if(event.touches.length === 1) { var touch = event.touches[0]; this.__touchStart = touch; this.__touchID = touch.identifier; target = document.elementFromPoint(touch.clientX, touch.clientY); target = this.util.findParent(target,"TR"); var offset = this.util.getMouseOffset(touch,target); var posEvent = this.util.getEventPosition(touch); if(this.__dragStart(target,offset,posEvent)) { if(event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; } return false; } } return true; }; NSTableRowMover.prototype.__mouseMoveHandler = function(event) { if(this.__currentRow) { event = this.util.getEvent(event); var mousePosition = this.util.getEventPosition(event); var target = this.util.getTarget(event); var posEvent = this.util.getEventPosition(event); if(this.__dragging(target,mousePosition,posEvent,event,this.__mouseStartEvent)) { event.preventDefault(); } } }; NSTableRowMover.prototype.__touchMoveHandler = function(event) { if(event.touches.length === 1 && this.__currentRow) { var touch = event.touches[0]; var target = document.elementFromPoint(touch.clientX, touch.clientY); var position = this.util.getEventPosition(touch); var posEvent = this.util.getEventPosition(touch); if(this.__dragging(target,position,posEvent,touch,this.__touchStart)) { if(event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; } } } }; NSTableRowMover.prototype.__documentMouseUpHandler = function(event) { if(this.__currentRow) { event = this.util.getEvent(event); var mousePosition = this.util.getEventPosition(event); this.__dragEnd(mousePosition); } }; NSTableRowMover.prototype.__documentTouchUpHandler = function(event) { if(event.touches.length === 1 && this.__currentRow) { var touch = event.touches[0]; var position = this.util.getEventPosition(touch); this.__dragEnd(position); } }; NSTableRowMover.prototype.__dragStart = function(target,offset,posEvent) { this.__currentRow = target; this.__rowMouseOffset = offset; this.util.makeBodyUnselectable(); if(this.__currentRow) { this.__oldIndex = this.__currentRow.rowIndex; if(this.__config.dragStartHandler) { this.__config.dragStartHandler(this.__currentRow,{targetTable: this.__config.table,targetRow: this.__currentRow,oldIndex:this.__oldIndex}); } this.__applyCustomClass(this.__config.table,"table",null); this.__applyCustomClass(this.__currentRow,"dragRow","nsDraggingRow"); if(!this.__config.isSameTableMove) { this.__divMovable = this.util.createDiv(this.__getID() + "clonedRow", "nsDraggableCloneRow"); var table = document.createElement("table"); var body = document.createElement("tbody"); var row = this.__currentRow.cloneNode(true); body.appendChild(row); table.appendChild(body); this.__divMovable.appendChild(table); document.body.appendChild(this.__divMovable); this.__setDivMovablePosition(posEvent); } this.__addDocumentEvents(); this.__createGhost(this.__currentRow); return true; } return false; }; NSTableRowMover.prototype.__dragging = function(target,position,posEvent,event,startEvent) { if(this.__currentRow) { var yPos = position.top - this.__rowMouseOffset.y; if(yPos != this.__lastYPos) { var movingDown = yPos > this.__lastYPos; this.__lastYPos = yPos; if(this.__config.isSameTableMove) { this.__targetRow = this.util.findParent(target,"TR"); var isAppendChild = false; var moveRow = null; if(this.__targetRow && this.__currentRow != this.__targetRow) { this.__positionGhost(event); moveRow = this.__targetRow; if(this.__config.enableAnimation) { if(this.__isRowDropable(moveRow,this.__currentRow)) { var currentProp = this.__currentRow.getAttribute("data-rowMover-id"); var targetProp = this.__targetRow.getAttribute("data-rowMover-id"); var currentValue = this.__mapRowIndex[currentProp].top; var targetValue = this.__mapRowIndex[targetProp].top; this.__currentRow.style.transform = "translateY(" + targetValue + "px)"; this.__targetRow.style.transform = "translateY(" + currentValue + "px)"; this.__mapRowIndex[currentProp].top = targetValue; this.__mapRowIndex[targetProp].top = currentValue; } } else { if (movingDown) { if(this.__targetRow.nextSibling) { moveRow = this.__targetRow.nextSibling; } else { moveRow = this.__currentRow; isAppendChild = true; } } else if (!movingDown) { moveRow = this.__targetRow; } if(moveRow) { if(this.__isRowDropable(moveRow,this.__currentRow)) { if(this.__config.enableAnimation) { var currentProp = this.__currentRow.getAttribute("data-rowMover-id"); var targetProp = this.__targetRow.getAttribute("data-rowMover-id"); var currentValue = this.__mapRowIndex[currentProp].top; var targetValue = this.__mapRowIndex[targetProp].top; this.__currentRow.style.transform = "translateY(" + targetValue + "px)"; this.__targetRow.style.transform = "translateY(" + currentValue + "px)"; this.__mapRowIndex[currentProp].top = targetValue; this.__mapRowIndex[targetProp].top = currentValue; } else { var parent = this.__targetRow.parentNode; if(isAppendChild) { parent.appendChild(moveRow); } else { parent.insertBefore(this.__currentRow,moveRow); } } } } } if(moveRow && this.__config.dragHandler) { this.__config.dragHandler(this.__currentRow,{targetTable: this.__config.table,targetRow: moveRow,oldIndex:this.__oldIndex,newIndex:moveRow.rowIndex}); } } } else if(this.__divMovable) { if(this.__lastVisitedRow) { this.util.removeStyleClass(this.__lastVisitedRow,"nsDottedRow"); this.__lastVisitedRow = null; } this.__setDivMovablePosition(posEvent); this.__divMovable.style.display = "none"; var target = document.elementFromPoint(position.left,position.top); //for Opera if (target.nodeType === 3) { target = target.parentNode; } var targetTable = this.util.findChild(target,"TABLE"); if(!targetTable) { targetTable = this.util.findParent(target,"TABLE"); } if(targetTable && targetTable.id != this.__config.table.id) { var targetRow = this.__findDroppableRow(targetTable,mousePosition.top); if(targetRow) { if(this.__config.dragHandler) { this.__config.dragHandler(this.__currentRow,{targetTable: targetTable,targetRow: targetRow,oldIndex:this.__oldIndex,newIndex:targetRow.rowIndex}); } this.util.addStyleClass(targetRow,"nsDottedRow"); this.__lastVisitedRow = targetRow; } } this.__divMovable.style.display = ""; } return true; } } return false; }; NSTableRowMover.prototype.__dragEnd = function(position) { this.__removeDocumentEvents(); if(this.__currentRow) { var droppedRow = this.__currentRow; this.__removeCustomClass(this.__config.table,"table",null); this.__removeCustomClass(this.__currentRow,"dragRow","nsDraggingRow"); if(this.__config.isSameTableMove) { if(this.__config.dragEndHandler) { this.__config.dragEndHandler(this.__currentRow,{targetTable: this.__config.table,targetRow: this.__targetRow,oldIndex:this.__oldIndex,newIndex:this.__currentRow.rowIndex}); } } else if(this.__divMovable) { this.__divMovable.style.display = "none"; var target = document.elementFromPoint(position.left,position.top); //for Opera if (target.nodeType === 3) { target = target.parentNode; } var targetTable = this.util.findChild(target,"TABLE"); if(!targetTable) { targetTable = this.util.findParent(target,"TABLE"); } var targetRow = this.__findDroppableRow(targetTable,position.top); if(this.__lastVisitedRow) { this.util.removeStyleClass(this.__lastVisitedRow,"nsDottedRow"); this.__lastVisitedRow = null; } this.__divMovable.parentNode.removeChild(this.__divMovable); this.__divMovable = null; if(this.__config.dragEndHandler) { this.__config.dragEndHandler(this.__currentRow,{targetTable: targetTable,targetRow: targetRow,oldIndex:this.__oldIndex,newIndex:this.__currentRow.rowIndex}); } } this.__currentRow = null; this.__oldIndex = -1; this.__removeGhost(); this.util.makeBodySelectable(); return true; } return false; }; NSTableRowMover.prototype.__getComputedTranslateXY = function(obj) { const transArr = []; if(!window.getComputedStyle) return; const style = window.getComputedStyle(obj), transform = style.transform || style.webkitTransform || style.mozTransform; let mat = transform.match(/^matrix3d\((.+)\)$/); if(mat) return parseFloat(mat[1].split(', ')[13]); mat = transform.match(/^matrix\((.+)\)$/); mat ? transArr.push(parseFloat(mat[1].split(', ')[4])) : 0; mat ? transArr.push(parseFloat(mat[1].split(', ')[5])) : 0; console.log(transArr); return transArr; }; NSTableRowMover.prototype.__makeRowDraggable = function(row) { if(row) { this.__makeRowAbsolute(row); if(!this.util.hasStyleClass(row,"nsDraggableRow")) { this.util.addStyleClass(row,"nsDraggableRow"); if(this.__config.handler) { var handler = row.querySelector(this.__config.handler); if(handler) { if(this.__touchSupport) { this.util.addEvent(handler,"touchstart",this.__rowTouchStartHandler.bind(this)); } else { this.util.addEvent(handler,"mousedown",this.__rowMouseDownHandler.bind(this)); } } } else { this.util.addStyleClass(row,"nsDraggableRowDrag"); if(this.__touchSupport) { this.util.addEvent(row,"touchstart",this.__rowTouchStartHandler.bind(this)); } else { this.util.addEvent(row,"mousedown",this.__rowMouseDownHandler.bind(this)); } } } } }; NSTableRowMover.prototype.__makeRowAbsolute = function(row) { var rect = row.getBoundingClientRect(); console.log(row.clientHeight + " == " + rect.height); if(this.__config.enableAnimation) { var rowID = "row-" + (this.__rowIndex++); this.util.addStyleClass(row,"nsTableRowMoverAnimation"); row.setAttribute("data-rowMover-id",rowID); row.style.transform = "translateY(" + this.__verticalOffset + "px)"; this.__mapRowIndex[rowID] = {top: this.__verticalOffset}; this.__verticalOffset += this.util.getOuterHeight(row); } }; NSTableRowMover.prototype.__isRowDropable = function(row,movingRow) { var retValue = false; if(row && this.util.isElement(row) && this.util.isElementOfType(row,"TR")) { retValue = true; if(this.util.hasAttribute(row,"nsDropAllowed")) { retValue = Boolean.parse(this.util.getAttribute(row,"nsDropAllowed")); } } return retValue; }; NSTableRowMover.prototype.__findDroppableRow = function(table,yPos) { if(table && table.tBodies && table.tBodies.length > 0) { var rows = table.tBodies[0].rows; for (var count = 0; count < rows.length; count++) { var row = rows[count]; var dropAllowed = true; if(row.getAttribute("nsDropAllowed")) { dropAllowed = Boolean.parse(row.getAttribute("nsDropAllowed")); } if(dropAllowed) { var offset = this.util.getCumulativeOffset(row); var rowHeight = parseInt(row.offsetHeight)/2; if (row.offsetHeight === 0) { offset = this.util.getCumulativeOffset(row.firstChild); rowHeight = parseInt(row.firstChild.offsetHeight)/2; } //console.log("yPos::" + yPos + ",offset.y::" + offset.y + ",rowHeight::" + rowHeight); // Because we always have to insert before, we need to offset the height a bit if ((yPos > offset.y - rowHeight) && (yPos < (offset.y + rowHeight))) { return row; } else { console.log(row.innerText); } } } } return null; }; NSTableRowMover.prototype.__setDivMovablePosition = function(posEvent) { if(this.__divMovable) { this.__divMovable.style.left = posEvent.left + "px"; this.__divMovable.style.top = posEvent.top + "px"; } }; NSTableRowMover.prototype.__addDocumentEvents = function() { if(this.__touchSupport) { if(!this.__mouseMoveRef) { this.__mouseMoveRef = this.__touchMoveHandler.bind(this); if(this.__config.isSameTableMove) { this.util.addEvent(this.__config.table,"touchmove",this.__mouseMoveRef); } else { this.util.addEvent(document,"touchmove",this.__mouseMoveRef); } } if(!this.__documentMouseUpRef) { this.__documentMouseUpRef = this.__documentTouchUpHandler.bind(this); this.util.addEvent(document,"touchend",this.__documentMouseUpRef); } } else { if(!this.__mouseMoveRef) { this.__mouseMoveRef = this.__mouseMoveHandler.bind(this); if(this.__config.isSameTableMove) { this.util.addEvent(this.__config.table,"mousemove",this.__mouseMoveRef); } else { this.util.addEvent(document,"mousemove",this.__mouseMoveRef); } } if(!this.__documentMouseUpRef) { this.__documentMouseUpRef = this.__documentMouseUpHandler.bind(this); this.util.addEvent(document,"mouseup",this.__documentMouseUpRef); } } }; NSTableRowMover.prototype.__removeDocumentEvents = function() { if(this.__mouseMoveRef) { this.util.removeEvent(this.__config.table,"mousemove",this.__mouseMoveRef); this.util.removeEvent(document,"mousemove",this.__mouseMoveRef); this.__mouseMoveRef = null; } if(this.__documentMouseUpRef) { this.util.removeEvent(document,"mouseup",this.__documentMouseUpRef); this.__documentMouseUpRef = null; } }; NSTableRowMover.prototype.__createGhost = function(row) { if(this.__config.enableMovingText && !this.__divGhost) { var html = this.__config.movingTextCallback ? this.__config.movingTextCallback(row) : row.cells[0].innerText; this.__divGhost = this.util.createDiv(null,"nsTableRowMoverGhost"); this.__divGhost.innerHTML = html; this.__divGhost.style.height = '25px'; this.__divGhost.style.top = '20px'; this.__divGhost.style.left = '20px'; document.body.appendChild(this.__divGhost); } }; NSTableRowMover.prototype.__removeGhost = function() { if (this.__divGhost && this.__divGhost.parentNode) { this.__divGhost.parentNode.removeChild(this.__divGhost); } this.__divGhost = null; }; NSTableRowMover.prototype.__positionGhost = function(event) { if(this.__divGhost) { event = this.util.getEvent(event); var ghostRect = this.__divGhost.getBoundingClientRect(); var ghostHeight = ghostRect.height; var bodyDimension = this.util.getBodyDimension(); var browserWidth = bodyDimension.width - 2; var browserHeight = bodyDimension.height - 2; var top = event.pageY - (ghostHeight / 2); var left = event.pageX - 30; var windowScrollY = window.pageYOffset || document.documentElement.scrollTop; var windowScrollX = window.pageXOffset || document.documentElement.scrollLeft; if (browserWidth > 0) { if ((left + this.__divGhost.clientWidth) > (browserWidth + windowScrollX)) { left = browserWidth + windowScrollX - this.__divGhost.clientWidth; } } if (left < 0) { left = 0; } if (browserHeight > 0) { if ((top + this.__divGhost.clientHeight) > (browserHeight + windowScrollY)) { top = browserHeight + windowScrollY - this.__divGhost.clientHeight; } } if (top < 0) { top = 0; } this.__divGhost.style.left = left + 'px'; this.__divGhost.style.top = top + 'px'; } }; NSTableRowMover.prototype.__applyCustomClass = function(element,type,defaultValue) { if(element) { if(type && this.__config.customClass[type]) { this.util.addStyleClass(element,this.__config.customClass[type]); } else if(defaultValue) { this.util.addStyleClass(element,defaultValue); } } }; NSTableRowMover.prototype.__removeCustomClass = function(element,type,defaultValue) { if(element) { if(type && this.__config.customClass[type]) { this.util.removeStyleClass(element,this.__config.customClass[type]); } else if(defaultValue) { this.util.removeStyleClass(element,defaultValue); } } }; NSTableRowMover.prototype.__getID = function() { if(!this.__id) { if(this.__config.table.hasAttribute("id")) { this.__id = this.__config.table.getAttribute("id"); } else if(this.__config.table.hasAttribute("name")) { this.__id = this.__config.table.getAttribute("name"); } else { this.__id = "comp" + this.util.getUniqueId(); } } return this.__id; }; return NSTableRowMover; })(); nsModuleExport(this,"NSTableRowMover",NSTableRowMover);