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