[gnome-maps/wip/modernize] Use modern JS features



commit dc7b140fa31cc8b2438859ceb8201e406528388c
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Wed Jan 22 07:53:32 2014 +0100

    Use modern JS features
    
    Since we run in a modern JS environment we are allowed to take advantage
    of newer JS stuff like for..of-loops (loops over iterables) and
    arrow-functions (anonymous functions that has lexical binding of
    'this').
    
    This patch makes us use the features mentioned above.

 src/contextMenu.js  |   12 ++++++------
 src/geoclue.js      |    4 ++--
 src/mainWindow.js   |   18 ++++++++----------
 src/mapLocation.js  |    8 ++++----
 src/mapView.js      |   16 ++++++++--------
 src/placeStore.js   |   23 +++++++++++------------
 src/settings.js     |    4 ++--
 src/sidebar.js      |    4 ++--
 src/userLocation.js |    4 ++--
 src/utils.js        |   15 ++++++++-------
 10 files changed, 53 insertions(+), 55 deletions(-)
---
diff --git a/src/contextMenu.js b/src/contextMenu.js
index 095e438..220ecb2 100644
--- a/src/contextMenu.js
+++ b/src/contextMenu.js
@@ -65,9 +65,9 @@ const ContextMenu = new Lang.Class({
                                               longitude: this._longitude,
                                               accuracy: 0 });
 
-        this._reverseGeocode(location, (function(place) {
+        this._reverseGeocode(location, (place) => {
             this._mapView.showLocation(place.location);
-        }).bind(this));
+        });
     },
 
     _onIAmHereActivated: function() {
@@ -75,17 +75,17 @@ const ContextMenu = new Lang.Class({
                                               longitude: this._longitude,
                                               accuracy: 0,
                                               description: "" });
-        this._reverseGeocode(location, (function(place) {
+        this._reverseGeocode(location, (place) => {
             location.description = place.name;
             this._mapView.geoclue.overrideLocation(location);
-        }).bind(this));
+        });
     },
 
     _reverseGeocode: function(location, resultCallback) {
         let reverse = Geocode.Reverse.new_for_location(location);
 
         Application.application.mark_busy();
-        reverse.resolve_async (null, (function(reverse, res) {
+        reverse.resolve_async (null, (reverse, res) => {
             Application.application.unmark_busy();
             try {
                 let place = reverse.resolve_finish(res);
@@ -97,7 +97,7 @@ const ContextMenu = new Lang.Class({
                      this._longitude + ": " +
                      e.message);
             }
-        }).bind(this));
+        });
     }
 });
 Utils.addSignalMethods(ContextMenu.prototype);
diff --git a/src/geoclue.js b/src/geoclue.js
index ed31ddf..db6ffbb 100644
--- a/src/geoclue.js
+++ b/src/geoclue.js
@@ -83,7 +83,7 @@ const Geoclue = new Lang.Class({
         if (this._locationUpdatedId > 0) {
             this._clientProxy.disconnectSignal(this._locationUpdatedId);
             this._locationUpdatedId = 0;
-            this._clientProxy.StopRemote(function(result, e) {
+            this._clientProxy.StopRemote((result, e) => {
                 if (e) {
                     log ("Failed to connect to GeoClue2 service: " + e.message);
                 }
@@ -98,7 +98,7 @@ const Geoclue = new Lang.Class({
             this._clientProxy.connectSignal("LocationUpdated",
                                             this._onLocationUpdated.bind(this));
 
-        this._clientProxy.StartRemote(function(result, e) {
+        this._clientProxy.StartRemote((result, e) => {
             if (e) {
                 log ("Failed to connect to GeoClue2 service: " + e.message);
             }
diff --git a/src/mainWindow.js b/src/mainWindow.js
index c936107..2493131 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -115,10 +115,10 @@ const MainWindow = new Lang.Class({
                                   this._searchPopup.hide.bind(this._searchPopup));
 
         this._searchCompletion.set_model(this._placeStore);
-        this._searchCompletion.connect('match-selected', (function(c, m, iter) {
+        this._searchCompletion.connect('match-selected', (c, m, iter) => {
             let place = m.get_value(iter, PlaceStore.Columns.PLACE);
             this.mapView.showNGotoLocation(place.location);
-        }).bind(this));
+        });
     },
 
     _initActions: function() {
@@ -202,11 +202,11 @@ const MainWindow = new Lang.Class({
             this._configureId = 0;
         }
 
-        this._configureId = Mainloop.timeout_add(_CONFIGURE_ID_TIMEOUT, (function() {
+        this._configureId = Mainloop.timeout_add(_CONFIGURE_ID_TIMEOUT, () => {
             this._saveWindowGeometry();
             this._configureId = 0;
             return false;
-        }).bind(this));
+        });
     },
 
     _onWindowStateEvent: function(widget, event) {
@@ -284,7 +284,7 @@ const MainWindow = new Lang.Class({
         // Lower case to match case insensitive
         let searchStringLower = this._searchEntry.text.toLowerCase();
 
-        places.forEach((function(place) {
+        for (let place of places) {
             let iter = model.append();
             let location = place.get_location();
             let icon = place.icon;
@@ -306,7 +306,7 @@ const MainWindow = new Lang.Class({
                     model.set(iter, [SearchResults.COL_ICON], [pixbuf]);
                 });
             }
-        }).bind(this));
+        }
         this._searchPopup.showResult();
     },
 
@@ -325,11 +325,9 @@ const MainWindow = new Lang.Class({
 
     _onGotoUserLocationActivate: function() {
         if (this.mapView.geoclue.userSetLocation) {
-            Utils.once(this.mapView.geoclue,
-                       'location-changed',
-                       (function() {
+            Utils.once(this.mapView.geoclue, 'location-changed', () => {
                 this.mapView.gotoUserLocation(true);
-            }).bind(this));
+            });
             this.mapView.geoclue.findLocation();
         } else
             this.mapView.gotoUserLocation(true);
diff --git a/src/mapLocation.js b/src/mapLocation.js
index 631f05d..1bafb74 100644
--- a/src/mapLocation.js
+++ b/src/mapLocation.js
@@ -68,16 +68,16 @@ const MapLocation = new Lang.Class({
          */
 
         this._view.goto_animation_mode = Clutter.AnimationMode.EASE_IN_CUBIC;
-        Utils.once(this._view, "animation-completed", (function() {
-            Utils.once(this._view, "animation-completed::go-to", (function() {
+        Utils.once(this._view, "animation-completed", () => {
+            Utils.once(this._view, "animation-completed::go-to", () => {
                 this.zoomToFit();
                 this._view.goto_animation_mode = Clutter.AnimationMode.EASE_IN_OUT_CUBIC;
                 this.emit('gone-to');
-            }).bind(this));
+            });
 
             this._view.goto_animation_mode = Clutter.AnimationMode.EASE_OUT_CUBIC;
             this._view.go_to(this.latitude, this.longitude);
-        }).bind(this));
+        });
 
         this._mapView.ensureVisible([this._getCurrentLocation(), this]);
     },
diff --git a/src/mapView.js b/src/mapView.js
index aae8d7b..75d3010 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -82,11 +82,11 @@ const MapView = new Lang.Class({
         this.view.add_layer(this._userLocationLayer);
 
         // switching map type will set view min-zoom-level from map source
-        this.view.connect('notify::min-zoom-level', (function() {
+        this.view.connect('notify::min-zoom-level', () => {
             if (this.view.min_zoom_level != MapMinZoom) {
                 this.view.min_zoom_level = MapMinZoom;
             }
-        }).bind(this));
+        });
 
         this._factory = Champlain.MapSourceFactory.dup_default();
         this.setMapType(MapType.STREET);
@@ -119,14 +119,14 @@ const MapView = new Lang.Class({
         });
         forward.bounded = false;
         forward.set_answer_count(answerCount);
-        forward.search_async (null, (function(forward, res) {
+        forward.search_async (null, (forward, res) => {
             try {
                 places = forward.search_finish(res);
             } catch (e) {
                 places = null;
             }
             searchCompleteCallback(places);
-        }).bind(this));
+        });
     },
 
     ensureVisible: function(locations) {
@@ -135,20 +135,20 @@ const MapView = new Lang.Class({
                                                bottom:  90,
                                                top:    -90 });
 
-        locations.forEach(function(location) {
+        for (let location of locations) {
             bbox.left   = Math.min(bbox.left,   location.longitude);
             bbox.right  = Math.max(bbox.right,  location.longitude);
             bbox.bottom = Math.min(bbox.bottom, location.latitude);
             bbox.top    = Math.max(bbox.top,    location.latitude);
-        });
+        }
         this.view.ensure_visible(bbox, true);
     },
 
     gotoUserLocation: function(animate) {
         this.emit('going-to-user-location');
-        this._userLocation.once("gone-to", (function() {
+        this._userLocation.once("gone-to", () => {
             this.emit('gone-to-user-location');
-        }).bind(this));
+        });
         this._userLocation.goTo(animate);
     },
 
diff --git a/src/placeStore.js b/src/placeStore.js
index 1377d6b..c3b2331 100644
--- a/src/placeStore.js
+++ b/src/placeStore.js
@@ -73,10 +73,10 @@ const PlaceStore = new Lang.Class({
             return;
 
         if (this._exists(place, PlaceType.RECENT)) {
-            this._removeIf((function(model, iter) {
+            this._removeIf((model, iter) => {
                 let p = model.get_value(iter, Columns.PLACE);
                 return p.name === place.name;
-            }), true);
+            }, true);
         }
         this._addPlace(place, PlaceType.FAVORITE, new Date().getTime());
     },
@@ -88,10 +88,10 @@ const PlaceStore = new Lang.Class({
         if (this._numRecent === this.recentLimit) {
             // Since all we do is append, the oldest recent will be
             // the first one we encounter.
-            this._removeIf((function(model, iter) {
+            this._removeIf((model, iter) => {
                 let type = model.get_value(iter, Columns.TYPE);
                 return type === PlaceType.RECENT;
-            }), true);
+            }, true);
         }
         this._addPlace(place, PlaceType.RECENT, new Date().getTime());
         this._numRecent++;
@@ -106,8 +106,7 @@ const PlaceStore = new Lang.Class({
             return;
 
         try {
-            let jsonArray = JSON.parse(buffer);
-            jsonArray.forEach((function(obj) {
+            for (let obj of JSON.parse(buffer)) {
                 let location = new Geocode.Location({
                     latitude:    obj.latitude,
                     longitude:   obj.longitude,
@@ -122,7 +121,7 @@ const PlaceStore = new Lang.Class({
                 this._addPlace(place, obj.type, obj.added);
                 if (obj.type === PlaceType.RECENT)
                     this._numRecent++;
-            }).bind(this));
+            }
         } catch (e) {
             throw new Error('failed to parse places file');
         }
@@ -133,7 +132,7 @@ const PlaceStore = new Lang.Class({
             return;
 
         let jsonArray = [];
-        this.foreach(function(model, path, iter) {
+        this.foreach((model, path, iter) => {
             let place    = model.get_value(iter, Columns.PLACE),
                 location = place.location,
                 type     = model.get_value(iter, Columns.TYPE),
@@ -172,9 +171,9 @@ const PlaceStore = new Lang.Class({
                   added]);
 
         if (place.icon !== null) {
-            Utils.load_icon(place.icon, _ICON_SIZE, (function(pixbuf) {
+            Utils.load_icon(place.icon, _ICON_SIZE, (pixbuf) => {
                 this.set(iter, [Columns.ICON], [pixbuf]);
-            }).bind(this));
+            });
         }
         this._typeTable[place.name] = type;
         this._dirty = true;
@@ -191,13 +190,13 @@ const PlaceStore = new Lang.Class({
     },
 
     _removeIf: function(evalFunc, stop) {
-        this.foreach((function(model, path, iter) {
+        this.foreach((model, path, iter) => {
             if (evalFunc(model, iter)) {
                 this.remove(iter);
                 if (stop)
                     return true;
             }
             return false;
-        }).bind(this));
+        });
     }
 });
diff --git a/src/settings.js b/src/settings.js
index c09ffe4..7ca8454 100644
--- a/src/settings.js
+++ b/src/settings.js
@@ -34,11 +34,11 @@ const Settings = new Lang.Class({
 
     _init: function(schema) {
         this.parent({ schema: schema });
-        this.list_keys().forEach((function(key) {
+        for (let key of this.list_keys()) {
             this._keyTypes[key] = this.get_value(key)
                                       .get_type()
                                       .dup_string();
-        }).bind(this));
+        }
     },
 
     get: function(name) {
diff --git a/src/sidebar.js b/src/sidebar.js
index b9d873e..28ae9c9 100644
--- a/src/sidebar.js
+++ b/src/sidebar.js
@@ -75,7 +75,7 @@ const Sidebar = new Lang.Class({
                                          transition_type: Gtk.RevealerTransitionType.CROSSFADE });
         revealer.show_all();
 
-        revealButton.connect('clicked', (function() {
+        revealButton.connect('clicked', () => {
             if (revealer.reveal_child) {
                 revealer.reveal_child = false;
                 revealButton.symbolic_icon_name = prevIconName;
@@ -83,7 +83,7 @@ const Sidebar = new Lang.Class({
                 revealer.reveal_child = true;
                 revealButton.symbolic_icon_name = nextIconName;
             }
-        }).bind(this));
+        });
 
         // now create actors
         let buttonActor = new GtkClutter.Actor({ contents: revealButton,
diff --git a/src/userLocation.js b/src/userLocation.js
index 3eddee2..585c26b 100644
--- a/src/userLocation.js
+++ b/src/userLocation.js
@@ -41,12 +41,12 @@ const UserLocation = new Lang.Class({
 
         this._locationMarker = new Champlain.CustomMarker();
         this._locationMarker.set_location(this.latitude, this.longitude);
-        this._locationMarker.connect('notify::size', (function() {
+        this._locationMarker.connect('notify::size', () => {
             let translate_x = -Math.floor(this._locationMarker.get_width() / 2);
             this._locationMarker.set_translation(translate_x,
                                                  -this._locationMarker.get_height(),
                                                  0);
-        }).bind(this));
+        });
         let pin_actor = Utils.CreateActorFromImageFile(Path.ICONS_DIR + "/pin.svg");
         if (!pin_actor)
             return;
diff --git a/src/utils.js b/src/utils.js
index 631ca12..5724c42 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -55,7 +55,7 @@ function debug(str) {
 
 // Connect to a signal on an object and disconnect on its first emission.
 function once(obj, signal, callback) {
-    let id = obj.connect(signal, function() {
+    let id = obj.connect(signal, () => {
         obj.disconnect(id);
         callback();
     });
@@ -83,7 +83,7 @@ function clearGtkClutterActorBg(actor) {
 }
 
 function initActions(actionMap, simpleActionEntries, context) {
-    simpleActionEntries.forEach(function(entry) {
+    for (let entry of simpleActionEntries) {
         let action = new Gio.SimpleAction(entry.properties);
 
         for(let signalHandler in entry.signalHandlers) {
@@ -92,7 +92,7 @@ function initActions(actionMap, simpleActionEntries, context) {
         }
 
         actionMap.add_action(action);
-    });
+    }
 }
 
 
@@ -128,9 +128,10 @@ function getUIObject(res, ids) {
     let builder = new Gtk.Builder();
     builder.add_from_resource('/org/gnome/maps/' + res + '.ui');
     let ret = {};
-    ids.forEach(function(id) {
+    
+    for (let id of ids) {
         ret[dashedToCamelCase(id)] = builder.get_object(id);
-    });
+    }
     return ret;
 }
 
@@ -180,7 +181,7 @@ function _load_file_icon(icon, loadCompleteCallback) {
         return;
     }
 
-    icon.load_async(-1, null, function(icon, res) {
+    icon.load_async(-1, null, (icon, res) => {
         try {
             let stream = icon.load_finish(res, null)[0];
 
@@ -199,7 +200,7 @@ function _load_http_icon(icon, loadCompleteCallback) {
     let msg = Soup.form_request_new_from_hash('GET', icon.file.get_uri(), {});
     let soup_session = _get_soup_session();
 
-    soup_session.queue_message(msg, function(session, msg) {
+    soup_session.queue_message(msg, (session, msg) => {
         if (msg.status_code != Soup.KnownStatusCode.OK) {
             log("Failed to load pixbuf: " + msg.reason_phrase);
             return;


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