forked from datokrat/X-Tree-M
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_data_paul.js
More file actions
392 lines (313 loc) · 12.4 KB
/
Copy pathlib_data_paul.js
File metadata and controls
392 lines (313 loc) · 12.4 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import {
c_LANG_UC_BROWSING_PANEL2_EVAL_CATS
} from "./uc_browsing_lang.js";
import {
CreateNodeCommand,
GetNodesCommand,
LinkCommand,
MoveCommand,
UnlinkCommand,
UpdateNodeDataCommand
} from "./common/api.js";
import {
Cache
} from "./common/cache.js";
import { Node } from "./common/objects.js";
// Bridge for Paul's approach to make a MySQL database accessible through PHP (alternative to local XML based
// approach)
//
// Usecases :
// - Import data from database
// - Navigate through tree
// - Track changes (GUI + local content storage + database)
//
// Example :
// http://datokrat.sirius.uberspace.de/xtreem/getNodeInfos.php?ids[]=1
// Object {
// .name: "Was n�tzt effektiv dem Weltfrieden?",
// .content: "Eine sehr schwierige Frage, die geeignete Werkzeuge braucht, um konstruktiv diskutiert zu werden.",
// .author: {�},
// .type: "general",
// .children: (1) [�],
// .parents: [] }
// http://datokrat.sirius.uberspace.de/xtreem/create_item.php?parentnodeid=1&authorid=1&name=Titel
// http://datokrat.sirius.uberspace.de/xtreem/changeItem.php?id=1&name=NeuerName&content=NeuerContent
export const c_EMPTY_EVAL_STRUCT = [];
for (var i=0; i < c_LANG_UC_BROWSING_PANEL2_EVAL_CATS.length-1; i++)
{
const item = {};
item.avg = 0.0;
item.num = 0;
c_EMPTY_EVAL_STRUCT.push(item);
}
// Class 'lib_data_paul'
export function lib_data_paul(data_src_path, data_src_params, defaultParentStorage, global_setup)
{
// import params
this.data_src_path = data_src_path;
this.data_src_params = data_src_params;
this.defaultParentStorage = defaultParentStorage;
this.data_root_id = data_src_params.root_item;
this.global_setup = global_setup; // don't change any of these setups since they're just copied by reference !!!
// tree functions
this.write_tree = lib_data_paul_write_tree.bind(this);
this.req_tree = lib_data_paul_req_tree.bind(this);
// item functions
this.get_multi_parents = lib_data_paul_get_multi_parents.bind(this);
this.req_all_parents = lib_data_paul_req_all_parents.bind(this);
this.get_all_parents = lib_data_paul_get_all_parents.bind(this);
this.item_exists = lib_data_paul_item_exists.bind(this);
this.create_tree_item = lib_data_paul_create_tree_item.bind(this);
this.delete_tree_item = lib_data_paul_delete_tree_item.bind(this);
this.copy_items = lib_data_paul_copy_items.bind(this);
this.clone_items = lib_data_paul_clone_items.bind(this);
this.move_items = lib_data_paul_move_items.bind(this);
this.change_link_type = lib_data_paul_change_link_type.bind(this);
// field functions
this.create_tree_item_field = lib_data_paul_create_tree_item_field.bind(this);
this.change_tree_item_field = lib_data_paul_change_tree_item_field.bind(this);
this.get_tree_item_field = lib_data_paul_get_tree_item_field.bind(this);
// private
this.generate_uri_from_command = lib_data_paul_generate_uri_from_command.bind(this);
// object variables
this.next_id = 0; // it's necessary to know which IDs can be created for new items
this.root_id = "1";
// variables
this.req_elem_ids = [];
this.is_deleted_arr = [];
this.parent_gui_id_arr = [ null ];
this.parent_elem_id_arr = [ null ];
this.req_tree_state = "rts_idle";
this.del_item_state = "di_idle";
this.rts_ret_struct = {}; // The cached tree structure from the last successfull req_tree execution
this.curr_item_parents = [];
this.move_item_state = "mis_idle";
this.eval_cat_num = c_LANG_UC_BROWSING_PANEL2_EVAL_CATS.length-1;
}
/**
* @param dataType the type of the commandObject, e.g. GetNodesCommand
* @param commandObject A GetNodesCommand or another object generated according to a dataspec
*/
function lib_data_paul_generate_uri_from_command(route, dataType, commandObject) {
const json = dataType.serialize(commandObject);
return this.data_src_path + route + "?command=" + encodeURIComponent(JSON.stringify(json));
}
//################################################################################################
//### Tree functions
//################################################################################################
// LEGACY
function lib_data_paul_write_tree(iparams)
{
alert("Paul : Write Tree not yet implemented");
}
/*
* Precondition: ids is a non-empty array of IDs.
*/
function lib_data_paul_req_tree({ids, depth})
{
const command = GetNodesCommand.create({ ids, depth });
const url = this.generate_uri_from_command("nodes/get", GetNodesCommand, command);
return $.get(url).then(result => Cache.create(result));
}
// LEGACY
function lib_data_paul_get_multi_parents(elem_id, my_parents)
{
var parent_from_storage = this.defaultParentStorage.read(elem_id);
if (parent_from_storage == undefined)
return my_parents[0];
else
return parent_from_storage;
}
// LEGACY
// find out whether or not an item exists
function lib_data_paul_item_exists(itemId)
{
alert("Paul : Item Exists not yet implemented");
}
// create new tree item
function lib_data_paul_create_tree_item({type, name, parent_elem_id, cb_success}) // iparams = {parent_elem_id, name, type, cb_success}
{
const command = CreateNodeCommand.create({
parentId: parent_elem_id,
nodeData: {
type: type,
title: name,
content: "",
},
})
const url = this.generate_uri_from_command("nodes/create", CreateNodeCommand, command);
$.get(url)
.done(raw_node => {
const node = Node.create(raw_node);
cb_success(node.id);
})
}
// delete links from parent to child
function lib_data_paul_delete_tree_item({links, cb_success}) // iparams = {links: {id, parent_id}, cb_success}
{
const command = UnlinkCommand.create({
links: links.map(({id, parent_id}) => ({sourceId: parent_id, targetId: id})),
});
const url = this.generate_uri_from_command("links/delete", UnlinkCommand, command);
$.get(url)
.done(cb_success);
}
// LEGACY
// get Item's parent nodes
function lib_data_paul_req_all_parents(iparams)
{
alert("Paul : Req All Parents not yet implemented");
}
// LEGACY
// get Item's parent nodes
function lib_data_paul_get_all_parents(itemId)
{
return this.curr_item_parents;
}
// cut&paste operations (later : for copy by reference)
function lib_data_paul_copy_items({src_ids, target_id, cb_success})
{
const copy_source_items = src_ids;
const copy_target_parent_id = target_id;
const command = LinkCommand.create({
links: copy_source_items.map(child_id => ({
sourceId: copy_target_parent_id,
targetId: child_id,
type: "general" // Use type "general" since only the user knows
// the type of these newly created links
})),
});
const url = this.generate_uri_from_command("links/add", LinkCommand, command)
$.get(url)
.done(cb_success);
}
// LEGACY, probably currently unused
function lib_data_paul_clone_items(iparams)
{
// create local copy of params
var iparams_cp = jQuery.extend(true, {}, iparams);
// URL example : .../cloneNode?id=[Kind-ID]&targetparentid=[ID, unter der der Knoten eingef�gt werden sollte]
if ((iparams_cp.src_elem[0].elem_id != undefined) && (iparams_cp.dst_elem.elem_id != undefined))
{
var post_params = "id=" + iparams_cp.src_elem[0].elem_id;
post_params = post_params + "&targetparentid=" + iparams_cp.dst_elem.elem_id;
$.post(this.data_src_path+"cloneNode?"+post_params, { })
.done(function(data) {
this.req_tree({elemId:[iparams_cp.dst_elem.elem_id], lock_id:iparams_cp.lock_id, favIds:[], tickerIds:[], cb_fct_call:iparams_cp.cb_fctn_str, mode:"tree_only"});
}.bind(this))
.fail(function()
{
// f_append_to_pad('div_panel4_pad','clone_item failed');
this.req_elem_ids = [];
this.req_tree_state = "rts_idle";
}
);
}
else
{
alert("clone_items : Incomplete Parameters");
}
}
// cut&paste operations (later : for copy by reference)
function lib_data_paul_move_items({sources, target_id: new_parent_id, cb_success}) // iparams = {sources: [{parent, child}], target_id, cb_success}
{
const command = MoveCommand.create({
oldLinks: sources.map(existing_link => ({
sourceId: existing_link.parent,
targetId: existing_link.child,
})),
newLinks: sources.map(existing_link => ({
sourceId: new_parent_id,
targetId: existing_link.child,
type: "general", // Use type "general" since only the user knows
// the type of these newly created links
})),
})
const url = this.generate_uri_from_command("links/move", MoveCommand, command);
$.get(url).done(cb_success);
}
// field functions
// LEGACY
// create fields of tree item
function lib_data_paul_create_tree_item_field(itemId, fieldId, content)
{
alert("Paul : Create Tree Item Field not yet implemented");
}
// change fields of tree item
function lib_data_paul_change_tree_item_field({elem_id, field_id, content, cb_success}) // iparams = {elem_id, field_id, content, cb_success}
{
// allow alternative field IDs
if (field_id === "name") {
field_id = "title";
} else if (field_id === "description") {
field_id = "content";
}
const command = UpdateNodeDataCommand.create({
id: elem_id,
title: null,
content: null,
[field_id]: content,
});
const url = this.generate_uri_from_command("nodes/update", UpdateNodeDataCommand, command);
$.get(url).done(cb_success);
}
// links: array of {parent_id, child_id, type}
function lib_data_paul_change_link_type({links, cb_success}) {
const command = LinkCommand.create({
links: links.map(({parent_id, child_id, type}) => ({
sourceId: parent_id,
targetId: child_id,
type: type,
})),
});
const url = this.generate_uri_from_command("links/add", LinkCommand, command);
$.get(url).done(cb_success);
}
// get field content
function lib_data_paul_get_tree_item_field(itemId, fieldId)
{
alert("Paul : Get Tree Item Field not yet implemented");
}
// LEGACY
//################################################################################################
//### Item functions
//################################################################################################
function ontology_xtm2disco_post(xtm_type)
{
switch (xtm_type)
{
case "topic" : return "1"; // "Topic";
case "fact" : return "5"; // "Information";
case "pro_arg" : return "2"; // "General";
case "con_arg" : return "2"; // "General";
case "question" : return "3"; // "Question";
case "problem" : return "7"; // to be defined : "Problem";
case "idea" : return "4"; // "Proposal";
case "aim" : return "8"; // to be defined : "Goal";
case "region" : return "9"; // to be defined : "Region";
default : return "2"; // "General";
}
}
function ontology_xtm2disco_postref(xtm_type)
{
switch (xtm_type)
{
case "topic" : return "1"; // "Child";
case "fact" : return "5"; // "Evidence";
case "pro_arg" : return "7"; // "Agreement";
case "con_arg" : return "8"; // "Disagreement";
case "question" : return "2"; // "General";
case "problem" : return "2"; // "General";
case "idea" : return "2"; // "General";
case "aim" : return "2"; // "General";
case "region" : return "2"; // "General";
default : return "2"; // "General";
}
}
// Bsp. f�r Peer-to-Perr :
// ========================
// Freenet
// Media Goblin
// https://de.wikipedia.org/wiki/Gnutella
// https://de.wikipedia.org/wiki/EDonkey2000
// https://de.wikipedia.org/wiki/FastTrack