forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigation.js
389 lines (362 loc) · 12.2 KB
/
navigation.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
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
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains classes used to manage the navigation structures in Moodle
* and was introduced as part of the changes occuring in Moodle 2.0
*
* @since 2.0
* @package javascript
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* This namespace will contain all of the contents of the navigation blocks
* global navigation and settings.
* @namespace
*/
M.block_navigation = M.block_navigation || {
/** The number of expandable branches in existence */
expandablebranchcount:0,
/** An array of initialised trees */
treecollection:[],
/**
* Will contain all of the classes for the navigation blocks
* @namespace
*/
classes:{},
courselimit : 20,
/**
* This function gets called when the module is first loaded as required by
* the YUI.add statement at the bottom of the page.
*
* NOTE: This will only be executed ONCE
* @function
*/
init:function(Y) {
M.core_dock.init(Y);
if (M.core_dock.genericblock) {
// Give the tree class the dock block properties
Y.augment(M.block_navigation.classes.tree, M.core_dock.genericblock);
}
},
/**
* Add new instance of navigation tree to tree collection
*/
init_add_tree:function(Y, id, properties) {
if (properties.courselimit) {
this.courselimit = properties.courselimit;
}
M.block_navigation.treecollection[id] = new M.block_navigation.classes.tree(Y, id, properties);
}
};
/**
* @class tree
* @constructor
* @base M.core_dock.genericblock
* @param {YUI} Y A yui instance to use with the navigation
* @param {string} id The name of the tree
* @param {object} properties Object containing tree properties
*/
M.block_navigation.classes.tree = function(Y, id, properties) {
this.Y = Y;
this.id = id;
this.key = id;
this.errorlog = [];
this.ajaxbranches = 0;
this.expansions = [];
this.instance = id;
this.cachedcontentnode = null;
this.cachedfooter = null;
this.position = 'block';
this.skipsetposition = false;
this.candock = false;
if (properties.expansions) {
this.expansions = properties.expansions;
}
if (properties.instance) {
this.instance = properties.instance;
}
if (properties.candock) {
this.candock = true;
}
var node = this.Y.one('#inst'+this.id);
// Can't find the block instance within the page
if (node === null) {
return;
}
// Attach event to toggle expansion
node.all('.tree_item.branch').on('click', this.toggleexpansion , this);
// Attach events to expand by AJAX
//var expandablenode;
for (var i in this.expansions) {
var expandablenode = Y.one('#'+this.expansions[i].id);
if (expandablenode) {
expandablenode.on('ajaxload|click', this.init_load_ajax, this, this.expansions[i]);
M.block_navigation.expandablebranchcount++;
} else if (M.cfg.debug) {
Y.one(document.body).append(Y.Node.create('<div class="notification" style="font-size:6pt;">Expandable node within navigation was missing [#'+this.expansions[i].id+']</div>'));
} else {
// Failing over silently
}
}
if (node.hasClass('block_js_expansion')) {
node.on('mouseover', function(e){this.toggleClass('mouseover');}, node);
node.on('mouseout', function(e){this.toggleClass('mouseover');}, node);
}
// Call the generic blocks init method to add all the generic stuff
if (this.candock) {
this.init(Y, node);
}
};
/**
* Loads a branch via AJAX
* @param {event} e The event object
* @param {object} branch A branch to load via ajax
*/
M.block_navigation.classes.tree.prototype.init_load_ajax = function(e, branch) {
e.stopPropagation();
if (e.target.get('nodeName').toUpperCase() != 'P') {
return true;
}
var cfginstance = '', Y = this.Y;
if (this.instance != null) {
cfginstance = '&instance='+this.instance
}
Y.io(M.cfg.wwwroot+'/lib/ajax/getnavbranch.php', {
method:'POST',
data:'elementid='+branch.id+'&id='+branch.branchid+'&type='+branch.type+'&sesskey='+M.cfg.sesskey+cfginstance,
on: {
complete:this.load_ajax,
success:function() {Y.detach('click', this.init_load_ajax, e.target);}
},
context:this,
arguments:{
target:e.target
}
});
return true;
};
/**
* Takes an branch provided through ajax and loads it into the tree
* @param {int} tid The transaction id
* @param {object} outcome
* @param {mixed} args
* @return bool
*/
M.block_navigation.classes.tree.prototype.load_ajax = function(tid, outcome, args) {
try {
var object = this.Y.JSON.parse(outcome.responseText);
if (this.add_branch(object, args.target.ancestor('li') ,1)) {
if (this.candock) {
M.core_dock.resize();
}
return true;
}
} catch (e) {
// If we got here then there was an error parsing the result
}
// The branch is empty so class it accordingly
args.target.replaceClass('branch', 'emptybranch');
return true;
};
/**
* Adds a branch into the tree provided with some XML
* @param {object} branchobj
* @param {Y.Node} target
* @param {int} depth
* @return bool
*/
M.block_navigation.classes.tree.prototype.add_branch = function(branchobj, target, depth) {
// Make the new branch into an object
var branch = new M.block_navigation.classes.branch(this, branchobj);
var childrenul = false, Y = this.Y;
if (depth === 1) {
if (!branch.children) {
return false;
}
childrenul = Y.Node.create('<ul></ul>');
target.appendChild(childrenul);
} else {
childrenul = branch.inject_into_dom(target);
}
if (childrenul) {
var count = 0;
for (var i in branch.children) {
// Add each branch to the tree
if (branch.children[i].type == 20) {
count++;
}
if (typeof(branch.children[i])=='object') {
this.add_branch(branch.children[i], childrenul, depth+1);
}
}
if (branch.type == 10 && count >= M.block_navigation.courselimit) {
var properties = Array();
properties['name'] = M.str.moodle.viewallcourses;
properties['title'] = M.str.moodle.viewallcourses;
properties['link'] = M.cfg.wwwroot+'/course/category.php?id='+branch.key;
properties['haschildren'] = false;
properties['icon'] = {'pix':"i/navigationitem",'component':'moodle'};
this.add_branch(properties, childrenul, depth+1);
}
}
return true;
};
/**
* Toggle a branch as expanded or collapsed
* @param {Event} e
*/
M.block_navigation.classes.tree.prototype.toggleexpansion = function(e) {
// First check if they managed to click on the li iteslf, then find the closest
// LI ancestor and use that
if (e.target.get('nodeName').toUpperCase() == 'A') {
// A link has been clicked don't fire any more events just do the default.
e.stopPropagation();
return;
}
var target = e.target;
if (!target.test('li')) {
target = target.ancestor('li')
}
if (target && !target.hasClass('depth_1')) {
target.toggleClass('collapsed');
}
if (this.candock) {
M.core_dock.resize();
}
};
/**
* This class represents a branch for a tree
* @class branch
* @constructor
* @param {M.block_navigation.classes.tree} tree
* @param {object|null} obj
*/
M.block_navigation.classes.branch = function(tree, obj) {
this.tree = tree;
this.name = null;
this.title = null;
this.classname = null;
this.id = null;
this.key = null;
this.type = null;
this.link = null;
this.icon = null;
this.expandable = null;
this.expansionceiling = null;
this.hidden = false;
this.haschildren = false;
this.children = false;
if (obj !== null) {
// Construct from the provided xml
this.construct_from_json(obj);
}
};
/**
* Populates this branch from a JSON object
* @param {object} obj
*/
M.block_navigation.classes.branch.prototype.construct_from_json = function(obj) {
for (var i in obj) {
this[i] = obj[i];
}
if (this.children && this.children.length > 0) {
this.haschildren = true;
} else {
this.children = [];
}
if (this.id && this.id.match(/^expandable_branch_\d+$/)) {
// Assign a new unique id for this new expandable branch
M.block_navigation.expandablebranchcount++;
this.id = 'expandable_branch_'+M.block_navigation.expandablebranchcount;
}
};
/**
* Injects a branch into the tree at the given location
* @param {element} element
*/
M.block_navigation.classes.branch.prototype.inject_into_dom = function(element) {
var Y = this.tree.Y;
var isbranch = ((this.expandable !== null || this.haschildren) && this.expansionceiling===null);
var branchli = Y.Node.create('<li></li>');
var branchp = Y.Node.create('<p class="tree_item"></p>');
if (isbranch) {
branchli.addClass('collapsed');
branchli.addClass('contains_branch');
branchp.addClass('branch');
branchp.on('click', this.tree.toggleexpansion, this.tree);
if (this.expandable) {
branchp.on('ajaxload|click', this.tree.init_load_ajax, this.tree, {branchid:this.key,id:this.id,type:this.type});
}
}
if (this.myclass !== null) {
branchp.addClass(this.myclass);
}
if (this.id !== null) {
branchp.setAttribute('id', this.id);
}
// Prepare the icon, should be an object representing a pix_icon
var branchicon = false;
if (this.icon != null && (!isbranch || this.type == 40)) {
branchicon = Y.Node.create('<img alt="" />');
branchicon.setAttribute('src', M.util.image_url(this.icon.pix, this.icon.component));
branchli.addClass('item_with_icon');
if (this.icon.alt) {
branchicon.setAttribute('alt', this.icon.alt);
}
if (this.icon.title) {
branchicon.setAttribute('alt', this.icon.title);
}
if (this.icon.classes) {
for (var i in this.icon.classes) {
branchicon.addClass(this.icon.classes[i]);
}
}
}
if (this.link === null) {
if (branchicon) {
branchp.appendChild(branchicon);
}
branchp.append(this.name.replace(/\n/g, '<br />'));
} else {
var branchlink = Y.Node.create('<a title="'+this.title+'" href="'+this.link+'"></a>');
if (branchicon) {
branchlink.appendChild(branchicon);
}
branchlink.append(this.name.replace(/\n/g, '<br />'));
if (this.hidden) {
branchlink.addClass('dimmed');
}
branchp.appendChild(branchlink);
}
branchli.appendChild(branchp);
if (this.haschildren) {
var childrenul = Y.Node.create('<ul></ul>');
branchli.appendChild(childrenul);
element.appendChild(branchli);
return childrenul
} else {
element.appendChild(branchli);
return false;
}
};
/**
* Causes the navigation block module to initalise the first time the module
* is used!
*
* NOTE: Never convert the second argument to a function reference...
* doing so causes scoping issues
*/
YUI.add('block_navigation', function(Y){M.block_navigation.init(Y);}, '0.0.0.1', M.yui.loader.modules.block_navigation.requires);