4 * @namespace M.course.management
10 Item.superclass.constructor.apply(this, arguments);
12 Item.NAME = 'moodle-course-management-item';
13 Item.CSS_PREFIX = 'management-item';
16 * The node for this item.
23 * The management console.
30 * Describes the type of this item. Should be set by the extending class.
41 * The highlight timeout for this item if there is one.
42 * @property highlighttimeout
47 highlighttimeout : null,
50 * Checks and parses an AJAX response for an item.
52 * @method checkAjaxResponse
54 * @param {Number} transactionid The transaction ID of the AJAX request (unique)
55 * @param {Object} response The response from the AJAX request.
56 * @param {Object} args The arguments given to the request.
57 * @return {Object|Boolean}
59 checkAjaxResponse : function(transactionid, response, args) {
60 if (response.status !== 200) {
61 Y.log('Error: AJAX response resulted in non 200 status.', 'error', 'Item.checkAjaxResponse');
64 if (transactionid === null || args === null) {
65 Y.log('Error: Invalid AJAX response details provided.', 'error', 'Item.checkAjaxResponse');
68 var outcome = Y.JSON.parse(response.responseText);
69 if (outcome.error !== false) {
70 new M.core.exception(outcome);
72 if (outcome.outcome === false) {
79 * Moves an item up by one.
82 * @param {Number} transactionid The transaction ID of the AJAX request (unique)
83 * @param {Object} response The response from the AJAX request.
84 * @param {Object} args The arguments given to the request.
87 moveup : function(transactionid, response, args) {
95 outcome = this.checkAjaxResponse(transactionid, response, args);
96 if (outcome === false) {
97 Y.log('AJAX request to move '+this.get('itemname')+' up failed by outcome.', 'warn', 'moodle-course-management');
100 node = this.get('node');
101 previous = node.previous('.listitem');
103 previous.insert(node, 'before');
104 previousup = previous.one(' > div a.action-moveup');
105 nodedown = node.one(' > div a.action-movedown');
106 if (!previousup || !nodedown) {
107 // We can have two situations here:
108 // 1. previousup is not set and nodedown is not set. This happens when there are only two courses.
109 // 2. nodedown is not set. This happens when they are moving the bottom course up.
110 // node up and previous down should always be there. They would be required to trigger the action.
111 nodeup = node.one(' > div a.action-moveup');
112 previousdown = previous.one(' > div a.action-movedown');
113 if (!previousup && !nodedown) {
114 // Ok, must be two courses. We need to switch the up and down icons.
115 tmpnode = Y.Node.create('<a style="visibility:hidden;"> </a>');
116 previousdown.replace(tmpnode);
117 nodeup.replace(previousdown);
118 tmpnode.replace(nodeup);
120 } else if (!nodedown) {
121 // previous down needs to be given to node.
122 nodeup.insert(previousdown, 'after');
125 nodeup = node.one(' > div a.action-moveup');
127 // Try to re-focus on up.
130 // If we can't focus up we're at the bottom, try to focus on up.
131 nodedown = node.one(' > div a.action-movedown');
137 Y.log('Success: '+this.get('itemname')+' moved up by AJAX.', 'info', 'moodle-course-management');
139 // Aha it succeeded but this is the top item in the list. Pagination is in play!
140 // Refresh to update the state of things.
141 Y.log(this.get('itemname')+' cannot be moved up as its the top item on this page.', 'info', 'moodle-course-management');
142 window.location.reload();
147 * Moves an item down by one.
150 * @param {Number} transactionid The transaction ID of the AJAX request (unique)
151 * @param {Object} response The response from the AJAX request.
152 * @param {Object} args The arguments given to the request.
155 movedown : function(transactionid, response, args) {
163 outcome = this.checkAjaxResponse(transactionid, response, args);
164 if (outcome === false) {
165 Y.log('AJAX request to move '+this.get('itemname')+' down failed by outcome.', 'warn', 'moodle-course-management');
168 node = this.get('node');
169 next = node.next('.listitem');
171 node.insert(next, 'before');
172 nextdown = next.one(' > div a.action-movedown');
173 nodeup = node.one(' > div a.action-moveup');
174 if (!nextdown || !nodeup) {
175 // next up and node down should always be there. They would be required to trigger the action.
176 nextup = next.one(' > div a.action-moveup');
177 nodedown = node.one(' > div a.action-movedown');
178 if (!nextdown && !nodeup) {
179 // We can have two situations here:
180 // 1. nextdown is not set and nodeup is not set. This happens when there are only two courses.
181 // 2. nodeup is not set. This happens when we are moving the first course down.
182 // Ok, must be two courses. We need to switch the up and down icons.
183 tmpnode = Y.Node.create('<a style="visibility:hidden;"> </a>');
184 nextup.replace(tmpnode);
185 nodedown.replace(nextup);
186 tmpnode.replace(nodedown);
188 } else if (!nodeup) {
189 // next up needs to be given to node.
190 nodedown.insert(nextup, 'before');
193 nodedown = node.one(' > div a.action-movedown');
195 // Try to ensure the up is focused again.
198 // If we can't focus up we're at the top, try to focus on down.
199 nodeup = node.one(' > div a.action-moveup');
205 Y.log('Success: '+this.get('itemname')+' moved down by AJAX.', 'info', 'moodle-course-management');
207 // Aha it succeeded but this is the bottom item in the list. Pagination is in play!
208 // Refresh to update the state of things.
209 Y.log(this.get('itemname')+' cannot be moved down as its the top item on this page.', 'info', 'moodle-course-management');
210 window.location.reload();
215 * Makes an item visible.
218 * @param {Number} transactionid The transaction ID of the AJAX request (unique)
219 * @param {Object} response The response from the AJAX request.
220 * @param {Object} args The arguments given to the request.
223 show : function(transactionid, response, args) {
224 var outcome = this.checkAjaxResponse(transactionid, response, args),
226 if (outcome === false) {
227 Y.log('AJAX request to show '+this.get('itemname')+' by outcome.', 'warn', 'moodle-course-management');
232 hidebtn = this.get('node').one('a[data-action=hide]');
237 Y.log('Success: '+this.get('itemname')+' made visible by AJAX.', 'info', 'moodle-course-management');
241 * Marks the item as visible
242 * @method markVisible
244 markVisible : function() {
245 this.get('node').setAttribute('data-visible', '1');
246 Y.log('Marked '+this.get('itemname')+' as visible', 'info', 'moodle-course-management');
254 * @param {Number} transactionid The transaction ID of the AJAX request (unique)
255 * @param {Object} response The response from the AJAX request.
256 * @param {Object} args The arguments given to the request.
259 hide : function(transactionid, response, args) {
260 var outcome = this.checkAjaxResponse(transactionid, response, args),
262 if (outcome === false) {
263 Y.log('AJAX request to hide '+this.get('itemname')+' by outcome.', 'warn', 'moodle-course-management');
267 showbtn = this.get('node').one('a[data-action=show]');
272 Y.log('Success: '+this.get('itemname')+' made hidden by AJAX.', 'info', 'moodle-course-management');
276 * Marks the item as hidden.
279 markHidden : function() {
280 this.get('node').setAttribute('data-visible', '0');
281 Y.log('Marked '+this.get('itemname')+' as hidden', 'info', 'moodle-course-management');
286 * Called when ever a node is updated.
289 * @param {Boolean} moved True if this item was moved.
291 updated : function(moved) {
298 * Highlights this option for a breif time.
302 highlight : function() {
303 var node = this.get('node');
304 node.siblings('.highlight').removeClass('highlight');
305 node.addClass('highlight');
306 if (this.highlighttimeout) {
307 window.clearTimeout(this.highlighttimeout);
309 this.highlighttimeout = window.setTimeout(function(){
310 node.removeClass('highlight');
314 Y.extend(Item, Y.Base, Item.prototype);