fix for MDL-7595, adding coursemanager config variable to determine what roles are...
[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.
14 *
e1c15ef7 15 * $Id$
0a0bb380 16 */
354e1130 17
18
e1c15ef7 19/**
20 * section_class
21 */
22function section_class(id, group, config, isDraggable) {
23 this.init_section(id, group, config, isDraggable);
0a0bb380 24}
e1c15ef7 25
0a0bb380 26YAHOO.extend(section_class, YAHOO.util.DDProxy);
27
db5308e2 28
9569653d 29section_class.prototype.debug = false;
d4df8fdc 30
db5308e2 31
e1c15ef7 32section_class.prototype.init_section = function(id, group, config, isDraggable) {
33
34 if (!id) {
35 return;
36 }
354e1130 37
db5308e2 38 this.is = 'section';
3203f104 39 this.sectionId = null; // Section number. This is NOT the section id from
40 // the database.
354e1130 41
3203f104 42 if (!isDraggable) {
e1c15ef7 43 this.initTarget(id, group, config);
354e1130 44 this.removeFromGroup('sections');
e1c15ef7 45 } else {
46 this.init(id, group, config);
47 this.handle = null;
0a0bb380 48 }
354e1130 49
64e5a68d 50 this.createFrame();
354e1130 51 this.isTarget = true;
52
53 this.resources = [];
3203f104 54 this.numberDisplay = null; // Used to display the section number on the top left
55 // of the section. Not used in all course formats.
354e1130 56 this.summary = null;
57 this.content_td = null;
58 this.hidden = false;
59 this.highlighted = false;
60 this.showOnly = false;
db5308e2 61 this.resources_ul = null;
354e1130 62 this.process_section();
63
64 this.viewButton = null;
65 this.highlightButton = null;
66 this.showOnlyButton = null;
67 this.init_buttons();
68
e1c15ef7 69 if (isDraggable) {
70 this.add_handle();
71 }
72 if (this.debug) {
73 YAHOO.log("init_section "+id+" draggable="+isDraggable);
74 }
75 if (YAHOO.util.Dom.hasClass(this.getEl(),'hidden')) {
354e1130 76 this.toggle_hide(null,null,true);
e1c15ef7 77 }
354e1130 78}
79
80section_class.prototype.init_buttons = function() {
81 var commandContainer = this.getEl().childNodes[2];
82
6f00683e 83 //clear all but show only button
354e1130 84 var commandContainerCount = commandContainer.childNodes.length;
e1c15ef7 85 for (var i=(commandContainerCount-1); i>0; i--) {
354e1130 86 commandContainer.removeChild(commandContainer.childNodes[i])
0a0bb380 87 }
88
354e1130 89 if (!this.isWeekFormat) {
d8158863 90 var highlightbutton = main.mk_button('div', '/i/marker.gif');
e1c15ef7 91 YAHOO.util.Event.addListener(highlightbutton, 'click', this.mk_marker, this, true);
354e1130 92 commandContainer.appendChild(highlightbutton);
93 this.highlightButton = highlightbutton;
94 }
d8158863 95 var viewbutton = main.mk_button('div', '/i/hide.gif');
e1c15ef7 96 YAHOO.util.Event.addListener(viewbutton, 'click', this.toggle_hide, this,true);
354e1130 97 commandContainer.appendChild(viewbutton);
98 this.viewButton = viewbutton;
99}
100
354e1130 101section_class.prototype.add_handle = function() {
d8158863 102 var handleRef = main.mk_button('a', '/i/move_2d.gif', [['style','cursor:move']]);
e1c15ef7 103 YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
354e1130 104
105 this.handle = handleRef;
106
107 this.getEl().childNodes[0].appendChild(handleRef);
108 this.setHandleElId(this.handle.id);
109}
eba88175 110
354e1130 111section_class.prototype.process_section = function() {
112 this.content_td = this.getEl().childNodes[1];
113
114 if (YAHOO.util.Dom.hasClass(this.getEl(),'current')) {
115 this.highlighted = true;
116 main.marker = this;
0a0bb380 117 }
eba88175 118
354e1130 119 //create holder for display number for access later
120
121 this.numberDisplay = document.createElement('div');
122 this.numberDisplay.innerHTML = this.getEl().childNodes[0].innerHTML;
123 this.getEl().childNodes[0].innerHTML = '';
124 this.getEl().childNodes[0].appendChild(this.numberDisplay);
125
3203f104 126 this.sectionId = this.id.replace(/section-/i, ''); // Okay, we will have to change this if we
127 // ever change the id attributes format
128 // for the sections.
eba88175 129
354e1130 130 if (this.debug)YAHOO.log("Creating section "+this.getEl().id+" in position "+this.sectionId);
131 //find/edit resources
132
133 this.resources_ul = this.content_td.getElementsByTagName('ul')[0];
6f00683e 134 if (!this.resources_ul) {
354e1130 135 this.resources_ul = document.createElement('ul');
136 this.resources_ul.className='section';
e1c15ef7 137 this.content_td.insertBefore(this.resources_ul, this.content_td.childNodes[1]);
d4df8fdc 138 }
354e1130 139 var resource_count = this.resources_ul.getElementsByTagName('li').length;
140
141 for (var i=0;i<resource_count;i++) {
142 var resource = this.resources_ul.getElementsByTagName('li')[i];
e1c15ef7 143 if (YAHOO.util.Dom.hasClass(resource, 'resource')) {
144 this.resources[this.resources.length] = new resource_class(resource.id, 'resources', null, this);
7f933d8f 145 if (this.debug) {
146 YAHOO.log("Found resource");
147 }
354e1130 148 } else {
e1c15ef7 149 this.resources[this.resources.length] = new activity_class(resource.id, 'resources', null, this);
354e1130 150 }
7f933d8f 151 }
e1c15ef7 152 this.summary = YAHOO.util.Dom.getElementsByClassName('summary', null, this.getEl())[0].firstChild.data || '';
354e1130 153}
154
0a0bb380 155section_class.prototype.startDrag = function(x, y) {
156 //operates in point mode
157 YAHOO.util.DDM.mode = YAHOO.util.DDM.POINT;
354e1130 158
0a0bb380 159 //remove from resources group temporarily
160 this.removeFromGroup('resources');
354e1130 161
0a0bb380 162 //reinitialize dd element
163 this.getDragEl().innerHTML = '';
0a0bb380 164
354e1130 165 var targets = YAHOO.util.DDM.getRelated(this, true);
e1c15ef7 166 if (this.debug)YAHOO.log(this.id + " startDrag, "+targets.length + " targets");
354e1130 167}
0a0bb380 168
169section_class.prototype.onDragDrop = function(e, id) {
170 // get the drag and drop object that was targeted
171 var target = YAHOO.util.DDM.getDDById(id);
172
6f00683e 173 if (this.debug) {
174 YAHOO.log("Section dropped on id="+id+" (I am "+this.getEl().id+") x="+YAHOO.util.Dom.getXY(this.getDragEl()));
175 }
0a0bb380 176 this.move_to_section(target);
354e1130 177
178 //add back to resources group
3203f104 179 this.addToGroup('resources');
354e1130 180}
181section_class.prototype.endDrag = function() {
182 //nessicary to defeat default action
183
184 //add back to resources group
0a0bb380 185 this.addToGroup('resources');
354e1130 186}
187
188section_class.prototype.move_to_section = function(target) {
189 var tempTd = document.createElement('td');
190 var tempStore = null;
191 var sectionCount = main.sections.length;
192 var found = null;
193
194 //determine if original is above or below target and adjust loop
6f00683e 195 var oIndex = main.get_section_index(this);
196 var tIndex = main.get_section_index(target);
354e1130 197
6f00683e 198 if (this.debug) {
199 YAHOO.log("original is at: "+oIndex+" target is at:"+tIndex+" of "+(sectionCount-1));
200 }
354e1130 201 if (oIndex < tIndex) {
202 var loopCondition = 'i<sectionCount';
203 var loopStart = 1;
204 var loopInc = 'i++';
6f00683e 205 var loopmodifier = 'i - 1';
354e1130 206 } else {
6f00683e 207 var loopCondition = 'i > 0';
208 var loopStart = sectionCount - 1;
354e1130 209 var loopInc = 'i--';
6f00683e 210 var loopmodifier = 'i + 1';
0a0bb380 211 }
354e1130 212
213 //move on backend
73d402ef 214 main.connect('POST','class=section&field=move',null,'id='+this.sectionId+'&value='
2469f7ea 215 +(target.sectionId - this.sectionId));
354e1130 216
217 //move on front end
db5308e2 218 for (var i=loopStart; eval(loopCondition); eval(loopInc)) {
354e1130 219
6f00683e 220 if ((main.sections[i] == this) && !found) {
354e1130 221 //enounter with original node
6f00683e 222 if (this.debug) {
223 YAHOO.log("Found Original "+main.sections[i].getEl().id);
224 }
354e1130 225 if (main.sections[i] == this) {
db5308e2 226 found = true;
354e1130 227 }
354e1130 228 } else if (main.sections[i] == target) {
229 //encounter with target node
6f00683e 230 if (this.debug) {
231 YAHOO.log("Found target "+main.sections[i].getEl().id);
232 }
354e1130 233 main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]);
234 found = false;
235 break;
354e1130 236 } else if (found) {
db5308e2 237 //encounter with nodes inbetween
354e1130 238 main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]);
db5308e2 239 }
240 }
354e1130 241}
242
354e1130 243section_class.prototype.swap_with_section = function(sectionIn) {
244 var tmpStore = null;
245
246 thisIndex = main.get_section_index(this);
247 targetIndex = main.get_section_index(sectionIn);
248 main.sections[targetIndex] = this;
249 main.sections[thisIndex] = sectionIn;
250
251 this.changeId(targetIndex);
252 sectionIn.changeId(thisIndex);
253
6f00683e 254 if (this.debug) {
255 YAHOO.log("Swapping "+this.getEl().id+" with "+sectionIn.getEl().id);
256 }
db5308e2 257 YAHOO.util.DDM.swapNode(this.getEl(), sectionIn.getEl());
354e1130 258}
259
260section_class.prototype.toggle_hide = function(e,target,superficial) {
261 if (this.hidden) {
6f00683e 262 YAHOO.util.Dom.removeClass(this.getEl(), 'hidden');
263 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show.gif/i, 'hide.gif');
354e1130 264 this.hidden = false;
265
266 if (!superficial) {
73d402ef 267 main.connect('POST', 'class=section&field=visible', null, 'value=1&id='+this.sectionId);
6f00683e 268 for (var x=0; x<this.resources.length; x++) {
269 this.resources[x].toggle_hide(null, null, true, this.resources[x].hiddenStored);
354e1130 270 this.resources[x].hiddenStored = null;
271 }
272 }
273
274 } else {
6f00683e 275 YAHOO.util.Dom.addClass(this.getEl(), 'hidden');
276 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide.gif/i, 'show.gif');
354e1130 277 this.hidden = true;
278
279 if (!superficial) {
73d402ef 280 main.connect('POST', 'class=section&field=visible', null, 'value=0&id='+this.sectionId);
6f00683e 281 for (var x=0; x<this.resources.length; x++) {
354e1130 282 this.resources[x].hiddenStored = this.resources[x].hidden;
6f00683e 283 this.resources[x].toggle_hide(null, null, true, true);
7f933d8f 284 }
354e1130 285 }
d4df8fdc 286 }
354e1130 287}
0a0bb380 288
354e1130 289section_class.prototype.toggle_highlight = function() {
290 if (this.highlighted) {
6f00683e 291 YAHOO.util.Dom.removeClass(this.getEl(), 'current');
354e1130 292 this.highlighted = false;
293 } else {
6f00683e 294 YAHOO.util.Dom.addClass(this.getEl(), 'current');
354e1130 295 this.highlighted = true;
0a0bb380 296 }
354e1130 297}
0a0bb380 298
354e1130 299section_class.prototype.mk_marker = function() {
300 if (main.marker != this) {
301 main.update_marker(this);
302
303 } else {//if currently the marker
304 main.marker = null;
305
73d402ef 306 main.connect('POST', 'class=course&field=marker', null, 'value=0');
354e1130 307 this.toggle_highlight();
354e1130 308 }
88c5092a 309}
354e1130 310
311section_class.prototype.changeId = function(newId) {
312 this.sectionId = newId;
88c5092a 313 this.numberDisplay.firstChild.data = newId;
354e1130 314
73d402ef 315 //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 316
317 if (main.marker == this) {
88c5092a 318 main.update_marker(this);
d4df8fdc 319 }
354e1130 320}
321
322section_class.prototype.get_resource_index = function(el) {
6f00683e 323 for (var x=0; x<this.resources.length; x++) {
324 if (this.resources[x] == el) {
354e1130 325 return x;
e1c15ef7 326 }
327 }
6f00683e 328 YAHOO.log("Could not find resource to remove "+el.getEl().id, "error");
354e1130 329 return -1;
330}
331
332section_class.prototype.remove_resource = function(el) {
333 var resourceCount = this.resources.length;
e1c15ef7 334
354e1130 335 if (resourceCount == 1) {
6f00683e 336 if (this.resources[0] == el) {
337 this.resources = new Array();
338 }
354e1130 339 } else {
340 var found = false;
6f00683e 341 for (var i=0; i<resourceCount; i++) {
354e1130 342 if (found) {
6f00683e 343 this.resources[i - 1] = this.resources[i];
344 if (i == resourceCount - 1) {
345 this.resources = this.resources.slice(0, -1);
354e1130 346 resourceCount--;
0a0bb380 347 }
6f00683e 348 this.resources[i - 1].update_index(i - 1);
349 } else if (this.resources[i] == el) {
354e1130 350 found = true;
0a0bb380 351 }
352 }
354e1130 353 }
354e1130 354 //remove "text" nodes to keep DOM clean
355 var childIndex = null;
356 var childrenCount = this.resources_ul.childNodes.length;
6f00683e 357 for (var i=0; i<childrenCount; i++) {
358 if (this.resources_ul.childNodes[i] == el.getEl()) {
354e1130 359 childIndex = i;
6f00683e 360 }
361 }
362 if (childIndex > 0 && childIndex < this.resources_ul.childNodes.length) {
363 this.resources_ul.removeChild(this.resources_ul.childNodes[childIndex - 1]);
364 }
365 YAHOO.log("Removing "+el.getEl().id);
366 if (el.getEl().parentNode != null) {
367 el.getEl().parentNode.removeChild(el.getEl());
368 }
e1c15ef7 369 this.write_sequence_list();
354e1130 370}
371
64e5a68d 372section_class.prototype.insert_resource = function(el, targetel) {
354e1130 373 var resourcecount = this.resources.length;
374 var found = false;
375 var tempStore = nextStore = null;
376
377 //update in backend
2469f7ea 378 var targetId = '';
6f00683e 379 if (targetel) {
64e5a68d 380 targetId = targetel.id;
381 }
2469f7ea 382 if (this.debug) {
383 YAHOO.log('id='+el.id+', beforeId='+targetId+', sectionId='+this.sectionId);
384 }
73d402ef 385 main.connect('POST', 'class=resource&field=move', null,
2469f7ea 386 'id='+el.id+'&beforeId='+targetId+'&sectionId='+this.sectionId);
354e1130 387
388 //if inserting into a hidden resource hide
389 if (this.hidden) {
390 el.hiddenStored = el.hidden;
6f00683e 391 el.toggle_hide(null, null, true, true);
354e1130 392 } else {
393 if (el.hiddenStored != null) {
6f00683e 394 el.toggle_hide(null, null, true, el.hiddenStored);
88c5092a 395 el.hiddenStored = null;
62cb4032 396 }
354e1130 397 }
354e1130 398 //update model
6f00683e 399 if (!targetel) {
354e1130 400 this.resources[this.resources.length] = el;
db5308e2 401 } else {
6f00683e 402 for (var i=0; i<resourcecount; i++) {
354e1130 403 if (found) {
404 tempStore = this.resources[i];
405 this.resources[i] = nextStore;
db5308e2 406 nextStore = tempStore;
6f00683e 407
354e1130 408 if (nextStore != null)
0a0bb380 409 nextStore.update_index(i+1);
0a0bb380 410
354e1130 411 } else if (this.resources[i] == targetel) {
412 found = true;
413 nextStore = this.resources[i];
414 this.resources[i] = el;
415 resourcecount++;
416
6f00683e 417 this.resources[i].update_index(i, this.ident);
418 nextStore.update_index(i + 1);
354e1130 419 }
420 }
db5308e2 421 }
6f00683e 422 //update on frontend
423 if (targetel) {
424 this.resources_ul.insertBefore(el.getEl(), targetel.getEl());
425 this.resources_ul.insertBefore(document.createTextNode(' '), targetel.getEl());
354e1130 426 } else {
427 this.resources_ul.appendChild(el.getEl());
6f00683e 428 this.resources_ul.appendChild(document.createTextNode(' '));
0a0bb380 429 }
354e1130 430 el.parentObj = this;
431}
432
433section_class.prototype.write_sequence_list = function(toReturn) {
434 var listOutput = '';
e1c15ef7 435
6f00683e 436 for (var i=0; i<this.resources.length; i++) {
354e1130 437 listOutput += this.resources[i].id;
e1c15ef7 438 if (i != (this.resources.length-1)) {
354e1130 439 listOutput += ',';
e1c15ef7 440 }
354e1130 441 }
db5308e2 442 if (toReturn) {
354e1130 443 return listOutput;
db5308e2 444 }
e1c15ef7 445}
354e1130 446
0a0bb380 447
354e1130 448
e1c15ef7 449/**
450 * resource_class extends util.DDProxy
451 */
354e1130 452function resource_class(id,group,config,parentObj) {
0a0bb380 453
454 this.init_resource(id,group,config,parentObj);
455}
e1c15ef7 456
0a0bb380 457YAHOO.extend(resource_class, YAHOO.util.DDProxy);
458
e1c15ef7 459
9569653d 460resource_class.prototype.debug = false;
d4df8fdc 461
e1c15ef7 462
6f00683e 463resource_class.prototype.init_resource = function(id, group, config, parentObj) {
354e1130 464 if (!id) {
6f00683e 465 YAHOO.log("Init resource, NO ID FOUND!", 'error');
354e1130 466 return;
467 }
354e1130 468 this.is = 'resource';
6f00683e 469 this.init(id, group, config);
354e1130 470 this.createFrame();
471 this.isTarget = true;
eba88175 472
6f00683e 473 this.id = this.getEl().id.replace(/module-/i, '');
eba88175 474
354e1130 475 this.hidden = false;
6f00683e 476 if (YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('a')[0], 'dimmed')) {
354e1130 477 this.hidden = true;
e1c15ef7 478 }
354e1130 479 this.hiddenStored = null;
480
481 this.linkContainer = this.getEl().getElementsByTagName('a')[0];
482
483 this.commandContainer = null;
484 this.viewButton = null;
485 this.handle = null;
486 this.init_buttons();
487
488 this.parentObj = parentObj;
489
e1c15ef7 490 if (this.debug) {
491 YAHOO.log("init_resource "+id+" parent = "+parentObj.getEl().id);
492 }
354e1130 493}
0a0bb380 494
354e1130 495resource_class.prototype.init_buttons = function() {
6f00683e 496 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands', 'span', this.getEl())[0];
2469f7ea 497 if (commandContainer == null) {
6f00683e 498 YAHOO.log('Cannot find command container for '+this.getEl().id, 'error');
354e1130 499 return;
0a0bb380 500 }
354e1130 501 this.commandContainer = commandContainer;
502
503 //find edit button
504 var updateButton = null;
6f00683e 505 var buttons = commandContainer.getElementsByTagName('a');
e1c15ef7 506
6f00683e 507 for (var x=0; x<buttons.length; x++) {
354e1130 508 if (buttons[x].title == main.portal.strings['update']) {
02059f46 509 updateButton = buttons[x].cloneNode(true);
354e1130 510 }
511 }
512
513 if (updateButton == null)
6f00683e 514 YAHOO.log('Cannot find updateButton for '+this.getEl().id, 'error');
354e1130 515
516 commandContainer.innerHTML = '';
517
354e1130 518 //add move-handle
d8158863 519 var handleRef = main.mk_button('a', '/i/move_2d.gif',
6f00683e 520 [['style', 'cursor:move']], [['height', '11'], ['width', '11'],
521 ['hspace', '2'], ['border', '0']]);
522
523 YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
354e1130 524 this.handle = handleRef;
354e1130 525 commandContainer.appendChild(handleRef);
526 this.setHandleElId(this.handle.id);
527
354e1130 528 //add edit button back in
529 commandContainer.appendChild(updateButton);
530
531 //add rest
d8158863 532 var button = main.mk_button('a', '/t/delete.gif');
6f00683e 533 YAHOO.util.Event.addListener(button, 'click', this.delete_button, this, true);
534 commandContainer.appendChild(button);
354e1130 535
e1c15ef7 536 if (this.hidden) {
d8158863 537 var button = main.mk_button('a', '/t/show.gif');
e1c15ef7 538 } else {
d8158863 539 var button = main.mk_button('a', '/t/hide.gif');
e1c15ef7 540 }
6f00683e 541 YAHOO.util.Event.addListener(button, 'click', this.toggle_hide, this, true);
542 commandContainer.appendChild(button);
354e1130 543 this.viewButton = button;
354e1130 544}
545
6f00683e 546resource_class.prototype.toggle_hide = function(target, e, superficial, force) {
354e1130 547 if (force != null) {
6f00683e 548 if (this.debug) {
549 YAHOO.log("Resource "+this.getEl().id+" forced to "+force);
550 }
354e1130 551 this.hidden = !force;
0a0bb380 552 }
553
354e1130 554 if (this.hidden) {
6f00683e 555 YAHOO.util.Dom.removeClass(this.linkContainer, 'dimmed');
556 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show.gif/i, 'hide.gif');
354e1130 557 this.hidden = false;
558
559 if (!superficial) {
73d402ef 560 main.connect('POST', 'class=resource&field=visible', null, 'value=1&id='+this.id);
1752e584 561 }
354e1130 562 } else {
6f00683e 563 YAHOO.util.Dom.addClass(this.linkContainer, 'dimmed');
564 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide.gif/i, 'show.gif');
354e1130 565 this.hidden = true;
566
567 if (!superficial) {
73d402ef 568 main.connect('POST', 'class=resource&field=visible', null, 'value=0&id='+this.id);
354e1130 569 }
570 }
571}
572
573resource_class.prototype.delete_button = function() {
e1c15ef7 574 if (this.debug) {
6f00683e 575 YAHOO.log("Deleting "+this.getEl().id+" from parent "+this.parentObj.getEl().id);
e1c15ef7 576 }
6f00683e 577 if (!confirm(main.getString('deletecheck', main.getString(this.is)+" "+this.id))) {
354e1130 578 return false;
579 }
354e1130 580 this.getEl().parentNode.removeChild(this.getEl());
581 this.parentObj.remove_resource(this);
582
73d402ef 583 main.connect('DELETE', 'class=resource&id='+this.id);
354e1130 584}
585
586resource_class.prototype.update_index = function(index) {
e1c15ef7 587 if (this.debug) {
6f00683e 588 YAHOO.log("Updating Index for resource "+this.getEl().id+" to "+index);
e1c15ef7 589 }
590}
354e1130 591
2469f7ea 592resource_class.prototype.startDrag = function(x, y) {
0a0bb380 593 YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
354e1130 594
0a0bb380 595 //reinitialize dd element
7f933d8f 596 this.getDragEl().innerHTML = '';
354e1130 597
0a0bb380 598 var targets = YAHOO.util.DDM.getRelated(this, true);
e1c15ef7 599 if (this.debug) {
600 YAHOO.log(this.id + " startDrag "+targets.length + " targets");
2469f7ea 601 /*
602 for (var i=0; i<targets.length; i++) {
603 YAHOO.log('target '+(i+1)+': '+targets[i].id);
604 }
605 */
e1c15ef7 606 }
354e1130 607}
0a0bb380 608
7f933d8f 609resource_class.prototype.clear_move_markers = function(target) {
610 if (target.is == 'section') {
611 resources = target.resources;
612 } else {
613 resources = target.parentObj.resources;
614 }
615 for (var i=0; i<resources.length; i++) {
616 YAHOO.util.Dom.setStyle(resources[i].getEl().id, 'border', 'none');
64e5a68d 617 }
7f933d8f 618}
619
620resource_class.prototype.onDragOver = function(e, ids) {
621 var target = YAHOO.util.DDM.getBestMatch(ids);
622
623 this.clear_move_markers(target);
624
625 if (target != this && (target.is == 'resource' || target.is == 'activity')) {
626 // Add a top border to show where the drop will place the resource.
627 YAHOO.util.Dom.setStyle(target.getEl().id, 'border-top', '1px solid #BBB');
628 } else if (target.is == 'section' && target.resources.length > 0) {
629 // We need to have a border at the bottom of the last activity in
630 // that section.
631 YAHOO.util.Dom.setStyle(target.resources[target.resources.length - 1].getEl().id,
632 'border-bottom', '1px solid #BBB');
64e5a68d 633 }
7f933d8f 634}
354e1130 635
7f933d8f 636resource_class.prototype.onDragOut = function(e, ids) {
637 var target = YAHOO.util.DDM.getBestMatch(ids);
2469f7ea 638 if (target) {
639 this.clear_move_markers(target);
640 }
7f933d8f 641}
0a0bb380 642
2469f7ea 643resource_class.prototype.onDragDrop = function(e, ids) {YAHOO.log('onDragDrop');
7f933d8f 644 var target = YAHOO.util.DDM.getBestMatch(ids);
2469f7ea 645 if (!target) {
646 YAHOO.log('onDragDrop: Target is not valid!', 'error');
647 }
648
64e5a68d 649 if (this.debug) {
3203f104 650 YAHOO.log("Dropped on section id="+target.sectionId
64e5a68d 651 +", el="+this.getEl().id
652 +", x="+YAHOO.util.Dom.getXY( this.getDragEl() ));
653 }
64e5a68d 654 this.parentObj.remove_resource(this);
354e1130 655
64e5a68d 656 if (target.is == 'resource' || target.is == 'activity') {
657 target.parentObj.insert_resource(this, target);
658 } else if (target.is == 'section') {
7f933d8f 659 target.insert_resource(this);
0a0bb380 660 }
7f933d8f 661 this.clear_move_markers(target);
0a0bb380 662 return;
64e5a68d 663}
0a0bb380 664
665resource_class.prototype.endDrag = function() {
64e5a68d 666 // Eliminates default action
354e1130 667}
668
354e1130 669
64e5a68d 670/**
e1c15ef7 671 * activity_class extends resource class
64e5a68d 672 */
7f933d8f 673function activity_class(id, group, config, parentObj) {
674 this.init_activity(id, group, config, parentObj);
0a0bb380 675}
e1c15ef7 676
0a0bb380 677YAHOO.extend(activity_class, resource_class);
678
e1c15ef7 679
7f933d8f 680activity_class.prototype.init_activity = function(id, group, config, parentObj) {
354e1130 681 if (!id) {
2469f7ea 682 YAHOO.log("Init activity, NO ID FOUND!", 'error');
354e1130 683 return;
d4df8fdc 684 }
354e1130 685 this.is = 'activity';
686 this.currentGroup = this.get_current_group(id);
7f933d8f 687 this.init_resource(id, group, config, parentObj);
354e1130 688 this.groupButton= null;
689 this.init_activity_button();
690
e1c15ef7 691 if (this.debug) {
692 YAHOO.log("--init_activity "+id);
693 }
354e1130 694}
695
d8158863 696activity_class.prototype.groupImages = ['/t/groupn.gif', '/t/groups.gif', '/t/groupv.gif'];
354e1130 697
698activity_class.prototype.init_activity_button = function() {
d8158863 699 var button = main.mk_button('a', this.groupImages[this.currentGroup]);
354e1130 700 YAHOO.util.Event.addListener(button,'click',this.toggle_group,this,true);
701 this.commandContainer.appendChild(button);
702 this.groupButton = button;
703}
704
705activity_class.prototype.get_current_group = function(id) {
706 if (document.getElementById(id) == null) {
707 return;
0a0bb380 708 }
354e1130 709 var groupNodeArray = document.getElementById(id).getElementsByTagName('a');
710 var groupNode = groupNodeArray[groupNodeArray.length-1];
711
712 for (var x=0;x<this.groupImages.length;x++) {
713 if (main.portal.wwwroot+this.groupImages[x] == groupNode.getElementsByTagName('img')[0].src) {
714 return x;
715 }
716 }
354e1130 717 return 0;
718}
719
720activity_class.prototype.toggle_group = function() {
721 this.currentGroup++;
e1c15ef7 722 if (this.currentGroup > 2) {
354e1130 723 this.currentGroup = 0;
e1c15ef7 724 }
354e1130 725 this.groupButton.getElementsByTagName('img')[0].src = main.portal.wwwroot + this.groupImages[this.currentGroup];
726
73d402ef 727 main.connect('POST', 'class=resource&field=groupmode', null, 'value='+this.currentGroup+'&id='+this.id);
354e1130 728}