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