sonata_jqueryui_js_jquery.ui.selectable_7.js 6.8 KB

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