MDL-31903 course: update patch to fix course highlight icon and title toggle
[moodle.git] / lib / ajax / ajaxcourse.js
CommitLineData
286e1f0e 1/**
2 * Contains Main class and supporting functions for ajax course layout
9bb74178 3 */
4
5
6//hide content body until done loading (manipulation looks ugly elsewise)
2469f7ea 7//document.getElementById('content').style.display = 'none';
286e1f0e 8
8c3c518f 9// If firebug console is undefined, define a fake one here
10if (window.console) {
11 console.vardump = function(data) {
12 retval = '';
13 for (key in data) {
14 retval += key+' = '+data[key] + "\n";
15 }
16 console.log(retval);
17 };
18}
9bb74178 19
2469f7ea 20//onload object for handling scripts on page load, this insures they run in my order
9bb74178 21function onload_class() {
22 this.scripts = new Array();
2469f7ea 23 this.debug = true;
9bb74178 24}
25
286e1f0e 26
9bb74178 27onload_class.prototype.add = function(script) {
2469f7ea 28 if (this.debug) {
72d28452 29 YAHOO.log("onloadobj.add - adding "+script, "junk");
30 }
9bb74178 31 this.scripts[this.scripts.length] = script;
b532c911 32};
9bb74178 33
2469f7ea 34
286e1f0e 35onload_class.prototype.load = function() {
9bb74178 36 var scriptcount = this.scripts.length;
72d28452 37 if (this.debug) {
38 YAHOO.log("onloadobj.load - loading "+scriptcount+" scripts", "info");
39 }
2469f7ea 40 for (i=0; i<scriptcount; i++) {
41 eval(this.scripts[i]);
9bb74178 42 }
b532c911 43};
9bb74178 44
286e1f0e 45
2469f7ea 46var onloadobj = new onload_class();
9bb74178 47
48
49//main page object
286e1f0e 50function main_class() {
72d28452 51 this.debug = true;
9bb74178 52 this.portal = new php_portal_class();
53
54 this.blocks = new Array();
55 this.sections = new Array();
8c3c518f 56 this.sectiondates = {};
9bb74178 57 this.leftcolumn = null;
2469f7ea 58 this.rightcolumn = null;
59 this.adminBlock = null;
ddc66060 60 this.tempBlock = null;
9bb74178 61 this.icons = [];
ddc66060 62 this.courseformat = null;
9bb74178 63 this.marker = null;
b1b4c2d7 64 this.numsections = null;
e8b32e2b 65 this.lastsection = null; // real last section num including unavailable
9bb74178 66
67 //things to process onload
2469f7ea 68 onloadobj.add('main.process_document();');
9badf215 69 onloadobj.add("if (document.getElementById('content')) document.getElementById('content').style.display='block';");
9bb74178 70
71 //connection queue allows xhttp requests to be sent in order
72 this.connectQueue = [];
73 this.connectQueueHead = 0;
74 this.connectQueueConnection = null;
9bb74178 75}
76
77
286e1f0e 78main_class.prototype.process_blocks = function() {
2469f7ea 79 //remove unneeded icons (old school position icons and delete/hide
72d28452 80 //although they will be read)
286e1f0e 81 var rmIconClasses = ['icon up', 'icon down', 'icon right', 'icon left', 'icon delete', 'icon hide'];
82 for (var c=0; c<rmIconClasses.length; c++) {
9bb74178 83 els = YAHOO.util.Dom.getElementsByClassName(rmIconClasses[c]);
84
286e1f0e 85 for (var x=0; x<els.length; x++) {
9bb74178 86 els[x].parentNode.removeChild(els[x]);
87 }
ca255392 88 }
9bb74178 89 //process the block ids passed from php
90 var blockcount = this.portal.blocks.length;
286e1f0e 91 YAHOO.log("main.processBlocks - processing "+blockcount+" blocks", "info");
92
2469f7ea 93 for (i=0; i<blockcount; i++) {
286e1f0e 94 this.blocks[i] = new block_class(this.portal.blocks[i][1], "blocks");
9bb74178 95
96 //put in correct side array also
97 if (this.portal.blocks[i][0] == 'l') {
98 main.leftcolumn.add_block(this.blocks[i]);
99 } else if (this.portal.blocks[i][0] == 'r') {
ca255392 100 main.rightcolumn.add_block(this.blocks[i]);
9bb74178 101 }
102
103 //hide if called for
104 if (this.portal.blocks[i][2] == 1) {
286e1f0e 105 this.blocks[i].toggle_hide(null, null, true);
9bb74178 106 }
ca255392 107 }
b532c911 108};
9bb74178 109
110
286e1f0e 111main_class.prototype.process_document = function() {
2469f7ea 112 //process the document to get important containers
286e1f0e 113 YAHOO.log("Processing Document", "info");
114
9bb74178 115 //process columns for blocks
286e1f0e 116 this.leftcolumn = new column_class('left-column', "blocks", null, 'l');
117 this.rightcolumn = new column_class('right-column', "blocks", null, 'r');
9bb74178 118
119 //process sections
b1b4c2d7
RW
120 //var ct = 0;
121 //while (document.getElementById('section-'+ct) != null) {
b1b4c2d7 122 this.courseformat = this.portal.courseformat;
e8b32e2b
PS
123 for(var ct=0; ct <= this.portal.lastsection; ct++){
124 if (document.getElementById('section-'+ct) != null) {
125 var dragable = ((ct > 0) && (ct <= this.portal.numsections));
126 this.sections[ct] = new section_class('section-'+ct, "sections", null, dragable);
b1b4c2d7
RW
127 this.sections[ct].addToGroup('resources');
128 if (ct > 0) {
129 var sectiontitle = YAHOO.util.Selector.query('#section-'+ct+' h3.weekdates')[0];
130 if (undefined !== sectiontitle) { // Only save date for weekly format
131 this.sectiondates[ct] = sectiontitle.innerHTML;
132 }
8c3c518f 133 }
e8b32e2b
PS
134 } else {
135 this.sections[ct] = null;
8c3c518f 136 }
b1b4c2d7 137 //ct++;
9bb74178 138 }
2469f7ea 139 if (this.debug) {
72d28452 140 YAHOO.log("Processed "+ct+" sections");
141 }
9bb74178 142
ca255392 143 this.adminBlock = YAHOO.util.Dom.getElementsByClassName('block_adminblock')[0];
b532c911
PS
144 this.tempBlock = YAHOO.util.Dom.getElementsByClassName('tempblockhandler')[0];
145};
9bb74178 146
286e1f0e 147
148main_class.prototype.mk_safe_for_transport = function(input) {
149 return input.replace(/&/i, '_.amp._');
b532c911 150};
9bb74178 151
286e1f0e 152
153//return block by id
154main_class.prototype.get_block_index = function(el) {
9bb74178 155 var blockcount = this.blocks.length;
286e1f0e 156 for (i=0; i<blockcount; i++) {
9bb74178 157 if (this.blocks[i] == el) {
158 return i;
159 }
160 }
b532c911 161};
9bb74178 162
286e1f0e 163
164main_class.prototype.get_section_index = function(el) {
9bb74178 165 var sectioncount = this.sections.length;
286e1f0e 166 for (i=0; i<sectioncount; i++) {
9bb74178 167 if (this.sections[i] == el) {
168 return i;
169 }
170 }
e8b32e2b 171 return -1;
b532c911 172};
9bb74178 173
f8eaeffa 174main_class.prototype.mk_button = function(tag, imgSrc, text, attributes, imgAttributes) {
175 //Create button and return object.
176 //Set the text: the container TITLE or image ALT attributes can be overridden, eg.
ddedf979 177 // main.mk_button('a', main.portal.icons['move_2d'], strmove, [['title', strmoveshort]]);
9bb74178 178 var container = document.createElement(tag);
f8eaeffa 179 container.style.cursor = 'pointer';
180 container.setAttribute('title', text);
9bb74178 181 var image = document.createElement('img');
d8158863 182
ddedf979 183 image.setAttribute('src', imgSrc);
f8eaeffa 184 image.setAttribute('alt', text);
9bb74178 185 container.appendChild(image);
186
187 if (attributes != null) {
286e1f0e 188 for (var c=0; c<attributes.length; c++) {
5cfea9fb 189 if (attributes[c][0] == 'title' && this.is_ie()) {
190 image.setAttribute(attributes[c][0], attributes[c][1]); //IE hack: transfer 'title'.
191 } else {
192 container.setAttribute(attributes[c][0], attributes[c][1]);
193 }
9bb74178 194 }
195 }
9bb74178 196 if (imgAttributes != null) {
11599e47 197 for (var c=0; c<imgAttributes.length; c++) {
f8eaeffa 198 image.setAttribute(imgAttributes[c][0], imgAttributes[c][1]);
9bb74178 199 }
200 }
f7538871 201 image.setAttribute('hspace', '3');
9bb74178 202 return container;
b532c911 203};
9bb74178 204
9bb74178 205
286e1f0e 206main_class.prototype.connect = function(method, urlStub, callback, body) {
2469f7ea 207 if (this.debug) {
72d28452 208 YAHOO.log("Making "+method+" connection to /course/rest.php?courseId="+main.portal.id+"&"+urlStub);
209 }
9bb74178 210 if (callback == null) {
ca255392 211 if (this.debug) {
212 callback = {
213 success: function(response) {
214 YAHOO.log("Response from the Request: " + response.statusText + ": " + response.responseText, 'info');
215 },
216 failure: function() {
217 YAHOO.log("Response from the Request: " + response.statusText + ": " + response.responseText, 'error');
218 }
219 };
220 } else {
221 callback = {};
222 }
9bb74178 223 }
92942376 224 return YAHOO.util.Connect.asyncRequest(method, this.portal.strings['wwwroot']+"/course/rest.php?courseId="+main.portal.id+"&sesskey="+this.portal.strings['sesskey']+"&"+urlStub, callback, body);
b532c911 225};
286e1f0e 226
9bb74178 227
286e1f0e 228main_class.prototype.connectQueue_add = function(method, urlStub, callback, body) {
9bb74178 229 var Qlength = main.connectQueue.length;
230 main.connectQueue[Qlength] = [];
231 main.connectQueue[Qlength]['method'] = method;
232 main.connectQueue[Qlength]['urlStub'] = urlStub;
233 main.connectQueue[Qlength]['body'] = body;
234
235 if (main.connectQueueConnection == null || !YAHOO.util.Connect.isCallInProgress(main.connectQueueConnection)) {
236 main.connectQueue_fireNext();
237 }
b532c911 238};
9bb74178 239
286e1f0e 240
241main_class.prototype.connectQueue_fireNext = function() {
9bb74178 242 var head = main.connectQueueHead;
243 if (head >= main.connectQueue.length) {
244 return;
245 }
9bb74178 246 var callback = {
72d28452 247 success: function(){
248 main.connectQueue_fireNext();
286e1f0e 249 }
b532c911 250 };
286e1f0e 251 main.connectQueueConnection = main.connect(main.connectQueue[head]['method'],
72d28452 252 main.connectQueue[head]['urlStub'],
253 callback,
254 main.connectQueue[head]['body']);
9bb74178 255 main.connectQueueHead++;
b532c911 256};
9bb74178 257
286e1f0e 258
259main_class.prototype.update_marker = function(newMarker) {
9bb74178 260 if (this.marker != null) {
261 this.marker.toggle_highlight();
262 }
ca255392 263 this.marker = newMarker;
9bb74178 264 this.marker.toggle_highlight();
265
286e1f0e 266 this.connect('post', 'class=course&field=marker', null, 'value='+this.marker.sectionId);
b532c911 267};
286e1f0e 268
269
f8eaeffa 270main_class.prototype.getString = function(identifier, variable) {
271 if (this.portal.strings[identifier]) {
272 return this.portal.strings[identifier].replace(/_var_/, variable);
72d28452 273 }
b532c911 274};
9bb74178 275
b31b2d84
DM
276main_class.prototype.hasString = function(identifier) {
277 if (this.portal.strings[identifier]) {
278 return true;
279 }
280 return false;
281};
282
5cfea9fb 283main_class.prototype.is_ie = function() {
284 var agent = navigator.userAgent.toLowerCase();
285 if ((agent.indexOf('msie') != -1)) {
286 return true;
287 }
288 return false;
b532c911 289};
5cfea9fb 290
2fb6deb5 291
9bb74178 292var main = new main_class();
293
9bb74178 294
286e1f0e 295function php_portal_class() {
296 //portal to php data
9bb74178 297 this.id = null;
72d28452 298 this.debug = null;
9bb74178 299
ca255392 300 //array of id's of blocks set at end of page load by php
9bb74178 301 this.blocks = new Array();
302 this.imagePath = null;
303
304 //flag for week fomat
305 this.isWeek = false;
306
ca255392 307 //strings
9bb74178 308 this.strings = [];
309
ddc66060 310 //icons
311 this.icons = [];
312
286e1f0e 313 YAHOO.log("Instantiated php_portal_class", "info");
9bb74178 314}