From 1fa8b70359db6b285c69a0bd4b09afefbc0e0056 Mon Sep 17 00:00:00 2001 From: miruka Date: Thu, 18 Jul 2019 05:18:13 -0400 Subject: [PATCH] =?UTF-8?q?ES5=20=E2=86=92=207:=20Use=20for=20in/of=20and?= =?UTF-8?q?=20let?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/qml/Base/HInterfaceBox.qml | 4 +- src/qml/Base/HListModel.qml | 50 +++++++++++-------------- src/qml/Chat/SendBox.qml | 2 +- src/qml/Chat/Timeline/EventDelegate.qml | 2 +- src/qml/EventHandlers/rooms.js | 25 ++++++------- src/qml/Models/Timelines.qml | 7 ++-- src/qml/Models/Users.qml | 2 +- src/qml/Pages/EditAccount/Profile.qml | 2 +- src/qml/Pages/SignIn.qml | 2 +- src/qml/Python.qml | 4 +- src/qml/UI.qml | 3 +- src/qml/utils.js | 33 +++++++--------- 12 files changed, 59 insertions(+), 77 deletions(-) diff --git a/src/qml/Base/HInterfaceBox.qml b/src/qml/Base/HInterfaceBox.qml index fb9bf6ef..c59feab0 100644 --- a/src/qml/Base/HInterfaceBox.qml +++ b/src/qml/Base/HInterfaceBox.qml @@ -15,8 +15,8 @@ HScalingBox { default property alias body: interfaceBody.children function clickEnterButtonTarget() { - for (var i = 0; i < buttonModel.length; i++) { - var btn = interfaceButtonsRepeater.itemAt(i) + for (let i = 0; i < buttonModel.length; i++) { + let btn = interfaceButtonsRepeater.itemAt(i) if (btn.name === enterButtonTarget) { btn.clicked() } } } diff --git a/src/qml/Base/HListModel.qml b/src/qml/Base/HListModel.qml index 80c9c9b8..e50cd30c 100644 --- a/src/qml/Base/HListModel.qml +++ b/src/qml/Base/HListModel.qml @@ -25,20 +25,18 @@ SortFilterProxyModel { } function extend(newItems) { - for (var i = 0; i < newItems.length; i++) { - model.append(newItems[i]) - } + for (let item of newItems) { model.append(item) } } function getIndices(whereRolesAre, maxResults=null, maxTries=null) { // maxResults, maxTries: null or int - var results = [] + let results = [] - for (var i = 0; i < model.count; i++) { - var item = model.get(i) - var include = true + for (let i = 0; i < model.count; i++) { + let item = model.get(i) + let include = true - for (var role in whereRolesAre) { + for (let role in whereRolesAre) { if (item[role] != whereRolesAre[role]) { include = false break @@ -60,33 +58,31 @@ SortFilterProxyModel { } function getWhere(rolesAre, maxResults=null, maxTries=null) { - var indices = getIndices(rolesAre, maxResults, maxTries) - var items = [] + let items = [] - for (var i = 0; i < indices.length; i++) { - items.push(model.get(indices[i])) + for (let indice of getIndices(rolesAre, maxResults, maxTries)) { + items.push(model.get(indice)) } return items } function forEachWhere(rolesAre, func, maxResults=null, maxTries=null) { - var items = getWhere(rolesAre, maxResults, maxTries) - for (var i = 0; i < items.length; i++) { - func(items[i]) + for (let item of getWhere(rolesAre, maxResults, maxTries)) { + func(item) } } function upsert( whereRolesAre, newItem, updateIfExist=true, maxTries=null ) { - var indices = getIndices(whereRolesAre, 1, maxTries) + let indices = getIndices(whereRolesAre, 1, maxTries) if (indices.length == 0) { model.append(newItem) return model.get(model.count) } - var existing = model.get(indices[0]) + let existing = model.get(indices[0]) if (! updateIfExist) { return existing } // Really update only if existing and new item have a difference @@ -107,31 +103,29 @@ SortFilterProxyModel { } function pop(index) { - var item = model.get(index) + let item = model.get(index) model.remove(index) return item } function popWhere(rolesAre, maxResults=null, maxTries=null) { - var indices = getIndices(rolesAre, maxResults, maxTries) - var items = [] + let items = [] - for (var i = 0; i < indices.length; i++) { - items.push(model.get(indices[i])) - model.remove(indices[i]) + for (let indice of getIndices(rolesAre, maxResults, maxTries)) { + items.push(model.get(indice)) + model.remove(indice) } return items } function toObject(itemList=sortFilteredModel) { - var objList = [] + let objList = [] - for (var i = 0; i < itemList.count; i++) { - var item = itemList.get(i) - var obj = JSON.parse(JSON.stringify(item)) + for (let item of itemList) { + let obj = JSON.parse(JSON.stringify(item)) - for (var role in obj) { + for (let role in obj) { if (obj[role]["objectName"] != undefined) { obj[role] = toObject(item[role]) } diff --git a/src/qml/Chat/SendBox.qml b/src/qml/Chat/SendBox.qml index b1846ce6..e369b8de 100644 --- a/src/qml/Chat/SendBox.qml +++ b/src/qml/Chat/SendBox.qml @@ -68,7 +68,7 @@ HRectangle { if (textArea.text === "") { return } - var args = [chatPage.roomId, textArea.text] + let args = [chatPage.roomId, textArea.text] py.callClientCoro(chatPage.userId, "send_markdown", args) area.clear() }) diff --git a/src/qml/Chat/Timeline/EventDelegate.qml b/src/qml/Chat/Timeline/EventDelegate.qml index 33184203..c21b665d 100644 --- a/src/qml/Chat/Timeline/EventDelegate.qml +++ b/src/qml/Chat/Timeline/EventDelegate.qml @@ -58,7 +58,7 @@ Column { readonly property int verticalPadding: theme.spacing / 2 ListView.onAdd: { - var nextDelegate = eventList.contentItem.children[index] + let nextDelegate = eventList.contentItem.children[index] if (nextDelegate) { nextDelegate.reloadPreviousItem() } } diff --git a/src/qml/EventHandlers/rooms.js b/src/qml/EventHandlers/rooms.js index 3ccf8aa4..500b6624 100644 --- a/src/qml/EventHandlers/rooms.js +++ b/src/qml/EventHandlers/rooms.js @@ -6,13 +6,11 @@ Qt.include("../utils.js") function typingTextFor(members, ourUserId) { - var profiles = [] - var names = [] + let profiles = [] + let names = [] - for (var i = 0; i < members.length; i++) { - if (members[i] != ourUserId) { - profiles.push(users.find(members[i])) - } + for (let member of members) { + if (member != ourUserId) { profiles.push(users.find(member)) } } profiles.sort((left, right) => { @@ -21,15 +19,14 @@ function typingTextFor(members, ourUserId) { return 0 }) - for (var i = 0; i < profiles.length; i++) { - var profile = profiles[i] + for (let profile of profiles) { names.push(coloredNameHtml(profile.displayName, profile.userId)) } if (names.length == 0) { return "" } if (names.length == 1) { return qsTr("%1 is typing...").arg(names[0]) } - var text = qsTr("%1 and %2 are typing...") + let text = qsTr("%1 and %2 are typing...") if (names.length == 2) { return text.arg(names[0]).arg(names[1]) } @@ -44,16 +41,16 @@ function onRoomUpdated( roomCategories.upsert({userId, name: category}, {userId, name: category}) function find(category) { - var found = rooms.getIndices({userId, roomId, category}, 1) + let found = rooms.getIndices({userId, roomId, category}, 1) return found.length > 0 ? found[0] : null } - var replace = null + let replace = null if (category == "Invites") { replace = find("Rooms") || find("Left") } else if (category == "Rooms") { replace = find("Invites") || find("Left") } else if (category == "Left") { replace = find("Invites") || find("Rooms")} - var item = { + let item = { typingText: typingTextFor(typingMembers, userId), userId, category, roomId, displayName, avatarUrl, topic, members, @@ -77,7 +74,7 @@ function onTimelineEventReceived( eventType, roomId, eventId, senderId, date, content, contentType, isLocalEcho, showNameLine, translatable, targetUserId ) { - var item = { + let item = { eventType: py.getattr(eventType, "__name__"), roomId, eventId, senderId, date, content, contentType, isLocalEcho, @@ -90,7 +87,7 @@ function onTimelineEventReceived( } // Replace first matching local echo - var found = timelines.getIndices( + let found = timelines.getIndices( {roomId, senderId, content, "isLocalEcho": true}, 1, 250 ) diff --git a/src/qml/Models/Timelines.qml b/src/qml/Models/Timelines.qml index 38645158..1de29d6e 100644 --- a/src/qml/Models/Timelines.qml +++ b/src/qml/Models/Timelines.qml @@ -7,12 +7,11 @@ import "../Base" HListModel { function lastEventOf(roomId) { - // Return an event item or undefined if none found - - for (var i = 0; i < count; i++) { - var item = get(i) // TODO: standardize + for (let i = 0; i < count; i++) { + let item = get(i) // TODO: standardize if (item.roomId == roomId) { return item } } + return null } sorters: ExpressionSorter { diff --git a/src/qml/Models/Users.qml b/src/qml/Models/Users.qml index db7ed8e5..465742c9 100644 --- a/src/qml/Models/Users.qml +++ b/src/qml/Models/Users.qml @@ -11,7 +11,7 @@ HListModel { // the expression with invalid data to establish property bindings if (! userId) { return } - var found = getWhere({userId}, 1) + let found = getWhere({userId}, 1) if (found.length > 0) { return found[0] } py.callCoro("request_user_update_event", [userId]) diff --git a/src/qml/Pages/EditAccount/Profile.qml b/src/qml/Pages/EditAccount/Profile.qml index 6a080485..3e8151b8 100644 --- a/src/qml/Pages/EditAccount/Profile.qml +++ b/src/qml/Pages/EditAccount/Profile.qml @@ -21,7 +21,7 @@ HGridLayout { if (avatar.changed) { saveButton.avatarChangeRunning = true - var path = Qt.resolvedUrl(avatar.imageUrl).replace(/^file:/, "") + let path = Qt.resolvedUrl(avatar.imageUrl).replace(/^file:/, "") py.callClientCoro( userId, "set_avatar_from_file", [path], response => { diff --git a/src/qml/Pages/SignIn.qml b/src/qml/Pages/SignIn.qml index c9aabf21..77ecc9c2 100644 --- a/src/qml/Pages/SignIn.qml +++ b/src/qml/Pages/SignIn.qml @@ -27,7 +27,7 @@ Item { login: button => { button.loading = true - var args = [idField.text, passwordField.text] + let args = [idField.text, passwordField.text] py.callCoro("login_client", args, userId => { pageStack.showPage("RememberAccount", {loginWith, userId}) diff --git a/src/qml/Python.qml b/src/qml/Python.qml index e4fff68f..25e5f709 100644 --- a/src/qml/Python.qml +++ b/src/qml/Python.qml @@ -20,14 +20,14 @@ Python { } function callCoro(name, args=[], callback=null) { - var uuid = Math.random() + "." + name + let uuid = Math.random() + "." + name pendingCoroutines[uuid] = callback || function() {} call("APP.call_backend_coro", [name, uuid, args]) } function callClientCoro(accountId, name, args=[], callback=null) { - var uuid = Math.random() + "." + name + let uuid = Math.random() + "." + name pendingCoroutines[uuid] = callback || function() {} call("APP.call_client_coro", [accountId, name, uuid, args]) diff --git a/src/qml/UI.qml b/src/qml/UI.qml index 42d41881..1961da88 100644 --- a/src/qml/UI.qml +++ b/src/qml/UI.qml @@ -64,8 +64,7 @@ Item { } function showRoom(userId, category, roomId) { - var info = rooms.getWhere({userId, roomId, category}, 1)[0] - + let info = rooms.getWhere({userId, roomId, category}, 1)[0] pageStack.replace("Chat/Chat.qml", {"roomInfo": info}) } diff --git a/src/qml/utils.js b/src/qml/utils.js index 386ffd32..beeb0c2f 100644 --- a/src/qml/utils.js +++ b/src/qml/utils.js @@ -18,11 +18,11 @@ function hsla(hue, saturation, lightness, alpha=1.0) { function arrayToModelItem(keysName, array) { // Convert an array to an object suitable to be in a model, example: // [1, 2, 3] → [{keysName: 1}, {keysName: 2}, {keysName: 3}] - var items = [] + let items = [] - for (var i = 0; i < array.length; i++) { - var obj = {} - obj[keysName] = array[i] + for (let item of array) { + let obj = {} + obj[keysName] = item items.push(obj) } return items @@ -31,8 +31,8 @@ function arrayToModelItem(keysName, array) { function hueFrom(string) { // Calculate and return a unique hue between 0 and 1 for the string - var hue = 0 - for (var i = 0; i < string.length; i++) { + let hue = 0 + for (let i = 0; i < string.length; i++) { hue += string.charCodeAt(i) * 99 } return hue % 360 / 360 @@ -87,23 +87,16 @@ function translatedEventContent(ev) { if (ev.translatable == false) { return ev.content } // %S → sender display name - var name = users.find(ev.senderId).displayName - var text = ev.content.replace("%S", coloredNameHtml(name, ev.senderId)) + let name = users.find(ev.senderId).displayName + let text = ev.content.replace("%S", coloredNameHtml(name, ev.senderId)) // %T → target (event state_key) display name if (ev.targetUserId) { - var tname = users.find(ev.targetUserId).displayName + let tname = users.find(ev.targetUserId).displayName text = text.replace("%T", coloredNameHtml(tname, ev.targetUserId)) } - text = qsTr(text) - if (ev.translatable == true) { return text } - - // Else, model.translatable should be an array of args - for (var i = 0; ev.translatable.length; i++) { - text = text.arg(ev.translatable[i]) - } - return text + return qsTr(text) } @@ -111,10 +104,10 @@ function filterMatches(filter, text) { filter = filter.toLowerCase() text = text.toLowerCase() - var words = filter.split(" ") + let words = filter.split(" ") - for (var i = 0; i < words.length; i++) { - if (words[i] && ! text.includes(words[i])) { + for (let word of words) { + if (word && ! text.includes(word)) { return false } }