[the-board] [ui] Unify selection handling around 'selected' property changes



commit b30f6eb0e7c3193221c991800052fa49576e50b8
Author: Lucas Rocha <lucasr gnome org>
Date:   Sun Jan 16 22:21:47 2011 +0000

    [ui] Unify selection handling around 'selected' property changes

 src/js/ui/page.js  |   55 +++++++++++++++++++++++++++++++++++----------------
 src/js/ui/thing.js |    2 +
 2 files changed, 40 insertions(+), 17 deletions(-)
---
diff --git a/src/js/ui/page.js b/src/js/ui/page.js
index 3e98f59..d3300c8 100644
--- a/src/js/ui/page.js
+++ b/src/js/ui/page.js
@@ -292,6 +292,10 @@ Page.prototype = {
             thing.connect("save",
                           Lang.bind(this, this._onThingSave));
 
+        thing._Page_selectedChangedId =
+            thing.connect("selected-changed",
+                          Lang.bind(this, this._onThingSelectedChanged));
+
         this._mainBox.append(thing.actor,
                              Tb.BoxPackFlags.FIXED);
 
@@ -379,23 +383,15 @@ Page.prototype = {
         return union;
     },
 
-    _setSelectedThings : function(selectedThings) {
-        this._selectedThings = selectedThings;
-
-        if (this._selectedThings.length == 0) {
-            delete this._dragArea;
-        }
-
-        let updateSelection = function(thing) {
-            thing.selected = this._selectedThings.indexOf(thing) >= 0;
+    _updateDragArea : function() {
+        delete this._dragArea;
 
-            if (thing.selected) {
-                this._dragArea =
-                    this._areaUnion(this._dragArea, thing.area);
-            }
+        let updateArea = function(thing) {
+            this._dragArea = this._areaUnion(this._dragArea,
+                                             thing.area);
         };
 
-        this._things.forEach(Lang.bind(this, updateSelection));
+        this._selectedThings.forEach(Lang.bind(this, updateArea));
     },
 
     _updateSelectedThingsFromBox : function() {
@@ -412,7 +408,11 @@ Page.prototype = {
             selectedThings = [];
         }
 
-        this._setSelectedThings(selectedThings);
+        let updateSelection = function(thing) {
+            thing.selected = selectedThings.indexOf(thing) >= 0;
+        };
+
+        this._things.forEach(updateSelection);
     },
 
     _ensureDragArea : function(draggedThing) {
@@ -544,6 +544,18 @@ Page.prototype = {
         this._model.updateThing(index, thing.onGetState());
     },
 
+    _onThingSelectedChanged : function(thing) {
+        let index = this._selectedThings.indexOf(thing);
+
+        if (thing.selected && index < 0) {
+            this._selectedThings.push(thing);
+        } else if (!thing.selected && index >= 0) {
+            this._selectedThings.splice(index, 1);
+        }
+
+        this._updateDragArea();
+    },
+
     setActiveThing : function(activeThing) {
         if (this._activeThing === activeThing) {
             return;
@@ -609,6 +621,7 @@ Page.prototype = {
         thing.disconnect(thing._Page_dragEndId);
         thing.disconnect(thing._Page_removeId);
         thing.disconnect(thing._Page_saveId);
+        thing.disconnect(thing._Page_selectedChangedId);
 
         if (this._activeThing == thing) {
             this.setActiveThing(null);
@@ -657,11 +670,19 @@ Page.prototype = {
     },
 
     selectAllThings : function() {
-        this._setSelectedThings(this._things);
+        let selectThing = function(thing) {
+            thing.selected = true;
+        };
+
+        this._things.forEach(selectThing);
     },
 
     unselectAllThings : function() {
-        this._setSelectedThings([]);
+        let unselectThing = function(thing) {
+            thing.selected = false;
+        };
+
+        this._things.forEach(unselectThing);
     },
 
     destroy : function() {
diff --git a/src/js/ui/thing.js b/src/js/ui/thing.js
index ec32fb5..a918460 100644
--- a/src/js/ui/thing.js
+++ b/src/js/ui/thing.js
@@ -474,6 +474,8 @@ Thing.prototype = {
 
         this._updateSelection();
         this._updateControls();
+
+        this.emit("selected-changed");
     },
 
     get area() {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]