[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/course/yui/src/management/js/ -> item.js (source)

   1  /**
   2   * A managed course.
   3   *
   4   * @namespace M.course.management
   5   * @class Item
   6   * @constructor
   7   * @extends Base
   8   */
   9  function Item() {
  10      Item.superclass.constructor.apply(this, arguments);
  11  }
  12  Item.NAME = 'moodle-course-management-item';
  13  Item.CSS_PREFIX = 'management-item';
  14  Item.ATTRS = {
  15      /**
  16       * The node for this item.
  17       * @attribute node
  18       * @type Node
  19       */
  20      node: {},
  21  
  22      /**
  23       * The management console.
  24       * @attribute console
  25       * @type Console
  26       */
  27      console: {},
  28  
  29      /**
  30       * Describes the type of this item. Should be set by the extending class.
  31       * @attribute itemname
  32       * @type {String}
  33       * @default item
  34       */
  35      itemname: {
  36          value: 'item'
  37      }
  38  };
  39  Item.prototype = {
  40      /**
  41       * The highlight timeout for this item if there is one.
  42       * @property highlighttimeout
  43       * @protected
  44       * @type Timeout
  45       * @default null
  46       */
  47      highlighttimeout: null,
  48  
  49      /**
  50       * Checks and parses an AJAX response for an item.
  51       *
  52       * @method checkAjaxResponse
  53       * @protected
  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}
  58       */
  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');
  62              return false;
  63          }
  64          if (transactionid === null || args === null) {
  65              Y.log('Error: Invalid AJAX response details provided.', 'error', 'Item.checkAjaxResponse');
  66              return false;
  67          }
  68          var outcome = Y.JSON.parse(response.responseText);
  69          if (outcome.error !== false) {
  70              new M.core.exception(outcome);
  71          }
  72          if (outcome.outcome === false) {
  73              return false;
  74          }
  75          return outcome;
  76      },
  77  
  78      /**
  79       * Moves an item up by one.
  80       *
  81       * @method moveup
  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.
  85       * @return {Boolean}
  86       */
  87      moveup: function(transactionid, response, args) {
  88          var node,
  89              nodeup,
  90              nodedown,
  91              previous,
  92              previousup,
  93              previousdown,
  94              tmpnode,
  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');
  98              return false;
  99          }
 100          node = this.get('node');
 101          previous = node.previous('.listitem');
 102          if (previous) {
 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;">&nbsp;</a>');
 116                      previousdown.replace(tmpnode);
 117                      nodeup.replace(previousdown);
 118                      tmpnode.replace(nodeup);
 119                      tmpnode.destroy();
 120                  } else if (!nodedown) {
 121                      // previous down needs to be given to node.
 122                      nodeup.insert(previousdown, 'after');
 123                  }
 124              }
 125              nodeup = node.one(' > div a.action-moveup');
 126              if (nodeup) {
 127                  // Try to re-focus on up.
 128                  nodeup.focus();
 129              } else {
 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');
 132                  if (nodedown) {
 133                      nodedown.focus();
 134                  }
 135              }
 136              this.updated(true);
 137              Y.log('Success: ' + this.get('itemname') + ' moved up by AJAX.', 'info', 'moodle-course-management');
 138          } else {
 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.',
 142                      'info', 'moodle-course-management');
 143              window.location.reload();
 144          }
 145      },
 146  
 147      /**
 148       * Moves an item down by one.
 149       *
 150       * @method movedown
 151       * @param {Number} transactionid The transaction ID of the AJAX request (unique)
 152       * @param {Object} response The response from the AJAX request.
 153       * @param {Object} args The arguments given to the request.
 154       * @return {Boolean}
 155       */
 156      movedown: function(transactionid, response, args) {
 157          var node,
 158              next,
 159              nodeup,
 160              nodedown,
 161              nextup,
 162              nextdown,
 163              tmpnode,
 164              outcome = this.checkAjaxResponse(transactionid, response, args);
 165          if (outcome === false) {
 166              Y.log('AJAX request to move ' + this.get('itemname') + ' down failed by outcome.', 'warn', 'moodle-course-management');
 167              return false;
 168          }
 169          node = this.get('node');
 170          next = node.next('.listitem');
 171          if (next) {
 172              node.insert(next, 'before');
 173              nextdown = next.one(' > div a.action-movedown');
 174              nodeup = node.one(' > div a.action-moveup');
 175              if (!nextdown || !nodeup) {
 176                  // next up and node down should always be there. They would be required to trigger the action.
 177                  nextup = next.one(' > div a.action-moveup');
 178                  nodedown = node.one(' > div a.action-movedown');
 179                  if (!nextdown && !nodeup) {
 180                      // We can have two situations here:
 181                      //   1. nextdown is not set and nodeup is not set. This happens when there are only two courses.
 182                      //   2. nodeup is not set. This happens when we are moving the first course down.
 183                      // Ok, must be two courses. We need to switch the up and down icons.
 184                      tmpnode = Y.Node.create('<a style="visibility:hidden;">&nbsp;</a>');
 185                      nextup.replace(tmpnode);
 186                      nodedown.replace(nextup);
 187                      tmpnode.replace(nodedown);
 188                      tmpnode.destroy();
 189                  } else if (!nodeup) {
 190                      // next up needs to be given to node.
 191                      nodedown.insert(nextup, 'before');
 192                  }
 193              }
 194              nodedown = node.one(' > div a.action-movedown');
 195              if (nodedown) {
 196                  // Try to ensure the up is focused again.
 197                  nodedown.focus();
 198              } else {
 199                  // If we can't focus up we're at the top, try to focus on down.
 200                  nodeup = node.one(' > div a.action-moveup');
 201                  if (nodeup) {
 202                      nodeup.focus();
 203                  }
 204              }
 205              this.updated(true);
 206              Y.log('Success: ' + this.get('itemname') + ' moved down by AJAX.', 'info', 'moodle-course-management');
 207          } else {
 208              // Aha it succeeded but this is the bottom item in the list. Pagination is in play!
 209              // Refresh to update the state of things.
 210              Y.log(this.get('itemname') + ' cannot be moved down as its the top item on this page.',
 211                      'info', 'moodle-course-management');
 212              window.location.reload();
 213          }
 214      },
 215  
 216      /**
 217       * Makes an item visible.
 218       *
 219       * @method show
 220       * @param {Number} transactionid The transaction ID of the AJAX request (unique)
 221       * @param {Object} response The response from the AJAX request.
 222       * @param {Object} args The arguments given to the request.
 223       * @return {Boolean}
 224       */
 225      show: function(transactionid, response, args) {
 226          var outcome = this.checkAjaxResponse(transactionid, response, args),
 227              hidebtn;
 228          if (outcome === false) {
 229              Y.log('AJAX request to show ' + this.get('itemname') + ' by outcome.', 'warn', 'moodle-course-management');
 230              return false;
 231          }
 232  
 233          this.markVisible();
 234          hidebtn = this.get('node').one('a[data-action=hide]');
 235          if (hidebtn) {
 236              hidebtn.focus();
 237          }
 238          this.updated();
 239          Y.log('Success: ' + this.get('itemname') + ' made visible by AJAX.', 'info', 'moodle-course-management');
 240      },
 241  
 242      /**
 243       * Marks the item as visible
 244       * @method markVisible
 245       */
 246      markVisible: function() {
 247          this.get('node').setAttribute('data-visible', '1');
 248          Y.log('Marked ' + this.get('itemname') + ' as visible', 'info', 'moodle-course-management');
 249          return true;
 250      },
 251  
 252      /**
 253       * Hides an item.
 254       *
 255       * @method hide
 256       * @param {Number} transactionid The transaction ID of the AJAX request (unique)
 257       * @param {Object} response The response from the AJAX request.
 258       * @param {Object} args The arguments given to the request.
 259       * @return {Boolean}
 260       */
 261      hide: function(transactionid, response, args) {
 262          var outcome = this.checkAjaxResponse(transactionid, response, args),
 263              showbtn;
 264          if (outcome === false) {
 265              Y.log('AJAX request to hide ' + this.get('itemname') + ' by outcome.', 'warn', 'moodle-course-management');
 266              return false;
 267          }
 268          this.markHidden();
 269          showbtn = this.get('node').one('a[data-action=show]');
 270          if (showbtn) {
 271              showbtn.focus();
 272          }
 273          this.updated();
 274          Y.log('Success: ' + this.get('itemname') + ' made hidden by AJAX.', 'info', 'moodle-course-management');
 275      },
 276  
 277      /**
 278       * Marks the item as hidden.
 279       * @method makeHidden
 280       */
 281      markHidden: function() {
 282          this.get('node').setAttribute('data-visible', '0');
 283          Y.log('Marked ' + this.get('itemname') + ' as hidden', 'info', 'moodle-course-management');
 284          return true;
 285      },
 286  
 287      /**
 288       * Called when ever a node is updated.
 289       *
 290       * @method updated
 291       * @param {Boolean} moved True if this item was moved.
 292       */
 293      updated: function(moved) {
 294          if (moved) {
 295              this.highlight();
 296          }
 297      },
 298  
 299      /**
 300       * Highlights this option for a breif time.
 301       *
 302       * @method highlight
 303       */
 304      highlight: function() {
 305          var node = this.get('node');
 306          node.siblings('.highlight').removeClass('highlight');
 307          node.addClass('highlight');
 308          if (this.highlighttimeout) {
 309              window.clearTimeout(this.highlighttimeout);
 310          }
 311          this.highlighttimeout = window.setTimeout(function() {
 312              node.removeClass('highlight');
 313          }, 2500);
 314      }
 315  };
 316  Y.extend(Item, Y.Base, Item.prototype);


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1