r3109 committed - dev selectable: implemented select/deselect methods, integrated into s...

r3109 committed - dev selectable: implemented select/deselect methods, integrated into s...


Revision: 3109
Author: paul.bakaus
Date: Sat Aug 22 06:16:29 2009
Log: dev selectable: implemented select/deselect methods, integrated into
serialize demo
http://code.google.com/p/jquery-ui/source/detail?r=3109
Modified:
/branches/dev/selectable/demos/selectable/serialize.html
/branches/dev/selectable/ui/ui.selectable.js
=======================================
--- /branches/dev/selectable/demos/selectable/serialize.html    Fri Aug 21
07:52:34 2009
+++ /branches/dev/selectable/demos/selectable/serialize.html    Sat Aug 22
06:16:29 2009
@@ -51,6 +51,18 @@
            $('#selectable').selectable('refresh');
        });
+        $('button.select').bind('click', function() {
+            $('#selectable').selectable('select', 2);
+        });
+
+        $('button.deselect').bind('click', function() {
+            $('#selectable').selectable('deselect', 2);
+        });
+
+        $('button.select2').bind('click', function() {
+            $('#selectable').selectable('select', '*');
+        });
+
    });
    </script>
</head>
@@ -64,6 +76,9 @@


    <button class="add">Add item</button>
    <button class="remove">Remove item</button>
+    <button class="select">Select item 3</button>
+    <button class="deselect">Deselect item 3</button>
+    <button class="select2">Select all items</button>







<ol id="selectable" tabindex='1'>
=======================================
--- /branches/dev/selectable/ui/ui.selectable.js    Fri Aug 21 07:52:34 2009
+++ /branches/dev/selectable/ui/ui.selectable.js    Sat Aug 22 06:16:29 2009
@@ -24,7 +24,7 @@
            this.currentFocus = this.items.eq(0);
            //Refresh item positions
-            this.refresh();
+            this.refresh(1);
            //Disable text selection
            this.element.disableSelection();
@@ -203,7 +203,7 @@
                return;
            //Cache positions
-            this.refresh();
+            this.refresh(1);
            //Trigger start event
            this._trigger("start", event, this._uiHash());
@@ -342,7 +342,8 @@
            var triggerItems = [];
            for (var i = this._previousSelection.length - 1; i >= 0; i--){
-                if(!this._previousSelection[i].data('selectable-item').selected)
triggerItems.push(this._previousSelection[i]);
+                var data = this._previousSelection[i].data('selectable-item');
+                if(!data || !data.selected)
triggerItems.push(this._previousSelection[i]);
            };
            this._previousSelection = [];
@@ -411,7 +412,7 @@
            var newlySelected = [];
-            if (event.shiftKey && this.options.multiple) {
+            if (event && event.shiftKey && this.options.multiple) {
                //Clear the previous selection to make room for a shift selection
                this._clearSelection();
@@ -428,7 +429,7 @@
            } else {
-                if (event.metaKey) {
+                if (event && event.metaKey) {
                    var withMetaIsNewlySelected =
this._toggleSelection(this.currentFocus, event);
                    if(withMetaIsNewlySelected) newlySelected.push(this.currentFocus);
                } else {
@@ -530,7 +531,7 @@
            this._selectAdjacent(event, 1);
        },
-        refresh: function() {
+        refresh: function(fromInside) {
            var o = this.options;
            this.items = $(o.filter, this.element);
@@ -546,16 +547,67 @@
                    selected: $this.hasClass(o.selectedClass)
                });
            });
+
+
+            if(!fromInside) {
+                this._previousSelection = this._selection.slice();
+                this._selection = [];
+                for (var i=0; i < this._previousSelection.length; i++) {
+                    if(this._previousSelection[i][0].parentNode)
this._selection.push(this._previousSelection[i]);
+                };
+
+                this._endSelection();
+            }
+
        },
        select: function(item) {
-            //TODO
+
+            if(!isNaN(parseInt(item)))
+                item = this.items.get(item);
+
+            item = $(item, this.element);
+            if(!item.length) return;
+
+            // clear the current selection
+            this._clearSelection();
+
+            // select all found
+            var newlySelected = [], self = this;
+            item.each(function() {
+                newlySelected.push(self._addToSelection($(this)));
+            });
+
+            // Ending the selection does a diff and then triggers appropriate events
+            this._endSelection(event, $($.map(newlySelected, function(i) { return
i[0]; })));
+
        },
        deselect: function(item) {
-            if(!item) this._clearSelection(true);
-            //TODO: Deselect single elements
+
+            // if no item was specified, deselect all
+            if(!item)
+                this._clearSelection(true);
+
+            if(!isNaN(parseInt(item)))
+                item = this.items.get(item);
+
+            item = $(item, this.element);
+            if(!item.length) return;
+
+            //store the current selection
+            this._previousSelection = this._selection.slice();
+
+            // deselect all found
+            var self = this;
+            item.each(function() {
+                self._removeFromSelection($(this));
+            });
+
+            // Ending the selection does a diff and then triggers appropriate events
+            this._endSelection(event);
+
        },
        _uiHash: function(items, specialKey) {