[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/quiz/yui/src/dragdrop/js/ -> section.js (source)

   1  /**
   2   * Section drag and drop.
   3   *
   4   * @class M.mod_quiz.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.mod_quiz.edit.get_sectionwrapperclass();
  18          this.parentnodeclass = M.mod_quiz.edit.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.mod_quiz.edit.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 .mod-quiz-edit-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.mod_quiz.edit.get_containernode() + '></' + M.mod_quiz.edit.get_containernode() + '>');
  99          containernode.addClass(M.mod_quiz.edit.get_containerclass());
 100          var sectionnode = Y.Node.create('<' + M.mod_quiz.edit.get_sectionwrappernode() + '></' + M.mod_quiz.edit.get_sectionwrappernode() + '>');
 101          sectionnode.addClass( M.mod_quiz.edit.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.mod_quiz.edit.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-mod_quiz-dragdrop');
 138              return;
 139          }
 140  
 141          Y.log("Moving from position " + dragnodeid + " to position " + dropnodeindex, 'debug', 'moodle-mod_quiz-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.quizid = this.get('quizid');
 173          params['class'] = 'section';
 174          params.field = 'move';
 175          params.id = dragnodeid;
 176          params.value = dropnodeindex;
 177  
 178          // Perform the AJAX request.
 179          var uri = M.cfg.wwwroot + this.get('ajaxurl');
 180          Y.io(uri, {
 181              method: 'POST',
 182              data: params,
 183              on: {
 184                  start: function() {
 185                      lightbox.show();
 186                  },
 187                  success: function(tid, response) {
 188                      // Update section titles, we can't simply swap them as
 189                      // they might have custom title
 190                      try {
 191                          var responsetext = Y.JSON.parse(response.responseText);
 192                          if (responsetext.error) {
 193                              new M.core.ajaxException(responsetext);
 194                          }
 195                          M.mod_quiz.edit.process_sections(Y, sectionlist, responsetext, loopstart, loopend);
 196                      } catch (e) {}
 197  
 198                      // Update all of the section IDs - first unset them, then set them
 199                      // to avoid duplicates in the DOM.
 200                      var index;
 201  
 202                      // Classic bubble sort algorithm is applied to the section
 203                      // nodes between original drag node location and the new one.
 204                      var swapped = false;
 205                      do {
 206                          swapped = false;
 207                          for (index = loopstart; index <= loopend; index++) {
 208                              if (Y.Moodle.core_course.util.section.getId(sectionlist.item(index - 1)) >
 209                                          Y.Moodle.core_course.util.section.getId(sectionlist.item(index))) {
 210                                  Y.log("Swapping " + Y.Moodle.core_course.util.section.getId(sectionlist.item(index - 1)) +
 211                                          " with " + Y.Moodle.core_course.util.section.getId(sectionlist.item(index)),
 212                                          "debug", "moodle-mod_quiz-dragdrop");
 213                                  // Swap section id.
 214                                  var sectionid = sectionlist.item(index - 1).get('id');
 215                                  sectionlist.item(index - 1).set('id', sectionlist.item(index).get('id'));
 216                                  sectionlist.item(index).set('id', sectionid);
 217  
 218                                  // See what format needs to swap.
 219                                  M.mod_quiz.edit.swap_sections(Y, index - 1, index);
 220  
 221                                  // Update flag.
 222                                  swapped = true;
 223                              }
 224                          }
 225                          loopend = loopend - 1;
 226                      } while (swapped);
 227  
 228                      window.setTimeout(function() {
 229                          lightbox.hide();
 230                      }, 250);
 231                  },
 232  
 233                  failure: function(tid, response) {
 234                      this.ajax_failure(response);
 235                      lightbox.hide();
 236                  }
 237              },
 238              context:this
 239          });
 240      }
 241  
 242  }, {
 243      NAME: 'mod_quiz-dragdrop-section',
 244      ATTRS: {
 245          courseid: {
 246              value: null
 247          },
 248          quizid: {
 249              value: null
 250          },
 251          ajaxurl: {
 252              value: 0
 253          },
 254          config: {
 255              value: 0
 256          }
 257      }
 258  });
 259  
 260  M.mod_quiz = M.mod_quiz || {};
 261  M.mod_quiz.init_section_dragdrop = function(params) {
 262      new DRAGSECTION(params);
 263  };


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1