weekly release 2.3dev (on-sync version 22_STABLE)
[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') ||
d9b49c23 569 YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('div')[1], '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;
fa820563 634 var duplicateButton = null;
1a0d7bd7 635 var assignButton = null;
3440ec12 636
472a4b92 637 // for RTL support
638 var isrtl = (document.getElementsByTagName("html")[0].dir=="rtl");
e1c15ef7 639
6f00683e 640 for (var x=0; x<buttons.length; x++) {
72d28452 641 if (buttons[x].className == 'editing_moveleft') {
d2a11d46 642 moveLeft = true;
90ebdf65 643 } else if (buttons[x].className == 'editing_moveright') {
d2a11d46 644 moveRight = true;
90ebdf65 645 } else if (buttons[x].className == 'editing_update') {
02059f46 646 updateButton = buttons[x].cloneNode(true);
fa820563
DM
647 } else if (buttons[x].className == 'editing_duplicate') {
648 duplicateButton = buttons[x].cloneNode(true);
1a0d7bd7
ARN
649 } else if (buttons[x].className == 'editing_assign') {
650 assignButton = buttons[x].cloneNode(true);
90ebdf65 651 } else if (buttons[x].className == 'editing_groupsnone') {
72d28452 652 this.groupmode = this.NOGROUPS;
653 } else if (buttons[x].className == 'editing_groupsseparate') {
654 this.groupmode = this.SEPARATEGROUPS;
655 } else if (buttons[x].className == 'editing_groupsvisible') {
656 this.groupmode = this.VISIBLEGROUPS;
657 }
354e1130 658 }
659
a66ddf19 660 if (updateButton == null) {
72d28452 661 // Update button must always be present.
6f00683e 662 YAHOO.log('Cannot find updateButton for '+this.getEl().id, 'error');
72d28452 663 }
354e1130 664
72d28452 665 // Clear all the buttons.
354e1130 666 commandContainer.innerHTML = '';
667
d2a11d46 668 // Add move-handle for drag and drop.
ddedf979 669 var handleRef = main.mk_button('a', main.portal.icons['move_2d'], main.portal.strings['move'],
a8a44f28 670 [['style', 'cursor:move']], [['class', 'iconsmall']]);
6f00683e 671
672 YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
354e1130 673 this.handle = handleRef;
354e1130 674 commandContainer.appendChild(handleRef);
675 this.setHandleElId(this.handle.id);
676
72d28452 677 // Add indentation buttons if needed (move left, move right).
678 if (moveLeft) {
ddedf979 679 var button = main.mk_button('a', main.portal.icons['backwards'], main.portal.strings['moveleft'],
a8a44f28 680 [['class', 'editing_moveleft']], [['class', 'iconsmall']]);
72d28452 681 YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true);
682 commandContainer.appendChild(button);
683 this.indentLeftButton = button;
684 }
685
686 if (moveRight) {
ddedf979 687 var button = main.mk_button('a', main.portal.icons['forwards'], main.portal.strings['moveright'],
a8a44f28 688 [['class', 'editing_moveright']], [['class', 'iconsmall']]);
72d28452 689 YAHOO.util.Event.addListener(button, 'click', this.indent_right, this, true);
690 commandContainer.appendChild(button);
691 this.indentRightButton = button;
692 }
d2a11d46 693
694 // Add edit button back in.
695 commandContainer.appendChild(updateButton);
354e1130 696
fa820563
DM
697 if (duplicateButton) {
698 commandContainer.appendChild(duplicateButton);
699 }
700
d2a11d46 701 // Add the delete button.
a8a44f28 702 var button = main.mk_button('a', main.portal.icons['delete'], main.portal.strings['delete'], null, [['class', 'iconsmall']]);
6f00683e 703 YAHOO.util.Event.addListener(button, 'click', this.delete_button, this, true);
704 commandContainer.appendChild(button);
354e1130 705
72d28452 706 // Add the hide or show button.
e1c15ef7 707 if (this.hidden) {
a8a44f28 708 var button = main.mk_button('a', main.portal.icons['show'], main.portal.strings['show'], null, [['class', 'iconsmall']]);
72d28452 709 } else {
a8a44f28 710 var button = main.mk_button('a', main.portal.icons['hide'], main.portal.strings['hide'], null, [['class', 'iconsmall']]);
72d28452 711 }
6f00683e 712 YAHOO.util.Event.addListener(button, 'click', this.toggle_hide, this, true);
713 commandContainer.appendChild(button);
354e1130 714 this.viewButton = button;
d2a11d46 715
72d28452 716 // Add the groupmode button if needed.
717 if (this.groupmode != null) {
718 if (this.groupmode == this.NOGROUPS) {
a8a44f28 719 var button = main.mk_button('a', main.portal.icons['groupn'], strgroupsnone, null, [['class', 'iconsmall']]);
72d28452 720 } else if (this.groupmode == this.SEPARATEGROUPS) {
a8a44f28 721 var button = main.mk_button('a', main.portal.icons['groups'], strgroupsseparate, null, [['class', 'iconsmall']]);
72d28452 722 } else {
a8a44f28 723 var button = main.mk_button('a', main.portal.icons['groupv'], strgroupsvisible, null, [['class', 'iconsmall']]);
72d28452 724 }
725 YAHOO.util.Event.addListener(button, 'click', this.toggle_groupmode, this, true);
726 commandContainer.appendChild(button);
727 this.groupButton = button;
728 }
1a0d7bd7
ARN
729
730 // Add the assign roles button back in
731 if (assignButton != null) {
732 commandContainer.appendChild(assignButton);
733 }
b532c911 734};
354e1130 735
a66ddf19 736
22206b67 737resource_class.prototype.indent_left = function() {
738
060cd0c8
SH
739 var indentdiv = YAHOO.util.Dom.getElementsByClassName('mod-indent', 'div', this.getEl())[0];
740 if (!indentdiv) {
72d28452 741 if (this.debug) {
060cd0c8 742 YAHOO.log('Could not indent left: intending div does not exist', 'error');
72d28452 743 }
744 return false;
745 }
218a4659 746 var oldindent = indentdiv.className.match(/mod-indent-(\d{1,})/);
060cd0c8
SH
747 if (oldindent && oldindent[1] > 0) {
748 oldindent = oldindent[1];
72d28452 749 } else {
060cd0c8
SH
750 return false;
751 }
752 var newindent = parseFloat(oldindent) - 1;
753 YAHOO.util.Dom.replaceClass(indentdiv, 'mod-indent-'+oldindent, 'mod-indent-'+newindent);
754 main.connect('POST', 'class=resource&field=indentleft', null, 'id='+this.id);
72d28452 755
060cd0c8 756 if (newindent == 0) {
72d28452 757 // Remove the indent left button as well.
758 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
759 'span', this.getEl())[0];
72d28452 760 commandContainer.removeChild(this.indentLeftButton);
761 this.indentLeftButton = null;
762 }
060cd0c8 763
72d28452 764 return true;
b532c911 765};
22206b67 766
767
768resource_class.prototype.indent_right = function() {
769
060cd0c8
SH
770 var indentdiv = YAHOO.util.Dom.getElementsByClassName('mod-indent', 'div', this.getEl())[0];
771 if (!indentdiv) {
772 if (this.debug) {
773 YAHOO.log('Could not indent left: intending div does not exist', 'error');
774 }
775 return false;
776 }
218a4659 777 var oldindent = indentdiv.className.match(/mod-indent-(\d{1,})/);
060cd0c8
SH
778 if (oldindent && oldindent[1] >= 0) {
779 oldindent = oldindent[1];
780 var newindent = parseFloat(oldindent) + 1;
781 YAHOO.util.Dom.replaceClass(indentdiv, 'mod-indent-'+oldindent, 'mod-indent-'+newindent);
72d28452 782 } else {
060cd0c8 783 YAHOO.util.Dom.addClass(indentdiv, 'mod-indent-1');
72d28452 784 }
060cd0c8 785 main.connect('POST', 'class=resource&field=indentright', null, 'id='+this.id);
72d28452 786
787 if (!this.indentLeftButton) {
060cd0c8
SH
788 // Add a indent left button if none is present.
789 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands', 'span', this.getEl())[0];
ddedf979 790 var button = main.mk_button('a', main.portal.icons['backwards'], main.portal.strings['moveleft'],
a8a44f28 791 [['class', 'editing_moveleft']], [['class', 'iconsmall']]);
72d28452 792 YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true);
793 commandContainer.insertBefore(button, this.indentRightButton);
794 this.indentLeftButton = button;
795 }
060cd0c8 796
72d28452 797 return true;
b532c911 798};
22206b67 799
800
6f00683e 801resource_class.prototype.toggle_hide = function(target, e, superficial, force) {
f8eaeffa 802 var strhide = main.portal.strings['hide'];
803 var strshow = main.portal.strings['show'];
354e1130 804 if (force != null) {
6f00683e 805 if (this.debug) {
72d28452 806 YAHOO.log("Resource "+this.getEl().id+" forced to "+force);
807 }
3440ec12 808 this.hidden = !force;
0a0bb380 809 }
354e1130 810 if (this.hidden) {
6f00683e 811 YAHOO.util.Dom.removeClass(this.linkContainer, 'dimmed');
56dc41d0 812 YAHOO.util.Dom.removeClass(this.divContainer, 'dimmed_text');
424a3530 813 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show/i, 'hide');
f8eaeffa 814 this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strshow, strhide);
815 this.viewButton.title = this.viewButton.title.replace(strshow, strhide);
3440ec12 816 this.hidden = false;
354e1130 817
818 if (!superficial) {
73d402ef 819 main.connect('POST', 'class=resource&field=visible', null, 'value=1&id='+this.id);
1752e584 820 }
354e1130 821 } else {
6f00683e 822 YAHOO.util.Dom.addClass(this.linkContainer, 'dimmed');
56dc41d0 823 YAHOO.util.Dom.addClass(this.divContainer, 'dimmed_text');
424a3530 824 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide/i, 'show');
f8eaeffa 825 this.viewButton.childNodes[0].alt = this.viewButton.childNodes[0].alt.replace(strhide, strshow);
826 this.viewButton.title = this.viewButton.title.replace(strhide, strshow);
354e1130 827 this.hidden = true;
828
829 if (!superficial) {
73d402ef 830 main.connect('POST', 'class=resource&field=visible', null, 'value=0&id='+this.id);
354e1130 831 }
832 }
b532c911 833};
354e1130 834
a66ddf19 835
81f92309 836resource_class.prototype.groupImages = ['groupn', 'groups', 'groupv'];
d2a11d46 837
838
839resource_class.prototype.toggle_groupmode = function() {
840 this.groupmode++;
841 if (this.groupmode > 2) {
842 this.groupmode = 0;
72d28452 843 }
844
f8eaeffa 845 var newtitle = this.groupButton.title;
cddbd5d5 846
847 switch (this.groupmode) {
848 case 0:
849 newtitle = main.portal.strings['groupsnone']+' ('+main.portal.strings['clicktochange']+')';
850 break;
851 case 1:
852 newtitle = main.portal.strings['groupsseparate']+' ('+main.portal.strings['clicktochange']+')';
853 break;
854 case 2:
855 newtitle = main.portal.strings['groupsvisible']+' ('+main.portal.strings['clicktochange']+')';
856 break;
857 }
f8eaeffa 858
859 this.groupButton.getElementsByTagName('img')[0].alt = newtitle;
860 this.groupButton.title = newtitle;
cddbd5d5 861
ddedf979 862 this.groupButton.getElementsByTagName('img')[0].src = main.portal.icons[this.groupImages[this.groupmode]];
d2a11d46 863 main.connect('POST', 'class=resource&field=groupmode', null, 'value='+this.groupmode+'&id='+this.id);
b532c911 864};
d2a11d46 865
866
354e1130 867resource_class.prototype.delete_button = function() {
446d6e7d 868 if (this.debug) {
72d28452 869 YAHOO.log("Deleting "+this.getEl().id+" from parent "+this.parentObj.getEl().id);
870 }
b31b2d84
DM
871
872 // default fallback to something like 'Resource 42'
873 var modtype = main.getString(this.is);
874 var modname = this.id;
875
876 // try to get less cryptic instance name from DOM
877 if (YAHOO.util.Dom.hasClass(this.getEl(), 'activity')) {
878 if (YAHOO.util.Dom.hasClass(this.getEl(), 'label')) {
879 // mod_label instance
880 modtype = main.getString('modtype_label');
881 modname = '';
882 } else {
883 // other mod instance, get the type first
884 matches = new RegExp(/modtype_(\w+)/).exec(this.getEl().className);
885 if (matches[1] && main.hasString('modtype_' + matches[1])) {
886 modtype = main.getString('modtype_' + matches[1]);
887 }
888 // look for span.instancename content to get the module instance name from it
889 instancename = YAHOO.util.Selector.query('.instancename', this.getEl(), true);
890 if (instancename) {
891 // remove the span.accesshide
892 accesshides = YAHOO.util.Selector.query('.accesshide', instancename);
893 for (x in accesshides) {
894 instancename.removeChild(accesshides[x]);
895 }
896 // strip HTML tags
897 instancenametext = instancename.innerHTML.replace(/<[^>]+>/g, '');
898 // and if anything survived, consider it the instance name
899 if (instancenametext) {
900 modname = instancenametext;
901 }
902 // put span.accesshides back
903 for (x in accesshides) {
904 instancename.appendChild(accesshides[x]);
905 }
906 }
907 }
908 }
909
910 if (modname) {
911 modname = "'" + modname + "'";
912 }
913 if (!confirm(main.getString('deletecheck', modtype + ' ' + modname))) {
446d6e7d 914 return false;
354e1130 915 }
446d6e7d 916 this.parentObj.remove_resource(this);
49c4d8ca 917 main.connect('POST', 'class=resource&action=DELETE&id='+this.id);
b532c911 918};
354e1130 919
a66ddf19 920
354e1130 921resource_class.prototype.update_index = function(index) {
e1c15ef7 922 if (this.debug) {
72d28452 923 YAHOO.log("Updating Index for resource "+this.getEl().id+" to "+index);
924 }
b532c911 925};
354e1130 926
a66ddf19 927
2469f7ea 928resource_class.prototype.startDrag = function(x, y) {
0a0bb380 929 YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
354e1130 930
0a0bb380 931 //reinitialize dd element
72d28452 932 this.getDragEl().innerHTML = '';
354e1130 933
0a0bb380 934 var targets = YAHOO.util.DDM.getRelated(this, true);
e1c15ef7 935 if (this.debug) {
72d28452 936 YAHOO.log(this.id + " startDrag "+targets.length + " targets");
937 }
b532c911 938};
0a0bb380 939
a66ddf19 940
7f933d8f 941resource_class.prototype.clear_move_markers = function(target) {
72d28452 942 if (target.is == 'section') {
943 resources = target.resources;
944 } else {
945 resources = target.parentObj.resources;
946 }
947 for (var i=0; i<resources.length; i++) {
9810d8a0 948 if (resources[i].getEl() != null) {
949 YAHOO.util.Dom.setStyle(resources[i].getEl().id, 'border', 'none');
950 }
72d28452 951 }
b532c911 952};
7f933d8f 953
a66ddf19 954
7f933d8f 955resource_class.prototype.onDragOver = function(e, ids) {
72d28452 956 var target = YAHOO.util.DDM.getBestMatch(ids);
957
958 this.clear_move_markers(target);
959
960 if (target != this && (target.is == 'resource' || target.is == 'activity')) {
961 // Add a top border to show where the drop will place the resource.
962 YAHOO.util.Dom.setStyle(target.getEl().id, 'border-top', '1px solid #BBB');
963 } else if (target.is == 'section' && target.resources.length > 0) {
964 // We need to have a border at the bottom of the last activity in
965 // that section.
9810d8a0 966 if (target.resources[target.resources.length - 1].getEl() != null) {
967 YAHOO.util.Dom.setStyle(target.resources[target.resources.length - 1].getEl().id,
72d28452 968 'border-bottom', '1px solid #BBB');
9810d8a0 969 }
72d28452 970 }
b532c911 971};
354e1130 972
a66ddf19 973
7f933d8f 974resource_class.prototype.onDragOut = function(e, ids) {
72d28452 975 var target = YAHOO.util.DDM.getBestMatch(ids);
976 if (target) {
977 this.clear_move_markers(target);
978 }
b532c911 979};
0a0bb380 980
a66ddf19 981
e50b0ad9 982resource_class.prototype.onDragDrop = function(e, ids) {
72d28452 983 var target = YAHOO.util.DDM.getBestMatch(ids);
984 if (!target) {
985 YAHOO.log('onDragDrop: Target is not valid!', 'error');
986 }
2469f7ea 987
64e5a68d 988 if (this.debug) {
72d28452 989 YAHOO.log("Dropped on section id="+target.sectionId
990 +", el="+this.getEl().id
991 +", x="+YAHOO.util.Dom.getXY( this.getDragEl() ));
992 }
64e5a68d 993 this.parentObj.remove_resource(this);
354e1130 994
64e5a68d 995 if (target.is == 'resource' || target.is == 'activity') {
996 target.parentObj.insert_resource(this, target);
997 } else if (target.is == 'section') {
72d28452 998 target.insert_resource(this);
0a0bb380 999 }
72d28452 1000 this.clear_move_markers(target);
0a0bb380 1001 return;
b532c911 1002};
0a0bb380 1003
a66ddf19 1004
0a0bb380 1005resource_class.prototype.endDrag = function() {
64e5a68d 1006 // Eliminates default action
b532c911 1007};
9837bd1d 1008
1009section_class.prototype.swap_dates = function(el){
8c3c518f 1010 var i=1;
1011 var divs = YAHOO.util.Selector.query('div .weekdates');
9837bd1d 1012
8c3c518f 1013 for (div in divs) {
1014 divs[div].innerHTML = main.sectiondates[i];
1015 i++;
9837bd1d 1016 }
b532c911 1017};
9837bd1d 1018