MoveWidgets.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2013, 2019
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['./Move', '../../../../lib/@waca/dashboard-common/dist/ui/interaction/Utils', 'jquery', '../../../../lib/@waca/core-client/js/core-client/utils/BrowserUtils'], function (BaseMoveClass, utils, $, BrowserUtils) {
  8. var MoveWidgets = BaseMoveClass.extend({
  9. init: function init() {
  10. MoveWidgets.inherited('init', this, arguments);
  11. this.isIE = BrowserUtils.isIE();
  12. },
  13. /**
  14. * Called by the base class to get the type of the data being drag.
  15. * Layout drop zone will only accept 'widget' types
  16. * @returns {String}
  17. */
  18. getDragDataType: function getDragDataType() {
  19. return 'widget';
  20. },
  21. /**
  22. * Called by the base class after a drop has occurred.
  23. * If we didn't drop in a drop zone, we restore the old position of the nodes
  24. * @param dropContext
  25. */
  26. handleDrop: function handleDrop(dropContext) {
  27. if (!dropContext.isDropped) {
  28. this.restoreOriginalPosition();
  29. }
  30. if (dropContext.dragObject) {
  31. $(dropContext.dragObject.data.nodeInfoList[0].node).parent().css('overflow', 'visible');
  32. }
  33. },
  34. /**
  35. * Restore the selected items to their original location
  36. *
  37. */
  38. restoreOriginalPosition: function restoreOriginalPosition() {
  39. var nList = this.dropInfo.nodeInfoList;
  40. var n;
  41. for (var i = 0, iLen = nList.length; i < iLen; i++) {
  42. var nodeInfo = nList[i];
  43. n = nodeInfo.node;
  44. if (!nodeInfo.oldValues.parent) {
  45. n.parentNode.removeChild(n);
  46. } else {
  47. nodeInfo.oldValues.parent.insertBefore(n, nodeInfo.oldValues.nextSibling);
  48. n.style.left = nodeInfo.oldValues.left;
  49. n.style.top = nodeInfo.oldValues.top;
  50. n.style.height = nodeInfo.oldValues.height;
  51. n.style.width = nodeInfo.oldValues.width;
  52. }
  53. }
  54. },
  55. /**
  56. * Called by the base class once a drag starts
  57. * to update the drop payload that the drop zone will receive.
  58. *
  59. * @param dropInfo
  60. */
  61. prepareDropInfo: function prepareDropInfo(dropInfo) {
  62. // Indicate to the drop zone that we are moving a widget
  63. dropInfo.operation = 'move';
  64. },
  65. /**
  66. * Called by the start of the dragging operation for every node that is being moved
  67. * @param {object} n - contains the dom node being dragged
  68. */
  69. prepareNodeForMove: function prepareNodeForMove(n) {
  70. var $n = $(n);
  71. // Set the height and width in case the CSS height/width is using percentage
  72. $n.height($n.height());
  73. $n.width($n.width());
  74. // Workaround for iOS 13 bug, RTC 299193
  75. if (!this._isIpadVersion13orGreater()) {
  76. $n.parent().css('overflow', 'hidden');
  77. this.getAvatarContainer().append(n);
  78. }
  79. return n;
  80. },
  81. postNodeForMove: function postNodeForMove(nodes) {
  82. if (this.isIE) {
  83. this.emit('moveWidgets:start', {
  84. 'nodes': nodes
  85. });
  86. }
  87. },
  88. /**
  89. * Called by the base class on every move event
  90. *
  91. */
  92. handleMove: function handleMove(nodeInfo, coords, dragContext) {
  93. // Calculate the drop position within the drop zone.
  94. var nDropZone = dragContext.dropTargetNode;
  95. if (nDropZone) {
  96. var $n = $(nodeInfo.node);
  97. var offset = void 0;
  98. // Workaround for iOS 13 bug, RTC 299193
  99. if (this._isIpadVersion13orGreater()) {
  100. offset = { left: 0, top: 0 };
  101. } else {
  102. offset = dragContext.dropTargetNodeOffset || $(nDropZone).offset();
  103. }
  104. var x = Math.round(coords.x - offset.left + nDropZone.scrollLeft);
  105. var y = Math.round(coords.y - offset.top + nDropZone.scrollTop);
  106. var bounds = utils.widgetSize($n);
  107. nodeInfo.dropPosition = {
  108. x: x,
  109. y: y,
  110. height: bounds.height,
  111. width: bounds.width,
  112. center_x: x + $n.outerWidth() / 2,
  113. center_y: y + $n.outerHeight() / 2,
  114. before: nDropZone === nodeInfo.oldValues.parent ? nodeInfo.oldValues.nextSibling : null
  115. };
  116. }
  117. },
  118. dragMoveEvent: function dragMoveEvent() {
  119. MoveWidgets.inherited('dragMoveEvent', this, arguments);
  120. },
  121. /**
  122. * Event handler call after the drag and drop had completed
  123. */
  124. dragStopEvent: function dragStopEvent() {
  125. MoveWidgets.inherited('dragStopEvent', this, arguments);
  126. if (this.isIE) {
  127. this.emit('moveWidgets:end', {
  128. 'nodes': this.getSelectedDomNodes()
  129. });
  130. }
  131. },
  132. /**
  133. * Determine if userAgent is an iPad and os version 13+
  134. * The format of an iOS userAgent is as follows:
  135. * 'Mozilla/5.0 (iPad; CPU OS 13_2_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
  136. */
  137. _isIpadVersion13orGreater: function _isIpadVersion13orGreater() {
  138. var regExMatch = window.navigator.userAgent.match(/ipad.*os\s((\d+_?){2,3})\s/i);
  139. var version = regExMatch ? regExMatch[1].split('_') : null;
  140. return version ? parseInt(version[0], 10) > 12 : false;
  141. }
  142. });
  143. return MoveWidgets;
  144. });
  145. //# sourceMappingURL=MoveWidgets.js.map