-
Notifications
You must be signed in to change notification settings - Fork 0
/
U1Backend.qml
executable file
·182 lines (148 loc) · 5.27 KB
/
U1Backend.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import QtQuick 2.0
import U1db 1.0 as U1db
import Ubuntu.Components 0.1
Item {
function setNote(id, title, body, category, tag, archive, view, links, doc) {
var values
if (doc === "notes") {
values = mainView.database.getDoc("notes")
values["notes"][mainView.database.getDoc("notes").notes.length] = {'id': id, 'title': title, 'body': body,
'category': category, 'tag': tag, 'archive': archive, 'view': view, 'links': links}
mainView.database.putDoc(values, "notes")
if (mainView.createNote) {
notesListView.model = mainView.database.getDoc("notes").notes
}
}
else {
values = mainView.database.getDoc("archive")
values["notes"][mainView.database.getDoc("archive").notes.length] = {'id': id, 'title': title, 'body': body,
'category': category, 'tag': tag, 'archive': archive, 'view': view, 'links': links}
mainView.database.putDoc(values, "archive")
}
}
function replaceNote(pos, doc, title, body, category, tag, archive, view, links) {
pos = parseInt(pos)
var values = getValues(doc)
values["notes"][pos].title = title
values["notes"][pos].body = body
values["notes"][pos].tag = tag
values["notes"][pos].category = category
values["notes"][pos].archive = archive
values["notes"][pos].view = view
values["notes"][pos].links = links
setValues(values, doc)
if (mainView.showArchive) {
notesListView.model = mainView.database.getDoc("archive").notes
}
else {
notesListView.model = mainView.database.getDoc("notes").notes
}
}
function removeNote(id, doc) {
id = parseInt(id)
var values = getValues(doc)
var j = 0
var newValues = {"notes": []}
for (var i = 0; i < values.notes.length; i++) {
if (i !== id) {
newValues["notes"][j] = values["notes"][i]
j++
}
}
setValues(newValues, doc)
}
function getValues(doc) {
var values
if (doc === "notes") {
values = mainView.database.getDoc("notes")
}
else {
values = mainView.database.getDoc("archive")
}
return values
}
function setValues(values, doc) {
if (doc === "notes") {
mainView.database.putDoc(values, "notes")
}
else {
mainView.database.putDoc(values, "archive")
}
}
function fecthAllNotesWithTag(tag) {
var res = {"notes": []}
var archive = mainView.database.getDoc("archive")
var all = mainView.database.getDoc("notes")
for (var i = 0; i < archive["notes"].length; i++) {
all["notes"][all["notes"].length] = archive["notes"][i]
}
var j = 0
for (var i = 0; i < all["notes"].length; i++) {
if (containTag(tag, all["notes"][i].tag)) {
res["notes"][j] = all["notes"][i]
j++
}
}
return res
}
function containTag(tag, tags) {
var t = tags.split(",")
for (var i = 0; i < t.length; i++) {
if (tag === t[i]) {
return true
}
}
return false
}
function fetchAllNotesWithCategory(cat) {
var res = {"notes": []}
var archive = mainView.database.getDoc("archive")
var all = mainView.database.getDoc("notes")
for (var i = 0; i < archive["notes"].length; i++) {
all["notes"][all["notes"].length] = archive["notes"][i]
}
var j = 0
for (var i = 0; i < all["notes"].length; i++) {
if (all["notes"][i].category === cat) {
res["notes"][j] = all["notes"][i]
j++
}
}
return res
}
function deleteArchive() {
mainView.database.putDoc({"notes" : []}, "archive")
notesListView.model = mainView.database.getDoc("archive").notes
}
function addCategory(name) {
var values = mainView.database.getDoc("categories")
values["categories"][values.categories.length] = name
mainView.database.putDoc(values, "categories")
}
function replaceCategory(index, name) {
var values = mainView.database.getDoc("categories")
values["categories"][index] = name
mainView.database.putDoc(values, "categories")
}
function removeCategory(index) {
var values = mainView.database.getDoc("categories")
var newValues = {"categories": []}
var j = 0
for (var i = 0; i < values.categories.length; i++) {
if (index !== i) {
newValues["categories"][j] = values["categories"][i]
j++
}
}
mainView.database.putDoc(newValues, "categories")
}
function addTag(old, newTag) {
var values = mainView.database.getDoc("tags")
if (old !== "") {
values["tags"][0] = values["tags"][1]
values["tags"][1] = values["tags"][2]
}
values["tags"][values["tags"].length] = newTag
mainView.database.putDoc(values, "tags")
}
}