UPDATE 4.4.0.0
[phpmyadmin.git] / js / jquery / src / jquery-ui / selectable.js
blobd3c428548130116314144daf2d910a2a031dc457
1 /*!
2  * jQuery UI Selectable 1.11.2
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 (function( factory ) {
12         if ( typeof define === "function" && define.amd ) {
14                 // AMD. Register as an anonymous module.
15                 define([
16                         "jquery",
17                         "./core",
18                         "./mouse",
19                         "./widget"
20                 ], factory );
21         } else {
23                 // Browser globals
24                 factory( jQuery );
25         }
26 }(function( $ ) {
28 return $.widget("ui.selectable", $.ui.mouse, {
29         version: "1.11.2",
30         options: {
31                 appendTo: "body",
32                 autoRefresh: true,
33                 distance: 0,
34                 filter: "*",
35                 tolerance: "touch",
37                 // callbacks
38                 selected: null,
39                 selecting: null,
40                 start: null,
41                 stop: null,
42                 unselected: null,
43                 unselecting: null
44         },
45         _create: function() {
46                 var selectees,
47                         that = this;
49                 this.element.addClass("ui-selectable");
51                 this.dragged = false;
53                 // cache selectee children based on filter
54                 this.refresh = function() {
55                         selectees = $(that.options.filter, that.element[0]);
56                         selectees.addClass("ui-selectee");
57                         selectees.each(function() {
58                                 var $this = $(this),
59                                         pos = $this.offset();
60                                 $.data(this, "selectable-item", {
61                                         element: this,
62                                         $element: $this,
63                                         left: pos.left,
64                                         top: pos.top,
65                                         right: pos.left + $this.outerWidth(),
66                                         bottom: pos.top + $this.outerHeight(),
67                                         startselected: false,
68                                         selected: $this.hasClass("ui-selected"),
69                                         selecting: $this.hasClass("ui-selecting"),
70                                         unselecting: $this.hasClass("ui-unselecting")
71                                 });
72                         });
73                 };
74                 this.refresh();
76                 this.selectees = selectees.addClass("ui-selectee");
78                 this._mouseInit();
80                 this.helper = $("<div class='ui-selectable-helper'></div>");
81         },
83         _destroy: function() {
84                 this.selectees
85                         .removeClass("ui-selectee")
86                         .removeData("selectable-item");
87                 this.element
88                         .removeClass("ui-selectable ui-selectable-disabled");
89                 this._mouseDestroy();
90         },
92         _mouseStart: function(event) {
93                 var that = this,
94                         options = this.options;
96                 this.opos = [ event.pageX, event.pageY ];
98                 if (this.options.disabled) {
99                         return;
100                 }
102                 this.selectees = $(options.filter, this.element[0]);
104                 this._trigger("start", event);
106                 $(options.appendTo).append(this.helper);
107                 // position helper (lasso)
108                 this.helper.css({
109                         "left": event.pageX,
110                         "top": event.pageY,
111                         "width": 0,
112                         "height": 0
113                 });
115                 if (options.autoRefresh) {
116                         this.refresh();
117                 }
119                 this.selectees.filter(".ui-selected").each(function() {
120                         var selectee = $.data(this, "selectable-item");
121                         selectee.startselected = true;
122                         if (!event.metaKey && !event.ctrlKey) {
123                                 selectee.$element.removeClass("ui-selected");
124                                 selectee.selected = false;
125                                 selectee.$element.addClass("ui-unselecting");
126                                 selectee.unselecting = true;
127                                 // selectable UNSELECTING callback
128                                 that._trigger("unselecting", event, {
129                                         unselecting: selectee.element
130                                 });
131                         }
132                 });
134                 $(event.target).parents().addBack().each(function() {
135                         var doSelect,
136                                 selectee = $.data(this, "selectable-item");
137                         if (selectee) {
138                                 doSelect = (!event.metaKey && !event.ctrlKey) || !selectee.$element.hasClass("ui-selected");
139                                 selectee.$element
140                                         .removeClass(doSelect ? "ui-unselecting" : "ui-selected")
141                                         .addClass(doSelect ? "ui-selecting" : "ui-unselecting");
142                                 selectee.unselecting = !doSelect;
143                                 selectee.selecting = doSelect;
144                                 selectee.selected = doSelect;
145                                 // selectable (UN)SELECTING callback
146                                 if (doSelect) {
147                                         that._trigger("selecting", event, {
148                                                 selecting: selectee.element
149                                         });
150                                 } else {
151                                         that._trigger("unselecting", event, {
152                                                 unselecting: selectee.element
153                                         });
154                                 }
155                                 return false;
156                         }
157                 });
159         },
161         _mouseDrag: function(event) {
163                 this.dragged = true;
165                 if (this.options.disabled) {
166                         return;
167                 }
169                 var tmp,
170                         that = this,
171                         options = this.options,
172                         x1 = this.opos[0],
173                         y1 = this.opos[1],
174                         x2 = event.pageX,
175                         y2 = event.pageY;
177                 if (x1 > x2) { tmp = x2; x2 = x1; x1 = tmp; }
178                 if (y1 > y2) { tmp = y2; y2 = y1; y1 = tmp; }
179                 this.helper.css({ left: x1, top: y1, width: x2 - x1, height: y2 - y1 });
181                 this.selectees.each(function() {
182                         var selectee = $.data(this, "selectable-item"),
183                                 hit = false;
185                         //prevent helper from being selected if appendTo: selectable
186                         if (!selectee || selectee.element === that.element[0]) {
187                                 return;
188                         }
190                         if (options.tolerance === "touch") {
191                                 hit = ( !(selectee.left > x2 || selectee.right < x1 || selectee.top > y2 || selectee.bottom < y1) );
192                         } else if (options.tolerance === "fit") {
193                                 hit = (selectee.left > x1 && selectee.right < x2 && selectee.top > y1 && selectee.bottom < y2);
194                         }
196                         if (hit) {
197                                 // SELECT
198                                 if (selectee.selected) {
199                                         selectee.$element.removeClass("ui-selected");
200                                         selectee.selected = false;
201                                 }
202                                 if (selectee.unselecting) {
203                                         selectee.$element.removeClass("ui-unselecting");
204                                         selectee.unselecting = false;
205                                 }
206                                 if (!selectee.selecting) {
207                                         selectee.$element.addClass("ui-selecting");
208                                         selectee.selecting = true;
209                                         // selectable SELECTING callback
210                                         that._trigger("selecting", event, {
211                                                 selecting: selectee.element
212                                         });
213                                 }
214                         } else {
215                                 // UNSELECT
216                                 if (selectee.selecting) {
217                                         if ((event.metaKey || event.ctrlKey) && selectee.startselected) {
218                                                 selectee.$element.removeClass("ui-selecting");
219                                                 selectee.selecting = false;
220                                                 selectee.$element.addClass("ui-selected");
221                                                 selectee.selected = true;
222                                         } else {
223                                                 selectee.$element.removeClass("ui-selecting");
224                                                 selectee.selecting = false;
225                                                 if (selectee.startselected) {
226                                                         selectee.$element.addClass("ui-unselecting");
227                                                         selectee.unselecting = true;
228                                                 }
229                                                 // selectable UNSELECTING callback
230                                                 that._trigger("unselecting", event, {
231                                                         unselecting: selectee.element
232                                                 });
233                                         }
234                                 }
235                                 if (selectee.selected) {
236                                         if (!event.metaKey && !event.ctrlKey && !selectee.startselected) {
237                                                 selectee.$element.removeClass("ui-selected");
238                                                 selectee.selected = false;
240                                                 selectee.$element.addClass("ui-unselecting");
241                                                 selectee.unselecting = true;
242                                                 // selectable UNSELECTING callback
243                                                 that._trigger("unselecting", event, {
244                                                         unselecting: selectee.element
245                                                 });
246                                         }
247                                 }
248                         }
249                 });
251                 return false;
252         },
254         _mouseStop: function(event) {
255                 var that = this;
257                 this.dragged = false;
259                 $(".ui-unselecting", this.element[0]).each(function() {
260                         var selectee = $.data(this, "selectable-item");
261                         selectee.$element.removeClass("ui-unselecting");
262                         selectee.unselecting = false;
263                         selectee.startselected = false;
264                         that._trigger("unselected", event, {
265                                 unselected: selectee.element
266                         });
267                 });
268                 $(".ui-selecting", this.element[0]).each(function() {
269                         var selectee = $.data(this, "selectable-item");
270                         selectee.$element.removeClass("ui-selecting").addClass("ui-selected");
271                         selectee.selecting = false;
272                         selectee.selected = true;
273                         selectee.startselected = true;
274                         that._trigger("selected", event, {
275                                 selected: selectee.element
276                         });
277                 });
278                 this._trigger("stop", event);
280                 this.helper.remove();
282                 return false;
283         }
287 }));