Shim Array methods added after current Qt version in Ubuntu

This commit is contained in:
Emil Lundberg 2017-12-17 00:17:51 +01:00
parent 610032869f
commit d0d3fde9b2
No known key found for this signature in database
GPG Key ID: 5B9688125FF0B636
3 changed files with 50 additions and 2 deletions

View File

@ -16,7 +16,7 @@ Item {
Keys.onUpPressed: goUp()
function findSelectedIndex() {
return credentials.findIndex(function(entry) {
return util.findIndex(credentials, function(entry) {
return entry.credential.key === selectedKey
}) || null
}

44
qml/Util.qml Normal file
View File

@ -0,0 +1,44 @@
import QtQuick 2.5
Item {
visible: false
/*
* Quick and dirty shim for Array.find added in Qt 5.9
*
* @param items: array
* @param predicate: function(any) => Boolean
* @return The first `item` in `arr` that matches the `predicate`, or
* `undefined` if none matches
*/
function find(items, predicate) {
if (items) {
for (var i = 0; i < items.length; ++i) {
if (predicate(items[i])) {
return items[i]
}
}
}
return undefined
}
/*
* Quick and dirty shim for Array.findIndex added in Qt 5.9
*
* @param items: array
* @param predicate: function(any) => Boolean
* @return The index of the first `item` in `arr` that matches the
* `predicate`, or `undefined` if none matches
*/
function findIndex(items, predicate) {
if (items) {
for (var i = 0; i < items.length; ++i) {
if (predicate(items[i])) {
return i
}
}
}
return undefined
}
}

View File

@ -371,6 +371,10 @@ ApplicationWindow {
id: noQr
}
Util {
id: util
}
function selectCredential(entry) {
selectedKey = entry.credential.key
}
@ -380,7 +384,7 @@ ApplicationWindow {
}
function getSelected() {
return credentials.find(function(entry) {
return util.find(credentials, function(entry) {
return isSelected(entry.credential)
}) || null
}