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