[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 /** 2 * Section drag and drop. 3 * 4 * @class M.course.dragdrop.section 5 * @constructor 6 * @extends M.core.dragdrop 7 */ 8 var DRAGSECTION = function() { 9 DRAGSECTION.superclass.constructor.apply(this, arguments); 10 }; 11 Y.extend(DRAGSECTION, M.core.dragdrop, { 12 sectionlistselector: null, 13 14 initializer: function() { 15 // Set group for parent class 16 this.groups = [ CSS.SECTIONDRAGGABLE ]; 17 this.samenodeclass = M.course.format.get_sectionwrapperclass(); 18 this.parentnodeclass = M.course.format.get_containerclass(); 19 20 // Check if we are in single section mode 21 if (Y.Node.one('.' + CSS.JUMPMENU)) { 22 return false; 23 } 24 // Initialise sections dragging 25 this.sectionlistselector = M.course.format.get_section_wrapper(Y); 26 if (this.sectionlistselector) { 27 this.sectionlistselector = '.' + CSS.COURSECONTENT + ' ' + this.sectionlistselector; 28 29 this.setup_for_section(this.sectionlistselector); 30 31 // Make each li element in the lists of sections draggable 32 var del = new Y.DD.Delegate({ 33 container: '.' + CSS.COURSECONTENT, 34 nodes: '.' + CSS.SECTIONDRAGGABLE, 35 target: true, 36 handles: ['.' + CSS.LEFT], 37 dragConfig: {groups: this.groups} 38 }); 39 del.dd.plug(Y.Plugin.DDProxy, { 40 // Don't move the node at the end of the drag 41 moveOnEnd: false 42 }); 43 del.dd.plug(Y.Plugin.DDConstrained, { 44 // Keep it inside the .course-content 45 constrain: '#' + CSS.PAGECONTENT, 46 stickY: true 47 }); 48 del.dd.plug(Y.Plugin.DDWinScroll); 49 } 50 }, 51 52 /** 53 * Apply dragdrop features to the specified selector or node that refers to section(s) 54 * 55 * @method setup_for_section 56 * @param {String} baseselector The CSS selector or node to limit scope to 57 */ 58 setup_for_section: function(baseselector) { 59 Y.Node.all(baseselector).each(function(sectionnode) { 60 // Determine the section ID 61 var sectionid = Y.Moodle.core_course.util.section.getId(sectionnode); 62 63 // We skip the top section as it is not draggable 64 if (sectionid > 0) { 65 // Remove move icons 66 var movedown = sectionnode.one('.' + CSS.RIGHT + ' a.' + CSS.MOVEDOWN); 67 var moveup = sectionnode.one('.' + CSS.RIGHT + ' a.' + CSS.MOVEUP); 68 69 // Add dragger icon 70 var title = M.util.get_string('movesection', 'moodle', sectionid); 71 var cssleft = sectionnode.one('.' + CSS.LEFT); 72 73 if ((movedown || moveup) && cssleft) { 74 cssleft.setStyle('cursor', 'move'); 75 cssleft.appendChild(this.get_drag_handle(title, CSS.SECTIONHANDLE, 'icon', true)); 76 77 if (moveup) { 78 moveup.remove(); 79 } 80 if (movedown) { 81 movedown.remove(); 82 } 83 84 // This section can be moved - add the class to indicate this to Y.DD. 85 sectionnode.addClass(CSS.SECTIONDRAGGABLE); 86 } 87 } 88 }, this); 89 }, 90 91 /* 92 * Drag-dropping related functions 93 */ 94 drag_start: function(e) { 95 // Get our drag object 96 var drag = e.target; 97 // Creat a dummy structure of the outer elemnents for clean styles application 98 var containernode = Y.Node.create('<' + M.course.format.get_containernode() + '></' + M.course.format.get_containernode() + '>'); 99 containernode.addClass(M.course.format.get_containerclass()); 100 var sectionnode = Y.Node.create('<' + M.course.format.get_sectionwrappernode() + '></' + M.course.format.get_sectionwrappernode() + '>'); 101 sectionnode.addClass( M.course.format.get_sectionwrapperclass()); 102 sectionnode.setStyle('margin', 0); 103 sectionnode.setContent(drag.get('node').get('innerHTML')); 104 containernode.appendChild(sectionnode); 105 drag.get('dragNode').setContent(containernode); 106 drag.get('dragNode').addClass(CSS.COURSECONTENT); 107 }, 108 109 drag_dropmiss: function(e) { 110 // Missed the target, but we assume the user intended to drop it 111 // on the last last ghost node location, e.drag and e.drop should be 112 // prepared by global_drag_dropmiss parent so simulate drop_hit(e). 113 this.drop_hit(e); 114 }, 115 116 get_section_index: function(node) { 117 var sectionlistselector = '.' + CSS.COURSECONTENT + ' ' + M.course.format.get_section_selector(Y), 118 sectionList = Y.all(sectionlistselector), 119 nodeIndex = sectionList.indexOf(node), 120 zeroIndex = sectionList.indexOf(Y.one('#section-0')); 121 122 return (nodeIndex - zeroIndex); 123 }, 124 125 drop_hit: function(e) { 126 var drag = e.drag; 127 128 // Get references to our nodes and their IDs. 129 var dragnode = drag.get('node'), 130 dragnodeid = Y.Moodle.core_course.util.section.getId(dragnode), 131 loopstart = dragnodeid, 132 133 dropnodeindex = this.get_section_index(dragnode), 134 loopend = dropnodeindex; 135 136 if (dragnodeid === dropnodeindex) { 137 Y.log("Skipping move - same location moving " + dragnodeid + " to " + dropnodeindex, 'debug', 'moodle-course-dragdrop'); 138 return; 139 } 140 141 Y.log("Moving from position " + dragnodeid + " to position " + dropnodeindex, 'debug', 'moodle-course-dragdrop'); 142 143 if (loopstart > loopend) { 144 // If we're going up, we need to swap the loop order 145 // because loops can't go backwards. 146 loopstart = dropnodeindex; 147 loopend = dragnodeid; 148 } 149 150 // Get the list of nodes. 151 drag.get('dragNode').removeClass(CSS.COURSECONTENT); 152 var sectionlist = Y.Node.all(this.sectionlistselector); 153 154 // Add a lightbox if it's not there. 155 var lightbox = M.util.add_lightbox(Y, dragnode); 156 157 // Handle any variables which we must pass via AJAX. 158 var params = {}, 159 pageparams = this.get('config').pageparams, 160 varname; 161 162 for (varname in pageparams) { 163 if (!pageparams.hasOwnProperty(varname)) { 164 continue; 165 } 166 params[varname] = pageparams[varname]; 167 } 168 169 // Prepare request parameters 170 params.sesskey = M.cfg.sesskey; 171 params.courseId = this.get('courseid'); 172 params['class'] = 'section'; 173 params.field = 'move'; 174 params.id = dragnodeid; 175 params.value = dropnodeindex; 176 177 // Perform the AJAX request. 178 var uri = M.cfg.wwwroot + this.get('ajaxurl'); 179 Y.io(uri, { 180 method: 'POST', 181 data: params, 182 on: { 183 start: function() { 184 lightbox.show(); 185 }, 186 success: function(tid, response) { 187 // Update section titles, we can't simply swap them as 188 // they might have custom title 189 try { 190 var responsetext = Y.JSON.parse(response.responseText); 191 if (responsetext.error) { 192 new M.core.ajaxException(responsetext); 193 } 194 M.course.format.process_sections(Y, sectionlist, responsetext, loopstart, loopend); 195 } catch (e) {} 196 197 // Update all of the section IDs - first unset them, then set them 198 // to avoid duplicates in the DOM. 199 var index; 200 201 // Classic bubble sort algorithm is applied to the section 202 // nodes between original drag node location and the new one. 203 var swapped = false; 204 do { 205 swapped = false; 206 for (index = loopstart; index <= loopend; index++) { 207 if (Y.Moodle.core_course.util.section.getId(sectionlist.item(index - 1)) > 208 Y.Moodle.core_course.util.section.getId(sectionlist.item(index))) { 209 Y.log("Swapping " + Y.Moodle.core_course.util.section.getId(sectionlist.item(index - 1)) + 210 " with " + Y.Moodle.core_course.util.section.getId(sectionlist.item(index))); 211 // Swap section id. 212 var sectionid = sectionlist.item(index - 1).get('id'); 213 sectionlist.item(index - 1).set('id', sectionlist.item(index).get('id')); 214 sectionlist.item(index).set('id', sectionid); 215 216 // See what format needs to swap. 217 M.course.format.swap_sections(Y, index - 1, index); 218 219 // Update flag. 220 swapped = true; 221 } 222 } 223 loopend = loopend - 1; 224 } while (swapped); 225 226 window.setTimeout(function() { 227 lightbox.hide(); 228 }, 250); 229 }, 230 231 failure: function(tid, response) { 232 this.ajax_failure(response); 233 lightbox.hide(); 234 } 235 }, 236 context:this 237 }); 238 } 239 240 }, { 241 NAME: 'course-dragdrop-section', 242 ATTRS: { 243 courseid: { 244 value: null 245 }, 246 ajaxurl: { 247 value: 0 248 }, 249 config: { 250 value: 0 251 } 252 } 253 }); 254 255 M.course = M.course || {}; 256 M.course.init_section_dragdrop = function(params) { 257 new DRAGSECTION(params); 258 };
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |