jqdnr.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * jqDnR - Minimalistic Drag'n'Resize for jQuery.
  3. *
  4. * Copyright (c) 2007 Brice Burgess <bhb@iceburg.net>, http://www.iceburg.net
  5. * Licensed under the MIT License:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. *
  8. * $Version: 2007.08.19 +r2
  9. * Updated by Oleg Kiriljuk to support touch devices
  10. * Copyright (c) 2014-2018, Oleg Kiriljuk, oleg.kiriljuk@ok-soft-gmbh.com
  11. */
  12. /*jslint browser: true, white: true */
  13. /*global jQuery, define, exports, module, require */
  14. (function (global, factory) {
  15. "use strict";
  16. if (typeof define === "function" && define.amd) {
  17. // AMD. Register as an anonymous module.
  18. define([
  19. "jquery"
  20. ], function ($) {
  21. return factory($, global, global.document);
  22. });
  23. } else if (typeof module === "object" && module.exports) {
  24. // Node/CommonJS
  25. module.exports = function (root, $) {
  26. if (!root) {
  27. root = window;
  28. }
  29. if ($ === undefined) {
  30. // require("jquery") returns a factory that requires window to
  31. // build a jQuery instance, we normalize how we use modules
  32. // that require this pattern but the window provided is a noop
  33. // if it's defined (how jquery works)
  34. $ = typeof window !== "undefined" ?
  35. require("jquery") :
  36. require("jquery")(root);
  37. }
  38. factory($, root, root.document);
  39. return $;
  40. };
  41. } else {
  42. // Browser globals
  43. factory(jQuery, global, global.document);
  44. }
  45. }(typeof window !== "undefined" ? window : this, function ($, window, document) {
  46. "use strict";
  47. // begin module jqdnr
  48. var namespace = ".jqGrid", mouseDown = "mousedown", mouseMove = "mousemove", mouseUp = "mouseup",
  49. getMouseCoordinates = function (e) {
  50. var orgEvent = e.originalEvent, touches = orgEvent.targetTouches;
  51. if (touches) {
  52. touches = touches[0];
  53. return { x: touches.pageX, y: touches.pageY };
  54. }
  55. return { x: e.pageX, y: e.pageY };
  56. },
  57. jqDnR = {
  58. drag: function (e) {
  59. var d = e.data, $mainDialog = d.e, dnrMainDialog = d.dnr, $alsoResize = d.ar, dnrAlsoResize = d.dnrAr,
  60. xy = getMouseCoordinates(e);
  61. if (dnrMainDialog.k === "move") {
  62. $mainDialog.css({
  63. left: dnrMainDialog.X + xy.x - dnrMainDialog.pX,
  64. top: dnrMainDialog.Y + xy.y - dnrMainDialog.pY
  65. });
  66. } else {
  67. $mainDialog.css({
  68. width: Math.max(xy.x - dnrMainDialog.pX + dnrMainDialog.W, 0),
  69. height: Math.max(xy.y - dnrMainDialog.pY + dnrMainDialog.H, 0)
  70. });
  71. if (dnrAlsoResize) {
  72. $alsoResize.css({
  73. width: Math.max(xy.x - dnrAlsoResize.pX + dnrAlsoResize.W, 0),
  74. height: Math.max(xy.y - dnrAlsoResize.pY + dnrAlsoResize.H, 0)
  75. });
  76. }
  77. }
  78. return false;
  79. },
  80. stop: function () {
  81. //$mainDialog.css("opacity", dnr.o);
  82. $(document).off(mouseMove, jqDnR.drag).off(mouseUp, jqDnR.stop);
  83. }
  84. },
  85. init = function ($this, handle, actionName, alsoResize) {
  86. return $this.each(function () {
  87. handle = handle ? $(handle, $this) : $this;
  88. handle.on(mouseDown, { e: $this, k: actionName }, function (e) {
  89. var d = e.data, p = {}, $inputDatePicker, $mainDialog, dnrMainDialog, $alsoResize, dnrAlsoResize,
  90. getCssProp = function ($elem, propertyName) {
  91. return parseInt($elem.css(propertyName), 10) || false;
  92. },
  93. getMainCssProp = function (propertyName) {
  94. return getCssProp($mainDialog, propertyName);
  95. },
  96. getAlsoResizeCssProp = function (propertyName) {
  97. return getCssProp($alsoResize, propertyName);
  98. },
  99. xy = getMouseCoordinates(e),
  100. eventData;
  101. if ($(e.target).hasClass("ui-jqdialog-titlebar-close") || $(e.target).parent().hasClass("ui-jqdialog-titlebar-close")) {
  102. //$(e.target).click();
  103. return;
  104. }
  105. $mainDialog = d.e;
  106. $alsoResize = alsoResize ? $(alsoResize) : false;
  107. // attempt utilization of dimensions plugin to fix IE issues
  108. if ($mainDialog.css("position") !== "relative") {
  109. try {
  110. // ???? probably one want to GET position and save it in p ?
  111. // the current implementation use always p = {}
  112. // probably one mean some additional work together with Dimensions Plugin (dimensions.js)
  113. $mainDialog.position(p);
  114. } catch (ignore) { }
  115. }
  116. dnrMainDialog = {
  117. X: p.left || getMainCssProp("left") || 0,
  118. Y: p.top || getMainCssProp("top") || 0,
  119. W: getMainCssProp("width") || $mainDialog[0].scrollWidth || 0,
  120. H: getMainCssProp("height") || $mainDialog[0].scrollHeight || 0,
  121. pX: xy.x,
  122. pY: xy.y,
  123. k: d.k
  124. //o:$mainDialog.css("opacity")
  125. };
  126. // also resize
  127. if ($alsoResize && d.k !== "move") {
  128. dnrAlsoResize = {
  129. X: p.left || getAlsoResizeCssProp("left") || 0,
  130. Y: p.top || getAlsoResizeCssProp("top") || 0,
  131. W: $alsoResize[0].offsetWidth || getAlsoResizeCssProp("width") || 0,
  132. H: $alsoResize[0].offsetHeight || getAlsoResizeCssProp("height") || 0,
  133. pX: xy.x,
  134. pY: xy.y,
  135. k: d.k
  136. };
  137. } else {
  138. dnrAlsoResize = false;
  139. }
  140. //E.css({opacity:0.8});
  141. $inputDatePicker = $mainDialog.find("input.hasDatepicker");
  142. if ($inputDatePicker.length > 0) {
  143. try {
  144. $inputDatePicker.datepicker("hide");
  145. } catch (ignore) { }
  146. }
  147. eventData = {
  148. e: $mainDialog,
  149. dnr: dnrMainDialog,
  150. ar: $alsoResize,
  151. dnrAr: dnrAlsoResize
  152. };
  153. $(document).on(mouseMove, eventData, jqDnR.drag);
  154. $(document).on(mouseUp, eventData, jqDnR.stop);
  155. return false;
  156. });
  157. });
  158. };
  159. // navigator.maxTouchPoints == 2, navigator.msMaxTouchPoints
  160. // https://msdn.microsoft.com/en-us/library/ie/dn304886(v=vs.85).aspx
  161. if (window.PointerEvent) {
  162. mouseDown += namespace + " pointerdown" + namespace;
  163. mouseMove += namespace + " pointermove" + namespace;
  164. mouseUp += namespace + " pointerup" + namespace;
  165. } else if (window.MSPointerEvent) {
  166. mouseDown += namespace + " mspointerdown" + namespace;
  167. mouseMove += namespace + " mspointermove" + namespace;
  168. mouseUp += namespace + " mspointerup";
  169. } else { //if (Object.prototype.hasOwnProperty.call(document, "ontouchend")) { or "ontouchstart" in document.documentElement or "ontouchstart" in window
  170. mouseDown += namespace + " touchstart" + namespace;
  171. mouseMove += namespace + " touchmove" + namespace;
  172. mouseUp += namespace + " touchend" + namespace;
  173. }
  174. $.jqDnR = jqDnR;
  175. $.fn.jqDrag = function (handle) {
  176. return init(this, handle, "move");
  177. };
  178. $.fn.jqResize = function (handle, alsoResize) {
  179. return init(this, handle, "resize", alsoResize);
  180. };
  181. // end module jqdnr
  182. }));