r3112 committed - selectable: fixed selectNext/selectPrevious/select issues, made all cu...

r3112 committed - selectable: fixed selectNext/selectPrevious/select issues, made all cu...


Revision: 3112
Author: paul.bakaus
Date: Sun Aug 23 02:26:34 2009
Log: selectable: fixed selectNext/selectPrevious/select issues, made all
current tests working, added tests for all methods
http://code.google.com/p/jquery-ui/source/detail?r=3112
Modified:
/branches/dev/selectable/demos/selectable/serialize.html
/branches/dev/selectable/tests/unit/selectable/selectable_defaults.js
/branches/dev/selectable/tests/unit/selectable/selectable_methods.js
/branches/dev/selectable/tests/unit/selectable/selectable_options.js
/branches/dev/selectable/ui/ui.selectable.js
=======================================
--- /branches/dev/selectable/demos/selectable/serialize.html    Sat Aug 22
06:16:29 2009
+++ /branches/dev/selectable/demos/selectable/serialize.html    Sun Aug 23
02:26:34 2009
@@ -63,6 +63,14 @@
            $('#selectable').selectable('select', '*');
        });
+        $('button.selectnext').bind('click', function() {
+            $('#selectable').selectable('selectNext');
+        });
+
+        $('button.selectprev').bind('click', function() {
+            $('#selectable').selectable('selectPrevious');
+        });
+
    });
    </script>
</head>
@@ -78,7 +86,9 @@
    <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>
+    <button class="select2">Select all items</button> |
+    <button class="selectprev">Select Previous</button>
+    <button class="selectnext">Select Next</button>

<ol id="selectable" tabindex='1'>
=======================================
--- /branches/dev/selectable/tests/unit/selectable/selectable_defaults.js    
Tue Feb 3 20:53:09 2009
+++ /branches/dev/selectable/tests/unit/selectable/selectable_defaults.js    
Sun Aug 23 02:26:34 2009
@@ -3,14 +3,21 @@
*/
var selectable_defaults = {
-    appendTo: 'body',
-    autoRefresh: true,
-    cancel: ':input,option',
-    delay: 0,
+    multiple: true,
+    closest: true,
+    filter: '> *',
    disabled: false,
-    distance: 0,
-    filter: '*',
-    tolerance: 'touch'
+
+    keyboard: true,
+    lasso: {
+        cancel: ":input,option",
+        delay: 0,
+        distance: 1,
+        tolerance: 'touch',
+        appendTo: 'body'
+    },
+
+    selectedClass: 'ui-state-active'
};
commonWidgetTests('selectable', { defaults: selectable_defaults });
=======================================
--- /branches/dev/selectable/tests/unit/selectable/selectable_methods.js    
Tue Apr 14 19:33:28 2009
+++ /branches/dev/selectable/tests/unit/selectable/selectable_methods.js    
Sun Aug 23 02:26:34 2009
@@ -100,4 +100,76 @@
    equals(actual, expected, 'disable is chainable');
});
+test("selectPrevious", function() {
+
+    el = $("#selectable1");
+    el.selectable();
+
+    el.selectable('select', 1);
+    equals($('li', el).index($('li.ui-state-active', el)[0]), 1, 'selected
element should have the index 1');
+
+    el.selectable('selectPrevious');
+    equals($('li', el).index($('li.ui-state-active', el)[0]), 0, 'selected
element should have the index 0');
+
+});
+
+test("selectNext", function() {
+
+    el = $("#selectable1");
+    el.selectable();
+
+    el.selectable('select', 1);
+    equals($('li', el).index($('li.ui-state-active', el)[0]), 1, 'selected
element should have the index 1');
+
+    el.selectable('selectNext');
+    equals($('li', el).index($('li.ui-state-active', el)[0]), 2, 'selected
element should have the index 2');
+
+});
+
+test("refresh", function() {
+
+    el = $("#selectable1");
+    el.selectable();
+
+    var item = $('<li>new item</li>').appendTo(el);
+
+    el.selectable('refresh');
+    el.selectable('select', 5);
+
+    equals($('li', el).index($('li.ui-state-active', el)[0]), 5, 'selected
element should have the index 5');
+
+});
+
+test("select", function() {
+
+    el = $("#selectable1");
+    el.selectable();
+
+    el.selectable('select', 3);
+    equals($('li', el).index($('li.ui-state-active', el)[0]), 3, 'selected
element should have the index 3');
+
+    el.selectable('select', '.special');
+    equals($('li', el).index($('li.ui-state-active', el)[0]), 2, 'selected
element should have the index 2');
+
+    el.selectable('select', '*');
+    equals($('li.ui-state-active', el).length, 5, 'all 5 elements should be
selected by star selector');
+
+});
+
+test("deselect", function() {
+
+    el = $("#selectable1");
+    el.selectable();
+
+    el.selectable('select', '*');
+    el.selectable('deselect');
+    equals($('li.ui-state-active', el).length, 0, 'all elements should be
deselected');
+
+    el.selectable('select', '*');
+    el.selectable('deselect', 3);
+    equals($('li', el).index($('li:not(.ui-state-active)', el)[0]), 3, 'only
deselected element should have the index 3');
+
+
+});
+
})(jQuery);
=======================================
--- /branches/dev/selectable/tests/unit/selectable/selectable_options.js    
Tue Feb 3 20:53:09 2009
+++ /branches/dev/selectable/tests/unit/selectable/selectable_options.js    
Sun Aug 23 02:26:34 2009
@@ -5,38 +5,13 @@
module("selectable: options");
-test("autoRefresh", function() {
-    expect(3);
-    el = $("#selectable1");
-    var actual, sel = $("*", el), selected = function() { actual += 1 };
-
-    actual = 0;
-    el = $("#selectable1").selectable({ autoRefresh: false,    selected:
selected });
-    sel.hide();
-    drag(1000, 1000);
-    equals(actual, sel.length);
-    el.selectable("destroy");
-
-    actual = 0;
-    sel.show();
-    el = $("#selectable1").selectable({ autoRefresh: true,    selected: selected
});
-    sel.hide();
-    drag(1000, 1000);
-    equals(actual, 0);
-    sel.show();
-    drag(1000, 1000);
-    equals(actual, sel.length);
-    el.selectable("destroy");
-    sel.show();
-});
-
test("filter", function() {
    expect(2);
    el = $("#selectable1");
    var actual, sel = $("*", el), selected = function() { actual += 1 };
    actual = 0;
-    el = $("#selectable1").selectable({ filter: '.special', selected:
selected });
+    el = $("#selectable1").selectable({ filter: '.special', select: selected
});
    drag(1000, 1000);
    ok(sel.length != 1, "this test assumes more than 1 selectee");
    equals(actual, 1);
=======================================
--- /branches/dev/selectable/ui/ui.selectable.js    Sat Aug 22 06:16:29 2009
+++ /branches/dev/selectable/ui/ui.selectable.js    Sun Aug 23 02:26:34 2009
@@ -30,7 +30,15 @@
            this.element.disableSelection();
            //Prepare caret selection
-            if(this.options.lasso) this._mouseInit();
+            if(this.options.lasso) {
+
+                // we need to move the lasso options onto the root options for the
mouse clas
+                if(this.options.lasso !== true) {
+                    $.extend(this.options, this.options.lasso);
+                }
+
+                this._mouseInit();
+            }
            this.element
                .bind('mousedown.selectable', function(event) {
@@ -448,7 +456,7 @@
            var newlySelected = [];
-            if (event.shiftKey && this.options.multiple) {
+            if (event && event.shiftKey && this.options.multiple) {
                if (this.currentFocus.data('selectable-item').selected) {
                    this._removeFromSelection(this.previousFocus, event);
@@ -470,7 +478,7 @@
            } else {
                //If the CTRL or Apple/Win key is pressed, only set focus
-                if (event.metaKey)
+                if (event && event.metaKey)
                    return;
                this._clearSelection(event);
@@ -575,7 +583,13 @@
            // select all found
            var newlySelected = [], self = this;
-            item.each(function() {
+            item.each(function(i) {
+                if(i == 0) { //Setting the focus on the first item in the list
+                    self.previousFocus = self.currentFocus;
+                    self.currentFocus = $(this);
+                    self.previousFocus.removeClass('ui-state-focus');
+                    self.currentFocus.addClass('ui-state-focus');
+                }
                newlySelected.push(self._addToSelection($(this)));
            });
@@ -625,12 +639,6 @@
    $.extend($.ui.selectable, {
        defaults: {
-            //TODO: Figure out how to move these defaults out
-            cancel: ":input,option",
-            delay: 0,
-            distance: 1,
-            appendTo: 'body',
-
            multiple: true,
            closest: true,
            filter: '> *',