-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodule.js
executable file
·115 lines (87 loc) · 3.78 KB
/
module.js
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
M.block_admin_presets = {
tree: null,
nodes: null,
/**
* Initializes the TreeView object and adds the submit listener
*/
init: function (Y) {
Y.use('yui2-treeview', function (Y) {
var context = M.block_admin_presets;
context.tree = new Y.YUI2.widget.TreeView("settings_tree_div");
context.nodes = [];
context.nodes.root = context.tree.getRoot();
});
},
/**
* Creates a tree branch
*/
addNodes: function (Y, ids, nodeids, labels, descriptions, parents) {
var context = M.block_admin_presets;
var nelements = ids.length;
for (var i = 0; i < nelements; i++) {
var settingId = ids[i];
var nodeId = nodeids[i];
var label = decodeURIComponent(labels[i]);
var description = decodeURIComponent(descriptions[i]);
var parent = parents[i];
var newNode = new Y.YUI2.widget.HTMLNode(label, context.nodes[parent]);
newNode.settingId = settingId;
newNode.setNodesProperty('title', description);
newNode.highlightState = 1;
context.nodes[nodeId] = newNode;
}
},
render: function (Y) {
var context = M.block_admin_presets;
var categories = context.tree.getNodesByProperty('settingId', 'category');
// Cleaning categories without children.
if (categories) {
for (var i = 0; i < categories.length; i++) {
if (!categories[i].hasChildren()) {
context.tree.popNode(categories[i]);
}
}
}
categories = context.tree.getRoot().children;
if (categories) {
for (var j = 0; j < categories.length; j++) {
if (!categories[j].hasChildren()) {
context.tree.popNode(categories[j]);
}
}
}
// Context.tree.expandAll();.
context.tree.setNodesProperty('propagateHighlightUp', true);
context.tree.setNodesProperty('propagateHighlightDown', true);
context.tree.subscribe('clickEvent', context.tree.onEventToggleHighlight);
context.tree.render();
// Listener to create one node for each selected setting.
Y.YUI2.util.Event.on('id_admin_presets_submit', 'click', function () {
// We need the moodle form to add the checked settings.
var settingsPresetsForm = document.getElementById('id_admin_presets_submit').parentNode;
var hiLit = context.tree.getNodesByProperty('highlightState', 1);
if (Y.YUI2.lang.isNull(hiLit)) {
Y.YUI2.log("Nothing selected");
} else {
// Only for debugging.
var labels = [];
for (var i = 0; i < hiLit.length; i++) {
var treeNode = hiLit[i];
// Only settings not setting categories nor settings pages.
if (treeNode.settingId !== 'category' && treeNode.settingId !== 'page') {
labels.push(treeNode.settingId);
// If the node does not exists we add it.
if (!document.getElementById(treeNode.settingId)) {
var settingInput = document.createElement('input');
settingInput.setAttribute('type', 'hidden');
settingInput.setAttribute('name', treeNode.settingId);
settingInput.setAttribute('value', '1');
settingsPresetsForm.appendChild(settingInput);
}
}
}
Y.YUI2.log("Checked settings:\n" + labels.join("\n"), "info");
}
});
}
};