moment/src/qml/utils.js

109 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-07-08 13:52:41 +10:00
// Copyright 2019 miruka
// This file is part of harmonyqml, licensed under LGPLv3.
2019-07-06 09:54:16 +10:00
function arrayToModelItem(keys_name, array) {
// Convert an array to an object suitable to be in a model, example:
// [1, 2, 3] → [{keys_name: 1}, {keys_name: 2}, {keys_name: 3}]
var items = []
for (var i = 0; i < array.length; i++) {
var obj = {}
obj[keys_name] = array[i]
items.push(obj)
}
return items
}
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++) {
hue += string.charCodeAt(i) * 99
}
return hue % 360 / 360
}
function avatarColor(name) {
return Qt.hsla(
hueFrom(name),
theme.avatar.background.saturation,
theme.avatar.background.lightness,
theme.avatar.background.alpha
)
}
function nameColor(name) {
return Qt.hsla(
hueFrom(name),
theme.displayName.saturation,
theme.displayName.lightness,
1
)
}
function coloredNameHtml(name, alt_id) {
// substring: remove leading @
return "<font color='" + nameColor(name || alt_id.substring(1)) + "'>" +
escapeHtml(name || alt_id) +
"</font>"
}
function escapeHtml(string) {
// Replace special HTML characters by encoded alternatives
return string.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace('"', "&quot;")
.replace("'", "&#039;")
}
function eventIsMessage(ev) {
return /^RoomMessage($|[A-Z])/.test(ev.eventType)
}
function translatedEventContent(ev) {
2019-07-05 06:01:44 +10:00
// ev: timelines item
if (ev.translatable == false) { return ev.content }
// %S → sender display name
2019-07-08 12:41:32 +10:00
var name = users.find(ev.senderId).displayName
var text = ev.content.replace("%S", coloredNameHtml(name, ev.senderId))
// %T → target (event state_key) display name
2019-07-08 12:41:32 +10:00
if (ev.tarfindId) {
var tname = users.find(ev.tarfindId).displayName
text = text.replace("%T", coloredNameHtml(tname, ev.tarfindId))
}
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
}
2019-07-07 19:49:02 +10:00
function filterMatches(filter, text) {
filter = filter.toLowerCase()
text = text.toLowerCase()
var words = filter.split(" ")
for (var i = 0; i < words.length; i++) {
if (words[i] && text.indexOf(words[i]) == -1) {
return false
}
}
return true
}