[polari/wip/fmuellner/es6-classes: 3/4] Port non-GObject classes to JS6 classes
- From: Gitlab Administrative User <gitlab src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [polari/wip/fmuellner/es6-classes: 3/4] Port non-GObject classes to JS6 classes
- Date: Fri, 27 Oct 2017 02:27:08 +0000 (UTC)
commit ce578754a6a0874138110b23f42fba80b36860bc
Author: Florian Müllner <fmuellner gnome org>
Date: Thu Oct 26 20:15:36 2017 +0200
Port non-GObject classes to JS6 classes
ES6 finally adds standard class syntax to the language, so we can
replace our custom Lang.Class framework with the new syntax. Any
classes that inherit from GObject will need some special treatment,
so limit the port to regular javascript classes for now.
Fixes https://gitlab.gnome.org/GNOME/polari/issues/14
src/accountsMonitor.js | 32 +++++++++++++++-----------------
src/ircParser.js | 21 +++++++++------------
src/networksManager.js | 31 ++++++++++++++-----------------
src/pasteManager.js | 13 ++++---------
src/roomManager.js | 42 ++++++++++++++++++++----------------------
src/serverRoomManager.js | 20 +++++++++-----------
src/tabCompletion.js | 36 +++++++++++++++++-------------------
src/telepathyClient.js | 16 +++++++---------
src/userTracker.js | 14 ++++++--------
9 files changed, 101 insertions(+), 124 deletions(-)
---
diff --git a/src/accountsMonitor.js b/src/accountsMonitor.js
index 2fde77f..a2fb92d 100644
--- a/src/accountsMonitor.js
+++ b/src/accountsMonitor.js
@@ -12,10 +12,8 @@ function getDefault() {
return _singleton;
}
-var AccountsMonitor = new Lang.Class({
- Name: 'AccountsMonitor',
-
- _init() {
+class AccountsMonitor {
+ constructor() {
this._accounts = new Map();
this._accountSettings = new Map();
@@ -33,23 +31,23 @@ var AccountsMonitor = new Lang.Class({
this._preparedCallbacks = [];
this._accountManager.prepare_async(null,
Lang.bind(this, this._onPrepared));
- },
+ }
get accounts() {
return [...this._accounts.values()];
- },
+ }
get enabledAccounts() {
return [...this._accounts.values()].filter(a => a.enabled);
- },
+ }
get accountManager() {
return this._accountManager;
- },
+ }
lookupAccount(accountPath) {
return this._accounts.get(accountPath);
- },
+ }
getAccountSettings(account) {
let accountPath = account.object_path;
@@ -62,7 +60,7 @@ var AccountsMonitor = new Lang.Class({
path: path });
this._accountSettings.set(accountPath, settings);
return settings;
- },
+ }
prepare(callback) {
let quark = Tp.AccountManager.get_feature_quark_core();
@@ -70,7 +68,7 @@ var AccountsMonitor = new Lang.Class({
callback();
else
this._preparedCallbacks.push(callback);
- },
+ }
_onPrepared(am, res) {
try {
@@ -97,7 +95,7 @@ var AccountsMonitor = new Lang.Class({
Lang.bind(this, this._accountEnabledChanged));
this._preparedCallbacks.forEach(callback => { callback(); });
- },
+ }
_onPrepareShutdown() {
let presence = Tp.ConnectionPresenceType.OFFLINE;
@@ -110,11 +108,11 @@ var AccountsMonitor = new Lang.Class({
this._app.release();
});
});
- },
+ }
_shouldMonitorAccount(account) {
return account.protocol_name == 'irc';
- },
+ }
_addAccount(account) {
if (!this._shouldMonitorAccount(account))
@@ -131,7 +129,7 @@ var AccountsMonitor = new Lang.Class({
this.emit('account-added', account);
this.emit('accounts-changed');
- },
+ }
_removeAccount(account) {
if (!this._accounts.delete(account.object_path))
@@ -142,7 +140,7 @@ var AccountsMonitor = new Lang.Class({
this.emit('account-removed', account);
this.emit('accounts-changed');
- },
+ }
_accountEnabledChanged(am, account) {
if (!this._accounts.has(account.object_path))
@@ -151,5 +149,5 @@ var AccountsMonitor = new Lang.Class({
: 'account-disabled', account);
this.emit('accounts-changed');
}
-});
+};
Signals.addSignalMethods(AccountsMonitor.prototype);
diff --git a/src/ircParser.js b/src/ircParser.js
index 0a7709f..26eab05 100644
--- a/src/ircParser.js
+++ b/src/ircParser.js
@@ -3,7 +3,6 @@ const GLib = imports.gi.GLib;
const Tp = imports.gi.TelepathyGLib;
const AppNotifications = imports.appNotifications;
-const Lang = imports.lang;
const RoomManager = imports.roomManager;
const Signals = imports.signals;
const Utils = imports.utils;
@@ -39,26 +38,24 @@ var knownCommands = {
const UNKNOWN_COMMAND_MESSAGE =
N_("Unknown command — try /HELP for a list of available commands");
-var IrcParser = new Lang.Class({
- Name: 'IrcParser',
-
- _init(room) {
+var IrcParser = class IrcParser {
+ constructor(room) {
this._app = Gio.Application.get_default();
this._roomManager = RoomManager.getDefault();
this._room = room;
- },
+ }
_createFeedbackLabel(text) {
return new AppNotifications.SimpleOutput(text);
- },
+ }
_createFeedbackUsage(cmd) {
return this._createFeedbackLabel(_("Usage: %s").format(_(knownCommands[cmd])));
- },
+ }
_createFeedbackGrid(header, items) {
return new AppNotifications.GridOutput(header, items);
- },
+ }
process(text) {
if (!this._room || !this._room.channel || !text.length)
@@ -279,13 +276,13 @@ var IrcParser = new Lang.Class({
if (output)
this._app.commandOutputQueue.addNotification(output);
return retval;
- },
+ }
_sendText(text) {
let type = Tp.ChannelTextMessageType.NORMAL;
let message = Tp.ClientMessage.new_text(type, text);
this._sendMessage(message);
- },
+ }
_sendMessage(message) {
this._room.channel.send_message_async(message, 0, (c, res) => {
@@ -297,5 +294,5 @@ var IrcParser = new Lang.Class({
}
});
}
-});
+};
Signals.addSignalMethods(IrcParser.prototype);
diff --git a/src/networksManager.js b/src/networksManager.js
index d3e6d32..9cd90b0 100644
--- a/src/networksManager.js
+++ b/src/networksManager.js
@@ -1,7 +1,6 @@
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
-const Lang = imports.lang;
const Signals = imports.signals;
let _singleton = null;
@@ -12,10 +11,8 @@ function getDefault() {
return _singleton;
}
-var NetworksManager = new Lang.Class({
- Name: 'NetworksManager',
-
- _init() {
+var NetworksManager = class NetworksManager {
+ constructor() {
this._networks = [];
this._networksById = new Map();
@@ -28,7 +25,7 @@ var NetworksManager = new Lang.Class({
} catch(e) {
log('Failed to load network list: ' + e.message);
}
- },
+ }
_onContentsReady(f, res) {
let success, data;
@@ -40,7 +37,7 @@ var NetworksManager = new Lang.Class({
}
if (this._parseNetworks(data))
this.emit('changed');
- },
+ }
_parseNetworks(data) {
let networks;
@@ -57,26 +54,26 @@ var NetworksManager = new Lang.Class({
this._networksById.set(network.id, network);
});
return true;
- },
+ }
_lookupNetwork(id) {
let network = this._networksById.get(id);
if (!network)
throw new Error('Invalid network ID');
return network;
- },
+ }
get networks() {
return this._networks;
- },
+ }
getAccountIsPredefined(account) {
return account && this._networksById.get(account.service) != null;
- },
+ }
getNetworkName(id) {
return this._lookupNetwork(id).name;
- },
+ }
getNetworkIsFavorite(id) {
let network = this._lookupNetwork(id);
@@ -85,7 +82,7 @@ var NetworksManager = new Lang.Class({
return network['favorite'];
return false;
- },
+ }
getNetworkDetails(id) {
let network = this._lookupNetwork(id);
@@ -99,21 +96,21 @@ var NetworksManager = new Lang.Class({
'port': new GLib.Variant('u', server.port),
'use-ssl': new GLib.Variant('b', server.ssl)
};
- },
+ }
getNetworkServers(id) {
let network = this._lookupNetwork(id);
let sslServers = network.servers.filter(s => s.ssl);
return sslServers.length > 0 ? sslServers
: network.servers.slice();
- },
+ }
getNetworkMatchTerms(id) {
let network = this._lookupNetwork(id);
let servers = network.servers.map(s => s.address.toLowerCase());
return [network.name.toLowerCase(),
network.id.toLowerCase()].concat(servers);
- },
+ }
findByServer(server) {
for (let n of this._networks)
@@ -121,5 +118,5 @@ var NetworksManager = new Lang.Class({
return n.id;
return null;
}
-});
+};
Signals.addSignalMethods(NetworksManager.prototype);
diff --git a/src/pasteManager.js b/src/pasteManager.js
index e506a3f..44d458f 100644
--- a/src/pasteManager.js
+++ b/src/pasteManager.js
@@ -28,12 +28,7 @@ function _getTargetForContentType(contentType) {
}
-var PasteManager = new Lang.Class({
- Name: 'PasteManager',
-
- _init() {
- },
-
+var PasteManager = class PasteManager {
pasteContent(content, title, callback) {
if (typeof content == 'string') {
Utils.gpaste(content, title, callback);
@@ -44,14 +39,14 @@ var PasteManager = new Lang.Class({
} else {
throw new Error('Unhandled content type');
}
- },
+ }
_pasteFile(file, title, callback) {
file.query_info_async(Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
Gio.FileQueryInfoFlags.NONE,
GLib.PRIORITY_DEFAULT, null,
Lang.bind(this, this._onFileQueryFinish, title, callback));
- },
+ }
_onFileQueryFinish(file, res, title, callback) {
let fileInfo = null;
@@ -80,7 +75,7 @@ var PasteManager = new Lang.Class({
else
callback(null);
}
-});
+};
var DropTargetIface = new Lang.Interface({
Name: 'DropTargetIface',
diff --git a/src/roomManager.js b/src/roomManager.js
index 5931faa..4af710e 100644
--- a/src/roomManager.js
+++ b/src/roomManager.js
@@ -15,10 +15,8 @@ function getDefault() {
return _singleton;
}
-var _RoomManager = new Lang.Class({
- Name: '_RoomManager',
-
- _init() {
+class _RoomManager {
+ constructor() {
this._rooms = new Map();
this._settings = new Gio.Settings({ schema_id: 'org.gnome.Polari' })
@@ -52,33 +50,33 @@ var _RoomManager = new Lang.Class({
this._removeSavedChannelsForAccount(account.object_path);
});
this._accountsMonitor.prepare(() => { this._restoreRooms(); });
- },
+ }
lookupRoom(id) {
return this._rooms.get(id);
- },
+ }
lookupRoomByName(name, account) {
for (let room of this._rooms.values())
if (room.channel_name == name && room.account == account)
return room;
return null;
- },
+ }
lookupRoomByChannel(channel) {
let account = channel.connection.get_account();
let channelName = channel.identifier;
let id = Polari.create_room_id(account, channelName, channel.handle_type);
return this._rooms.get(id);
- },
+ }
get roomCount() {
return this._rooms.size;
- },
+ }
get rooms() {
return [...this._rooms.values()];
- },
+ }
_onJoinActivated(action, parameter) {
let [accountPath, channelName, time] = parameter.deep_unpack();
@@ -87,7 +85,7 @@ var _RoomManager = new Lang.Class({
this._accountsMonitor.prepare(() => {
this._ensureRoom(accountPath, channelName, Tp.HandleType.ROOM, time);
});
- },
+ }
_onQueryActivated(action, parameter) {
let [accountPath, channelName, , time] = parameter.deep_unpack();
@@ -95,7 +93,7 @@ var _RoomManager = new Lang.Class({
this._accountsMonitor.prepare(() => {
this._ensureRoom(accountPath, channelName, Tp.HandleType.CONTACT, time);
});
- },
+ }
_onLeaveActivated(action, parameter) {
let [id, ] = parameter.deep_unpack();
@@ -103,7 +101,7 @@ var _RoomManager = new Lang.Class({
this._removeSavedChannel(room.account.object_path, room.channel_name);
this._removeRoom(room);
- },
+ }
_restoreRooms(accountPath) {
this._settings.get_value('saved-channel-list').deep_unpack().forEach(c => {
@@ -113,13 +111,13 @@ var _RoomManager = new Lang.Class({
this._ensureRoom(c.account, c.channel, Tp.HandleType.ROOM, 0)
});
this.emit('rooms-loaded');
- },
+ }
_removeRooms(accountPath) {
for (let room of this._rooms.values())
if (accountPath == null || room.account.object_path == accountPath)
this._removeRoom(room);
- },
+ }
_findChannelIndex(channels, accountPath, channelName) {
let matchName = channelName.toLowerCase();
@@ -128,7 +126,7 @@ var _RoomManager = new Lang.Class({
channels[i].channel.deep_unpack().toLowerCase() == matchName)
return i;
return -1;
- },
+ }
_addSavedChannel(accountPath, channelName) {
let channels = this._settings.get_value('saved-channel-list').deep_unpack();
@@ -140,7 +138,7 @@ var _RoomManager = new Lang.Class({
});
this._settings.set_value('saved-channel-list',
new GLib.Variant('aa{sv}', channels));
- },
+ }
_removeSavedChannel(accountPath, channelName) {
let channels = this._settings.get_value('saved-channel-list').deep_unpack();
@@ -150,7 +148,7 @@ var _RoomManager = new Lang.Class({
channels.splice(pos, 1);
this._settings.set_value('saved-channel-list',
new GLib.Variant('aa{sv}', channels));
- },
+ }
_removeSavedChannelsForAccount(accountPath) {
let channels = this._settings.get_value('saved-channel-list').deep_unpack();
@@ -159,7 +157,7 @@ var _RoomManager = new Lang.Class({
channels = channels.filter(c => !c.account.equal(account));
this._settings.set_value('saved-channel-list',
new GLib.Variant('aa{sv}', channels));
- },
+ }
_ensureRoom(accountPath, channelName, type, time) {
let account = this._accountsMonitor.lookupAccount(accountPath);
@@ -187,7 +185,7 @@ var _RoomManager = new Lang.Class({
this._app.active_window.active_room = room;
return room;
- },
+ }
ensureRoomForChannel(channel, time) {
let accountPath = channel.connection.get_account().object_path;
@@ -196,11 +194,11 @@ var _RoomManager = new Lang.Class({
: channel.identifier;
let room = this._ensureRoom(accountPath, channelName, channel.handle_type, time);
room.channel = channel;
- },
+ }
_removeRoom(room) {
if (this._rooms.delete(room.id))
this.emit('room-removed', room);
}
-});
+};
Signals.addSignalMethods(_RoomManager.prototype);
diff --git a/src/serverRoomManager.js b/src/serverRoomManager.js
index 7fcb2d6..3e6166b 100644
--- a/src/serverRoomManager.js
+++ b/src/serverRoomManager.js
@@ -22,10 +22,8 @@ function getDefault() {
return _singleton;
}
-var _ServerRoomManager = new Lang.Class({
- Name: '_ServerRoomManager',
-
- _init() {
+class _ServerRoomManager {
+ constructor() {
this._roomLists = new Map();
this._accountsMonitor = AccountsMonitor.getDefault();
@@ -38,21 +36,21 @@ var _ServerRoomManager = new Lang.Class({
this._onAccountStatusChanged(this._accountsMonitor, a);
});
});
- },
+ }
getRoomInfos(account) {
let roomList = this._roomLists.get(account);
if (!roomList || roomList.list.listing)
return [];
return roomList.rooms.slice();
- },
+ }
isLoading(account) {
let roomList = this._roomLists.get(account);
if (!roomList)
return account.connection_status == Tp.ConnectionStatus.CONNECTING;
return roomList.list.listing;
- },
+ }
_onAccountStatusChanged(mon, account) {
if (account.connection_status == Tp.ConnectionStatus.CONNECTING)
@@ -78,7 +76,7 @@ var _ServerRoomManager = new Lang.Class({
roomList.connect('notify::listing',
Lang.bind(this, this._onListingChanged));
this._roomLists.set(account, { list: roomList, rooms: [] });
- },
+ }
_onAccountRemoved(mon, account) {
let roomList = this._roomLists.get(account);
@@ -87,19 +85,19 @@ var _ServerRoomManager = new Lang.Class({
roomList.list.run_dispose();
this._roomLists.delete(account);
- },
+ }
_onGotRoom(list, roomInfo) {
let roomList = this._roomLists.get(list.account);
if (!roomList)
return;
roomList.rooms.push(roomInfo);
- },
+ }
_onListingChanged(list) {
this.emit('loading-changed', list.account);
}
-});
+};
Signals.addSignalMethods(_ServerRoomManager.prototype);
diff --git a/src/tabCompletion.js b/src/tabCompletion.js
index bd36e70..1d3ac65 100644
--- a/src/tabCompletion.js
+++ b/src/tabCompletion.js
@@ -5,10 +5,8 @@ const Pango = imports.gi.Pango;
const IrcParser = imports.ircParser;
const Lang = imports.lang;
-var TabCompletion = new Lang.Class({
- Name: 'TabCompletion',
-
- _init(entry) {
+var TabCompletion = class TabCompletion {
+ constructor(entry) {
this._entry = entry;
this._canComplete = false;
this._key = '';
@@ -50,7 +48,7 @@ var TabCompletion = new Lang.Class({
margin_end: 6 }));
this._list.add(row);
}
- },
+ }
_showPopup() {
this._list.show_all();
@@ -73,7 +71,7 @@ var TabCompletion = new Lang.Class({
y += allocation.y - height;
this._popup.move(x, y);
this._popup.show();
- },
+ }
setCompletions(completions) {
if (this._popup.visible) {
@@ -118,7 +116,7 @@ var TabCompletion = new Lang.Class({
this._list.add(row);
}
this._canComplete = completions.length > 0;
- },
+ }
_onKeyPress(w, event) {
let [, keyval] = event.get_keyval();
@@ -156,7 +154,7 @@ var TabCompletion = new Lang.Class({
keyval == Gdk.KEY_ISO_Enter);
}
return Gdk.EVENT_PROPAGATE;
- },
+ }
_getRowCompletion(row) {
this._previousWasCommand = this._isCommand;
@@ -166,18 +164,18 @@ var TabCompletion = new Lang.Class({
if (this._startPos == 0 || this._isChained)
return row._text + ': ';
return row._text;
- },
+ }
_onRowSelected(w, row) {
if (row)
this._insertCompletion(this._getRowCompletion(row));
- },
+ }
_filter(row) {
if (this._key.length == 0)
return false;
return row._casefoldedText.startsWith(this._key);
- },
+ }
_insertCompletion(completion) {
let pos = this._entry.get_position();
@@ -185,14 +183,14 @@ var TabCompletion = new Lang.Class({
this._entry.delete_text(this._startPos, pos);
this._entry.insert_text(completion, -1, this._startPos);
this._entry.set_position(this._endPos);
- },
+ }
_setPreviousCompletionChained(chained) {
let repl = chained ? ',' : ':';
let start = this._startPos - 2;
this._entry.delete_text(start, start + 1);
this._entry.insert_text(repl, -1, start);
- },
+ }
_start() {
if (!this._canComplete)
@@ -227,7 +225,7 @@ var TabCompletion = new Lang.Class({
this._list.select_row(visibleRows[0]);
this._showPopup()
}
- },
+ }
_onKeynavFailed(w, dir) {
if (this._inHandler)
@@ -237,13 +235,13 @@ var TabCompletion = new Lang.Class({
this._moveSelection(Gtk.MovementStep.BUFFER_ENDS, count);
this._inHandler = false;
return Gdk.EVENT_STOP;
- },
+ }
_moveSelection(movement, count) {
this._list.emit('move-cursor', movement, count);
let row = this._list.get_focus_child();
this._list.select_row(row);
- },
+ }
_stop() {
if (this._key.length == 0)
@@ -255,7 +253,7 @@ var TabCompletion = new Lang.Class({
this._key = '';
this._list.invalidate_filter();
- },
+ }
_cancel() {
if (this._key.length == 0)
@@ -264,5 +262,5 @@ var TabCompletion = new Lang.Class({
this._setPreviousCompletionChained(false);
this._insertCompletion('');
this._stop();
- },
-});
+ }
+};
diff --git a/src/telepathyClient.js b/src/telepathyClient.js
index d682838..3c02712 100644
--- a/src/telepathyClient.js
+++ b/src/telepathyClient.js
@@ -44,16 +44,14 @@ const SASLAbortReason = {
USER_ABORT: 1
};
-var SASLAuthHandler = new Lang.Class({
- Name: 'SASLAuthHandler',
-
- _init(channel) {
+class SASLAuthHandler {
+ constructor(channel) {
this._channel = channel;
this._proxy = new SASLAuthProxy(Gio.DBus.session,
channel.bus_name,
channel.object_path,
Lang.bind(this, this._onProxyReady));
- },
+ }
_onProxyReady(proxy) {
this._proxy.connectSignal('SASLStatusChanged',
@@ -62,7 +60,7 @@ var SASLAuthHandler = new Lang.Class({
let account = this._channel.connection.get_account();
Utils.lookupAccountPassword(account,
Lang.bind(this, this._onPasswordReady));
- },
+ }
_onPasswordReady(password) {
if (password)
@@ -72,7 +70,7 @@ var SASLAuthHandler = new Lang.Class({
this._proxy.AbortSASLRemote(SASLAbortReason.USER_ABORT,
'Password not available',
Lang.bind(this, this._resetPrompt));
- },
+ }
_onSASLStatusChanged(proxy, sender, [status]) {
let name = this._channel.connection.get_account().display_name;
@@ -95,7 +93,7 @@ var SASLAuthHandler = new Lang.Class({
this._channel.close_async(null);
break;
}
- },
+ }
_resetPrompt() {
let account = this._channel.connection.get_account();
@@ -107,7 +105,7 @@ var SASLAuthHandler = new Lang.Class({
'available', '', null);
});
}
-});
+};
var TelepathyClient = new Lang.Class({
Name: 'TelepathyClient',
diff --git a/src/userTracker.js b/src/userTracker.js
index 87f9bfe..f3e0ca1 100644
--- a/src/userTracker.js
+++ b/src/userTracker.js
@@ -18,10 +18,8 @@ function getUserStatusMonitor() {
return _singleton;
}
-var UserStatusMonitor = new Lang.Class({
- Name: 'UserStatusMonitor',
-
- _init() {
+class UserStatusMonitor {
+ constructor() {
this._userTrackers = new Map();
this._accountsMonitor = AccountsMonitor.getDefault();
@@ -32,23 +30,23 @@ var UserStatusMonitor = new Lang.Class({
this._accountsMonitor.accounts.forEach(
a => { this._onAccountAdded(this._accountsMonitor, a); });
- },
+ }
_onAccountAdded(accountsMonitor, account) {
if (this._userTrackers.has(account))
return;
this._userTrackers.set(account, new UserTracker(account));
- },
+ }
_onAccountRemoved(accountsMonitor, account) {
this._userTrackers.delete(account);
- },
+ }
getUserTrackerForAccount(account) {
return this._userTrackers.get(account);
}
-});
+};
var UserTracker = new Lang.Class({
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]