fixed risks associated with capabilitites in modules MDL-7174; merged from MOODLE_17_...
[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) {
e1c15ef7 90 var highlightbutton = main.mk_button('div', '/pix/i/marker.gif');
91 YAHOO.util.Event.addListener(highlightbutton, 'click', this.mk_marker, this, true);
354e1130 92 commandContainer.appendChild(highlightbutton);
93 this.highlightButton = highlightbutton;
94 }
e1c15ef7 95 var viewbutton = main.mk_button('div', '/pix/i/hide.gif');
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() {
e1c15ef7 102 var handleRef = main.mk_button('a', '/pix/i/move_2d.gif', [['style','cursor:move']]);
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
6f00683e 214 main.connect('post','class=section&field=move',null,'id='+this.sectionId+'&value='
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) {
6f00683e 267 main.connect('post', 'class=section&field=visible', null, 'value=1&id='+this.sectionId);
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) {
6f00683e 280 main.connect('post', 'class=section&field=visible', null, 'value=0&id='+this.sectionId);
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
6f00683e 306 main.connect('post', 'class=course&field=marker', null, 'value=0');
354e1130 307 this.toggle_highlight();
354e1130 308 }
309}
310
311section_class.prototype.changeId = function(newId) {
312 this.sectionId = newId;
313 this.numberDisplay.firstChild.data = newId;
314
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))
316
317 if (main.marker == this) {
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
64e5a68d 378 targetId = '';
6f00683e 379 if (targetel) {
64e5a68d 380 targetId = targetel.id;
381 }
354e1130 382
64e5a68d 383 main.connect('post', 'class=resource&field=move', null,
384 'id='+el.id+'&beforeId='+targetId
3203f104 385 +'&sectionId='+this.sectionId);
354e1130 386
387 //if inserting into a hidden resource hide
388 if (this.hidden) {
389 el.hiddenStored = el.hidden;
6f00683e 390 el.toggle_hide(null, null, true, true);
354e1130 391 } else {
392 if (el.hiddenStored != null) {
6f00683e 393 el.toggle_hide(null, null, true, el.hiddenStored);
354e1130 394 el.hiddenStored = null;
62cb4032 395 }
354e1130 396 }
354e1130 397 //update model
6f00683e 398 if (!targetel) {
354e1130 399 this.resources[this.resources.length] = el;
db5308e2 400 } else {
6f00683e 401 for (var i=0; i<resourcecount; i++) {
354e1130 402 if (found) {
403 tempStore = this.resources[i];
404 this.resources[i] = nextStore;
db5308e2 405 nextStore = tempStore;
6f00683e 406
354e1130 407 if (nextStore != null)
0a0bb380 408 nextStore.update_index(i+1);
0a0bb380 409
354e1130 410 } else if (this.resources[i] == targetel) {
411 found = true;
412 nextStore = this.resources[i];
413 this.resources[i] = el;
414 resourcecount++;
415
6f00683e 416 this.resources[i].update_index(i, this.ident);
417 nextStore.update_index(i + 1);
354e1130 418 }
419 }
db5308e2 420 }
6f00683e 421 //update on frontend
422 if (targetel) {
423 this.resources_ul.insertBefore(el.getEl(), targetel.getEl());
424 this.resources_ul.insertBefore(document.createTextNode(' '), targetel.getEl());
354e1130 425 } else {
426 this.resources_ul.appendChild(el.getEl());
6f00683e 427 this.resources_ul.appendChild(document.createTextNode(' '));
0a0bb380 428 }
354e1130 429 el.parentObj = this;
430}
431
432section_class.prototype.write_sequence_list = function(toReturn) {
433 var listOutput = '';
e1c15ef7 434
6f00683e 435 for (var i=0; i<this.resources.length; i++) {
354e1130 436 listOutput += this.resources[i].id;
e1c15ef7 437 if (i != (this.resources.length-1)) {
354e1130 438 listOutput += ',';
e1c15ef7 439 }
354e1130 440 }
db5308e2 441 if (toReturn) {
354e1130 442 return listOutput;
db5308e2 443 }
e1c15ef7 444}
354e1130 445
0a0bb380 446
354e1130 447
e1c15ef7 448/**
449 * resource_class extends util.DDProxy
450 */
354e1130 451function resource_class(id,group,config,parentObj) {
0a0bb380 452
453 this.init_resource(id,group,config,parentObj);
454}
e1c15ef7 455
0a0bb380 456YAHOO.extend(resource_class, YAHOO.util.DDProxy);
457
e1c15ef7 458
9569653d 459resource_class.prototype.debug = false;
d4df8fdc 460
e1c15ef7 461
6f00683e 462resource_class.prototype.init_resource = function(id, group, config, parentObj) {
354e1130 463 if (!id) {
6f00683e 464 YAHOO.log("Init resource, NO ID FOUND!", 'error');
354e1130 465 return;
466 }
354e1130 467 this.is = 'resource';
6f00683e 468 this.init(id, group, config);
354e1130 469 this.createFrame();
470 this.isTarget = true;
eba88175 471
6f00683e 472 this.id = this.getEl().id.replace(/module-/i, '');
eba88175 473
354e1130 474 this.hidden = false;
6f00683e 475 if (YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('a')[0], 'dimmed')) {
354e1130 476 this.hidden = true;
e1c15ef7 477 }
354e1130 478 this.hiddenStored = null;
479
480 this.linkContainer = this.getEl().getElementsByTagName('a')[0];
481
482 this.commandContainer = null;
483 this.viewButton = null;
484 this.handle = null;
485 this.init_buttons();
486
487 this.parentObj = parentObj;
488
e1c15ef7 489 if (this.debug) {
490 YAHOO.log("init_resource "+id+" parent = "+parentObj.getEl().id);
491 }
354e1130 492}
0a0bb380 493
354e1130 494resource_class.prototype.init_buttons = function() {
6f00683e 495 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands', 'span', this.getEl())[0];
354e1130 496 if ( commandContainer == null) {
6f00683e 497 YAHOO.log('Cannot find command container for '+this.getEl().id, 'error');
354e1130 498 return;
0a0bb380 499 }
354e1130 500 this.commandContainer = commandContainer;
501
502 //find edit button
503 var updateButton = null;
6f00683e 504 var buttons = commandContainer.getElementsByTagName('a');
e1c15ef7 505
6f00683e 506 for (var x=0; x<buttons.length; x++) {
354e1130 507 if (buttons[x].title == main.portal.strings['update']) {
508 updateButton = buttons[x];
509 }
510 }
511
512 if (updateButton == null)
6f00683e 513 YAHOO.log('Cannot find updateButton for '+this.getEl().id, 'error');
354e1130 514
515 commandContainer.innerHTML = '';
516
354e1130 517 //add move-handle
6f00683e 518 var handleRef = main.mk_button('a', '/pix/i/move_2d.gif',
519 [['style', 'cursor:move']], [['height', '11'], ['width', '11'],
520 ['hspace', '2'], ['border', '0']]);
521
522 YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
354e1130 523 this.handle = handleRef;
354e1130 524 commandContainer.appendChild(handleRef);
525 this.setHandleElId(this.handle.id);
526
354e1130 527 //add edit button back in
528 commandContainer.appendChild(updateButton);
529
530 //add rest
6f00683e 531 var button = main.mk_button('a', '/pix/t/delete.gif');
532 YAHOO.util.Event.addListener(button, 'click', this.delete_button, this, true);
533 commandContainer.appendChild(button);
354e1130 534
e1c15ef7 535 if (this.hidden) {
6f00683e 536 var button = main.mk_button('a', '/pix/t/show.gif');
e1c15ef7 537 } else {
6f00683e 538 var button = main.mk_button('a', '/pix/t/hide.gif');
e1c15ef7 539 }
6f00683e 540 YAHOO.util.Event.addListener(button, 'click', this.toggle_hide, this, true);
541 commandContainer.appendChild(button);
354e1130 542 this.viewButton = button;
354e1130 543}
544
6f00683e 545resource_class.prototype.toggle_hide = function(target, e, superficial, force) {
354e1130 546 if (force != null) {
6f00683e 547 if (this.debug) {
548 YAHOO.log("Resource "+this.getEl().id+" forced to "+force);
549 }
354e1130 550 this.hidden = !force;
0a0bb380 551 }
552
354e1130 553 if (this.hidden) {
6f00683e 554 YAHOO.util.Dom.removeClass(this.linkContainer, 'dimmed');
555 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show.gif/i, 'hide.gif');
354e1130 556 this.hidden = false;
557
558 if (!superficial) {
6f00683e 559 main.connect('post', 'class=resource&field=visible', null, 'value=1&id='+this.id);
1752e584 560 }
354e1130 561 } else {
6f00683e 562 YAHOO.util.Dom.addClass(this.linkContainer, 'dimmed');
563 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide.gif/i, 'show.gif');
354e1130 564 this.hidden = true;
565
566 if (!superficial) {
6f00683e 567 main.connect('post', 'class=resource&field=visible', null, 'value=0&id='+this.id);
354e1130 568 }
569 }
570}
571
572resource_class.prototype.delete_button = function() {
e1c15ef7 573 if (this.debug) {
6f00683e 574 YAHOO.log("Deleting "+this.getEl().id+" from parent "+this.parentObj.getEl().id);
e1c15ef7 575 }
6f00683e 576 if (!confirm(main.getString('deletecheck', main.getString(this.is)+" "+this.id))) {
354e1130 577 return false;
578 }
354e1130 579 this.getEl().parentNode.removeChild(this.getEl());
580 this.parentObj.remove_resource(this);
581
6f00683e 582 main.connect('delete', 'class=resource&id='+this.id);
354e1130 583}
584
585resource_class.prototype.update_index = function(index) {
e1c15ef7 586 if (this.debug) {
6f00683e 587 YAHOO.log("Updating Index for resource "+this.getEl().id+" to "+index);
e1c15ef7 588 }
589}
354e1130 590
0a0bb380 591resource_class.prototype.startDrag = function(x, y) {
0a0bb380 592 YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
354e1130 593
0a0bb380 594 //reinitialize dd element
7f933d8f 595 this.getDragEl().innerHTML = '';
354e1130 596
0a0bb380 597 var targets = YAHOO.util.DDM.getRelated(this, true);
e1c15ef7 598 if (this.debug) {
599 YAHOO.log(this.id + " startDrag "+targets.length + " targets");
600 }
354e1130 601}
0a0bb380 602
7f933d8f 603resource_class.prototype.clear_move_markers = function(target) {
604 if (target.is == 'section') {
605 resources = target.resources;
606 } else {
607 resources = target.parentObj.resources;
608 }
609 for (var i=0; i<resources.length; i++) {
610 YAHOO.util.Dom.setStyle(resources[i].getEl().id, 'border', 'none');
64e5a68d 611 }
7f933d8f 612}
613
614resource_class.prototype.onDragOver = function(e, ids) {
615 var target = YAHOO.util.DDM.getBestMatch(ids);
616
617 this.clear_move_markers(target);
618
619 if (target != this && (target.is == 'resource' || target.is == 'activity')) {
620 // Add a top border to show where the drop will place the resource.
621 YAHOO.util.Dom.setStyle(target.getEl().id, 'border-top', '1px solid #BBB');
622 } else if (target.is == 'section' && target.resources.length > 0) {
623 // We need to have a border at the bottom of the last activity in
624 // that section.
625 YAHOO.util.Dom.setStyle(target.resources[target.resources.length - 1].getEl().id,
626 'border-bottom', '1px solid #BBB');
64e5a68d 627 }
7f933d8f 628}
354e1130 629
7f933d8f 630resource_class.prototype.onDragOut = function(e, ids) {
631 var target = YAHOO.util.DDM.getBestMatch(ids);
632 this.clear_move_markers(target);
633}
0a0bb380 634
7f933d8f 635resource_class.prototype.onDragDrop = function(e, ids) {
636 var target = YAHOO.util.DDM.getBestMatch(ids);
637
64e5a68d 638 if (this.debug) {
3203f104 639 YAHOO.log("Dropped on section id="+target.sectionId
64e5a68d 640 +", el="+this.getEl().id
641 +", x="+YAHOO.util.Dom.getXY( this.getDragEl() ));
642 }
64e5a68d 643 this.parentObj.remove_resource(this);
354e1130 644
64e5a68d 645 if (target.is == 'resource' || target.is == 'activity') {
646 target.parentObj.insert_resource(this, target);
647 } else if (target.is == 'section') {
7f933d8f 648 target.insert_resource(this);
0a0bb380 649 }
7f933d8f 650 this.clear_move_markers(target);
0a0bb380 651 return;
64e5a68d 652}
0a0bb380 653
654resource_class.prototype.endDrag = function() {
64e5a68d 655 // Eliminates default action
354e1130 656}
657
354e1130 658
64e5a68d 659/**
e1c15ef7 660 * activity_class extends resource class
64e5a68d 661 */
7f933d8f 662function activity_class(id, group, config, parentObj) {
663 this.init_activity(id, group, config, parentObj);
0a0bb380 664}
e1c15ef7 665
0a0bb380 666YAHOO.extend(activity_class, resource_class);
667
e1c15ef7 668
7f933d8f 669activity_class.prototype.init_activity = function(id, group, config, parentObj) {
354e1130 670 if (!id) {
671 YAHOO.log("Init activity, NO ID FOUND!",'error');
672 return;
d4df8fdc 673 }
354e1130 674 this.is = 'activity';
675 this.currentGroup = this.get_current_group(id);
7f933d8f 676 this.init_resource(id, group, config, parentObj);
354e1130 677 this.groupButton= null;
678 this.init_activity_button();
679
e1c15ef7 680 if (this.debug) {
681 YAHOO.log("--init_activity "+id);
682 }
354e1130 683}
684
7f933d8f 685activity_class.prototype.groupImages = ['/pix/t/groupn.gif', '/pix/t/groups.gif', '/pix/t/groupv.gif'];
354e1130 686
687activity_class.prototype.init_activity_button = function() {
688 var button = main.mk_button('a',this.groupImages[this.currentGroup]);
689 YAHOO.util.Event.addListener(button,'click',this.toggle_group,this,true);
690 this.commandContainer.appendChild(button);
691 this.groupButton = button;
692}
693
694activity_class.prototype.get_current_group = function(id) {
695 if (document.getElementById(id) == null) {
696 return;
0a0bb380 697 }
354e1130 698 var groupNodeArray = document.getElementById(id).getElementsByTagName('a');
699 var groupNode = groupNodeArray[groupNodeArray.length-1];
700
701 for (var x=0;x<this.groupImages.length;x++) {
702 if (main.portal.wwwroot+this.groupImages[x] == groupNode.getElementsByTagName('img')[0].src) {
703 return x;
704 }
705 }
354e1130 706 return 0;
707}
708
709activity_class.prototype.toggle_group = function() {
710 this.currentGroup++;
e1c15ef7 711 if (this.currentGroup > 2) {
354e1130 712 this.currentGroup = 0;
e1c15ef7 713 }
354e1130 714 this.groupButton.getElementsByTagName('img')[0].src = main.portal.wwwroot + this.groupImages[this.currentGroup];
715
7f933d8f 716 main.connect('post', 'class=resource&field=groupmode', null, 'value='+this.currentGroup+'&id='+this.id);
354e1130 717}