MDL-9742 Converting tabs to spaces
[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;
d2a11d46 86
e1c15ef7 87 for (var i=(commandContainerCount-1); i>0; i--) {
354e1130 88 commandContainer.removeChild(commandContainer.childNodes[i])
0a0bb380 89 }
90
354e1130 91 if (!this.isWeekFormat) {
d8158863 92 var highlightbutton = main.mk_button('div', '/i/marker.gif');
e1c15ef7 93 YAHOO.util.Event.addListener(highlightbutton, 'click', this.mk_marker, this, true);
354e1130 94 commandContainer.appendChild(highlightbutton);
95 this.highlightButton = highlightbutton;
96 }
d8158863 97 var viewbutton = main.mk_button('div', '/i/hide.gif');
e1c15ef7 98 YAHOO.util.Event.addListener(viewbutton, 'click', this.toggle_hide, this,true);
354e1130 99 commandContainer.appendChild(viewbutton);
100 this.viewButton = viewbutton;
101}
102
a66ddf19 103
354e1130 104section_class.prototype.add_handle = function() {
d2a11d46 105 var handleRef = main.mk_button('a', '/i/move_2d.gif',
106 [['style','cursor:move'], ['title', main.portal.strings['move']]]);
107
e1c15ef7 108 YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
354e1130 109
110 this.handle = handleRef;
111
112 this.getEl().childNodes[0].appendChild(handleRef);
113 this.setHandleElId(this.handle.id);
114}
eba88175 115
a66ddf19 116
354e1130 117section_class.prototype.process_section = function() {
118 this.content_td = this.getEl().childNodes[1];
119
120 if (YAHOO.util.Dom.hasClass(this.getEl(),'current')) {
121 this.highlighted = true;
122 main.marker = this;
0a0bb380 123 }
eba88175 124
a66ddf19 125 // Create holder for display number for access later
354e1130 126
127 this.numberDisplay = document.createElement('div');
128 this.numberDisplay.innerHTML = this.getEl().childNodes[0].innerHTML;
129 this.getEl().childNodes[0].innerHTML = '';
130 this.getEl().childNodes[0].appendChild(this.numberDisplay);
131
3203f104 132 this.sectionId = this.id.replace(/section-/i, ''); // Okay, we will have to change this if we
133 // ever change the id attributes format
134 // for the sections.
a66ddf19 135 if (this.debug) {
136 YAHOO.log("Creating section "+this.getEl().id+" in position "+this.sectionId);
137 }
138
139 // Find/edit resources
354e1130 140 this.resources_ul = this.content_td.getElementsByTagName('ul')[0];
6f00683e 141 if (!this.resources_ul) {
354e1130 142 this.resources_ul = document.createElement('ul');
143 this.resources_ul.className='section';
4b5c8f24 144 this.content_td.insertBefore(this.resources_ul, this.content_td.lastChild);
d4df8fdc 145 }
354e1130 146 var resource_count = this.resources_ul.getElementsByTagName('li').length;
147
148 for (var i=0;i<resource_count;i++) {
149 var resource = this.resources_ul.getElementsByTagName('li')[i];
d2a11d46 150 this.resources[this.resources.length] = new resource_class(resource.id, 'resources', null, this);
7f933d8f 151 }
e1c15ef7 152 this.summary = YAHOO.util.Dom.getElementsByClassName('summary', null, this.getEl())[0].firstChild.data || '';
354e1130 153}
154
a66ddf19 155
d2a11d46 156section_class.prototype.startDrag = function(x, y) {
0a0bb380 157 //operates in point mode
158 YAHOO.util.DDM.mode = YAHOO.util.DDM.POINT;
354e1130 159
0a0bb380 160 //remove from resources group temporarily
161 this.removeFromGroup('resources');
354e1130 162
0a0bb380 163 //reinitialize dd element
164 this.getDragEl().innerHTML = '';
0a0bb380 165
354e1130 166 var targets = YAHOO.util.DDM.getRelated(this, true);
a66ddf19 167
168 if (this.debug) {
169 YAHOO.log(this.id + " startDrag, "+targets.length + " targets");
170 }
354e1130 171}
0a0bb380 172
a66ddf19 173
0a0bb380 174section_class.prototype.onDragDrop = function(e, id) {
175 // get the drag and drop object that was targeted
176 var target = YAHOO.util.DDM.getDDById(id);
177
6f00683e 178 if (this.debug) {
a66ddf19 179 YAHOO.log("Section dropped on id="+id+" (I am "+this.getEl().id+") x="
180 +YAHOO.util.Dom.getXY(this.getDragEl()));
6f00683e 181 }
0a0bb380 182 this.move_to_section(target);
354e1130 183
184 //add back to resources group
3203f104 185 this.addToGroup('resources');
a66ddf19 186}
187
188
354e1130 189section_class.prototype.endDrag = function() {
190 //nessicary to defeat default action
191
192 //add back to resources group
0a0bb380 193 this.addToGroup('resources');
354e1130 194}
195
a66ddf19 196
354e1130 197section_class.prototype.move_to_section = function(target) {
198 var tempTd = document.createElement('td');
199 var tempStore = null;
200 var sectionCount = main.sections.length;
201 var found = null;
202
203 //determine if original is above or below target and adjust loop
6f00683e 204 var oIndex = main.get_section_index(this);
205 var tIndex = main.get_section_index(target);
354e1130 206
6f00683e 207 if (this.debug) {
208 YAHOO.log("original is at: "+oIndex+" target is at:"+tIndex+" of "+(sectionCount-1));
209 }
354e1130 210 if (oIndex < tIndex) {
211 var loopCondition = 'i<sectionCount';
212 var loopStart = 1;
213 var loopInc = 'i++';
6f00683e 214 var loopmodifier = 'i - 1';
354e1130 215 } else {
6f00683e 216 var loopCondition = 'i > 0';
217 var loopStart = sectionCount - 1;
354e1130 218 var loopInc = 'i--';
6f00683e 219 var loopmodifier = 'i + 1';
0a0bb380 220 }
354e1130 221
222 //move on backend
73d402ef 223 main.connect('POST','class=section&field=move',null,'id='+this.sectionId+'&value='
2469f7ea 224 +(target.sectionId - this.sectionId));
354e1130 225
226 //move on front end
db5308e2 227 for (var i=loopStart; eval(loopCondition); eval(loopInc)) {
354e1130 228
6f00683e 229 if ((main.sections[i] == this) && !found) {
354e1130 230 //enounter with original node
6f00683e 231 if (this.debug) {
232 YAHOO.log("Found Original "+main.sections[i].getEl().id);
233 }
354e1130 234 if (main.sections[i] == this) {
db5308e2 235 found = true;
354e1130 236 }
354e1130 237 } else if (main.sections[i] == target) {
238 //encounter with target node
6f00683e 239 if (this.debug) {
240 YAHOO.log("Found target "+main.sections[i].getEl().id);
241 }
354e1130 242 main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]);
243 found = false;
244 break;
354e1130 245 } else if (found) {
db5308e2 246 //encounter with nodes inbetween
354e1130 247 main.sections[i].swap_with_section(main.sections[eval(loopmodifier)]);
db5308e2 248 }
249 }
354e1130 250}
251
a66ddf19 252
354e1130 253section_class.prototype.swap_with_section = function(sectionIn) {
254 var tmpStore = null;
255
256 thisIndex = main.get_section_index(this);
257 targetIndex = main.get_section_index(sectionIn);
258 main.sections[targetIndex] = this;
259 main.sections[thisIndex] = sectionIn;
260
261 this.changeId(targetIndex);
262 sectionIn.changeId(thisIndex);
263
6f00683e 264 if (this.debug) {
265 YAHOO.log("Swapping "+this.getEl().id+" with "+sectionIn.getEl().id);
266 }
53a4443b 267 // Swap the sections.
db5308e2 268 YAHOO.util.DDM.swapNode(this.getEl(), sectionIn.getEl());
53a4443b 269
270 // Sections contain forms to add new resources/activities. These forms
271 // have not been updated to reflect the new positions of the sections that
272 // we have swapped. Let's swap the two sections' forms around.
273 if (this.getEl().getElementsByTagName('form')[0].parentNode
274 && sectionIn.getEl().getElementsByTagName('form')[0].parentNode) {
275
276 YAHOO.util.DDM.swapNode(this.getEl().getElementsByTagName('form')[0].parentNode,
277 sectionIn.getEl().getElementsByTagName('form')[0].parentNode);
278 } else {
279 YAHOO.log("Swapping sections: form not present in one or both sections", "warn");
280 }
354e1130 281}
282
a66ddf19 283
354e1130 284section_class.prototype.toggle_hide = function(e,target,superficial) {
285 if (this.hidden) {
6f00683e 286 YAHOO.util.Dom.removeClass(this.getEl(), 'hidden');
287 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show.gif/i, 'hide.gif');
354e1130 288 this.hidden = false;
289
290 if (!superficial) {
73d402ef 291 main.connect('POST', 'class=section&field=visible', null, 'value=1&id='+this.sectionId);
6f00683e 292 for (var x=0; x<this.resources.length; x++) {
293 this.resources[x].toggle_hide(null, null, true, this.resources[x].hiddenStored);
354e1130 294 this.resources[x].hiddenStored = null;
295 }
296 }
297
298 } else {
6f00683e 299 YAHOO.util.Dom.addClass(this.getEl(), 'hidden');
300 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide.gif/i, 'show.gif');
354e1130 301 this.hidden = true;
302
303 if (!superficial) {
73d402ef 304 main.connect('POST', 'class=section&field=visible', null, 'value=0&id='+this.sectionId);
6f00683e 305 for (var x=0; x<this.resources.length; x++) {
354e1130 306 this.resources[x].hiddenStored = this.resources[x].hidden;
6f00683e 307 this.resources[x].toggle_hide(null, null, true, true);
7f933d8f 308 }
354e1130 309 }
d4df8fdc 310 }
354e1130 311}
0a0bb380 312
a66ddf19 313
354e1130 314section_class.prototype.toggle_highlight = function() {
315 if (this.highlighted) {
6f00683e 316 YAHOO.util.Dom.removeClass(this.getEl(), 'current');
354e1130 317 this.highlighted = false;
318 } else {
6f00683e 319 YAHOO.util.Dom.addClass(this.getEl(), 'current');
354e1130 320 this.highlighted = true;
0a0bb380 321 }
354e1130 322}
0a0bb380 323
a66ddf19 324
354e1130 325section_class.prototype.mk_marker = function() {
326 if (main.marker != this) {
327 main.update_marker(this);
d2a11d46 328 } else {
329 // If currently the marker
354e1130 330 main.marker = null;
331
73d402ef 332 main.connect('POST', 'class=course&field=marker', null, 'value=0');
354e1130 333 this.toggle_highlight();
354e1130 334 }
88c5092a 335}
354e1130 336
a66ddf19 337
354e1130 338section_class.prototype.changeId = function(newId) {
339 this.sectionId = newId;
88c5092a 340 this.numberDisplay.firstChild.data = newId;
354e1130 341
73d402ef 342 //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 343
344 if (main.marker == this) {
88c5092a 345 main.update_marker(this);
d4df8fdc 346 }
354e1130 347}
348
a66ddf19 349
354e1130 350section_class.prototype.get_resource_index = function(el) {
6f00683e 351 for (var x=0; x<this.resources.length; x++) {
352 if (this.resources[x] == el) {
354e1130 353 return x;
e1c15ef7 354 }
355 }
6f00683e 356 YAHOO.log("Could not find resource to remove "+el.getEl().id, "error");
354e1130 357 return -1;
358}
359
a66ddf19 360
354e1130 361section_class.prototype.remove_resource = function(el) {
e50b0ad9 362
363 var resourceEl = el.getEl();
364 var parentEl = resourceEl.parentNode;
365 if (!parentEl) {
366 return false;
367 }
368
354e1130 369 var resourceCount = this.resources.length;
e1c15ef7 370
354e1130 371 if (resourceCount == 1) {
6f00683e 372 if (this.resources[0] == el) {
373 this.resources = new Array();
374 }
354e1130 375 } else {
376 var found = false;
6f00683e 377 for (var i=0; i<resourceCount; i++) {
354e1130 378 if (found) {
6f00683e 379 this.resources[i - 1] = this.resources[i];
380 if (i == resourceCount - 1) {
381 this.resources = this.resources.slice(0, -1);
354e1130 382 resourceCount--;
0a0bb380 383 }
6f00683e 384 this.resources[i - 1].update_index(i - 1);
385 } else if (this.resources[i] == el) {
354e1130 386 found = true;
0a0bb380 387 }
388 }
354e1130 389 }
e50b0ad9 390 // Remove any extra text nodes to keep DOM clean.
391 var kids = parentEl.childNodes;
392
393 for (var i=0; i<kids.length; i++) {
394 if (kids[i].nodeType == 3) {
395 YAHOO.log('Removed extra text node.');
396 parentEl.removeChild(kids[i]);
397 }
398 }
e50b0ad9 399 parentEl.removeChild(resourceEl);
400
e1c15ef7 401 this.write_sequence_list();
e50b0ad9 402 return true;
9f4dff70 403}
354e1130 404
a66ddf19 405
64e5a68d 406section_class.prototype.insert_resource = function(el, targetel) {
354e1130 407 var resourcecount = this.resources.length;
408 var found = false;
409 var tempStore = nextStore = null;
410
411 //update in backend
2469f7ea 412 var targetId = '';
6f00683e 413 if (targetel) {
64e5a68d 414 targetId = targetel.id;
415 }
2469f7ea 416 if (this.debug) {
417 YAHOO.log('id='+el.id+', beforeId='+targetId+', sectionId='+this.sectionId);
418 }
73d402ef 419 main.connect('POST', 'class=resource&field=move', null,
2469f7ea 420 'id='+el.id+'&beforeId='+targetId+'&sectionId='+this.sectionId);
354e1130 421
422 //if inserting into a hidden resource hide
423 if (this.hidden) {
424 el.hiddenStored = el.hidden;
6f00683e 425 el.toggle_hide(null, null, true, true);
354e1130 426 } else {
427 if (el.hiddenStored != null) {
6f00683e 428 el.toggle_hide(null, null, true, el.hiddenStored);
88c5092a 429 el.hiddenStored = null;
62cb4032 430 }
354e1130 431 }
354e1130 432 //update model
6f00683e 433 if (!targetel) {
354e1130 434 this.resources[this.resources.length] = el;
db5308e2 435 } else {
6f00683e 436 for (var i=0; i<resourcecount; i++) {
354e1130 437 if (found) {
438 tempStore = this.resources[i];
439 this.resources[i] = nextStore;
db5308e2 440 nextStore = tempStore;
6f00683e 441
354e1130 442 if (nextStore != null)
0a0bb380 443 nextStore.update_index(i+1);
0a0bb380 444
354e1130 445 } else if (this.resources[i] == targetel) {
446 found = true;
447 nextStore = this.resources[i];
448 this.resources[i] = el;
449 resourcecount++;
450
6f00683e 451 this.resources[i].update_index(i, this.ident);
452 nextStore.update_index(i + 1);
354e1130 453 }
454 }
db5308e2 455 }
6f00683e 456 //update on frontend
457 if (targetel) {
458 this.resources_ul.insertBefore(el.getEl(), targetel.getEl());
4c72307e 459 //this.resources_ul.insertBefore(document.createTextNode(' '), targetel.getEl());
354e1130 460 } else {
461 this.resources_ul.appendChild(el.getEl());
4c72307e 462 //this.resources_ul.appendChild(document.createTextNode(' '));
0a0bb380 463 }
354e1130 464 el.parentObj = this;
465}
466
a66ddf19 467
354e1130 468section_class.prototype.write_sequence_list = function(toReturn) {
469 var listOutput = '';
e1c15ef7 470
6f00683e 471 for (var i=0; i<this.resources.length; i++) {
354e1130 472 listOutput += this.resources[i].id;
e1c15ef7 473 if (i != (this.resources.length-1)) {
354e1130 474 listOutput += ',';
e1c15ef7 475 }
354e1130 476 }
db5308e2 477 if (toReturn) {
354e1130 478 return listOutput;
db5308e2 479 }
e1c15ef7 480}
354e1130 481
0a0bb380 482
354e1130 483
d2a11d46 484
e1c15ef7 485/**
486 * resource_class extends util.DDProxy
487 */
354e1130 488function resource_class(id,group,config,parentObj) {
0a0bb380 489 this.init_resource(id,group,config,parentObj);
490}
e1c15ef7 491
0a0bb380 492YAHOO.extend(resource_class, YAHOO.util.DDProxy);
493
e1c15ef7 494
9569653d 495resource_class.prototype.debug = false;
d4df8fdc 496
e1c15ef7 497
6f00683e 498resource_class.prototype.init_resource = function(id, group, config, parentObj) {
354e1130 499 if (!id) {
6f00683e 500 YAHOO.log("Init resource, NO ID FOUND!", 'error');
354e1130 501 return;
502 }
d2a11d46 503
504 // Some constants.
505 this.NOGROUPS = 0;
506 this.SEPARATEGROUPS = 1;
507 this.VISIBLEGROUPS = 2;
508
354e1130 509 this.is = 'resource';
6f00683e 510 this.init(id, group, config);
354e1130 511 this.createFrame();
512 this.isTarget = true;
eba88175 513
6f00683e 514 this.id = this.getEl().id.replace(/module-/i, '');
eba88175 515
354e1130 516 this.hidden = false;
6f00683e 517 if (YAHOO.util.Dom.hasClass(this.getEl().getElementsByTagName('a')[0], 'dimmed')) {
354e1130 518 this.hidden = true;
e1c15ef7 519 }
354e1130 520 this.hiddenStored = null;
521
d2a11d46 522 this.groupmode = null; // Can be null (i.e. does not apply), 0, 1 or 2.
523
354e1130 524 this.linkContainer = this.getEl().getElementsByTagName('a')[0];
525
526 this.commandContainer = null;
d14d739d 527 this.indentLeftButton = null;
528 this.indentRightButton = null;
354e1130 529 this.viewButton = null;
d2a11d46 530 this.groupButton = null;
531 this.handle = null;
354e1130 532 this.init_buttons();
533
d2a11d46 534 this.parentObj = parentObj;
354e1130 535
e1c15ef7 536 if (this.debug) {
537 YAHOO.log("init_resource "+id+" parent = "+parentObj.getEl().id);
538 }
354e1130 539}
0a0bb380 540
a66ddf19 541
d2a11d46 542/**
543 * The current strategy is to look at the DOM tree to get information on the
544 * resource and it's current mode. This is bad since we are dependant on
545 * the html that is output from serverside logic. Seemingly innocuous changes
546 * like changing the language string for the title of a button will break
547 * our JavaScript here. This is brittle.
548 *
549 * First, we clear the buttons container. Then:
550 * We need to add the new-style move handle.
551 * The old style move button (up/down) needs to be removed.
552 * Move left button (if any) needs an event handler.
553 * Move right button (if any) needs an event handler.
554 * Update button stays as it is. Add it back.
555 * Delete button needs an event handler.
556 * Visible button is a toggle. It needs an event handler too.
557 * Group mode button is a toggle. It needs an event handler too.
558 */
354e1130 559resource_class.prototype.init_buttons = function() {
d2a11d46 560
561 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
22206b67 562 'span', this.getEl())[0];
d2a11d46 563
2469f7ea 564 if (commandContainer == null) {
6f00683e 565 YAHOO.log('Cannot find command container for '+this.getEl().id, 'error');
354e1130 566 return;
0a0bb380 567 }
d2a11d46 568
569 // Language strings.
570 var strgroupsnone = main.portal.strings['groupsnone']+' ('+main.portal.strings['clicktochange']+')';
571 var strgroupsseparate = main.portal.strings['groupsseparate']+' ('+main.portal.strings['clicktochange']+')';
572 var strgroupsvisible = main.portal.strings['groupsvisible']+' ('+main.portal.strings['clicktochange']+')';
573
354e1130 574 this.commandContainer = commandContainer;
d2a11d46 575 var buttons = commandContainer.getElementsByTagName('a');
354e1130 576
d2a11d46 577 // Buttons that we might need to add back in.
578 var moveLeft = false;
579 var moveRight = false;
354e1130 580 var updateButton = null;
e1c15ef7 581
6f00683e 582 for (var x=0; x<buttons.length; x++) {
90ebdf65 583 if (buttons[x].className == 'editing_moveleft') {
d2a11d46 584 moveLeft = true;
90ebdf65 585 } else if (buttons[x].className == 'editing_moveright') {
d2a11d46 586 moveRight = true;
90ebdf65 587 } else if (buttons[x].className == 'editing_update') {
02059f46 588 updateButton = buttons[x].cloneNode(true);
90ebdf65 589 } else if (buttons[x].className == 'editing_groupsnone') {
d2a11d46 590 this.groupmode = this.NOGROUPS;
90ebdf65 591 } else if (buttons[x].className == 'editing_groupsseparate') {
d2a11d46 592 this.groupmode = this.SEPARATEGROUPS;
90ebdf65 593 } else if (buttons[x].className == 'editing_groupsvisible') {
d2a11d46 594 this.groupmode = this.VISIBLEGROUPS;
595 }
354e1130 596 }
597
a66ddf19 598 if (updateButton == null) {
d2a11d46 599 // Update button must always be present.
6f00683e 600 YAHOO.log('Cannot find updateButton for '+this.getEl().id, 'error');
a66ddf19 601 }
354e1130 602
d2a11d46 603 // Clear all the buttons.
354e1130 604 commandContainer.innerHTML = '';
605
d2a11d46 606 // Add move-handle for drag and drop.
d8158863 607 var handleRef = main.mk_button('a', '/i/move_2d.gif',
d2a11d46 608 [['style', 'cursor:move'], ['title', main.portal.strings['move']]],
609 [['height', '11'], ['width', '11'], ['style', 'margin-right:3px; border:0;']]);
6f00683e 610
611 YAHOO.util.Dom.generateId(handleRef, 'sectionHandle');
354e1130 612 this.handle = handleRef;
354e1130 613 commandContainer.appendChild(handleRef);
614 this.setHandleElId(this.handle.id);
615
d2a11d46 616 // Add indentation buttons if needed (move left, move right).
617 if (moveLeft) {
22206b67 618 var button = main.mk_button('a', '/t/left.gif', [['title', main.portal.strings['moveleft']],
619 ['class', 'editing_moveleft']]);
620 YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true);
d2a11d46 621 commandContainer.appendChild(button);
d14d739d 622 this.indentLeftButton = button;
d2a11d46 623 }
624 if (moveRight) {
22206b67 625 var button = main.mk_button('a', '/t/right.gif', [['title', main.portal.strings['moveright']],
626 ['class', 'editing_moveright']]);
627 YAHOO.util.Event.addListener(button, 'click', this.indent_right, this, true);
d2a11d46 628 commandContainer.appendChild(button);
d14d739d 629 this.indentRightButton = button;
d2a11d46 630 }
631
632 // Add edit button back in.
633 commandContainer.appendChild(updateButton);
354e1130 634
d2a11d46 635 // Add the delete button.
636 var button = main.mk_button('a', '/t/delete.gif');
6f00683e 637 YAHOO.util.Event.addListener(button, 'click', this.delete_button, this, true);
638 commandContainer.appendChild(button);
354e1130 639
d2a11d46 640 // Add the hide or show button.
e1c15ef7 641 if (this.hidden) {
d8158863 642 var button = main.mk_button('a', '/t/show.gif');
e1c15ef7 643 } else {
d8158863 644 var button = main.mk_button('a', '/t/hide.gif');
e1c15ef7 645 }
6f00683e 646 YAHOO.util.Event.addListener(button, 'click', this.toggle_hide, this, true);
647 commandContainer.appendChild(button);
354e1130 648 this.viewButton = button;
d2a11d46 649
650 // Add the groupmode button if needed.
651 if (this.groupmode != null) {
652 if (this.groupmode == this.NOGROUPS) {
653 var button = main.mk_button('a', '/t/groupn.gif', [['title', strgroupsnone]]);
654 } else if (this.groupmode == this.SEPARATEGROUPS) {
655 var button = main.mk_button('a', '/t/groups.gif', [['title', strgroupsseparate]]);
656 } else {
657 var button = main.mk_button('a', '/t/groupv.gif', [['title', strgroupsvisible]]);
658 }
659 YAHOO.util.Event.addListener(button, 'click', this.toggle_groupmode, this, true);
660 commandContainer.appendChild(button);
661 this.groupButton = button;
662 }
354e1130 663}
664
a66ddf19 665
22206b67 666resource_class.prototype.indent_left = function() {
667
668 var spacer = YAHOO.util.Dom.getElementsByClassName('spacer',
669 'img', this.getEl())[0];
670 if (!spacer) {
74a344bf 671 if (this.debug) {
672 YAHOO.log('Could not indent left: spacer image does not exist', 'error');
673 }
607c1bc1 674 return false;
22206b67 675 }
676 if (spacer.width > 20) {
677 spacer.width -= 20;
678 } else {
679 // Remove the spacer.
680 resource = this.getEl();
681 resource.removeChild(spacer);
682
683 // Remove the indent left button as well.
684 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
685 'span', this.getEl())[0];
22206b67 686
d14d739d 687 commandContainer.removeChild(this.indentLeftButton);
688 this.indentLeftButton = null;
22206b67 689 }
607c1bc1 690 main.connect('POST', 'class=resource&field=indentleft', null, 'id='+this.id);
691 return true;
22206b67 692}
693
694
695resource_class.prototype.indent_right = function() {
696
697 var spacer = YAHOO.util.Dom.getElementsByClassName('spacer',
698 'img', this.getEl())[0];
699 if (!spacer) {
700 var spacer = document.createElement('img');
701
702 spacer.setAttribute('src', main.portal.strings['pixpath']+'/spacer.gif');
74a344bf 703 spacer.className = 'spacer';
22206b67 704 spacer.setAttribute('width', '20');
705 spacer.setAttribute('height', '12');
706
707 var resource = this.getEl();
708 resource.insertBefore(spacer, resource.childNodes[0]);
709 } else {
710 spacer.width += 20;
711 }
712 // Add a indent left button if none is present.
713 var commandContainer = YAHOO.util.Dom.getElementsByClassName('commands',
714 'span', this.getEl())[0];
715
d14d739d 716 if (!this.indentLeftButton) {
22206b67 717 var button = main.mk_button('a', '/t/left.gif', [['title', main.portal.strings['moveleft']],
718 ['class', 'editing_moveleft']]);
719 YAHOO.util.Event.addListener(button, 'click', this.indent_left, this, true);
d14d739d 720 commandContainer.insertBefore(button, this.indentRightButton);
721 this.indentLeftButton = button;
22206b67 722 }
607c1bc1 723 main.connect('POST', 'class=resource&field=indentright', null, 'id='+this.id);
22206b67 724 return true;
725}
726
727
6f00683e 728resource_class.prototype.toggle_hide = function(target, e, superficial, force) {
354e1130 729 if (force != null) {
6f00683e 730 if (this.debug) {
731 YAHOO.log("Resource "+this.getEl().id+" forced to "+force);
732 }
354e1130 733 this.hidden = !force;
0a0bb380 734 }
354e1130 735 if (this.hidden) {
6f00683e 736 YAHOO.util.Dom.removeClass(this.linkContainer, 'dimmed');
737 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/show.gif/i, 'hide.gif');
354e1130 738 this.hidden = false;
739
740 if (!superficial) {
73d402ef 741 main.connect('POST', 'class=resource&field=visible', null, 'value=1&id='+this.id);
1752e584 742 }
354e1130 743 } else {
6f00683e 744 YAHOO.util.Dom.addClass(this.linkContainer, 'dimmed');
745 this.viewButton.childNodes[0].src = this.viewButton.childNodes[0].src.replace(/hide.gif/i, 'show.gif');
354e1130 746 this.hidden = true;
747
748 if (!superficial) {
73d402ef 749 main.connect('POST', 'class=resource&field=visible', null, 'value=0&id='+this.id);
354e1130 750 }
751 }
752}
753
a66ddf19 754
d2a11d46 755resource_class.prototype.groupImages = ['/t/groupn.gif', '/t/groups.gif', '/t/groupv.gif'];
756
757
758resource_class.prototype.toggle_groupmode = function() {
759 this.groupmode++;
760 if (this.groupmode > 2) {
761 this.groupmode = 0;
762 }
cddbd5d5 763
764 var newtitle = this.groupButton.getElementsByTagName('img')[0].title;
765
766 switch (this.groupmode) {
767 case 0:
768 newtitle = main.portal.strings['groupsnone']+' ('+main.portal.strings['clicktochange']+')';
769 break;
770 case 1:
771 newtitle = main.portal.strings['groupsseparate']+' ('+main.portal.strings['clicktochange']+')';
772 break;
773 case 2:
774 newtitle = main.portal.strings['groupsvisible']+' ('+main.portal.strings['clicktochange']+')';
775 break;
776 }
777
778 this.groupButton.getElementsByTagName('img')[0].title = newtitle;
779
d2a11d46 780 this.groupButton.getElementsByTagName('img')[0].src = main.portal.strings['pixpath']+this.groupImages[this.groupmode];
781 main.connect('POST', 'class=resource&field=groupmode', null, 'value='+this.groupmode+'&id='+this.id);
782}
783
784
354e1130 785resource_class.prototype.delete_button = function() {
446d6e7d 786 if (this.debug) {
787 YAHOO.log("Deleting "+this.getEl().id+" from parent "+this.parentObj.getEl().id);
788 }
789 if (!confirm(main.getString('deletecheck', main.getString(this.is)+" "+this.id))) {
790 return false;
354e1130 791 }
446d6e7d 792 this.parentObj.remove_resource(this);
793 main.connect('DELETE', 'class=resource&id='+this.id);
c4ca9cb3 794}
354e1130 795
a66ddf19 796
354e1130 797resource_class.prototype.update_index = function(index) {
e1c15ef7 798 if (this.debug) {
6f00683e 799 YAHOO.log("Updating Index for resource "+this.getEl().id+" to "+index);
e1c15ef7 800 }
801}
354e1130 802
a66ddf19 803
2469f7ea 804resource_class.prototype.startDrag = function(x, y) {
0a0bb380 805 YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
354e1130 806
0a0bb380 807 //reinitialize dd element
7f933d8f 808 this.getDragEl().innerHTML = '';
354e1130 809
0a0bb380 810 var targets = YAHOO.util.DDM.getRelated(this, true);
e1c15ef7 811 if (this.debug) {
812 YAHOO.log(this.id + " startDrag "+targets.length + " targets");
813 }
354e1130 814}
0a0bb380 815
a66ddf19 816
7f933d8f 817resource_class.prototype.clear_move_markers = function(target) {
818 if (target.is == 'section') {
819 resources = target.resources;
820 } else {
821 resources = target.parentObj.resources;
822 }
823 for (var i=0; i<resources.length; i++) {
824 YAHOO.util.Dom.setStyle(resources[i].getEl().id, 'border', 'none');
64e5a68d 825 }
7f933d8f 826}
827
a66ddf19 828
7f933d8f 829resource_class.prototype.onDragOver = function(e, ids) {
830 var target = YAHOO.util.DDM.getBestMatch(ids);
831
832 this.clear_move_markers(target);
833
834 if (target != this && (target.is == 'resource' || target.is == 'activity')) {
835 // Add a top border to show where the drop will place the resource.
836 YAHOO.util.Dom.setStyle(target.getEl().id, 'border-top', '1px solid #BBB');
837 } else if (target.is == 'section' && target.resources.length > 0) {
838 // We need to have a border at the bottom of the last activity in
839 // that section.
840 YAHOO.util.Dom.setStyle(target.resources[target.resources.length - 1].getEl().id,
841 'border-bottom', '1px solid #BBB');
64e5a68d 842 }
7f933d8f 843}
354e1130 844
a66ddf19 845
7f933d8f 846resource_class.prototype.onDragOut = function(e, ids) {
847 var target = YAHOO.util.DDM.getBestMatch(ids);
2469f7ea 848 if (target) {
849 this.clear_move_markers(target);
850 }
7f933d8f 851}
0a0bb380 852
a66ddf19 853
e50b0ad9 854resource_class.prototype.onDragDrop = function(e, ids) {
7f933d8f 855 var target = YAHOO.util.DDM.getBestMatch(ids);
2469f7ea 856 if (!target) {
857 YAHOO.log('onDragDrop: Target is not valid!', 'error');
858 }
859
64e5a68d 860 if (this.debug) {
3203f104 861 YAHOO.log("Dropped on section id="+target.sectionId
64e5a68d 862 +", el="+this.getEl().id
863 +", x="+YAHOO.util.Dom.getXY( this.getDragEl() ));
864 }
64e5a68d 865 this.parentObj.remove_resource(this);
354e1130 866
64e5a68d 867 if (target.is == 'resource' || target.is == 'activity') {
868 target.parentObj.insert_resource(this, target);
869 } else if (target.is == 'section') {
7f933d8f 870 target.insert_resource(this);
0a0bb380 871 }
7f933d8f 872 this.clear_move_markers(target);
0a0bb380 873 return;
64e5a68d 874}
0a0bb380 875
a66ddf19 876
0a0bb380 877resource_class.prototype.endDrag = function() {
64e5a68d 878 // Eliminates default action
354e1130 879}