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