source: trunk/template-common/lib/ui/ui.selectable.js @ 3282

Last change on this file since 3282 was 3282, checked in by plg, 15 years ago

change: according to topic:15067, svn:keywords property was removed

  • Property svn:eol-style set to LF
File size: 6.9 KB
Line 
1/*
2 * jQuery UI Selectable
3 *
4 * Copyright (c) 2008 Richard D. Worth (rdworth.org)
5 * Dual licensed under the MIT (MIT-LICENSE.txt)
6 * and GPL (GPL-LICENSE.txt) licenses.
7 *
8 * http://docs.jquery.com/UI/Selectables
9 *
10 * Depends:
11 *      ui.core.js
12 */
13(function($) {
14
15$.widget("ui.selectable", $.extend({}, $.ui.mouse, {
16        init: function() {
17                var self = this;
18               
19                this.element.addClass("ui-selectable");
20               
21                this.dragged = false;
22
23                // cache selectee children based on filter
24                var selectees;
25                this.refresh = function() {
26                        selectees = $(self.options.filter, self.element[0]);
27                        selectees.each(function() {
28                                var $this = $(this);
29                                var pos = $this.offset();
30                                $.data(this, "selectable-item", {
31                                        element: this,
32                                        $element: $this,
33                                        left: pos.left,
34                                        top: pos.top,
35                                        right: pos.left + $this.width(),
36                                        bottom: pos.top + $this.height(),
37                                        startselected: false,
38                                        selected: $this.hasClass('ui-selected'),
39                                        selecting: $this.hasClass('ui-selecting'),
40                                        unselecting: $this.hasClass('ui-unselecting')
41                                });
42                        });
43                };
44                this.refresh();
45
46                this.selectees = selectees.addClass("ui-selectee");
47               
48                this.mouseInit();
49               
50                this.helper = $(document.createElement('div')).css({border:'1px dotted black'});
51        },
52        toggle: function() {
53                if(this.options.disabled){
54                        this.enable();
55                } else {
56                        this.disable();
57                }
58        },
59        destroy: function() {
60                this.element
61                        .removeClass("ui-selectable ui-selectable-disabled")
62                        .removeData("selectable")
63                        .unbind(".selectable");
64                this.mouseDestroy();
65        },
66        mouseStart: function(e) {
67                var self = this;
68               
69                this.opos = [e.pageX, e.pageY];
70               
71                if (this.options.disabled)
72                        return;
73
74                var options = this.options;
75
76                this.selectees = $(options.filter, this.element[0]);
77
78                // selectable START callback
79                this.element.triggerHandler("selectablestart", [e, {
80                        "selectable": this.element[0],
81                        "options": options
82                }], options.start);
83
84                $('body').append(this.helper);
85                // position helper (lasso)
86                this.helper.css({
87                        "z-index": 100,
88                        "position": "absolute",
89                        "left": e.clientX,
90                        "top": e.clientY,
91                        "width": 0,
92                        "height": 0
93                });
94
95                if (options.autoRefresh) {
96                        this.refresh();
97                }
98
99                this.selectees.filter('.ui-selected').each(function() {
100                        var selectee = $.data(this, "selectable-item");
101                        selectee.startselected = true;
102                        if (!e.ctrlKey) {
103                                selectee.$element.removeClass('ui-selected');
104                                selectee.selected = false;
105                                selectee.$element.addClass('ui-unselecting');
106                                selectee.unselecting = true;
107                                // selectable UNSELECTING callback
108                                self.element.triggerHandler("selectableunselecting", [e, {
109                                        selectable: self.element[0],
110                                        unselecting: selectee.element,
111                                        options: options
112                                }], options.unselecting);
113                        }
114                });
115               
116                var isSelectee = false;
117                $(e.target).parents().andSelf().each(function() {
118                        if($.data(this, "selectable-item")) isSelectee = true;
119                });
120                return this.options.keyboard ? !isSelectee : true;
121        },
122        mouseDrag: function(e) {
123                var self = this;
124                this.dragged = true;
125               
126                if (this.options.disabled)
127                        return;
128
129                var options = this.options;
130
131                var x1 = this.opos[0], y1 = this.opos[1], x2 = e.pageX, y2 = e.pageY;
132                if (x1 > x2) { var tmp = x2; x2 = x1; x1 = tmp; }
133                if (y1 > y2) { var tmp = y2; y2 = y1; y1 = tmp; }
134                this.helper.css({left: x1, top: y1, width: x2-x1, height: y2-y1});
135
136                this.selectees.each(function() {
137                        var selectee = $.data(this, "selectable-item");
138                        //prevent helper from being selected if appendTo: selectable
139                        if (!selectee || selectee.element == self.element[0])
140                                return;
141                        var hit = false;
142                        if (options.tolerance == 'touch') {
143                                hit = ( !(selectee.left > x2 || selectee.right < x1 || selectee.top > y2 || selectee.bottom < y1) );
144                        } else if (options.tolerance == 'fit') {
145                                hit = (selectee.left > x1 && selectee.right < x2 && selectee.top > y1 && selectee.bottom < y2);
146                        }
147
148                        if (hit) {
149                                // SELECT
150                                if (selectee.selected) {
151                                        selectee.$element.removeClass('ui-selected');
152                                        selectee.selected = false;
153                                }
154                                if (selectee.unselecting) {
155                                        selectee.$element.removeClass('ui-unselecting');
156                                        selectee.unselecting = false;
157                                }
158                                if (!selectee.selecting) {
159                                        selectee.$element.addClass('ui-selecting');
160                                        selectee.selecting = true;
161                                        // selectable SELECTING callback
162                                        self.element.triggerHandler("selectableselecting", [e, {
163                                                selectable: self.element[0],
164                                                selecting: selectee.element,
165                                                options: options
166                                        }], options.selecting);
167                                }
168                        } else {
169                                // UNSELECT
170                                if (selectee.selecting) {
171                                        if (e.ctrlKey && selectee.startselected) {
172                                                selectee.$element.removeClass('ui-selecting');
173                                                selectee.selecting = false;
174                                                selectee.$element.addClass('ui-selected');
175                                                selectee.selected = true;
176                                        } else {
177                                                selectee.$element.removeClass('ui-selecting');
178                                                selectee.selecting = false;
179                                                if (selectee.startselected) {
180                                                        selectee.$element.addClass('ui-unselecting');
181                                                        selectee.unselecting = true;
182                                                }
183                                                // selectable UNSELECTING callback
184                                                self.element.triggerHandler("selectableunselecting", [e, {
185                                                        selectable: self.element[0],
186                                                        unselecting: selectee.element,
187                                                        options: options
188                                                }], options.unselecting);
189                                        }
190                                }
191                                if (selectee.selected) {
192                                        if (!e.ctrlKey && !selectee.startselected) {
193                                                selectee.$element.removeClass('ui-selected');
194                                                selectee.selected = false;
195
196                                                selectee.$element.addClass('ui-unselecting');
197                                                selectee.unselecting = true;
198                                                // selectable UNSELECTING callback
199                                                self.element.triggerHandler("selectableunselecting", [e, {
200                                                        selectable: self.element[0],
201                                                        unselecting: selectee.element,
202                                                        options: options
203                                                }], options.unselecting);
204                                        }
205                                }
206                        }
207                });
208               
209                return false;
210        },
211        mouseStop: function(e) {
212                var self = this;
213               
214                this.dragged = false;
215               
216                var options = this.options;
217
218                $('.ui-unselecting', this.element[0]).each(function() {
219                        var selectee = $.data(this, "selectable-item");
220                        selectee.$element.removeClass('ui-unselecting');
221                        selectee.unselecting = false;
222                        selectee.startselected = false;
223                        self.element.triggerHandler("selectableunselected", [e, {
224                                selectable: self.element[0],
225                                unselected: selectee.element,
226                                options: options
227                        }], options.unselected);
228                });
229                $('.ui-selecting', this.element[0]).each(function() {
230                        var selectee = $.data(this, "selectable-item");
231                        selectee.$element.removeClass('ui-selecting').addClass('ui-selected');
232                        selectee.selecting = false;
233                        selectee.selected = true;
234                        selectee.startselected = true;
235                        self.element.triggerHandler("selectableselected", [e, {
236                                selectable: self.element[0],
237                                selected: selectee.element,
238                                options: options
239                        }], options.selected);
240                });
241                this.element.triggerHandler("selectablestop", [e, {
242                        selectable: self.element[0],
243                        options: this.options
244                }], this.options.stop);
245               
246                this.helper.remove();
247               
248                return false;
249        }
250}));
251
252$.extend($.ui.selectable, {
253        defaults: {
254                distance: 1,
255                delay: 0,
256                cancel: ":input",
257                appendTo: 'body',
258                autoRefresh: true,
259                filter: '*',
260                tolerance: 'touch'
261        }
262});
263
264})(jQuery);
Note: See TracBrowser for help on using the repository browser.