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