Make more use of "Lang.bind" to make this be known in closures

multiprovider
Jens Lody 11 years ago
parent 10e6e7ac56
commit 4b01cf9ff1
  1. 64
      src/extension.js
  2. 93
      src/prefs.js

@ -327,35 +327,37 @@ const WeatherMenuButton = new Lang.Class({
}, },
loadConfig: function() { loadConfig: function() {
let that = this;
this._settings = Convenience.getSettings(WEATHER_SETTINGS_SCHEMA); this._settings = Convenience.getSettings(WEATHER_SETTINGS_SCHEMA);
this._settingsC = this._settings.connect("changed", function() { this._settingsC = this._settings.connect("changed", Lang.bind(this, function() {
that.rebuildFutureWeatherUi(); this.rebuildCurrentWeatherUi();
if (that.locationChanged()) { this.rebuildFutureWeatherUi();
that.currentWeatherCache = undefined; if (this.locationChanged()) {
that.forecastWeatherCache = undefined; this.currentWeatherCache = undefined;
this.forecastWeatherCache = undefined;
} }
that.parseWeatherCurrent(); this.parseWeatherCurrent();
}); }));
}, },
loadConfigInterface: function() { loadConfigInterface: function() {
let that = this;
let schemaInterface = "org.gnome.desktop.interface"; let schemaInterface = "org.gnome.desktop.interface";
if (Gio.Settings.list_schemas().indexOf(schemaInterface) == -1) if (Gio.Settings.list_schemas().indexOf(schemaInterface) == -1)
throw _("Schema \"%s\" not found.").replace("%s", schemaInterface); throw _("Schema \"%s\" not found.").replace("%s", schemaInterface);
this._settingsInterface = new Gio.Settings({ this._settingsInterface = new Gio.Settings({
schema: schemaInterface schema: schemaInterface
}); });
this._settingsInterfaceC = this._settingsInterface.connect("changed", function() { this._settingsInterfaceC = this._settingsInterface.connect("changed", Lang.bind(this, function() {
if (that.locationChanged()) { this.rebuildCurrentWeatherUi();
that.currentWeatherCache = undefined; this.rebuildFutureWeatherUi();
that.forecastWeatherCache = undefined; if (this.locationChanged()) {
that.rebuildCurrentWeatherUi(); this.currentWeatherCache = undefined;
that.rebuildFutureWeatherUi(); this.forecastWeatherCache = undefined;
this.rebuildCurrentWeatherUi();
this.rebuildFutureWeatherUi();
} }
that.parseWeatherCurrent(); this.parseWeatherCurrent();
}); }));
},
_onNetworkStateChanged: function() { _onNetworkStateChanged: function() {
this._checkConnectionState(); this._checkConnectionState();
@ -644,7 +646,6 @@ const WeatherMenuButton = new Lang.Class({
}, },
rebuildSelectCityItem: function() { rebuildSelectCityItem: function() {
let that = this;
this._selectCity.menu.removeAll(); this._selectCity.menu.removeAll();
let item = null; let item = null;
@ -666,9 +667,9 @@ const WeatherMenuButton = new Lang.Class({
} }
this._selectCity.menu.addMenuItem(item); this._selectCity.menu.addMenuItem(item);
item.connect('activate', function(actor, event) { item.connect('activate', Lang.bind(this, function(actor, event) {
that._actual_city = actor.location; this._actual_city = actor.location;
}); }));
} }
if (cities.length == 1) if (cities.length == 1)
@ -706,7 +707,6 @@ const WeatherMenuButton = new Lang.Class({
}, },
updateCities: function() { updateCities: function() {
let that = this;
let cities = this._cities; let cities = this._cities;
cities = cities.split(" && "); cities = cities.split(" && ");
@ -731,7 +731,7 @@ const WeatherMenuButton = new Lang.Class({
if (this._appid) if (this._appid)
params['APPID'] = this._appid; params['APPID'] = this._appid;
this.load_json_async(WEATHER_URL_CURRENT, params, function() { this.load_json_async(WEATHER_URL_CURRENT, params, Lang.bind(this, function() {
let city = arguments[0]; let city = arguments[0];
if (Number(city.cod) != 200) if (Number(city.cod) != 200)
@ -747,9 +747,9 @@ const WeatherMenuButton = new Lang.Class({
cities = cities.join(" && "); cities = cities.join(" && ");
if (typeof cities != "string") if (typeof cities != "string")
cities = cities[0]; cities = cities[0];
that._cities = cities; this._cities = cities;
that.updateCities(); this.updateCities();
}); }));
return; return;
} else } else
continue; continue;
@ -1144,24 +1144,22 @@ weather-storm.png = weather-storm-symbolic.svg
_httpSession = new Soup.Session(); _httpSession = new Soup.Session();
} }
let here = this;
let message = Soup.form_request_new_from_hash('GET', url, params); let message = Soup.form_request_new_from_hash('GET', url, params);
_httpSession.queue_message(message, function(_httpSession, message) { _httpSession.queue_message(message, Lang.bind(this, function(_httpSession, message) {
if (!message.response_body.data) { if (!message.response_body.data) {
fun.call(here, 0); fun.call(this, 0);
return; return;
} }
try { try {
let jp = JSON.parse(message.response_body.data); let jp = JSON.parse(message.response_body.data);
fun.call(here, jp); fun.call(this, jp);
} catch (e) { } catch (e) {
fun.call(here, 0); fun.call(this, 0);
return; return;
} }
}); }));
return; return;
}, },

@ -88,7 +88,6 @@ const WeatherPrefsWidget = new GObject.Class({
Window: new Gtk.Builder(), Window: new Gtk.Builder(),
initWindow: function() { initWindow: function() {
let that = this;
mCities = null; mCities = null;
this.Window.add_from_file(EXTENSIONDIR + "/weather-settings.ui"); this.Window.add_from_file(EXTENSIONDIR + "/weather-settings.ui");
@ -98,17 +97,17 @@ const WeatherPrefsWidget = new GObject.Class({
this.liststore = this.Window.get_object("liststore"); this.liststore = this.Window.get_object("liststore");
this.Iter = this.liststore.get_iter_first(); this.Iter = this.liststore.get_iter_first();
this.Window.get_object("tree-toolbutton-add").connect("clicked", function() { this.Window.get_object("tree-toolbutton-add").connect("clicked", Lang.bind(this, function() {
that.addCity(); this.addCity();
}); }));
this.Window.get_object("tree-toolbutton-remove").connect("clicked", function() { this.Window.get_object("tree-toolbutton-remove").connect("clicked", Lang.bind(this, function() {
that.removeCity(); this.removeCity();
}); }));
this.Window.get_object("treeview-selection").connect("changed", function(selection) { this.Window.get_object("treeview-selection").connect("changed", Lang.bind(this, function(selection) {
that.selectionChanged(selection); this.selectionChanged(selection);
}); }));
this.treeview.set_model(this.liststore); this.treeview.set_model(this.liststore);
@ -234,7 +233,6 @@ const WeatherPrefsWidget = new GObject.Class({
}, },
addComboBox: function(a, b) { addComboBox: function(a, b) {
let that = this;
let cf = new Gtk.ComboBoxText(); let cf = new Gtk.ComboBoxText();
this.configWidgets.push([cf, b]); this.configWidgets.push([cf, b]);
cf.visible = 1; cf.visible = 1;
@ -243,29 +241,27 @@ const WeatherPrefsWidget = new GObject.Class({
for (let i in a) for (let i in a)
cf.append_text(a[i]); cf.append_text(a[i]);
cf.active = this[b]; cf.active = this[b];
cf.connect("changed", function() { cf.connect("changed", Lang.bind(this, function() {
that[b] = arguments[0].active; this[b] = arguments[0].active;
}); }));
this.right_widget.attach(cf, this.x[0], this.x[1], this.y[0], this.y[1], 0, 0, 0, 0); this.right_widget.attach(cf, this.x[0], this.x[1], this.y[0], this.y[1], 0, 0, 0, 0);
this.inc(); this.inc();
}, },
addSwitch: function(a) { addSwitch: function(a) {
let that = this;
let sw = new Gtk.Switch(); let sw = new Gtk.Switch();
this.configWidgets.push([sw, a]); this.configWidgets.push([sw, a]);
sw.visible = 1; sw.visible = 1;
sw.can_focus = 0; sw.can_focus = 0;
sw.active = this[a]; sw.active = this[a];
sw.connect("notify::active", function() { sw.connect("notify::active", Lang.bind(this, function() {
that[a] = arguments[0].active; this[a] = arguments[0].active;
}); }));
this.right_widget.attach(sw, this.x[0], this.x[1], this.y[0], this.y[1], 0, 0, 0, 0); this.right_widget.attach(sw, this.x[0], this.x[1], this.y[0], this.y[1], 0, 0, 0, 0);
this.inc(); this.inc();
}, },
addAppidEntry: function(a) { addAppidEntry: function(a) {
let that = this;
let en = new Gtk.Entry(); let en = new Gtk.Entry();
this.configWidgets.push([en, a]); this.configWidgets.push([en, a]);
en.visible = 1; en.visible = 1;
@ -275,15 +271,15 @@ const WeatherPrefsWidget = new GObject.Class({
if (this[a].length != 32) if (this[a].length != 32)
en.set_icon_from_icon_name(Gtk.PositionType.LEFT, 'dialog-warning'); en.set_icon_from_icon_name(Gtk.PositionType.LEFT, 'dialog-warning');
en.connect("notify::text", function() { en.connect("notify::text", Lang.bind(this, function() {
let key = arguments[0].text; let key = arguments[0].text;
let rgba = new Gdk.Color(); let rgba = new Gdk.Color();
that[a] = key; this[a] = key;
if (key.length == 32) if (key.length == 32)
en.set_icon_from_icon_name(Gtk.PositionType.LEFT, ''); en.set_icon_from_icon_name(Gtk.PositionType.LEFT, '');
else else
en.set_icon_from_icon_name(Gtk.PositionType.LEFT, 'dialog-warning'); en.set_icon_from_icon_name(Gtk.PositionType.LEFT, 'dialog-warning');
}); }));
this.right_widget.attach(en, this.x[0], this.x[1], this.y[0], this.y[1], 0, 0, 0, 0); this.right_widget.attach(en, this.x[0], this.x[1], this.y[0], this.y[1], 0, 0, 0, 0);
this.inc(); this.inc();
}, },
@ -297,7 +293,6 @@ const WeatherPrefsWidget = new GObject.Class({
}, },
addCity: function() { addCity: function() {
let that = this;
let textDialog = _("Name of the city"); let textDialog = _("Name of the city");
let dialog = new Gtk.Dialog({ let dialog = new Gtk.Dialog({
@ -339,7 +334,7 @@ const WeatherPrefsWidget = new GObject.Class({
dialog.set_default(d); dialog.set_default(d);
entry.activates_default = true; entry.activates_default = true;
let testLocation = function(location) { let testLocation = Lang.bind(this, function(location) {
if (location.search(/\[/) == -1 || location.search(/\]/) == -1) if (location.search(/\[/) == -1 || location.search(/\]/) == -1)
return 0; return 0;
@ -347,7 +342,7 @@ const WeatherPrefsWidget = new GObject.Class({
if (!id) if (!id)
return 0; return 0;
that.loadJsonAsync(WEATHER_URL_CURRENT, { this.loadJsonAsync(WEATHER_URL_CURRENT, {
id: id id: id
}, function() { }, function() {
d.sensitive = 0; d.sensitive = 0;
@ -366,9 +361,9 @@ const WeatherPrefsWidget = new GObject.Class({
return 0; return 0;
}, "testLocation"); }, "testLocation");
return 0; return 0;
}; });
let searchLocation = function() { let searchLocation = Lang.bind(this, function() {
let location = entry.get_text(); let location = entry.get_text();
let params = { let params = {
cnt: '30', cnt: '30',
@ -380,7 +375,7 @@ const WeatherPrefsWidget = new GObject.Class({
if (this._appid) if (this._appid)
params['APPID'] = this._appid; params['APPID'] = this._appid;
if (testLocation(location) == 0) if (testLocation(location) == 0)
that.loadJsonAsync(WEATHER_URL_FIND, params, function() { this.loadJsonAsync(WEATHER_URL_FIND, params, function() {
if (!arguments[0]) if (!arguments[0])
return 0; return 0;
let city = arguments[0]; let city = arguments[0];
@ -423,14 +418,14 @@ const WeatherPrefsWidget = new GObject.Class({
return 0; return 0;
}, "getInfo"); }, "getInfo");
return 0; return 0;
}; });
entry.connect("changed", searchLocation); entry.connect("changed", searchLocation);
let dialog_area = dialog.get_content_area(); let dialog_area = dialog.get_content_area();
dialog_area.pack_start(label, 0, 0, 0); dialog_area.pack_start(label, 0, 0, 0);
dialog_area.pack_start(entry, 0, 0, 0); dialog_area.pack_start(entry, 0, 0, 0);
dialog.connect("response", function(w, response_id) { dialog.connect("response", Lang.bind(this, function(w, response_id) {
if (response_id) { if (response_id) {
if (entry.get_text().search(/\[/) == -1 || entry.get_text().search(/\]/) == -1) if (entry.get_text().search(/\[/) == -1 || entry.get_text().search(/\]/) == -1)
return 0; return 0;
@ -445,7 +440,7 @@ const WeatherPrefsWidget = new GObject.Class({
}; };
if (this._appid) if (this._appid)
params['APPID'] = this._appid; params['APPID'] = this._appid;
that.loadJsonAsync(WEATHER_URL_CURRENT, params, function() { this.loadJsonAsync(WEATHER_URL_CURRENT, params, Lang.bind(this, function() {
if (!arguments[0]) if (!arguments[0])
return 0; return 0;
let city = arguments[0]; let city = arguments[0];
@ -465,23 +460,22 @@ const WeatherPrefsWidget = new GObject.Class({
if (city.sys) if (city.sys)
cityText += " (" + city.sys.country + ")"; cityText += " (" + city.sys.country + ")";
if (that.city) if (this.city)
that.city = that.city + " && " + city.id + ">" + cityText; this.city = this.city + " && " + city.id + ">" + cityText;
else else
that.city = city.id + ">" + cityText; this.city = city.id + ">" + cityText;
return 0; return 0;
}, "lastTest"); }), "lastTest");
} }
dialog.hide(); dialog.hide();
return 0; return 0;
}); }));
dialog.show_all(); dialog.show_all();
}, },
removeCity: function() { removeCity: function() {
let that = this;
let city = this.city.split(" && "); let city = this.city.split(" && ");
if (!city.length) if (!city.length)
return 0; return 0;
@ -508,7 +502,7 @@ const WeatherPrefsWidget = new GObject.Class({
let dialog_area = dialog.get_content_area(); let dialog_area = dialog.get_content_area();
dialog_area.pack_start(label, 0, 0, 0); dialog_area.pack_start(label, 0, 0, 0);
dialog.connect("response", function(w, response_id) { dialog.connect("response", Lang.bind(this, function(w, response_id) {
if (response_id) { if (response_id) {
if (city.length == 0) if (city.length == 0)
city = []; city = [];
@ -520,15 +514,15 @@ const WeatherPrefsWidget = new GObject.Class({
city.splice(ac, 1); city.splice(ac, 1);
if (city.length > 1) if (city.length > 1)
that.city = city.join(" && "); this.city = city.join(" && ");
else if (city[0]) else if (city[0])
that.city = city[0]; this.city = city[0];
else else
that.city = ""; this.city = "";
} }
dialog.hide(); dialog.hide();
return 0; return 0;
}); }));
dialog.show_all(); dialog.show_all();
return 0; return 0;
@ -584,11 +578,10 @@ const WeatherPrefsWidget = new GObject.Class({
}, },
loadConfig: function() { loadConfig: function() {
let that = this;
this.Settings = Convenience.getSettings(WEATHER_SETTINGS_SCHEMA); this.Settings = Convenience.getSettings(WEATHER_SETTINGS_SCHEMA);
this.Settings.connect("changed", function() { this.Settings.connect("changed", Lang.bind(this, function() {
that.refreshUI(); this.refreshUI();
}); }));
}, },
get units() { get units() {
@ -760,26 +753,26 @@ const WeatherPrefsWidget = new GObject.Class({
if (!this.Settings) if (!this.Settings)
this.loadConfig(); this.loadConfig();
let v = this.Settings.get_int(WEATHER_REFRESH_INTERVAL_CURRENT); let v = this.Settings.get_int(WEATHER_REFRESH_INTERVAL_CURRENT);
return ((v >= 600) ? v : 600); return ((v >= 60) ? v : 60);
}, },
set refresh_interval_current(v) { set refresh_interval_current(v) {
if (!this.Settings) if (!this.Settings)
this.loadConfig(); this.loadConfig();
this.Settings.set_int(WEATHER_REFRESH_INTERVAL_CURRENT, ((v >= 600) ? v : 600)); this.Settings.set_int(WEATHER_REFRESH_INTERVAL_CURRENT, ((v >= 60) ? v : 60));
}, },
get refresh_interval_forecast() { get refresh_interval_forecast() {
if (!this.Settings) if (!this.Settings)
this.loadConfig(); this.loadConfig();
let v = this.Settings.get_int(WEATHER_REFRESH_INTERVAL_FORECAST); let v = this.Settings.get_int(WEATHER_REFRESH_INTERVAL_FORECAST);
return ((v >= 600) ? v : 600); return ((v >= 60) ? v : 60);
}, },
set refresh_interval_forecast(v) { set refresh_interval_forecast(v) {
if (!this.Settings) if (!this.Settings)
this.loadConfig(); this.loadConfig();
this.Settings.set_int(WEATHER_REFRESH_INTERVAL_FORECAST, ((v >= 600) ? v : 600)); this.Settings.set_int(WEATHER_REFRESH_INTERVAL_FORECAST, ((v >= 60) ? v : 60));
}, },
get center_forecast() { get center_forecast() {

Loading…
Cancel
Save