ES5 → 7: Use for in/of and let

This commit is contained in:
miruka 2019-07-18 05:18:13 -04:00
parent 8a38274280
commit 1fa8b70359
12 changed files with 59 additions and 77 deletions

View File

@ -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() }
}
}

View File

@ -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])
}

View File

@ -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()
})

View File

@ -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() }
}

View File

@ -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
)

View File

@ -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 {

View File

@ -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])

View File

@ -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 => {

View File

@ -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})

View File

@ -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])

View File

@ -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})
}

View File

@ -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
}
}