2019-07-08 13:52:41 +10:00
|
|
|
// Copyright 2019 miruka
|
|
|
|
// This file is part of harmonyqml, licensed under LGPLv3.
|
|
|
|
|
2019-07-13 19:39:01 +10:00
|
|
|
import QtQuick 2.12
|
2019-07-03 03:59:52 +10:00
|
|
|
import SortFilterProxyModel 0.2
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
SortFilterProxyModel {
|
2019-06-27 16:31:03 +10:00
|
|
|
// To initialize a HListModel with items,
|
|
|
|
// use `Component.onCompleted: extend([{"foo": 1, "bar": 2}, ...])`
|
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
id: sortFilteredModel
|
|
|
|
|
2019-07-07 07:58:59 +10:00
|
|
|
property ListModel model: ListModel {}
|
2019-07-03 03:59:52 +10:00
|
|
|
sourceModel: model // Can't assign a "ListModel {}" directly here
|
|
|
|
|
|
|
|
function append(dict) { return model.append(dict) }
|
|
|
|
function clear() { return model.clear() }
|
|
|
|
function insert(index, dict) { return model.inset(index, dict) }
|
|
|
|
function move(from, to, n) { return model.move(from, to, n) }
|
|
|
|
function remove(index, count) { return model.remove(index, count) }
|
|
|
|
function set(index, dict) { return model.set(index, dict) }
|
|
|
|
function sync() { return model.sync() }
|
|
|
|
function setProperty(index, prop, value) {
|
|
|
|
return model.setProperty(index, prop, value)
|
|
|
|
}
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
function extend(new_items) {
|
|
|
|
for (var i = 0; i < new_items.length; i++) {
|
2019-07-03 03:59:52 +10:00
|
|
|
model.append(new_items[i])
|
2019-06-27 16:31:03 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
function getIndices(where_roles_are, max_results, max_tries) {
|
|
|
|
// max arguments: unefined or int
|
2019-06-27 16:31:03 +10:00
|
|
|
var results = []
|
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
for (var i = 0; i < model.count; i++) {
|
|
|
|
var item = model.get(i)
|
|
|
|
var include = true
|
|
|
|
|
|
|
|
for (var role in where_roles_are) {
|
|
|
|
if (item[role] != where_roles_are[role]) {
|
|
|
|
include = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
if (include) {
|
|
|
|
results.push(i)
|
|
|
|
if (max_results && results.length >= max_results) {
|
2019-06-27 16:31:03 +10:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 03:59:52 +10:00
|
|
|
|
|
|
|
if (max_tries && i >= max_tries) {
|
|
|
|
break
|
|
|
|
}
|
2019-06-27 16:31:03 +10:00
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
function getWhere(roles_are, max_results, max_tries) {
|
|
|
|
var indices = getIndices(roles_are, max_results, max_tries)
|
|
|
|
var items = []
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
for (var i = 0; i < indices.length; i++) {
|
2019-07-03 03:59:52 +10:00
|
|
|
items.push(model.get(indices[i]))
|
2019-06-27 16:31:03 +10:00
|
|
|
}
|
2019-07-03 03:59:52 +10:00
|
|
|
return items
|
2019-06-27 16:31:03 +10:00
|
|
|
}
|
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
function forEachWhere(roles_are, func, max_results, max_tries) {
|
|
|
|
var items = getWhere(roles_are, max_results, max_tries)
|
2019-06-29 08:12:45 +10:00
|
|
|
for (var i = 0; i < items.length; i++) {
|
2019-07-03 03:59:52 +10:00
|
|
|
func(items[i])
|
2019-06-29 08:12:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
function upsert(where_roles_are, new_item, update_if_exist, max_tries) {
|
|
|
|
var indices = getIndices(where_roles_are, 1, max_tries)
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
if (indices.length == 0) {
|
2019-07-03 03:59:52 +10:00
|
|
|
model.append(new_item)
|
|
|
|
return model.get(model.count)
|
2019-06-29 08:12:45 +10:00
|
|
|
}
|
|
|
|
|
2019-07-05 08:37:15 +10:00
|
|
|
var existing = model.get(indices[0])
|
|
|
|
if (update_if_exist == false) { return existing }
|
|
|
|
|
|
|
|
// Really update only if existing and new item have a difference
|
|
|
|
for (var role in existing) {
|
|
|
|
if (Boolean(existing[role].getTime)) {
|
|
|
|
if (existing[role].getTime() != new_item[role].getTime()) {
|
|
|
|
model.set(indices[0], new_item)
|
|
|
|
return existing
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (existing[role] != new_item[role]) {
|
|
|
|
model.set(indices[0], new_item)
|
|
|
|
return existing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return existing
|
2019-06-27 16:31:03 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
function pop(index) {
|
2019-07-03 03:59:52 +10:00
|
|
|
var item = model.get(index)
|
|
|
|
model.remove(index)
|
2019-06-27 16:31:03 +10:00
|
|
|
return item
|
|
|
|
}
|
2019-06-29 08:12:45 +10:00
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
function popWhere(roles_are, max_results, max_tries) {
|
|
|
|
var indices = getIndices(roles_are, max_results, max_tries)
|
|
|
|
var items = []
|
2019-06-29 08:12:45 +10:00
|
|
|
|
|
|
|
for (var i = 0; i < indices.length; i++) {
|
2019-07-03 03:59:52 +10:00
|
|
|
items.push(model.get(indices[i]))
|
|
|
|
model.remove(indices[i])
|
2019-06-29 08:12:45 +10:00
|
|
|
}
|
2019-07-03 03:59:52 +10:00
|
|
|
return items
|
2019-06-29 08:12:45 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function toObject(item_list) {
|
2019-07-03 03:59:52 +10:00
|
|
|
item_list = item_list || sortFilteredModel
|
2019-06-29 08:12:45 +10:00
|
|
|
var obj_list = []
|
|
|
|
|
|
|
|
for (var i = 0; i < item_list.count; i++) {
|
|
|
|
var item = item_list.get(i)
|
|
|
|
var obj = JSON.parse(JSON.stringify(item))
|
|
|
|
|
|
|
|
for (var role in obj) {
|
|
|
|
if (obj[role]["objectName"] != undefined) {
|
|
|
|
obj[role] = toObject(item[role])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
obj_list.push(obj)
|
|
|
|
}
|
|
|
|
return obj_list
|
|
|
|
}
|
|
|
|
|
|
|
|
function toJson() {
|
|
|
|
return JSON.stringify(toObject(), null, 4)
|
|
|
|
}
|
2019-06-27 16:31:03 +10:00
|
|
|
}
|