You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
7.9 KiB

1 year ago
  1. /*!
  2. * jQuery UI Selectable 1.13.2
  3. * http://jqueryui.com
  4. *
  5. * Copyright jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. */
  9. //>>label: Selectable
  10. //>>group: Interactions
  11. //>>description: Allows groups of elements to be selected with the mouse.
  12. //>>docs: http://api.jqueryui.com/selectable/
  13. //>>demos: http://jqueryui.com/selectable/
  14. //>>css.structure: ../../themes/base/selectable.css
  15. ( function( factory ) {
  16. "use strict";
  17. if ( typeof define === "function" && define.amd ) {
  18. // AMD. Register as an anonymous module.
  19. define( [
  20. "jquery",
  21. "./mouse",
  22. "./core"
  23. ], factory );
  24. } else {
  25. // Browser globals
  26. factory( jQuery );
  27. }
  28. } )( function( $ ) {
  29. "use strict";
  30. return $.widget( "ui.selectable", $.ui.mouse, {
  31. version: "1.13.2",
  32. options: {
  33. appendTo: "body",
  34. autoRefresh: true,
  35. distance: 0,
  36. filter: "*",
  37. tolerance: "touch",
  38. // Callbacks
  39. selected: null,
  40. selecting: null,
  41. start: null,
  42. stop: null,
  43. unselected: null,
  44. unselecting: null
  45. },
  46. _create: function() {
  47. var that = this;
  48. this._addClass( "ui-selectable" );
  49. this.dragged = false;
  50. // Cache selectee children based on filter
  51. this.refresh = function() {
  52. that.elementPos = $( that.element[ 0 ] ).offset();
  53. that.selectees = $( that.options.filter, that.element[ 0 ] );
  54. that._addClass( that.selectees, "ui-selectee" );
  55. that.selectees.each( function() {
  56. var $this = $( this ),
  57. selecteeOffset = $this.offset(),
  58. pos = {
  59. left: selecteeOffset.left - that.elementPos.left,
  60. top: selecteeOffset.top - that.elementPos.top
  61. };
  62. $.data( this, "selectable-item", {
  63. element: this,
  64. $element: $this,
  65. left: pos.left,
  66. top: pos.top,
  67. right: pos.left + $this.outerWidth(),
  68. bottom: pos.top + $this.outerHeight(),
  69. startselected: false,
  70. selected: $this.hasClass( "ui-selected" ),
  71. selecting: $this.hasClass( "ui-selecting" ),
  72. unselecting: $this.hasClass( "ui-unselecting" )
  73. } );
  74. } );
  75. };
  76. this.refresh();
  77. this._mouseInit();
  78. this.helper = $( "<div>" );
  79. this._addClass( this.helper, "ui-selectable-helper" );
  80. },
  81. _destroy: function() {
  82. this.selectees.removeData( "selectable-item" );
  83. this._mouseDestroy();
  84. },
  85. _mouseStart: function( event ) {
  86. var that = this,
  87. options = this.options;
  88. this.opos = [ event.pageX, event.pageY ];
  89. this.elementPos = $( this.element[ 0 ] ).offset();
  90. if ( this.options.disabled ) {
  91. return;
  92. }
  93. this.selectees = $( options.filter, this.element[ 0 ] );
  94. this._trigger( "start", event );
  95. $( options.appendTo ).append( this.helper );
  96. // position helper (lasso)
  97. this.helper.css( {
  98. "left": event.pageX,
  99. "top": event.pageY,
  100. "width": 0,
  101. "height": 0
  102. } );
  103. if ( options.autoRefresh ) {
  104. this.refresh();
  105. }
  106. this.selectees.filter( ".ui-selected" ).each( function() {
  107. var selectee = $.data( this, "selectable-item" );
  108. selectee.startselected = true;
  109. if ( !event.metaKey && !event.ctrlKey ) {
  110. that._removeClass( selectee.$element, "ui-selected" );
  111. selectee.selected = false;
  112. that._addClass( selectee.$element, "ui-unselecting" );
  113. selectee.unselecting = true;
  114. // selectable UNSELECTING callback
  115. that._trigger( "unselecting", event, {
  116. unselecting: selectee.element
  117. } );
  118. }
  119. } );
  120. $( event.target ).parents().addBack().each( function() {
  121. var doSelect,
  122. selectee = $.data( this, "selectable-item" );
  123. if ( selectee ) {
  124. doSelect = ( !event.metaKey && !event.ctrlKey ) ||
  125. !selectee.$element.hasClass( "ui-selected" );
  126. that._removeClass( selectee.$element, doSelect ? "ui-unselecting" : "ui-selected" )
  127. ._addClass( selectee.$element, doSelect ? "ui-selecting" : "ui-unselecting" );
  128. selectee.unselecting = !doSelect;
  129. selectee.selecting = doSelect;
  130. selectee.selected = doSelect;
  131. // selectable (UN)SELECTING callback
  132. if ( doSelect ) {
  133. that._trigger( "selecting", event, {
  134. selecting: selectee.element
  135. } );
  136. } else {
  137. that._trigger( "unselecting", event, {
  138. unselecting: selectee.element
  139. } );
  140. }
  141. return false;
  142. }
  143. } );
  144. },
  145. _mouseDrag: function( event ) {
  146. this.dragged = true;
  147. if ( this.options.disabled ) {
  148. return;
  149. }
  150. var tmp,
  151. that = this,
  152. options = this.options,
  153. x1 = this.opos[ 0 ],
  154. y1 = this.opos[ 1 ],
  155. x2 = event.pageX,
  156. y2 = event.pageY;
  157. if ( x1 > x2 ) {
  158. tmp = x2; x2 = x1; x1 = tmp;
  159. }
  160. if ( y1 > y2 ) {
  161. tmp = y2; y2 = y1; y1 = tmp;
  162. }
  163. this.helper.css( { left: x1, top: y1, width: x2 - x1, height: y2 - y1 } );
  164. this.selectees.each( function() {
  165. var selectee = $.data( this, "selectable-item" ),
  166. hit = false,
  167. offset = {};
  168. //prevent helper from being selected if appendTo: selectable
  169. if ( !selectee || selectee.element === that.element[ 0 ] ) {
  170. return;
  171. }
  172. offset.left = selectee.left + that.elementPos.left;
  173. offset.right = selectee.right + that.elementPos.left;
  174. offset.top = selectee.top + that.elementPos.top;
  175. offset.bottom = selectee.bottom + that.elementPos.top;
  176. if ( options.tolerance === "touch" ) {
  177. hit = ( !( offset.left > x2 || offset.right < x1 || offset.top > y2 ||
  178. offset.bottom < y1 ) );
  179. } else if ( options.tolerance === "fit" ) {
  180. hit = ( offset.left > x1 && offset.right < x2 && offset.top > y1 &&
  181. offset.bottom < y2 );
  182. }
  183. if ( hit ) {
  184. // SELECT
  185. if ( selectee.selected ) {
  186. that._removeClass( selectee.$element, "ui-selected" );
  187. selectee.selected = false;
  188. }
  189. if ( selectee.unselecting ) {
  190. that._removeClass( selectee.$element, "ui-unselecting" );
  191. selectee.unselecting = false;
  192. }
  193. if ( !selectee.selecting ) {
  194. that._addClass( selectee.$element, "ui-selecting" );
  195. selectee.selecting = true;
  196. // selectable SELECTING callback
  197. that._trigger( "selecting", event, {
  198. selecting: selectee.element
  199. } );
  200. }
  201. } else {
  202. // UNSELECT
  203. if ( selectee.selecting ) {
  204. if ( ( event.metaKey || event.ctrlKey ) && selectee.startselected ) {
  205. that._removeClass( selectee.$element, "ui-selecting" );
  206. selectee.selecting = false;
  207. that._addClass( selectee.$element, "ui-selected" );
  208. selectee.selected = true;
  209. } else {
  210. that._removeClass( selectee.$element, "ui-selecting" );
  211. selectee.selecting = false;
  212. if ( selectee.startselected ) {
  213. that._addClass( selectee.$element, "ui-unselecting" );
  214. selectee.unselecting = true;
  215. }
  216. // selectable UNSELECTING callback
  217. that._trigger( "unselecting", event, {
  218. unselecting: selectee.element
  219. } );
  220. }
  221. }
  222. if ( selectee.selected ) {
  223. if ( !event.metaKey && !event.ctrlKey && !selectee.startselected ) {
  224. that._removeClass( selectee.$element, "ui-selected" );
  225. selectee.selected = false;
  226. that._addClass( selectee.$element, "ui-unselecting" );
  227. selectee.unselecting = true;
  228. // selectable UNSELECTING callback
  229. that._trigger( "unselecting", event, {
  230. unselecting: selectee.element
  231. } );
  232. }
  233. }
  234. }
  235. } );
  236. return false;
  237. },
  238. _mouseStop: function( event ) {
  239. var that = this;
  240. this.dragged = false;
  241. $( ".ui-unselecting", this.element[ 0 ] ).each( function() {
  242. var selectee = $.data( this, "selectable-item" );
  243. that._removeClass( selectee.$element, "ui-unselecting" );
  244. selectee.unselecting = false;
  245. selectee.startselected = false;
  246. that._trigger( "unselected", event, {
  247. unselected: selectee.element
  248. } );
  249. } );
  250. $( ".ui-selecting", this.element[ 0 ] ).each( function() {
  251. var selectee = $.data( this, "selectable-item" );
  252. that._removeClass( selectee.$element, "ui-selecting" )
  253. ._addClass( selectee.$element, "ui-selected" );
  254. selectee.selecting = false;
  255. selectee.selected = true;
  256. selectee.startselected = true;
  257. that._trigger( "selected", event, {
  258. selected: selectee.element
  259. } );
  260. } );
  261. this._trigger( "stop", event );
  262. this.helper.remove();
  263. return false;
  264. }
  265. } );
  266. } );