MDL-31903 course: update patch to fix course highlight icon and title toggle
[moodle.git] / lib / ajax / section_classes.js
CommitLineData
e1c15ef7 1/**
2 * library for ajaxcourse formats, the classes and related functions for
3 * sections and resources.
3e1e2b69 4 *
e1c15ef7 5 * This library requires a 'main' object created in calling document.
3e1e2b69 6 *
7f933d8f 7 * Drag and drop notes:
8 *
9 * Dropping an activity or resource on a section will always add the activity
10 * or resource at the end of that section.
11 *
12 * Dropping an activity or resource on another activity or resource will
13 * always move the former just above the latter.
0a0bb380 14 */
354e1130 15
16
e1c15ef7 17/**
18 * section_class
19 */
20function section_class(id, group, config, isDraggable) {
21 this.init_section(id, group, config, isDraggable);
0a0bb380 22}
e1c15ef7 23
0a0bb380 24YAHOO.extend(section_class, YAHOO.util.DDProxy);
25
db5308e2 26
9569653d 27section_class.prototype.debug = false;
d4df8fdc 28
db5308e2 29
e1c15ef7 30section_class.prototype.init_section = function(id, group, config, isDraggable) {
31
72d28452 32 if (!id) {
33 return;
34 }
354e1130 35
db5308e2 36 this.is = 'section';
72d28452 37 this.sectionId = null; // Section number. This is NOT the section id from
3203f104 38 // the database.
354e1130 39
3203f104 40 if (!isDraggable) {
e1c15ef7 41 this.initTarget(id, group, config);
354e1130 42 this.removeFromGroup('sections');
e1c15ef7 43 } else {
44 this.init(id, group, config);
45 this.handle = null;
0a0bb380 46 }
354e1130 47
64e5a68d 48 this.createFrame();
354e1130 49 this.isTarget = true;
50
51 this.resources = [];
72d28452 52 this.numberDisplay = null; // Used to display the section number on the top left
3203f104 53 // of the section. Not used in all course formats.
354e1130 54 this.summary = null;
1ce18c08 55 this.content_div = null;
354e1130 56 this.hidden = false;
57 this.highlighted = false;
58 this.showOnly = false;
db5308e2 59 this.resources_ul = null;
354e1130 60 this.process_section();
61
62 this.viewButton = null;
63 this.highlightButton = null;
64 this.showOnlyButton = null;
65 this.init_buttons();
66
e1c15ef7 67 if (isDraggable) {
72d28452 68 this.add_handle();
69 }
e1c15ef7 70 if (this.debug) {
72d28452 71 YAHOO.log("init_section "+id+" draggable="+isDraggable);
72 }
e1c15ef7 73 if (YAHOO.util.Dom.hasClass(this.getEl(),'hidden')) {
354e1130 74 this.toggle_hide(null,null,true);
72d28452 75 }
b532c911 76};
354e1130 77
a66ddf19 78
3440ec12 79section_class.prototype.init_buttons = function() {
e8b32e2b
PS
80 if (this.sectionId > main.portal.numsections) {
81 // no need to do anything in orphaned sections
82 return;
83 }
84
1ce18c08 85 var commandContainer = YAHOO.util.Dom.getElementsByClassName('right',null,this.getEl())[0];
354e1130 86
6f00683e 87 //clear all but show only button
354e1130 88 var commandContainerCount = commandContainer.childNodes.length;
d2a11d46 89
e1c15ef7 90 for (var i=(commandContainerCount-1); i>0; i--) {
354e1130 91 commandContainer.removeChild(commandContainer.childNodes[i])
0a0bb380 92 }
93
ddc66060 94 if (main.getString('courseformat', this.sectionId) != "weeks" && this.sectionId > 0) {
f7538871 95 var highlightbutton = main.mk_button('div', main.portal.icons['marker'], main.getString('marker', this.sectionId));
e1c15ef7 96 YAHOO.util.Event.addListener(highlightbutton, 'click', this.mk_marker, this, true);
3440ec12 97 commandContainer.appendChild(highlightbutton);
f8eaeffa 98 this.highlightButton = highlightbutton;
5884a8e6 99 }
e8b32e2b 100 if (this.sectionId > 0) {
ddc66060 101 var viewbutton = main.mk_button('div', main.portal.icons['hide'], main.getString('hidesection', this.sectionId),
f7538871 102 [['title', main.portal.strings['hide'] ]]);
ddc66060 103 YAHOO.util.Event.addListener(viewbutton, 'click', this.toggle_hide, this,true);
104 commandContainer.appendChild(viewbutton);
105 this.viewButton = viewbutton;
354e1130 106 }
b532c911 107};
354e1130 108
a66ddf19 109
354e1130 110section_class.prototype.add_handle = function() {
ddedf979 111 var handleRef = main.mk_button('a', main.portal.icons['move_2d'], main.getString('movesection', this.sectionId),
f8eaeffa 112 [['title', main.portal.strings['move'] ], ['style','cursor:move']]);
d2a11d46 113
72d28452 114 YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
354e1130 115
116 this.handle = handleRef;
117
118 this.getEl().childNodes[0].appendChild(handleRef);
119 this.setHandleElId(this.handle.id);
b532c911 120};
eba88175 121
a66ddf19 122
354e1130 123section_class.prototype.process_section = function() {
1ce18c08 124 this.content_div = YAHOO.util.Dom.getElementsByClassName('content',null,this.getEl())[0];
354e1130 125
126 if (YAHOO.util.Dom.hasClass(this.getEl(),'current')) {
127 this.highlighted = true;
128 main.marker = this;
0a0bb380 129 }
eba88175 130
a66ddf19 131 // Create holder for display number for access later
354e1130 132
133 this.numberDisplay = document.createElement('div');
134 this.numberDisplay.innerHTML = this.getEl().childNodes[0].innerHTML;
135 this.getEl().childNodes[0].innerHTML = '';
3440ec12 136 this.getEl().childNodes[0].appendChild(this.numberDisplay);
354e1130 137
72d28452 138 this.sectionId = this.id.replace(/section-/i, ''); // Okay, we will have to change this if we
139 // ever change the id attributes format
140 // for the sections.
a66ddf19 141 if (this.debug) {
72d28452 142 YAHOO.log("Creating section "+this.getEl().id+" in position "+this.sectionId);
143 }
144
a66ddf19 145 // Find/edit resources
1ce18c08 146 this.resources_ul = this.content_div.getElementsByTagName('ul')[0];
b2b204b7
RW
147 var i=0;
148 while (this.resources_ul && this.resources_ul.className != 'section img-text') {
149 i++;
150 this.resources_ul = this.content_div.getElementsByTagName('ul')[i]; i++;
151 }
6f00683e 152 if (!this.resources_ul) {
354e1130 153 this.resources_ul = document.createElement('ul');
154 this.resources_ul.className='section';
1ce18c08 155 this.content_div.insertBefore(this.resources_ul, this.content_div.lastChild);
d4df8fdc 156 }
354e1130 157 var resource_count = this.resources_ul.getElementsByTagName('li').length;
158
159 for (var i=0;i<resource_count;i++) {
160 var resource = this.resources_ul.getElementsByTagName('li')[i];
72d28452 161 this.resources[this.resources.length] = new resource_class(resource.id, 'resources', null, this);
7f933d8f 162 }
e8b32e2b
PS
163
164 var sum = YAHOO.util.Dom.getElementsByClassName('summary', null, this.getEl());
165 if (sum[0]) {
166 this.summary = sum[0].firstChild.data || '';
167 } else {
168 // orphaned activities
169 this.summary = null;
170 }
b532c911 171};
354e1130 172
a66ddf19 173
d2a11d46 174section_class.prototype.startDrag = function(x, y) {
0a0bb380 175 //operates in point mode
176 YAHOO.util.DDM.mode = YAHOO.util.DDM.POINT;
354e1130 177
0a0bb380 178 //remove from resources group temporarily
179 this.removeFromGroup('resources');
354e1130 180
0a0bb380 181 //reinitialize dd element
182 this.getDragEl().innerHTML = '';
0a0bb380 183
354e1130 184 var targets = YAHOO.util.DDM.getRelated(this, true);
a66ddf19 185
72d28452 186 if (this.debug) {
187 YAHOO.log(this.id + " startDrag, "+targets.length + " targets");
188 }
b532c911 189};
0a0bb380 190
a66ddf19 191
5884a8e6 192section_class.prototype.onDragDrop = function(e, id) {
0a0bb380 193 // get the drag and drop object that was targeted
194 var target = YAHOO.util.DDM.getDDById(id);
195
6f00683e 196 if (this.debug) {
72d28452 197 YAHOO.log("Section dropped on id="+id+" (I am "+this.getEl().id+") x="
198 +YAHOO.util.Dom.getXY(this.getDragEl()));
199 }
0a0bb380 200 this.move_to_section(target);
354e1130 201
202 //add back to resources group
3203f104 203 this.addToGroup('resources');
b532c911 204};
a66ddf19 205
206
354e1130 207section_class.prototype.endDrag = function() {
208 //nessicary to defeat default action
209
210 //add back to resources group
0a0bb380 211 this.addToGroup('resources');
b532c911 212};
354e1130 213
a66ddf19 214
354e1130 215section_class.prototype.move_to_section = function(target) {
1ce18c08 216 var tempDiv = document.createElement('div');
354e1130 217 var tempStore = null;
218 var sectionCount = main.sections.length;
219 var found = null;
220
221 //determine if original is above or below target and adjust loop
6f00683e 222 var oIndex = main.get_section_index(this);
223 var tIndex = main.get_section_index(target);
354e1130 224
e8b32e2b
PS
225 if (oIndex == -1) {
226 // source must exist
227 return;
228 }
229 if (tIndex == -1) {
230 // target must exist
231 return;
232 }
6f00683e 233 if (this.debug) {
72d28452 234 YAHOO.log("original is at: "+oIndex+" target is at:"+tIndex+" of "+(sectionCount-1));
235 }
354e1130 236 if (oIndex < tIndex) {
237 var loopCondition = 'i<sectionCount';
238 var loopStart = 1;
239 var loopInc = 'i++';
3440ec12 240 var loopmodifier = 'i - 1';
241 var targetOffset = 0;
354e1130 242 } else {
6f00683e 243 var loopCondition = 'i > 0';
3440ec12 244 var loopStart = sectionCount - 1;
245 var loopInc = 'i--';
246 var loopmodifier = 'i + 1';
247 var targetOffset = 1;
0a0bb380 248 }
354e1130 249
250 //move on backend
3440ec12 251 main.connect('POST','class=section&field=move',null,'id='+this.sectionId+'&value=' + (target.sectionId - targetOffset));
354e1130 252
253 //move on front end
db5308e2 254 for (var i=loopStart; eval(loopCondition); eval(loopInc)) {
354e1130 255
5884a8e6 256 if ((main.sections[i] == this) && !found) {
ca255392 257 //encounter with original node
6f00683e 258 if (this.debug) {
72d28452 259 YAHOO.log("Found Original "+main.sections[i].getEl().id);
260 }
354e1130 261 if (main.sections[i] == this) {
db5308e2 262 found = true;
354e1130 263 }
354e1130 264 } else if (main.sections[i] == target) {
265 //encounter with target node
6f00683e 266 if (this.debug) {
472a4b92 267 YAHOO.log("Found target "+main.sections[i].getEl().id);
268 }
354e1130 269 main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]);
5884a8e6 270 main.sections[i].swap_dates(main.sections[eval(loopmodifier)]);
354e1130 271 found = false;
272 break;
354e1130 273 } else if (found) {
db5308e2 274 //encounter with nodes inbetween
354e1130 275 main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]);
8c3c518f 276 main.sections[i].swap_dates(main.sections[eval(loopmodifier)]);
db5308e2 277 }
278 }
b532c911 279};
354e1130 280
a66ddf19 281
3440ec12 282section_class.prototype.swap_with_section = function(sectionIn) {
354e1130 283 var tmpStore = null;
284
e8b32e2b
PS
285 var thisIndex = main.get_section_index(this);
286 var targetIndex = main.get_section_index(sectionIn);
287 if (thisIndex == -1) {
288 // source must exist
289 return;
290 }
291 if (targetIndex == -1) {
292 // target must exist
293 return;
294 }
295
354e1130 296 main.sections[targetIndex] = this;
297 main.sections[thisIndex] = sectionIn;
298
299 this.changeId(targetIndex);
300 sectionIn.changeId(thisIndex);
301
6f00683e 302 if (this.debug) {
72d28452 303 YAHOO.log("Swapping "+this.getEl().id+" with "+sectionIn.getEl().id);
304 }
305 // Swap the sections.
db5308e2 306 YAHOO.util.DDM.swapNode(this.getEl(), sectionIn.getEl());
53a4443b 307
72d28452 308 // Sections contain forms to add new resources/activities. These forms
309 // have not been updated to reflect the new positions of the sections that
310 // we have swapped. Let's swap the two sections' forms around.
311 if (this.getEl().getElementsByTagName('form')[0].parentNode
312 && sectionIn.getEl().getElementsByTagName('form')[0].parentNode) {
313
314 YAHOO.util.DDM.swapNode(this.getEl().getElementsByTagName('form')[0].parentNode,
315 sectionIn.getEl().getElementsByTagName('form')[0].parentNode);
316 } else {
317 YAHOO.log("Swapping sections: form not present in one or both sections", "warn");
318 }
b532c911 319};
354e1130 320
a66ddf19 321
354e1130 322section_class.prototype.toggle_hide = function(e,target,superficial) {
e8b32e2b
PS
323 if (this.sectionId > main.portal.numsections) {
324 // no need to do anything in orphaned sections
325 return;
326 }
327
f8eaeffa 328 var strhide = main.portal.strings['hide'];
329 var strshow = main.portal.strings['show'];
330 if (this.hidden) {
6f00683e 331 YAHOO.util.Dom.removeClass(this.getEl(), 'hidden');
424a3530 332 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show/i, 'hide');
f8eaeffa 333 this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strshow, strhide);
5cfea9fb 334 this.viewButton.childNodes[0].title = this.viewButton.childNodes[0].title.replace(strshow, strhide); //IE hack.
f8eaeffa 335 this.viewButton.title = this.viewButton.title.replace(strshow, strhide);
354e1130 336 this.hidden = false;
337
338 if (!superficial) {
73d402ef 339 main.connect('POST', 'class=section&field=visible', null, 'value=1&id='+this.sectionId);
3440ec12 340 for (var x=0; x<this.resources.length; x++) {
6f00683e 341 this.resources[x].toggle_hide(null, null, true, this.resources[x].hiddenStored);
354e1130 342 this.resources[x].hiddenStored = null;
343 }
344 }
345
346 } else {
6f00683e 347 YAHOO.util.Dom.addClass(this.getEl(), 'hidden');
424a3530 348 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide/i, 'show');
f8eaeffa 349 this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strhide, strshow);
5cfea9fb 350 this.viewButton.childNodes[0].title = this.viewButton.childNodes[0].title.replace(strhide, strshow); //IE hack.
f8eaeffa 351 this.viewButton.title = this.viewButton.title.replace(strhide, strshow);
354e1130 352 this.hidden = true;
353
354 if (!superficial) {
73d402ef 355 main.connect('POST', 'class=section&field=visible', null, 'value=0&id='+this.sectionId);
6f00683e 356 for (var x=0; x<this.resources.length; x++) {
3440ec12 357 this.resources[x].hiddenStored = this.resources[x].hidden;
6f00683e 358 this.resources[x].toggle_hide(null, null, true, true);
7f933d8f 359 }
354e1130 360 }
d4df8fdc 361 }
b532c911 362};
0a0bb380 363
a66ddf19 364
354e1130 365section_class.prototype.toggle_highlight = function() {
f7538871
RT
366 var strmarker = main.portal.strings['marker'];
367 var strmarked = main.portal.strings['marked'];
368
3440ec12 369 if (this.highlighted) {
6f00683e 370 YAHOO.util.Dom.removeClass(this.getEl(), 'current');
730477a8 371 this.highlightButton.childNodes[0].src = main.portal.icons['marker'];
f7538871
RT
372 this.highlightButton.childNodes[0].alt = strmarker;
373 this.highlightButton.childNodes[0].title = strmarker; //for IE
374 this.highlightButton.title = strmarker;
354e1130 375 this.highlighted = false;
376 } else {
6f00683e 377 YAHOO.util.Dom.addClass(this.getEl(), 'current');
730477a8 378 this.highlightButton.childNodes[0].src = main.portal.icons['marked'];
f7538871
RT
379 this.highlightButton.childNodes[0].alt = strmarked;
380 this.highlightButton.childNodes[0].title = strmarked; //for IE
381 this.highlightButton.title = strmarked;
354e1130 382 this.highlighted = true;
0a0bb380 383 }
b532c911 384};
0a0bb380 385
a66ddf19 386
354e1130 387section_class.prototype.mk_marker = function() {
388 if (main.marker != this) {
389 main.update_marker(this);
d2a11d46 390 } else {
72d28452 391 // If currently the marker
354e1130 392 main.marker = null;
393
73d402ef 394 main.connect('POST', 'class=course&field=marker', null, 'value=0');
354e1130 395 this.toggle_highlight();
354e1130 396 }
b532c911 397};
354e1130 398
a66ddf19 399
3440ec12 400section_class.prototype.changeId = function(newId) {
401 this.sectionId = newId;
88c5092a 402 this.numberDisplay.firstChild.data = newId;
354e1130 403
3440ec12 404 //main.connectQueue_add('POST','class=section&field=all',null,'id='+newId+"&summary="+main.mk_safe_for_transport(this.summary)+"&sequence="+this.write_sequence_list(true)+'&visible='+(this.hidden?0:1))
354e1130 405
406 if (main.marker == this) {
88c5092a 407 main.update_marker(this);
d4df8fdc 408 }
b532c911 409};
354e1130 410
a66ddf19 411
354e1130 412section_class.prototype.get_resource_index = function(el) {
6f00683e 413 for (var x=0; x<this.resources.length; x++) {
414 if (this.resources[x] == el) {
354e1130 415 return x;
72d28452 416 }
417 }
6f00683e 418 YAHOO.log("Could not find resource to remove "+el.getEl().id, "error");
354e1130 419 return -1;
b532c911 420};
354e1130 421
a66ddf19 422
354e1130 423section_class.prototype.remove_resource = function(el) {
e50b0ad9 424
425 var resourceEl = el.getEl();
426 var parentEl = resourceEl.parentNode;
427 if (!parentEl) {
428 return false;
429 }
430
354e1130 431 var resourceCount = this.resources.length;
e1c15ef7 432
354e1130 433 if (resourceCount == 1) {
6f00683e 434 if (this.resources[0] == el) {
435 this.resources = new Array();
72d28452 436 }
354e1130 437 } else {
438 var found = false;
6f00683e 439 for (var i=0; i<resourceCount; i++) {
354e1130 440 if (found) {
3440ec12 441 this.resources[i - 1] = this.resources[i];
6f00683e 442 if (i == resourceCount - 1) {
443 this.resources = this.resources.slice(0, -1);
354e1130 444 resourceCount--;
0a0bb380 445 }
6f00683e 446 this.resources[i - 1].update_index(i - 1);
447 } else if (this.resources[i] == el) {
354e1130 448 found = true;
0a0bb380 449 }
450 }
354e1130 451 }
e50b0ad9 452 // Remove any extra text nodes to keep DOM clean.
453 var kids = parentEl.childNodes;
454
72d28452 455 for (var i=0; i<kids.length; i++) {
456 if (kids[i].nodeType == 3) {
457 YAHOO.log('Removed extra text node.');
458 parentEl.removeChild(kids[i]);
459 }
460 }
461 parentEl.removeChild(resourceEl);
e50b0ad9 462
e1c15ef7 463 this.write_sequence_list();
e50b0ad9 464 return true;
b532c911 465};
354e1130 466
a66ddf19 467
64e5a68d 468section_class.prototype.insert_resource = function(el, targetel) {
354e1130 469 var resourcecount = this.resources.length;
470 var found = false;
471 var tempStore = nextStore = null;
472
473 //update in backend
72d28452 474 var targetId = '';
475 if (targetel) {
476 targetId = targetel.id;
477 }
478 if (this.debug) {
479 YAHOO.log('id='+el.id+', beforeId='+targetId+', sectionId='+this.sectionId);
480 }
481 main.connect('POST', 'class=resource&field=move', null,
482 'id='+el.id+'&beforeId='+targetId+'&sectionId='+this.sectionId);
354e1130 483
484 //if inserting into a hidden resource hide
485 if (this.hidden) {
3440ec12 486 el.hiddenStored = el.hidden;
487 el.toggle_hide(null, null, true, true);
354e1130 488 } else {
489 if (el.hiddenStored != null) {
6f00683e 490 el.toggle_hide(null, null, true, el.hiddenStored);
88c5092a 491 el.hiddenStored = null;
62cb4032 492 }
354e1130 493 }
354e1130 494 //update model
6f00683e 495 if (!targetel) {
354e1130 496 this.resources[this.resources.length] = el;
db5308e2 497 } else {
6f00683e 498 for (var i=0; i<resourcecount; i++) {
354e1130 499 if (found) {
500 tempStore = this.resources[i];
501 this.resources[i] = nextStore;
db5308e2 502 nextStore = tempStore;
72d28452 503
3440ec12 504 if (nextStore != null)
505 nextStore.update_index(i+1);
0a0bb380 506
354e1130 507 } else if (this.resources[i] == targetel) {
508 found = true;
509 nextStore = this.resources[i];
3440ec12 510 this.resources[i] = el;
354e1130 511 resourcecount++;
512
6f00683e 513 this.resources[i].update_index(i, this.ident);
3440ec12 514 nextStore.update_index(i + 1);
515 }
354e1130 516 }
72d28452 517 }
6f00683e 518 //update on frontend
519 if (targetel) {
520 this.resources_ul.insertBefore(el.getEl(), targetel.getEl());
4c72307e 521 //this.resources_ul.insertBefore(document.createTextNode(' '), targetel.getEl());
354e1130 522 } else {
523 this.resources_ul.appendChild(el.getEl());
4c72307e 524 //this.resources_ul.appendChild(document.createTextNode(' '));
3440ec12 525 }
526 el.parentObj = this;
b532c911 527};
354e1130 528
a66ddf19 529
354e1130 530section_class.prototype.write_sequence_list = function(toReturn) {
531 var listOutput = '';
e1c15ef7 532
6f00683e 533 for (var i=0; i<this.resources.length; i++) {
354e1130 534 listOutput += this.resources[i].id;
e1c15ef7 535 if (i != (this.resources.length-1)) {
354e1130 536 listOutput += ',';
72d28452 537 }
354e1130 538 }
db5308e2 539 if (toReturn) {
354e1130 540 return listOutput;
72d28452 541 }
b532c911 542};
354e1130 543
0a0bb380 544
354e1130 545
d2a11d46 546
e1c15ef7 547/**
548 * resource_class extends util.DDProxy
549 */
354e1130 550function resource_class(id,group,config,parentObj) {
0a0bb380 551 this.init_resource(id,group,config,parentObj);
552}
e1c15ef7 553
0a0bb380 554YAHOO.extend(resource_class, YAHOO.util.DDProxy);
555
e1c15ef7 556
9569653d 557resource_class.prototype.debug = false;
d4df8fdc 558
e1c15ef7 559
6f00683e 560resource_class.prototype.init_resource = function(id, group, config, parentObj) {
3440ec12 561 if (!id) {
6f00683e 562 YAHOO.log("Init resource, NO ID FOUND!", 'error');
3440ec12 563 return;
354e1130 564 }
d2a11d46 565
72d28452 566 // Some constants.
567 this.NOGROUPS = 0;
568 this.SEPARATEGROUPS = 1;
569 this.VISIBLEGROUPS = 2;
d2a11d46 570
3440ec12 571 this.is = 'resource';
6f00683e 572 this.init(id, group, config);
3440ec12 573 this.createFrame();
354e1130 574 this.isTarget = true;
eba88175 575
6f00683e 576 this.id = this.getEl().id.replace(/module-/i, '');
eba88175 577
354e1130 578 this.hidden = false;
fcc286a4 579 if (YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('a')[0], 'dimmed') ||
d9b49c23 580 YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('div')[1], 'dimmed_text')) {
354e1130 581 this.hidden = true;
72d28452 582 }
354e1130 583 this.hiddenStored = null;
584
72d28452 585 this.groupmode = null; // Can be null (i.e. does not apply), 0, 1 or 2.
d2a11d46 586
354e1130 587 this.linkContainer = this.getEl().getElementsByTagName('a')[0];
56dc41d0 588 this.divContainer = this.getEl().getElementsByTagName('div')[0];
354e1130 589
590 this.commandContainer = null;
72d28452 591 this.indentLeftButton = null;
592 this.indentRightButton = null;
354e1130 593 this.viewButton = null;
72d28452 594 this.groupButton = null;
d2a11d46 595 this.handle = null;
354e1130 596 this.init_buttons();
597
d2a11d46 598 this.parentObj = parentObj;
354e1130 599
e1c15ef7 600 if (this.debug) {
72d28452 601 YAHOO.log("init_resource "+id+" parent = "+parentObj.getEl().id);
602 }
b532c911 603};
0a0bb380 604
a66ddf19 605
d2a11d46 606/**
607 * The current strategy is to look at the DOM tree to get information on the
608 * resource and it's current mode. This is bad since we are dependant on
609 * the html that is output from serverside logic. Seemingly innocuous changes
610 * like changing the language string for the title of a button will break
611 * our JavaScript here. This is brittle.
612 *
613 * First, we clear the buttons container. Then:
614 * We need to add the new-style move handle.
615 * The old style move button (up/down) needs to be removed.
616 * Move left button (if any) needs an event handler.
617 * Move right button (if any) needs an event handler.
618 * Update button stays as it is. Add it back.
619 * Delete button needs an event handler.
620 * Visible button is a toggle. It needs an event handler too.
621 * Group mode button is a toggle. It needs an event handler too.
622 */
354e1130 623resource_class.prototype.init_buttons = function() {
d2a11d46 624
72d28452 625 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
626 'span', this.getEl())[0];
d2a11d46 627
2469f7ea 628 if (commandContainer == null) {
6f00683e 629 YAHOO.log('Cannot find command container for '+this.getEl().id, 'error');
354e1130 630 return;
0a0bb380 631 }
d2a11d46 632
72d28452 633 // Language strings.
6dc5908e
ARN
634 var strgroupsnone = main.portal.strings['groupsnone'];
635 var strgroupsseparate = main.portal.strings['groupsseparate'];
636 var strgroupsvisible = main.portal.strings['groupsvisible'];
d2a11d46 637
354e1130 638 this.commandContainer = commandContainer;
72d28452 639 var buttons = commandContainer.getElementsByTagName('a');
354e1130 640
d2a11d46 641 // Buttons that we might need to add back in.
af189935
PS
642 var deletePresent = false;
643 var hideshow = false;
644 var movehandle = false;
72d28452 645 var moveLeft = false;
646 var moveRight = false;
354e1130 647 var updateButton = null;
fa820563 648 var duplicateButton = null;
1a0d7bd7 649 var assignButton = null;
3440ec12 650
472a4b92 651 // for RTL support
652 var isrtl = (document.getElementsByTagName("html")[0].dir=="rtl");
e1c15ef7 653
6f00683e 654 for (var x=0; x<buttons.length; x++) {
72d28452 655 if (buttons[x].className == 'editing_moveleft') {
d2a11d46 656 moveLeft = true;
90ebdf65 657 } else if (buttons[x].className == 'editing_moveright') {
d2a11d46 658 moveRight = true;
90ebdf65 659 } else if (buttons[x].className == 'editing_update') {
02059f46 660 updateButton = buttons[x].cloneNode(true);
fa820563
DM
661 } else if (buttons[x].className == 'editing_duplicate') {
662 duplicateButton = buttons[x].cloneNode(true);
1a0d7bd7
ARN
663 } else if (buttons[x].className == 'editing_assign') {
664 assignButton = buttons[x].cloneNode(true);
90ebdf65 665 } else if (buttons[x].className == 'editing_groupsnone') {
72d28452 666 this.groupmode = this.NOGROUPS;
667 } else if (buttons[x].className == 'editing_groupsseparate') {
668 this.groupmode = this.SEPARATEGROUPS;
669 } else if (buttons[x].className == 'editing_groupsvisible') {
670 this.groupmode = this.VISIBLEGROUPS;
af189935
PS
671 } else if (buttons[x].className == 'editing_delete') {
672 deletePresent = true;
673 } else if (buttons[x].className == 'editing_hide') {
674 hideshow = true;
675 } else if (buttons[x].className == 'editing_show') {
676 hideshow = true;
677 } else if (buttons[x].className == 'editing_moveup') {
678 movehandle = true;
679 } else if (buttons[x].className == 'editing_movedown') {
680 movehandle = true;
681 } else if (buttons[x].className == 'editing_move') {
682 movehandle = true;
72d28452 683 }
354e1130 684 }
685
72d28452 686 // Clear all the buttons.
354e1130 687 commandContainer.innerHTML = '';
688
d2a11d46 689 // Add move-handle for drag and drop.
af189935
PS
690 if (movehandle) {
691 var handleRef = main.mk_button('a', main.portal.icons['move_2d'], main.portal.strings['move'],
692 [['style', 'cursor:move']], [['class', 'iconsmall']]);
6f00683e 693
af189935
PS
694 YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
695 this.handle = handleRef;
696 commandContainer.appendChild(handleRef);
697 this.setHandleElId(this.handle.id);
698 }
354e1130 699
72d28452 700 // Add indentation buttons if needed (move left, move right).
701 if (moveLeft) {
ddedf979 702 var button = main.mk_button('a', main.portal.icons['backwards'], main.portal.strings['moveleft'],
a8a44f28 703 [['class', 'editing_moveleft']], [['class', 'iconsmall']]);
72d28452 704 YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true);
705 commandContainer.appendChild(button);
706 this.indentLeftButton = button;
707 }
708
709 if (moveRight) {
ddedf979 710 var button = main.mk_button('a', main.portal.icons['forwards'], main.portal.strings['moveright'],
a8a44f28 711 [['class', 'editing_moveright']], [['class', 'iconsmall']]);
72d28452 712 YAHOO.util.Event.addListener(button, 'click', this.indent_right, this, true);
713 commandContainer.appendChild(button);
714 this.indentRightButton = button;
715 }
d2a11d46 716
af189935
PS
717 if (updateButton) {
718 // Add edit button back in.
719 commandContainer.appendChild(updateButton);
720 }
354e1130 721
fa820563
DM
722 if (duplicateButton) {
723 commandContainer.appendChild(duplicateButton);
724 }
725
d2a11d46 726 // Add the delete button.
af189935
PS
727 if (deletePresent) {
728 var button = main.mk_button('a', main.portal.icons['delete'], main.portal.strings['delete'], null, [['class', 'iconsmall']]);
729 YAHOO.util.Event.addListener(button, 'click', this.delete_button, this, true);
730 commandContainer.appendChild(button);
731 }
354e1130 732
72d28452 733 // Add the hide or show button.
af189935
PS
734 if (hideshow) {
735 if (this.hidden) {
736 var button = main.mk_button('a', main.portal.icons['show'], main.portal.strings['show'], null, [['class', 'iconsmall']]);
737 } else {
738 var button = main.mk_button('a', main.portal.icons['hide'], main.portal.strings['hide'], null, [['class', 'iconsmall']]);
739 }
740 YAHOO.util.Event.addListener(button, 'click', this.toggle_hide, this, true);
741 commandContainer.appendChild(button);
742 this.viewButton = button;
72d28452 743 }
d2a11d46 744
72d28452 745 // Add the groupmode button if needed.
746 if (this.groupmode != null) {
747 if (this.groupmode == this.NOGROUPS) {
a8a44f28 748 var button = main.mk_button('a', main.portal.icons['groupn'], strgroupsnone, null, [['class', 'iconsmall']]);
72d28452 749 } else if (this.groupmode == this.SEPARATEGROUPS) {
a8a44f28 750 var button = main.mk_button('a', main.portal.icons['groups'], strgroupsseparate, null, [['class', 'iconsmall']]);
72d28452 751 } else {
a8a44f28 752 var button = main.mk_button('a', main.portal.icons['groupv'], strgroupsvisible, null, [['class', 'iconsmall']]);
72d28452 753 }
754 YAHOO.util.Event.addListener(button, 'click', this.toggle_groupmode, this, true);
755 commandContainer.appendChild(button);
756 this.groupButton = button;
757 }
1a0d7bd7
ARN
758
759 // Add the assign roles button back in
760 if (assignButton != null) {
761 commandContainer.appendChild(assignButton);
762 }
b532c911 763};
354e1130 764
a66ddf19 765
22206b67 766resource_class.prototype.indent_left = function() {
767
060cd0c8
SH
768 var indentdiv = YAHOO.util.Dom.getElementsByClassName('mod-indent', 'div', this.getEl())[0];
769 if (!indentdiv) {
72d28452 770 if (this.debug) {
060cd0c8 771 YAHOO.log('Could not indent left: intending div does not exist', 'error');
72d28452 772 }
773 return false;
774 }
218a4659 775 var oldindent = indentdiv.className.match(/mod-indent-(\d{1,})/);
060cd0c8
SH
776 if (oldindent && oldindent[1] > 0) {
777 oldindent = oldindent[1];
72d28452 778 } else {
060cd0c8
SH
779 return false;
780 }
781 var newindent = parseFloat(oldindent) - 1;
782 YAHOO.util.Dom.replaceClass(indentdiv, 'mod-indent-'+oldindent, 'mod-indent-'+newindent);
783 main.connect('POST', 'class=resource&field=indentleft', null, 'id='+this.id);
72d28452 784
060cd0c8 785 if (newindent == 0) {
72d28452 786 // Remove the indent left button as well.
787 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
788 'span', this.getEl())[0];
72d28452 789 commandContainer.removeChild(this.indentLeftButton);
790 this.indentLeftButton = null;
791 }
060cd0c8 792
72d28452 793 return true;
b532c911 794};
22206b67 795
796
797resource_class.prototype.indent_right = function() {
798
060cd0c8
SH
799 var indentdiv = YAHOO.util.Dom.getElementsByClassName('mod-indent', 'div', this.getEl())[0];
800 if (!indentdiv) {
801 if (this.debug) {
802 YAHOO.log('Could not indent left: intending div does not exist', 'error');
803 }
804 return false;
805 }
218a4659 806 var oldindent = indentdiv.className.match(/mod-indent-(\d{1,})/);
060cd0c8
SH
807 if (oldindent && oldindent[1] >= 0) {
808 oldindent = oldindent[1];
809 var newindent = parseFloat(oldindent) + 1;
810 YAHOO.util.Dom.replaceClass(indentdiv, 'mod-indent-'+oldindent, 'mod-indent-'+newindent);
72d28452 811 } else {
060cd0c8 812 YAHOO.util.Dom.addClass(indentdiv, 'mod-indent-1');
72d28452 813 }
060cd0c8 814 main.connect('POST', 'class=resource&field=indentright', null, 'id='+this.id);
72d28452 815
816 if (!this.indentLeftButton) {
060cd0c8
SH
817 // Add a indent left button if none is present.
818 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands', 'span', this.getEl())[0];
ddedf979 819 var button = main.mk_button('a', main.portal.icons['backwards'], main.portal.strings['moveleft'],
a8a44f28 820 [['class', 'editing_moveleft']], [['class', 'iconsmall']]);
72d28452 821 YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true);
822 commandContainer.insertBefore(button, this.indentRightButton);
823 this.indentLeftButton = button;
824 }
060cd0c8 825
72d28452 826 return true;
b532c911 827};
22206b67 828
829
6f00683e 830resource_class.prototype.toggle_hide = function(target, e, superficial, force) {
f8eaeffa 831 var strhide = main.portal.strings['hide'];
832 var strshow = main.portal.strings['show'];
354e1130 833 if (force != null) {
6f00683e 834 if (this.debug) {
72d28452 835 YAHOO.log("Resource "+this.getEl().id+" forced to "+force);
836 }
3440ec12 837 this.hidden = !force;
0a0bb380 838 }
354e1130 839 if (this.hidden) {
6f00683e 840 YAHOO.util.Dom.removeClass(this.linkContainer, 'dimmed');
56dc41d0 841 YAHOO.util.Dom.removeClass(this.divContainer, 'dimmed_text');
424a3530 842 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show/i, 'hide');
f8eaeffa 843 this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strshow, strhide);
844 this.viewButton.title = this.viewButton.title.replace(strshow, strhide);
3440ec12 845 this.hidden = false;
354e1130 846
847 if (!superficial) {
73d402ef 848 main.connect('POST', 'class=resource&field=visible', null, 'value=1&id='+this.id);
1752e584 849 }
354e1130 850 } else {
6f00683e 851 YAHOO.util.Dom.addClass(this.linkContainer, 'dimmed');
56dc41d0 852 YAHOO.util.Dom.addClass(this.divContainer, 'dimmed_text');
424a3530 853 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide/i, 'show');
f8eaeffa 854 this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strhide, strshow);
855 this.viewButton.title = this.viewButton.title.replace(strhide, strshow);
354e1130 856 this.hidden = true;
857
858 if (!superficial) {
73d402ef 859 main.connect('POST', 'class=resource&field=visible', null, 'value=0&id='+this.id);
354e1130 860 }
861 }
b532c911 862};
354e1130 863
a66ddf19 864
81f92309 865resource_class.prototype.groupImages = ['groupn', 'groups', 'groupv'];
d2a11d46 866
867
868resource_class.prototype.toggle_groupmode = function() {
869 this.groupmode++;
870 if (this.groupmode > 2) {
871 this.groupmode = 0;
72d28452 872 }
873
f8eaeffa 874 var newtitle = this.groupButton.title;
cddbd5d5 875
876 switch (this.groupmode) {
877 case 0:
6dc5908e 878 newtitle = main.portal.strings['groupsnone'];
cddbd5d5 879 break;
880 case 1:
6dc5908e 881 newtitle = main.portal.strings['groupsseparate'];
cddbd5d5 882 break;
883 case 2:
6dc5908e 884 newtitle = main.portal.strings['groupsvisible'];
cddbd5d5 885 break;
886 }
f8eaeffa 887
888 this.groupButton.getElementsByTagName('img')[0].alt = newtitle;
889 this.groupButton.title = newtitle;
cddbd5d5 890
ddedf979 891 this.groupButton.getElementsByTagName('img')[0].src = main.portal.icons[this.groupImages[this.groupmode]];
d2a11d46 892 main.connect('POST', 'class=resource&field=groupmode', null, 'value='+this.groupmode+'&id='+this.id);
b532c911 893};
d2a11d46 894
895
354e1130 896resource_class.prototype.delete_button = function() {
446d6e7d 897 if (this.debug) {
72d28452 898 YAHOO.log("Deleting "+this.getEl().id+" from parent "+this.parentObj.getEl().id);
899 }
b31b2d84
DM
900
901 // default fallback to something like 'Resource 42'
902 var modtype = main.getString(this.is);
903 var modname = this.id;
904
905 // try to get less cryptic instance name from DOM
906 if (YAHOO.util.Dom.hasClass(this.getEl(), 'activity')) {
907 if (YAHOO.util.Dom.hasClass(this.getEl(), 'label')) {
908 // mod_label instance
909 modtype = main.getString('modtype_label');
910 modname = '';
911 } else {
912 // other mod instance, get the type first
913 matches = new RegExp(/modtype_(\w+)/).exec(this.getEl().className);
914 if (matches[1] && main.hasString('modtype_' + matches[1])) {
915 modtype = main.getString('modtype_' + matches[1]);
916 }
917 // look for span.instancename content to get the module instance name from it
918 instancename = YAHOO.util.Selector.query('.instancename', this.getEl(), true);
919 if (instancename) {
920 // remove the span.accesshide
921 accesshides = YAHOO.util.Selector.query('.accesshide', instancename);
922 for (x in accesshides) {
923 instancename.removeChild(accesshides[x]);
924 }
925 // strip HTML tags
926 instancenametext = instancename.innerHTML.replace(/<[^>]+>/g, '');
927 // and if anything survived, consider it the instance name
928 if (instancenametext) {
929 modname = instancenametext;
930 }
931 // put span.accesshides back
932 for (x in accesshides) {
933 instancename.appendChild(accesshides[x]);
934 }
935 }
936 }
937 }
938
939 if (modname) {
940 modname = "'" + modname + "'";
941 }
942 if (!confirm(main.getString('deletecheck', modtype + ' ' + modname))) {
446d6e7d 943 return false;
354e1130 944 }
446d6e7d 945 this.parentObj.remove_resource(this);
49c4d8ca 946 main.connect('POST', 'class=resource&action=DELETE&id='+this.id);
b532c911 947};
354e1130 948
a66ddf19 949
354e1130 950resource_class.prototype.update_index = function(index) {
e1c15ef7 951 if (this.debug) {
72d28452 952 YAHOO.log("Updating Index for resource "+this.getEl().id+" to "+index);
953 }
b532c911 954};
354e1130 955
a66ddf19 956
2469f7ea 957resource_class.prototype.startDrag = function(x, y) {
0a0bb380 958 YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
354e1130 959
0a0bb380 960 //reinitialize dd element
72d28452 961 this.getDragEl().innerHTML = '';
354e1130 962
0a0bb380 963 var targets = YAHOO.util.DDM.getRelated(this, true);
e1c15ef7 964 if (this.debug) {
72d28452 965 YAHOO.log(this.id + " startDrag "+targets.length + " targets");
966 }
b532c911 967};
0a0bb380 968
a66ddf19 969
7f933d8f 970resource_class.prototype.clear_move_markers = function(target) {
72d28452 971 if (target.is == 'section') {
972 resources = target.resources;
973 } else {
974 resources = target.parentObj.resources;
975 }
976 for (var i=0; i<resources.length; i++) {
9810d8a0 977 if (resources[i].getEl() != null) {
978 YAHOO.util.Dom.setStyle(resources[i].getEl().id, 'border', 'none');
979 }
72d28452 980 }
b532c911 981};
7f933d8f 982
a66ddf19 983
7f933d8f 984resource_class.prototype.onDragOver = function(e, ids) {
72d28452 985 var target = YAHOO.util.DDM.getBestMatch(ids);
986
987 this.clear_move_markers(target);
988
989 if (target != this && (target.is == 'resource' || target.is == 'activity')) {
990 // Add a top border to show where the drop will place the resource.
991 YAHOO.util.Dom.setStyle(target.getEl().id, 'border-top', '1px solid #BBB');
992 } else if (target.is == 'section' && target.resources.length > 0) {
993 // We need to have a border at the bottom of the last activity in
994 // that section.
9810d8a0 995 if (target.resources[target.resources.length - 1].getEl() != null) {
996 YAHOO.util.Dom.setStyle(target.resources[target.resources.length - 1].getEl().id,
72d28452 997 'border-bottom', '1px solid #BBB');
9810d8a0 998 }
72d28452 999 }
b532c911 1000};
354e1130 1001
a66ddf19 1002
7f933d8f 1003resource_class.prototype.onDragOut = function(e, ids) {
72d28452 1004 var target = YAHOO.util.DDM.getBestMatch(ids);
1005 if (target) {
1006 this.clear_move_markers(target);
1007 }
b532c911 1008};
0a0bb380 1009
a66ddf19 1010
e50b0ad9 1011resource_class.prototype.onDragDrop = function(e, ids) {
72d28452 1012 var target = YAHOO.util.DDM.getBestMatch(ids);
1013 if (!target) {
1014 YAHOO.log('onDragDrop: Target is not valid!', 'error');
1015 }
2469f7ea 1016
64e5a68d 1017 if (this.debug) {
72d28452 1018 YAHOO.log("Dropped on section id="+target.sectionId
1019 +", el="+this.getEl().id
1020 +", x="+YAHOO.util.Dom.getXY( this.getDragEl() ));
1021 }
64e5a68d 1022 this.parentObj.remove_resource(this);
354e1130 1023
64e5a68d 1024 if (target.is == 'resource' || target.is == 'activity') {
1025 target.parentObj.insert_resource(this, target);
1026 } else if (target.is == 'section') {
72d28452 1027 target.insert_resource(this);
0a0bb380 1028 }
72d28452 1029 this.clear_move_markers(target);
0a0bb380 1030 return;
b532c911 1031};
0a0bb380 1032
a66ddf19 1033
0a0bb380 1034resource_class.prototype.endDrag = function() {
64e5a68d 1035 // Eliminates default action
b532c911 1036};
9837bd1d 1037
1038section_class.prototype.swap_dates = function(el){
8c3c518f 1039 var i=1;
1040 var divs = YAHOO.util.Selector.query('div .weekdates');
9837bd1d 1041
8c3c518f 1042 for (div in divs) {
1043 divs[div].innerHTML = main.sectiondates[i];
1044 i++;
9837bd1d 1045 }
b532c911 1046};
9837bd1d 1047