-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleSelectTreeView.js
More file actions
350 lines (306 loc) · 16.2 KB
/
Copy pathsingleSelectTreeView.js
File metadata and controls
350 lines (306 loc) · 16.2 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
var initDivSt = document.getElementsByClassName('singleTreeView');
if (initDivSt.length) {
const root_urlSt = jQuery(initDivSt).find('.root_url')[0].value;
define(['jquery', root_urlSt + '/plugins/fabrik_element/databasejoin/tree.jquery.js'], function ($) {
singleSelectTreeView();
function singleSelectTreeView() {
[].forEach.call(document.getElementsByClassName('singleTreeView'), function (element) {
const mainDiv = element;
const nameElement = $(element).find('.attribute_name')[0].value;
var selectedCheckbox = $(mainDiv).find('.selected-checkbox-' + nameElement)[0];
const join_name = $(mainDiv).find('.join_name-' + nameElement)[0].value;
const elementTreeId = String('#' + 'tree_' + join_name + '_single');
var el = $(mainDiv).find(elementTreeId)[0];
const table_name = $(mainDiv).find('.table_name-' + nameElement)[0].value;
const attribute_name = $(mainDiv).find('.attribute_name')[0].value;
const join_val_column = $(mainDiv).find('.join_val_column-' + nameElement)[0].value;
const join_key_column = $(mainDiv).find('.join_key_column-' + nameElement)[0].value;
const tree_parent_id = $(mainDiv).find('.tree_parent_id-' + nameElement)[0].value;
const filter_sortedby = $(mainDiv).find('.filter_sortedby-' + nameElement)[0].value;
const elName = $(mainDiv).find('.el_name-' + nameElement)[0].value;
//If the user set the data-WHERE propriety in the administrator page
const dataWhereInput = $(mainDiv).find('.data_where-' + nameElement);
const data_where = dataWhereInput[0] ? JSON.parse(dataWhereInput[0].value) : '';
const selfRelationship = $(mainDiv).find('.self-relationship-' + nameElement)[0].value;
const rootCategory = $(mainDiv).find('.category_root-' + nameElement)[0].value;
const concatInput = $(mainDiv).find('.concat-' + nameElement);
const concat_val = concatInput[0] ? JSON.parse(concatInput[0].value) : '';
let tags = [];
const databasejoin_linked_items = $(mainDiv).find('.databasejoin_linked_items-' + nameElement)[0].value;
const form = $(mainDiv).closest('.fabrikForm');
loadTags(selectedCheckbox);
var cssId = 'tagsCss'; // you could encode the css path itself to generate id..
if (!document.getElementById(cssId)) {
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = cssId;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = root_urlSt + 'plugins/fabrik_element/databasejoin/tags.css';
link.media = 'all';
head.appendChild(link);
}
var cssId = 'jqtree'; // you could encode the css path itself to generate id..
if (!document.getElementById(cssId)) {
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = cssId;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = root_urlSt + 'plugins/fabrik_element/databasejoin/jqtree.css';
link.media = 'all';
head.appendChild(link);
}
// Build the tree making an AJAX request getting only the root nodes
$.ajax({
url: '',
data: {
option: 'com_fabrik',
format: 'raw',
task: 'plugin.pluginAjax',
g: 'element',
plugin: 'databasejoin',
method: 'treeViewSearch',
value: null,
join_name: join_name,
join_val_column: join_val_column,
join_key_column: join_key_column,
tree_parent_id: tree_parent_id,
filter_sortedby: filter_sortedby,
data_where: data_where,
concat_val: concat_val,
rootCategory: rootCategory
},
success: function (result) {
var res = result;
var dataTreeFormatted = Array();
dataTreeFormatted = validateDataTree(res);
dataTreeFormatted.forEach(node => {
node.children = [{}];
});
buscaSelecionados(function (result2) {
$(element).parent().find(elementTreeId).tree({
data: dataTreeFormatted,
selectable: false,
onCreateLi: function (node, $li) {
if (Boolean(Number(databasejoin_linked_items))) {
spanElement = $li[0].firstChild.lastElementChild;
result2.forEach(value => {
if (value.id == node.id) {
spanElement.setAttribute("style", "color: #A9A9A9; font-style:italic;");
return;
}
})
}
}
});
})
},
dataType: "json"
});
var parentDiv = jQuery(mainDiv).parent()[0];
var btnRefreshTree = parentDiv ? jQuery(parentDiv).parent().find(".refreshTree")[0] : null;
//add a refresh in the tree when there is a refresh button in the form
if (btnRefreshTree) {
btnRefreshTree.addEventListener("click", function (e) {
var tree = $(element).parent().find(elementTreeId).tree('getTree');
//fechar todos os nós da árvore primeiro
tree.iterate(function(node) {
if (node.hasChildren()) {
$(element).parent().find(elementTreeId).tree('closeNode', node, true);
}
});
// Build the tree making an AJAX request getting only the root nodes
$.ajax({
url: '',
data: {
option: 'com_fabrik',
format: 'raw',
task: 'plugin.pluginAjax',
g: 'element',
plugin: 'databasejoin',
method: 'treeViewSearch',
value: null,
join_name: join_name,
join_val_column: join_val_column,
join_key_column: join_key_column,
tree_parent_id: tree_parent_id,
filter_sortedby: filter_sortedby,
data_where: data_where,
concat_val: concat_val,
format: 'raw'
},
success: function (result) {
var res = result;
var dataTreeFormatted = Array();
dataTreeFormatted = validateDataTree(res);
dataTreeFormatted.forEach(node => {
if (node.children) {
node.children = [{}];
}
});
$(element).parent().find(elementTreeId).tree('loadData', dataTreeFormatted);
},
dataType: "json"
});
});
}
// Make an AJAX request when open a tree branch
$(element).parent().find(elementTreeId).off('tree.open').on(
'tree.open',
function (e) {
var id = parseInt(e.node.id);
$.ajax({
url: '',
data: {
option: 'com_fabrik',
format: 'raw',
task: 'plugin.pluginAjax',
g: 'element',
plugin: 'databasejoin',
method: 'treeViewSearch',
value: id,
join_name: join_name,
join_val_column: join_val_column,
join_key_column: join_key_column,
tree_parent_id: tree_parent_id,
filter_sortedby: filter_sortedby,
data_where: data_where,
concat_val: concat_val,
format: 'raw'
},
success: function (result) {
var res = result;
var dataTreeFormatted = Array();
dataTreeFormatted = validateDataTree(res);
dataTreeFormatted.forEach(node => {
if (node.children) {
node.children = [{}];
}
});
$(element).parent().find(elementTreeId).tree('loadData', dataTreeFormatted, e.node);
},
dataType: "json"
});
}
);
function buscaSelecionados(handleData) {
$.ajax({
url: root_urlSt + 'plugins/fabrik_element/databasejoin/selectedNodesForSingleTreeview.php',
data: {
attribute_name: attribute_name,
table_name: table_name,
},
success: function (result) {
handleData(result);
},
dataType: "json"
});
}
function addTag(text, id, replace) {
var tag = {
id: id,
text: text,
container: document.createElement('div'),
content: document.createElement('span'),
input: document.createElement('input'),
closeButton: document.createElement('span')
};
tag.container.classList.add('tag-container');
tag.content.classList.add('tag-content');
tag.closeButton.classList.add('tag-close-button');
tag.input.value = id;
tag.input.setAttribute('type', 'checkbox');
tag.input.setAttribute('style', 'display: none');
tag.input.setAttribute('checked', 'checked');
tag.input.setAttribute('name', elName);
tag.input.setAttribute('hidden', true);
selectedCheckbox.appendChild(tag.input);
tag.content.textContent = tag.text;
tag.closeButton.textContent = 'x';
tag.closeButton.addEventListener('click', function () {
removeTag(tags.indexOf(tag));
});
tag.container.appendChild(tag.content);
tag.container.appendChild(tag.closeButton);
removeTag(0);
tags[0] = tag;
mainDiv.insertBefore(tag.container, el);
}
function removeTag(index) {
var tag = tags[index];
if (tag) {
tags.splice(index, 1);
mainDiv.removeChild(tag.container);
selectedCheckbox.removeChild(tag.input);
}
}
// Load the selected options to tags
function loadTags(selectOptions) {
var option = selectOptions.firstElementChild;
if (option) {
var tag = {
id: option.getAttribute("value"),
text: option.getAttribute("data"),
container: document.createElement('div'),
content: document.createElement('span'),
input: option,
closeButton: document.createElement('span')
};
tag.container.classList.add('tag-container');
tag.content.classList.add('tag-content');
tag.closeButton.classList.add('tag-close-button');
tag.content.textContent = tag.text;
tag.closeButton.textContent = 'x';
tag.closeButton.addEventListener('click', function () {
removeTag(tags.indexOf(tag));
});
tag.container.appendChild(tag.content);
tag.container.appendChild(tag.closeButton);
tags[0] = tag;
mainDiv.insertBefore(tag.container, el);
}
}
function validateDataTree(dataTree) {
var dataTreeFormatted = Array();
// If the element has a self-relationship, we need to remove the current row from the tree
if(selfRelationship) {
dataTree.forEach((node) => {
if(node.id != form.find("input[name='rowid']").val()) {
dataTreeFormatted.push(node);
}
});
}
return dataTreeFormatted.length ? dataTreeFormatted : dataTree;
}
// On click on a node adds it to tags
$(element).parent().find(elementTreeId).off('tree.click').on(
'tree.click',
function (event) {
var node = event.node;
if (Boolean(Number(databasejoin_linked_items))) {
buscaSelecionados(function (result2) {
var found = false;
result2.forEach(value => {
if (node.id == value.id) {
found = true;
alert('This node has already been selected.');
return;
}
})
if (!found) {
addTag(node.name, node.id, true);
}
});
} else {
addTag(node.name, node.id, true);
}
}
);
})
}
return {
singleSelectTreeView: singleSelectTreeView
}
});
}