Merge "Typo fix"
[mediawiki.git] / resources / jquery.ui / jquery.ui.selectable.js
blob44c6e8c54e2d3a4f0fa29c3efc32bc3b87b218f2
1 /*!
2  * jQuery UI Selectable 1.8.24
3  *
4  * Copyright 2012, AUTHORS.txt (http://jqueryui.com/about)
5  * Dual licensed under the MIT or GPL Version 2 licenses.
6  * http://jquery.org/license
7  *
8  * http://docs.jquery.com/UI/Selectables
9  *
10  * Depends:
11  *      jquery.ui.core.js
12  *      jquery.ui.mouse.js
13  *      jquery.ui.widget.js
14  */
15 (function( $, undefined ) {
17 $.widget("ui.selectable", $.ui.mouse, {
18         options: {
19                 appendTo: 'body',
20                 autoRefresh: true,
21                 distance: 0,
22                 filter: '*',
23                 tolerance: 'touch'
24         },
25         _create: function() {
26                 var self = this;
28                 this.element.addClass("ui-selectable");
30                 this.dragged = false;
32                 // cache selectee children based on filter
33                 var selectees;
34                 this.refresh = function() {
35                         selectees = $(self.options.filter, self.element[0]);
36                         selectees.addClass("ui-selectee");
37                         selectees.each(function() {
38                                 var $this = $(this);
39                                 var pos = $this.offset();
40                                 $.data(this, "selectable-item", {
41                                         element: this,
42                                         $element: $this,
43                                         left: pos.left,
44                                         top: pos.top,
45                                         right: pos.left + $this.outerWidth(),
46                                         bottom: pos.top + $this.outerHeight(),
47                                         startselected: false,
48                                         selected: $this.hasClass('ui-selected'),
49                                         selecting: $this.hasClass('ui-selecting'),
50                                         unselecting: $this.hasClass('ui-unselecting')
51                                 });
52                         });
53                 };
54                 this.refresh();
56                 this.selectees = selectees.addClass("ui-selectee");
58                 this._mouseInit();
60                 this.helper = $("<div class='ui-selectable-helper'></div>");
61         },
63         destroy: function() {
64                 this.selectees
65                         .removeClass("ui-selectee")
66                         .removeData("selectable-item");
67                 this.element
68                         .removeClass("ui-selectable ui-selectable-disabled")
69                         .removeData("selectable")
70                         .unbind(".selectable");
71                 this._mouseDestroy();
73                 return this;
74         },
76         _mouseStart: function(event) {
77                 var self = this;
79                 this.opos = [event.pageX, event.pageY];
81                 if (this.options.disabled)
82                         return;
84                 var options = this.options;
86                 this.selectees = $(options.filter, this.element[0]);
88                 this._trigger("start", event);
90                 $(options.appendTo).append(this.helper);
91                 // position helper (lasso)
92                 this.helper.css({
93                         "left": event.clientX,
94                         "top": event.clientY,
95                         "width": 0,
96                         "height": 0
97                 });
99                 if (options.autoRefresh) {
100                         this.refresh();
101                 }
103                 this.selectees.filter('.ui-selected').each(function() {
104                         var selectee = $.data(this, "selectable-item");
105                         selectee.startselected = true;
106                         if (!event.metaKey && !event.ctrlKey) {
107                                 selectee.$element.removeClass('ui-selected');
108                                 selectee.selected = false;
109                                 selectee.$element.addClass('ui-unselecting');
110                                 selectee.unselecting = true;
111                                 // selectable UNSELECTING callback
112                                 self._trigger("unselecting", event, {
113                                         unselecting: selectee.element
114                                 });
115                         }
116                 });
118                 $(event.target).parents().andSelf().each(function() {
119                         var selectee = $.data(this, "selectable-item");
120                         if (selectee) {
121                                 var doSelect = (!event.metaKey && !event.ctrlKey) || !selectee.$element.hasClass('ui-selected');
122                                 selectee.$element
123                                         .removeClass(doSelect ? "ui-unselecting" : "ui-selected")
124                                         .addClass(doSelect ? "ui-selecting" : "ui-unselecting");
125                                 selectee.unselecting = !doSelect;
126                                 selectee.selecting = doSelect;
127                                 selectee.selected = doSelect;
128                                 // selectable (UN)SELECTING callback
129                                 if (doSelect) {
130                                         self._trigger("selecting", event, {
131                                                 selecting: selectee.element
132                                         });
133                                 } else {
134                                         self._trigger("unselecting", event, {
135                                                 unselecting: selectee.element
136                                         });
137                                 }
138                                 return false;
139                         }
140                 });
142         },
144         _mouseDrag: function(event) {
145                 var self = this;
146                 this.dragged = true;
148                 if (this.options.disabled)
149                         return;
151                 var options = this.options;
153                 var x1 = this.opos[0], y1 = this.opos[1], x2 = event.pageX, y2 = event.pageY;
154                 if (x1 > x2) { var tmp = x2; x2 = x1; x1 = tmp; }
155                 if (y1 > y2) { var tmp = y2; y2 = y1; y1 = tmp; }
156                 this.helper.css({left: x1, top: y1, width: x2-x1, height: y2-y1});
158                 this.selectees.each(function() {
159                         var selectee = $.data(this, "selectable-item");
160                         //prevent helper from being selected if appendTo: selectable
161                         if (!selectee || selectee.element == self.element[0])
162                                 return;
163                         var hit = false;
164                         if (options.tolerance == 'touch') {
165                                 hit = ( !(selectee.left > x2 || selectee.right < x1 || selectee.top > y2 || selectee.bottom < y1) );
166                         } else if (options.tolerance == 'fit') {
167                                 hit = (selectee.left > x1 && selectee.right < x2 && selectee.top > y1 && selectee.bottom < y2);
168                         }
170                         if (hit) {
171                                 // SELECT
172                                 if (selectee.selected) {
173                                         selectee.$element.removeClass('ui-selected');
174                                         selectee.selected = false;
175                                 }
176                                 if (selectee.unselecting) {
177                                         selectee.$element.removeClass('ui-unselecting');
178                                         selectee.unselecting = false;
179                                 }
180                                 if (!selectee.selecting) {
181                                         selectee.$element.addClass('ui-selecting');
182                                         selectee.selecting = true;
183                                         // selectable SELECTING callback
184                                         self._trigger("selecting", event, {
185                                                 selecting: selectee.element
186                                         });
187                                 }
188                         } else {
189                                 // UNSELECT
190                                 if (selectee.selecting) {
191                                         if ((event.metaKey || event.ctrlKey) && selectee.startselected) {
192                                                 selectee.$element.removeClass('ui-selecting');
193                                                 selectee.selecting = false;
194                                                 selectee.$element.addClass('ui-selected');
195                                                 selectee.selected = true;
196                                         } else {
197                                                 selectee.$element.removeClass('ui-selecting');
198                                                 selectee.selecting = false;
199                                                 if (selectee.startselected) {
200                                                         selectee.$element.addClass('ui-unselecting');
201                                                         selectee.unselecting = true;
202                                                 }
203                                                 // selectable UNSELECTING callback
204                                                 self._trigger("unselecting", event, {
205                                                         unselecting: selectee.element
206                                                 });
207                                         }
208                                 }
209                                 if (selectee.selected) {
210                                         if (!event.metaKey && !event.ctrlKey && !selectee.startselected) {
211                                                 selectee.$element.removeClass('ui-selected');
212                                                 selectee.selected = false;
214                                                 selectee.$element.addClass('ui-unselecting');
215                                                 selectee.unselecting = true;
216                                                 // selectable UNSELECTING callback
217                                                 self._trigger("unselecting", event, {
218                                                         unselecting: selectee.element
219                                                 });
220                                         }
221                                 }
222                         }
223                 });
225                 return false;
226         },
228         _mouseStop: function(event) {
229                 var self = this;
231                 this.dragged = false;
233                 var options = this.options;
235                 $('.ui-unselecting', this.element[0]).each(function() {
236                         var selectee = $.data(this, "selectable-item");
237                         selectee.$element.removeClass('ui-unselecting');
238                         selectee.unselecting = false;
239                         selectee.startselected = false;
240                         self._trigger("unselected", event, {
241                                 unselected: selectee.element
242                         });
243                 });
244                 $('.ui-selecting', this.element[0]).each(function() {
245                         var selectee = $.data(this, "selectable-item");
246                         selectee.$element.removeClass('ui-selecting').addClass('ui-selected');
247                         selectee.selecting = false;
248                         selectee.selected = true;
249                         selectee.startselected = true;
250                         self._trigger("selected", event, {
251                                 selected: selectee.element
252                         });
253                 });
254                 this._trigger("stop", event);
256                 this.helper.remove();
258                 return false;
259         }
263 $.extend($.ui.selectable, {
264         version: "1.8.24"
267 })(jQuery);