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