DragDropAction.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2011
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or
  8. *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *|
  10. *+------------------------------------------------------------------------+
  11. */
  12. function DragDropAction()
  13. {
  14. this.m_source = null;
  15. this.m_target = null;
  16. this.m_insertBefore = false;
  17. this.m_sAction = "Reorder";
  18. }
  19. DragDropAction.prototype = new ModifyReportAction();
  20. DragDropAction.prototype.getUndoHint = function()
  21. {
  22. return RV_RES.IDS_JS_MOVE;
  23. };
  24. DragDropAction.prototype.getOffsetCoords = function(startAt)
  25. {
  26. var rtTable = document.getElementById("rt" + this.getCognosViewer().getId());
  27. var offsetParent = startAt;
  28. var topCoord = 0;
  29. var leftCoord = 0;
  30. while(offsetParent != rtTable)
  31. {
  32. topCoord += offsetParent.offsetTop;
  33. leftCoord += offsetParent.offsetLeft;
  34. offsetParent = offsetParent.offsetParent;
  35. }
  36. return { left: leftCoord, top: topCoord };
  37. };
  38. DragDropAction.prototype.showDragDropCaret = function(evt, cell, parentTable)
  39. {
  40. var dragDropCaret = document.getElementById("VDDC" + this.getCognosViewer().getId());
  41. if(dragDropCaret == null)
  42. {
  43. dragDropCaret = document.createElement("span");
  44. dragDropCaret.setAttribute("id", "VDDC" + this.getCognosViewer().getId());
  45. dragDropCaret.className = "dropCaret";
  46. if(dragDropCaret.attachEvent)
  47. {
  48. dragDropCaret.attachEvent("onmousemove", stopEventBubble);
  49. }
  50. else
  51. {
  52. dragDropCaret.addEventListener("mousemove", stopEventBubble, false);
  53. }
  54. dragDropCaret.style.width = "8px";
  55. dragDropCaret.innerHTML = "<img style=\"margin:1px;width:2px;height:100%;\" src=\"" + this.getCognosViewer().getWebContentRoot() + "/rv/images/drop_caret.gif\"/>";
  56. parentTable.appendChild(dragDropCaret);
  57. }
  58. var offsetCoords = this.getOffsetCoords(parentTable);
  59. dragDropCaret.style.top = (offsetCoords.top - 1) + "px";
  60. var eventXCoord;
  61. if(typeof evt.offsetX == "undefined") {
  62. eventXCoord = evt.layerX;
  63. } else {
  64. offsetCoords = this.getOffsetCoords(evt.srcElement);
  65. eventXCoord = evt.offsetX + offsetCoords.left;
  66. }
  67. offsetCoords = this.getOffsetCoords(cell);
  68. var halfWayPoint = offsetCoords.left + (cell.clientWidth / 2);
  69. this.m_insertBefore = (eventXCoord < halfWayPoint);
  70. dragDropCaret.style.height = parentTable.clientHeight + "px";
  71. if(this.m_insertBefore == false)
  72. {
  73. dragDropCaret.style.left = (offsetCoords.left + cell.clientWidth + 1) + "px";
  74. }
  75. else
  76. {
  77. dragDropCaret.style.left = offsetCoords.left + "px";
  78. }
  79. dragDropCaret.style.display = "inline";
  80. };
  81. DragDropAction.prototype.showDragDropIndicators = function(evt)
  82. {
  83. if(this.m_target != null)
  84. {
  85. var cell = this.m_target.getCellRef();
  86. var parentTable = cell;
  87. while(parentTable.getAttribute("lid") == null)
  88. {
  89. parentTable = parentTable.parentNode;
  90. }
  91. this.showDragDropCaret(evt, cell, parentTable);
  92. }
  93. };
  94. DragDropAction.prototype.showDragDropToolTip = function(evt)
  95. {
  96. var imageRef = "";
  97. if(this.canDrop() == true)
  98. {
  99. imageRef = "/rv/images/cursor_move.gif";
  100. }
  101. else
  102. {
  103. imageRef = "/rv/images/cursor_nodrop.gif";
  104. }
  105. this.showCustomCursor(evt, "viewerTooltipSpan", imageRef);
  106. };
  107. DragDropAction.prototype.canMove = function()
  108. {
  109. if (this.m_oCV.isBlacklisted("Move")) {
  110. return false;
  111. }
  112. var selectionController = this.getCognosViewer().getSelectionController();
  113. this.m_source = selectionController.getAllSelectedObjects();
  114. if(this.m_source != null && this.m_source.length > 0)
  115. {
  116. if(typeof this.m_source[0].m_dataContainerType != "undefined" && this.m_source[0].m_dataContainerType == "list" && this.m_source[0].getLayoutType() != "summary")
  117. {
  118. return true;
  119. }
  120. }
  121. return false;
  122. };
  123. DragDropAction.prototype.onDrag = function(evt)
  124. {
  125. clearTextSelection();
  126. var sourceNode = getNodeFromEvent(evt);
  127. var selectionController = this.getCognosViewer().getSelectionController();
  128. this.m_target = selectionController.buildSelectionObject(sourceNode, evt);
  129. this.showDragDropToolTip(evt);
  130. if(this.canDrop())
  131. {
  132. this.showDragDropIndicators(evt);
  133. }
  134. else
  135. {
  136. this.hideDropIndicators();
  137. }
  138. };
  139. DragDropAction.prototype.hideDropIndicators = function()
  140. {
  141. var dragDropIndicator = document.getElementById("VDDC" + this.getCognosViewer().getId());
  142. if(dragDropIndicator != null)
  143. {
  144. dragDropIndicator.style.display = "none";
  145. }
  146. };
  147. DragDropAction.prototype.onMouseDown = function(evt)
  148. {
  149. if(this.canMove())
  150. {
  151. window.oCVDragDropObject = { action:this, x:evt.clientX, y:evt.clientY, dragging:false };
  152. }
  153. };
  154. DragDropAction.prototype.canDrop = function()
  155. {
  156. return this.m_target != null && this.m_source != null && this.m_target.getLayoutType() != "summary" && (this.m_target.getLayoutElementId() == this.m_source[0].getLayoutElementId());
  157. };
  158. DragDropAction.prototype.onDrop = function(evt)
  159. {
  160. this.hideCustomCursor("viewerTooltipSpan");
  161. this.hideDropIndicators();
  162. if(this.canDrop(evt)) {
  163. //Determine if the user's drop results in a change in column
  164. //order. A user can change column order in one or more of
  165. //three ways:
  166. //1. Drag one or more columns to a new destination.
  167. //2. Select multiple columns which are not next to each
  168. // other - so wherever they are dropped, at least one
  169. // will change position.
  170. //3. Select multiple columns in a new order - so wherever
  171. // they are dropped, the selected columns will be in a
  172. // new configuration relative to each other.
  173. //A reorder occurs iff one or more of these conditions are
  174. //met. If no reorder occurs, don't make a server request.
  175. var executeDrop = true;
  176. //Determine if the selected columns are all next to each
  177. //other and in the original order.
  178. var column;
  179. var first = parseInt(this.m_source[0].getColumnRef(), 10);
  180. var last = first;
  181. var consecutiveColumns = true;
  182. for(var index = 0; index < this.m_source.length; ++index) {
  183. column = parseInt(this.m_source[index].getColumnRef(), 10);
  184. if(index > 0 && column !== last + 1) {
  185. consecutiveColumns = false;
  186. break;
  187. }
  188. last = column;
  189. }
  190. if(consecutiveColumns) {
  191. //Determine if the columns are being moved to a new location
  192. var destination = parseInt(this.m_target.getColumnRef(), 10);
  193. destination += this.m_insertBefore ? 0 : 1;
  194. if (destination >= first && destination <= last + 1) {
  195. //None of the three ways to move a column is satisfied -
  196. //don't execute the drop action.
  197. executeDrop = false;
  198. }
  199. }
  200. if(executeDrop) {
  201. this.execute();
  202. }
  203. }
  204. };
  205. DragDropAction.prototype.addActionContextAdditionalParms = function()
  206. {
  207. var tag = this.m_insertBefore == true ? "before" : "after";
  208. //always use layout tag when it is available.
  209. var cellRef = this.m_target.getCellRef();
  210. var tagValue = this.getRAPLayoutTag(cellRef);
  211. tagValue = (tagValue != null ) ? tagValue : this.m_target.getColumnName();
  212. return this.getSelectedCellTags() + "<" + tag + ">" + xml_encode(tagValue) + "</" + tag + ">";
  213. };
  214. function DragDropAction_isDragging(evt)
  215. {
  216. var oCVDDO = window.oCVDragDropObject;
  217. if(oCVDDO)
  218. {
  219. var currentX = evt.clientX;
  220. var currentY = evt.clientY;
  221. var originalX = oCVDDO.x;
  222. var originalY = oCVDDO.y;
  223. if((currentX >= (originalX+2)) || (currentX <= (originalX-2)) || (currentY >= (originalY+2)) || (currentY <= (originalY-2)))
  224. {
  225. oCVDDO.dragging = true;
  226. }
  227. return oCVDDO.dragging;
  228. }
  229. return false;
  230. }
  231. function DragDropAction_onmouseup(evt)
  232. {
  233. if(DragDropAction_isDragging(evt))
  234. {
  235. window.oCVDragDropObject.action.onDrop(evt);
  236. }
  237. window.oCVDragDropObject = null;
  238. }
  239. function DragDropAction_onmousemove(evt)
  240. {
  241. if(DragDropAction_isDragging(evt))
  242. {
  243. window.oCVDragDropObject.action.onDrag(evt);
  244. }
  245. }