lib/ajax/ MDL-25675 Use className instead of classList.toString()
[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) {
ddedf979 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),
102 [['title', main.portal.strings['hide'] ]]);
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() {
3440ec12 366 if (this.highlighted) {
6f00683e 367 YAHOO.util.Dom.removeClass(this.getEl(), 'current');
354e1130 368 this.highlighted = false;
369 } else {
6f00683e 370 YAHOO.util.Dom.addClass(this.getEl(), 'current');
354e1130 371 this.highlighted = true;
0a0bb380 372 }
b532c911 373};
0a0bb380 374
a66ddf19 375
354e1130 376section_class.prototype.mk_marker = function() {
377 if (main.marker != this) {
378 main.update_marker(this);
d2a11d46 379 } else {
72d28452 380 // If currently the marker
354e1130 381 main.marker = null;
382
73d402ef 383 main.connect('POST', 'class=course&field=marker', null, 'value=0');
354e1130 384 this.toggle_highlight();
354e1130 385 }
b532c911 386};
354e1130 387
a66ddf19 388
3440ec12 389section_class.prototype.changeId = function(newId) {
390 this.sectionId = newId;
88c5092a 391 this.numberDisplay.firstChild.data = newId;
354e1130 392
3440ec12 393 //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 394
395 if (main.marker == this) {
88c5092a 396 main.update_marker(this);
d4df8fdc 397 }
b532c911 398};
354e1130 399
a66ddf19 400
354e1130 401section_class.prototype.get_resource_index = function(el) {
6f00683e 402 for (var x=0; x<this.resources.length; x++) {
403 if (this.resources[x] == el) {
354e1130 404 return x;
72d28452 405 }
406 }
6f00683e 407 YAHOO.log("Could not find resource to remove "+el.getEl().id, "error");
354e1130 408 return -1;
b532c911 409};
354e1130 410
a66ddf19 411
354e1130 412section_class.prototype.remove_resource = function(el) {
e50b0ad9 413
414 var resourceEl = el.getEl();
415 var parentEl = resourceEl.parentNode;
416 if (!parentEl) {
417 return false;
418 }
419
354e1130 420 var resourceCount = this.resources.length;
e1c15ef7 421
354e1130 422 if (resourceCount == 1) {
6f00683e 423 if (this.resources[0] == el) {
424 this.resources = new Array();
72d28452 425 }
354e1130 426 } else {
427 var found = false;
6f00683e 428 for (var i=0; i<resourceCount; i++) {
354e1130 429 if (found) {
3440ec12 430 this.resources[i - 1] = this.resources[i];
6f00683e 431 if (i == resourceCount - 1) {
432 this.resources = this.resources.slice(0, -1);
354e1130 433 resourceCount--;
0a0bb380 434 }
6f00683e 435 this.resources[i - 1].update_index(i - 1);
436 } else if (this.resources[i] == el) {
354e1130 437 found = true;
0a0bb380 438 }
439 }
354e1130 440 }
e50b0ad9 441 // Remove any extra text nodes to keep DOM clean.
442 var kids = parentEl.childNodes;
443
72d28452 444 for (var i=0; i<kids.length; i++) {
445 if (kids[i].nodeType == 3) {
446 YAHOO.log('Removed extra text node.');
447 parentEl.removeChild(kids[i]);
448 }
449 }
450 parentEl.removeChild(resourceEl);
e50b0ad9 451
e1c15ef7 452 this.write_sequence_list();
e50b0ad9 453 return true;
b532c911 454};
354e1130 455
a66ddf19 456
64e5a68d 457section_class.prototype.insert_resource = function(el, targetel) {
354e1130 458 var resourcecount = this.resources.length;
459 var found = false;
460 var tempStore = nextStore = null;
461
462 //update in backend
72d28452 463 var targetId = '';
464 if (targetel) {
465 targetId = targetel.id;
466 }
467 if (this.debug) {
468 YAHOO.log('id='+el.id+', beforeId='+targetId+', sectionId='+this.sectionId);
469 }
470 main.connect('POST', 'class=resource&field=move', null,
471 'id='+el.id+'&beforeId='+targetId+'&sectionId='+this.sectionId);
354e1130 472
473 //if inserting into a hidden resource hide
474 if (this.hidden) {
3440ec12 475 el.hiddenStored = el.hidden;
476 el.toggle_hide(null, null, true, true);
354e1130 477 } else {
478 if (el.hiddenStored != null) {
6f00683e 479 el.toggle_hide(null, null, true, el.hiddenStored);
88c5092a 480 el.hiddenStored = null;
62cb4032 481 }
354e1130 482 }
354e1130 483 //update model
6f00683e 484 if (!targetel) {
354e1130 485 this.resources[this.resources.length] = el;
db5308e2 486 } else {
6f00683e 487 for (var i=0; i<resourcecount; i++) {
354e1130 488 if (found) {
489 tempStore = this.resources[i];
490 this.resources[i] = nextStore;
db5308e2 491 nextStore = tempStore;
72d28452 492
3440ec12 493 if (nextStore != null)
494 nextStore.update_index(i+1);
0a0bb380 495
354e1130 496 } else if (this.resources[i] == targetel) {
497 found = true;
498 nextStore = this.resources[i];
3440ec12 499 this.resources[i] = el;
354e1130 500 resourcecount++;
501
6f00683e 502 this.resources[i].update_index(i, this.ident);
3440ec12 503 nextStore.update_index(i + 1);
504 }
354e1130 505 }
72d28452 506 }
6f00683e 507 //update on frontend
508 if (targetel) {
509 this.resources_ul.insertBefore(el.getEl(), targetel.getEl());
4c72307e 510 //this.resources_ul.insertBefore(document.createTextNode(' '), targetel.getEl());
354e1130 511 } else {
512 this.resources_ul.appendChild(el.getEl());
4c72307e 513 //this.resources_ul.appendChild(document.createTextNode(' '));
3440ec12 514 }
515 el.parentObj = this;
b532c911 516};
354e1130 517
a66ddf19 518
354e1130 519section_class.prototype.write_sequence_list = function(toReturn) {
520 var listOutput = '';
e1c15ef7 521
6f00683e 522 for (var i=0; i<this.resources.length; i++) {
354e1130 523 listOutput += this.resources[i].id;
e1c15ef7 524 if (i != (this.resources.length-1)) {
354e1130 525 listOutput += ',';
72d28452 526 }
354e1130 527 }
db5308e2 528 if (toReturn) {
354e1130 529 return listOutput;
72d28452 530 }
b532c911 531};
354e1130 532
0a0bb380 533
354e1130 534
d2a11d46 535
e1c15ef7 536/**
537 * resource_class extends util.DDProxy
538 */
354e1130 539function resource_class(id,group,config,parentObj) {
0a0bb380 540 this.init_resource(id,group,config,parentObj);
541}
e1c15ef7 542
0a0bb380 543YAHOO.extend(resource_class, YAHOO.util.DDProxy);
544
e1c15ef7 545
9569653d 546resource_class.prototype.debug = false;
d4df8fdc 547
e1c15ef7 548
6f00683e 549resource_class.prototype.init_resource = function(id, group, config, parentObj) {
3440ec12 550 if (!id) {
6f00683e 551 YAHOO.log("Init resource, NO ID FOUND!", 'error');
3440ec12 552 return;
354e1130 553 }
d2a11d46 554
72d28452 555 // Some constants.
556 this.NOGROUPS = 0;
557 this.SEPARATEGROUPS = 1;
558 this.VISIBLEGROUPS = 2;
d2a11d46 559
3440ec12 560 this.is = 'resource';
6f00683e 561 this.init(id, group, config);
3440ec12 562 this.createFrame();
354e1130 563 this.isTarget = true;
eba88175 564
6f00683e 565 this.id = this.getEl().id.replace(/module-/i, '');
eba88175 566
354e1130 567 this.hidden = false;
fcc286a4 568 if (YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('a')[0], 'dimmed') ||
569 YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('div')[0], 'dimmed_text')) {
354e1130 570 this.hidden = true;
72d28452 571 }
354e1130 572 this.hiddenStored = null;
573
72d28452 574 this.groupmode = null; // Can be null (i.e. does not apply), 0, 1 or 2.
d2a11d46 575
354e1130 576 this.linkContainer = this.getEl().getElementsByTagName('a')[0];
56dc41d0 577 this.divContainer = this.getEl().getElementsByTagName('div')[0];
354e1130 578
579 this.commandContainer = null;
72d28452 580 this.indentLeftButton = null;
581 this.indentRightButton = null;
354e1130 582 this.viewButton = null;
72d28452 583 this.groupButton = null;
d2a11d46 584 this.handle = null;
354e1130 585 this.init_buttons();
586
d2a11d46 587 this.parentObj = parentObj;
354e1130 588
e1c15ef7 589 if (this.debug) {
72d28452 590 YAHOO.log("init_resource "+id+" parent = "+parentObj.getEl().id);
591 }
b532c911 592};
0a0bb380 593
a66ddf19 594
d2a11d46 595/**
596 * The current strategy is to look at the DOM tree to get information on the
597 * resource and it's current mode. This is bad since we are dependant on
598 * the html that is output from serverside logic. Seemingly innocuous changes
599 * like changing the language string for the title of a button will break
600 * our JavaScript here. This is brittle.
601 *
602 * First, we clear the buttons container. Then:
603 * We need to add the new-style move handle.
604 * The old style move button (up/down) needs to be removed.
605 * Move left button (if any) needs an event handler.
606 * Move right button (if any) needs an event handler.
607 * Update button stays as it is. Add it back.
608 * Delete button needs an event handler.
609 * Visible button is a toggle. It needs an event handler too.
610 * Group mode button is a toggle. It needs an event handler too.
611 */
354e1130 612resource_class.prototype.init_buttons = function() {
d2a11d46 613
72d28452 614 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
615 'span', this.getEl())[0];
d2a11d46 616
2469f7ea 617 if (commandContainer == null) {
6f00683e 618 YAHOO.log('Cannot find command container for '+this.getEl().id, 'error');
354e1130 619 return;
0a0bb380 620 }
d2a11d46 621
72d28452 622 // Language strings.
623 var strgroupsnone = main.portal.strings['groupsnone']+' ('+main.portal.strings['clicktochange']+')';
624 var strgroupsseparate = main.portal.strings['groupsseparate']+' ('+main.portal.strings['clicktochange']+')';
625 var strgroupsvisible = main.portal.strings['groupsvisible']+' ('+main.portal.strings['clicktochange']+')';
d2a11d46 626
354e1130 627 this.commandContainer = commandContainer;
72d28452 628 var buttons = commandContainer.getElementsByTagName('a');
354e1130 629
d2a11d46 630 // Buttons that we might need to add back in.
72d28452 631 var moveLeft = false;
632 var moveRight = false;
354e1130 633 var updateButton = null;
1a0d7bd7 634 var assignButton = null;
3440ec12 635
472a4b92 636 // for RTL support
637 var isrtl = (document.getElementsByTagName("html")[0].dir=="rtl");
e1c15ef7 638
6f00683e 639 for (var x=0; x<buttons.length; x++) {
72d28452 640 if (buttons[x].className == 'editing_moveleft') {
d2a11d46 641 moveLeft = true;
90ebdf65 642 } else if (buttons[x].className == 'editing_moveright') {
d2a11d46 643 moveRight = true;
90ebdf65 644 } else if (buttons[x].className == 'editing_update') {
02059f46 645 updateButton = buttons[x].cloneNode(true);
1a0d7bd7
ARN
646 } else if (buttons[x].className == 'editing_assign') {
647 assignButton = buttons[x].cloneNode(true);
90ebdf65 648 } else if (buttons[x].className == 'editing_groupsnone') {
72d28452 649 this.groupmode = this.NOGROUPS;
650 } else if (buttons[x].className == 'editing_groupsseparate') {
651 this.groupmode = this.SEPARATEGROUPS;
652 } else if (buttons[x].className == 'editing_groupsvisible') {
653 this.groupmode = this.VISIBLEGROUPS;
654 }
354e1130 655 }
656
a66ddf19 657 if (updateButton == null) {
72d28452 658 // Update button must always be present.
6f00683e 659 YAHOO.log('Cannot find updateButton for '+this.getEl().id, 'error');
72d28452 660 }
354e1130 661
72d28452 662 // Clear all the buttons.
354e1130 663 commandContainer.innerHTML = '';
664
d2a11d46 665 // Add move-handle for drag and drop.
ddedf979 666 var handleRef = main.mk_button('a', main.portal.icons['move_2d'], main.portal.strings['move'],
a8a44f28 667 [['style', 'cursor:move']], [['class', 'iconsmall']]);
6f00683e 668
669 YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
354e1130 670 this.handle = handleRef;
354e1130 671 commandContainer.appendChild(handleRef);
672 this.setHandleElId(this.handle.id);
673
72d28452 674 // Add indentation buttons if needed (move left, move right).
675 if (moveLeft) {
ddedf979 676 var button = main.mk_button('a', main.portal.icons['backwards'], main.portal.strings['moveleft'],
a8a44f28 677 [['class', 'editing_moveleft']], [['class', 'iconsmall']]);
72d28452 678 YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true);
679 commandContainer.appendChild(button);
680 this.indentLeftButton = button;
681 }
682
683 if (moveRight) {
ddedf979 684 var button = main.mk_button('a', main.portal.icons['forwards'], main.portal.strings['moveright'],
a8a44f28 685 [['class', 'editing_moveright']], [['class', 'iconsmall']]);
72d28452 686 YAHOO.util.Event.addListener(button, 'click', this.indent_right, this, true);
687 commandContainer.appendChild(button);
688 this.indentRightButton = button;
689 }
d2a11d46 690
691 // Add edit button back in.
692 commandContainer.appendChild(updateButton);
354e1130 693
d2a11d46 694 // Add the delete button.
a8a44f28 695 var button = main.mk_button('a', main.portal.icons['delete'], main.portal.strings['delete'], null, [['class', 'iconsmall']]);
6f00683e 696 YAHOO.util.Event.addListener(button, 'click', this.delete_button, this, true);
697 commandContainer.appendChild(button);
354e1130 698
72d28452 699 // Add the hide or show button.
e1c15ef7 700 if (this.hidden) {
a8a44f28 701 var button = main.mk_button('a', main.portal.icons['show'], main.portal.strings['show'], null, [['class', 'iconsmall']]);
72d28452 702 } else {
a8a44f28 703 var button = main.mk_button('a', main.portal.icons['hide'], main.portal.strings['hide'], null, [['class', 'iconsmall']]);
72d28452 704 }
6f00683e 705 YAHOO.util.Event.addListener(button, 'click', this.toggle_hide, this, true);
706 commandContainer.appendChild(button);
354e1130 707 this.viewButton = button;
d2a11d46 708
72d28452 709 // Add the groupmode button if needed.
710 if (this.groupmode != null) {
711 if (this.groupmode == this.NOGROUPS) {
a8a44f28 712 var button = main.mk_button('a', main.portal.icons['groupn'], strgroupsnone, null, [['class', 'iconsmall']]);
72d28452 713 } else if (this.groupmode == this.SEPARATEGROUPS) {
a8a44f28 714 var button = main.mk_button('a', main.portal.icons['groups'], strgroupsseparate, null, [['class', 'iconsmall']]);
72d28452 715 } else {
a8a44f28 716 var button = main.mk_button('a', main.portal.icons['groupv'], strgroupsvisible, null, [['class', 'iconsmall']]);
72d28452 717 }
718 YAHOO.util.Event.addListener(button, 'click', this.toggle_groupmode, this, true);
719 commandContainer.appendChild(button);
720 this.groupButton = button;
721 }
1a0d7bd7
ARN
722
723 // Add the assign roles button back in
724 if (assignButton != null) {
725 commandContainer.appendChild(assignButton);
726 }
b532c911 727};
354e1130 728
a66ddf19 729
22206b67 730resource_class.prototype.indent_left = function() {
731
060cd0c8
SH
732 var indentdiv = YAHOO.util.Dom.getElementsByClassName('mod-indent', 'div', this.getEl())[0];
733 if (!indentdiv) {
72d28452 734 if (this.debug) {
060cd0c8 735 YAHOO.log('Could not indent left: intending div does not exist', 'error');
72d28452 736 }
737 return false;
738 }
218a4659 739 var oldindent = indentdiv.className.match(/mod-indent-(\d{1,})/);
060cd0c8
SH
740 if (oldindent && oldindent[1] > 0) {
741 oldindent = oldindent[1];
72d28452 742 } else {
060cd0c8
SH
743 return false;
744 }
745 var newindent = parseFloat(oldindent) - 1;
746 YAHOO.util.Dom.replaceClass(indentdiv, 'mod-indent-'+oldindent, 'mod-indent-'+newindent);
747 main.connect('POST', 'class=resource&field=indentleft', null, 'id='+this.id);
72d28452 748
060cd0c8 749 if (newindent == 0) {
72d28452 750 // Remove the indent left button as well.
751 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
752 'span', this.getEl())[0];
72d28452 753 commandContainer.removeChild(this.indentLeftButton);
754 this.indentLeftButton = null;
755 }
060cd0c8 756
72d28452 757 return true;
b532c911 758};
22206b67 759
760
761resource_class.prototype.indent_right = function() {
762
060cd0c8
SH
763 var indentdiv = YAHOO.util.Dom.getElementsByClassName('mod-indent', 'div', this.getEl())[0];
764 if (!indentdiv) {
765 if (this.debug) {
766 YAHOO.log('Could not indent left: intending div does not exist', 'error');
767 }
768 return false;
769 }
218a4659 770 var oldindent = indentdiv.className.match(/mod-indent-(\d{1,})/);
060cd0c8
SH
771 if (oldindent && oldindent[1] >= 0) {
772 oldindent = oldindent[1];
773 var newindent = parseFloat(oldindent) + 1;
774 YAHOO.util.Dom.replaceClass(indentdiv, 'mod-indent-'+oldindent, 'mod-indent-'+newindent);
72d28452 775 } else {
060cd0c8 776 YAHOO.util.Dom.addClass(indentdiv, 'mod-indent-1');
72d28452 777 }
060cd0c8 778 main.connect('POST', 'class=resource&field=indentright', null, 'id='+this.id);
72d28452 779
780 if (!this.indentLeftButton) {
060cd0c8
SH
781 // Add a indent left button if none is present.
782 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands', 'span', this.getEl())[0];
ddedf979 783 var button = main.mk_button('a', main.portal.icons['backwards'], main.portal.strings['moveleft'],
a8a44f28 784 [['class', 'editing_moveleft']], [['class', 'iconsmall']]);
72d28452 785 YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true);
786 commandContainer.insertBefore(button, this.indentRightButton);
787 this.indentLeftButton = button;
788 }
060cd0c8 789
72d28452 790 return true;
b532c911 791};
22206b67 792
793
6f00683e 794resource_class.prototype.toggle_hide = function(target, e, superficial, force) {
f8eaeffa 795 var strhide = main.portal.strings['hide'];
796 var strshow = main.portal.strings['show'];
354e1130 797 if (force != null) {
6f00683e 798 if (this.debug) {
72d28452 799 YAHOO.log("Resource "+this.getEl().id+" forced to "+force);
800 }
3440ec12 801 this.hidden = !force;
0a0bb380 802 }
354e1130 803 if (this.hidden) {
6f00683e 804 YAHOO.util.Dom.removeClass(this.linkContainer, 'dimmed');
56dc41d0 805 YAHOO.util.Dom.removeClass(this.divContainer, 'dimmed_text');
424a3530 806 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show/i, 'hide');
f8eaeffa 807 this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strshow, strhide);
808 this.viewButton.title = this.viewButton.title.replace(strshow, strhide);
3440ec12 809 this.hidden = false;
354e1130 810
811 if (!superficial) {
73d402ef 812 main.connect('POST', 'class=resource&field=visible', null, 'value=1&id='+this.id);
1752e584 813 }
354e1130 814 } else {
6f00683e 815 YAHOO.util.Dom.addClass(this.linkContainer, 'dimmed');
56dc41d0 816 YAHOO.util.Dom.addClass(this.divContainer, 'dimmed_text');
424a3530 817 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide/i, 'show');
f8eaeffa 818 this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strhide, strshow);
819 this.viewButton.title = this.viewButton.title.replace(strhide, strshow);
354e1130 820 this.hidden = true;
821
822 if (!superficial) {
73d402ef 823 main.connect('POST', 'class=resource&field=visible', null, 'value=0&id='+this.id);
354e1130 824 }
825 }
b532c911 826};
354e1130 827
a66ddf19 828
81f92309 829resource_class.prototype.groupImages = ['groupn', 'groups', 'groupv'];
d2a11d46 830
831
832resource_class.prototype.toggle_groupmode = function() {
833 this.groupmode++;
834 if (this.groupmode > 2) {
835 this.groupmode = 0;
72d28452 836 }
837
f8eaeffa 838 var newtitle = this.groupButton.title;
cddbd5d5 839
840 switch (this.groupmode) {
841 case 0:
842 newtitle = main.portal.strings['groupsnone']+' ('+main.portal.strings['clicktochange']+')';
843 break;
844 case 1:
845 newtitle = main.portal.strings['groupsseparate']+' ('+main.portal.strings['clicktochange']+')';
846 break;
847 case 2:
848 newtitle = main.portal.strings['groupsvisible']+' ('+main.portal.strings['clicktochange']+')';
849 break;
850 }
f8eaeffa 851
852 this.groupButton.getElementsByTagName('img')[0].alt = newtitle;
853 this.groupButton.title = newtitle;
cddbd5d5 854
ddedf979 855 this.groupButton.getElementsByTagName('img')[0].src = main.portal.icons[this.groupImages[this.groupmode]];
d2a11d46 856 main.connect('POST', 'class=resource&field=groupmode', null, 'value='+this.groupmode+'&id='+this.id);
b532c911 857};
d2a11d46 858
859
354e1130 860resource_class.prototype.delete_button = function() {
446d6e7d 861 if (this.debug) {
72d28452 862 YAHOO.log("Deleting "+this.getEl().id+" from parent "+this.parentObj.getEl().id);
863 }
b31b2d84
DM
864
865 // default fallback to something like 'Resource 42'
866 var modtype = main.getString(this.is);
867 var modname = this.id;
868
869 // try to get less cryptic instance name from DOM
870 if (YAHOO.util.Dom.hasClass(this.getEl(), 'activity')) {
871 if (YAHOO.util.Dom.hasClass(this.getEl(), 'label')) {
872 // mod_label instance
873 modtype = main.getString('modtype_label');
874 modname = '';
875 } else {
876 // other mod instance, get the type first
877 matches = new RegExp(/modtype_(\w+)/).exec(this.getEl().className);
878 if (matches[1] && main.hasString('modtype_' + matches[1])) {
879 modtype = main.getString('modtype_' + matches[1]);
880 }
881 // look for span.instancename content to get the module instance name from it
882 instancename = YAHOO.util.Selector.query('.instancename', this.getEl(), true);
883 if (instancename) {
884 // remove the span.accesshide
885 accesshides = YAHOO.util.Selector.query('.accesshide', instancename);
886 for (x in accesshides) {
887 instancename.removeChild(accesshides[x]);
888 }
889 // strip HTML tags
890 instancenametext = instancename.innerHTML.replace(/<[^>]+>/g, '');
891 // and if anything survived, consider it the instance name
892 if (instancenametext) {
893 modname = instancenametext;
894 }
895 // put span.accesshides back
896 for (x in accesshides) {
897 instancename.appendChild(accesshides[x]);
898 }
899 }
900 }
901 }
902
903 if (modname) {
904 modname = "'" + modname + "'";
905 }
906 if (!confirm(main.getString('deletecheck', modtype + ' ' + modname))) {
446d6e7d 907 return false;
354e1130 908 }
446d6e7d 909 this.parentObj.remove_resource(this);
49c4d8ca 910 main.connect('POST', 'class=resource&action=DELETE&id='+this.id);
b532c911 911};
354e1130 912
a66ddf19 913
354e1130 914resource_class.prototype.update_index = function(index) {
e1c15ef7 915 if (this.debug) {
72d28452 916 YAHOO.log("Updating Index for resource "+this.getEl().id+" to "+index);
917 }
b532c911 918};
354e1130 919
a66ddf19 920
2469f7ea 921resource_class.prototype.startDrag = function(x, y) {
0a0bb380 922 YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
354e1130 923
0a0bb380 924 //reinitialize dd element
72d28452 925 this.getDragEl().innerHTML = '';
354e1130 926
0a0bb380 927 var targets = YAHOO.util.DDM.getRelated(this, true);
e1c15ef7 928 if (this.debug) {
72d28452 929 YAHOO.log(this.id + " startDrag "+targets.length + " targets");
930 }
b532c911 931};
0a0bb380 932
a66ddf19 933
7f933d8f 934resource_class.prototype.clear_move_markers = function(target) {
72d28452 935 if (target.is == 'section') {
936 resources = target.resources;
937 } else {
938 resources = target.parentObj.resources;
939 }
940 for (var i=0; i<resources.length; i++) {
9810d8a0 941 if (resources[i].getEl() != null) {
942 YAHOO.util.Dom.setStyle(resources[i].getEl().id, 'border', 'none');
943 }
72d28452 944 }
b532c911 945};
7f933d8f 946
a66ddf19 947
7f933d8f 948resource_class.prototype.onDragOver = function(e, ids) {
72d28452 949 var target = YAHOO.util.DDM.getBestMatch(ids);
950
951 this.clear_move_markers(target);
952
953 if (target != this && (target.is == 'resource' || target.is == 'activity')) {
954 // Add a top border to show where the drop will place the resource.
955 YAHOO.util.Dom.setStyle(target.getEl().id, 'border-top', '1px solid #BBB');
956 } else if (target.is == 'section' && target.resources.length > 0) {
957 // We need to have a border at the bottom of the last activity in
958 // that section.
9810d8a0 959 if (target.resources[target.resources.length - 1].getEl() != null) {
960 YAHOO.util.Dom.setStyle(target.resources[target.resources.length - 1].getEl().id,
72d28452 961 'border-bottom', '1px solid #BBB');
9810d8a0 962 }
72d28452 963 }
b532c911 964};
354e1130 965
a66ddf19 966
7f933d8f 967resource_class.prototype.onDragOut = function(e, ids) {
72d28452 968 var target = YAHOO.util.DDM.getBestMatch(ids);
969 if (target) {
970 this.clear_move_markers(target);
971 }
b532c911 972};
0a0bb380 973
a66ddf19 974
e50b0ad9 975resource_class.prototype.onDragDrop = function(e, ids) {
72d28452 976 var target = YAHOO.util.DDM.getBestMatch(ids);
977 if (!target) {
978 YAHOO.log('onDragDrop: Target is not valid!', 'error');
979 }
2469f7ea 980
64e5a68d 981 if (this.debug) {
72d28452 982 YAHOO.log("Dropped on section id="+target.sectionId
983 +", el="+this.getEl().id
984 +", x="+YAHOO.util.Dom.getXY( this.getDragEl() ));
985 }
64e5a68d 986 this.parentObj.remove_resource(this);
354e1130 987
64e5a68d 988 if (target.is == 'resource' || target.is == 'activity') {
989 target.parentObj.insert_resource(this, target);
990 } else if (target.is == 'section') {
72d28452 991 target.insert_resource(this);
0a0bb380 992 }
72d28452 993 this.clear_move_markers(target);
0a0bb380 994 return;
b532c911 995};
0a0bb380 996
a66ddf19 997
0a0bb380 998resource_class.prototype.endDrag = function() {
64e5a68d 999 // Eliminates default action
b532c911 1000};
9837bd1d 1001
1002section_class.prototype.swap_dates = function(el){
8c3c518f 1003 var i=1;
1004 var divs = YAHOO.util.Selector.query('div .weekdates');
9837bd1d 1005
8c3c518f 1006 for (div in divs) {
1007 divs[div].innerHTML = main.sectiondates[i];
1008 i++;
9837bd1d 1009 }
b532c911 1010};
9837bd1d 1011