2 * jQuery UI Selectable 1.8.21
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
8 * http://docs.jquery.com/UI/Selectables
15 (function( $, undefined ) {
17 $.widget("ui.selectable", $.ui
.mouse
, {
28 this.element
.addClass("ui-selectable");
32 // cache selectee children based on filter
34 this.refresh = function() {
35 selectees
= $(self
.options
.filter
, self
.element
[0]);
36 selectees
.addClass("ui-selectee");
37 selectees
.each(function() {
39 var pos
= $this.offset();
40 $.data(this, "selectable-item", {
45 right
: pos
.left
+ $this.outerWidth(),
46 bottom
: pos
.top
+ $this.outerHeight(),
48 selected
: $this.hasClass('ui-selected'),
49 selecting
: $this.hasClass('ui-selecting'),
50 unselecting
: $this.hasClass('ui-unselecting')
56 this.selectees
= selectees
.addClass("ui-selectee");
60 this.helper
= $("<div class='ui-selectable-helper'></div>");
65 .removeClass("ui-selectee")
66 .removeData("selectable-item");
68 .removeClass("ui-selectable ui-selectable-disabled")
69 .removeData("selectable")
70 .unbind(".selectable");
76 _mouseStart: function(event
) {
79 this.opos
= [event
.pageX
, event
.pageY
];
81 if (this.options
.disabled
)
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)
93 "left": event
.clientX
,
99 if (options
.autoRefresh
) {
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
118 $(event
.target
).parents().andSelf().each(function() {
119 var selectee
= $.data(this, "selectable-item");
121 var doSelect
= (!event
.metaKey
&& !event
.ctrlKey
) || !selectee
.$element
.hasClass('ui-selected');
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
130 self
._trigger("selecting", event
, {
131 selecting
: selectee
.element
134 self
._trigger("unselecting", event
, {
135 unselecting
: selectee
.element
144 _mouseDrag: function(event
) {
148 if (this.options
.disabled
)
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])
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
);
172 if (selectee
.selected
) {
173 selectee
.$element
.removeClass('ui-selected');
174 selectee
.selected
= false;
176 if (selectee
.unselecting
) {
177 selectee
.$element
.removeClass('ui-unselecting');
178 selectee
.unselecting
= false;
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
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;
197 selectee
.$element
.removeClass('ui-selecting');
198 selectee
.selecting
= false;
199 if (selectee
.startselected
) {
200 selectee
.$element
.addClass('ui-unselecting');
201 selectee
.unselecting
= true;
203 // selectable UNSELECTING callback
204 self
._trigger("unselecting", event
, {
205 unselecting
: selectee
.element
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
228 _mouseStop: function(event
) {
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
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
254 this._trigger("stop", event
);
256 this.helper
.remove();
263 $.extend($.ui
.selectable
, {