[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

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

   1  /**
   2   * A managed category.
   3   *
   4   * @namespace M.course.management
   5   * @class Category
   6   * @constructor
   7   * @extends Item
   8   */
   9  function Category() {
  10      Category.superclass.constructor.apply(this, arguments);
  11  }
  12  Category.NAME = 'moodle-course-management-category';
  13  Category.CSS_PREFIX = 'management-category';
  14  Category.ATTRS = {
  15      /**
  16       * The category ID relating to this category.
  17       * @attribute categoryid
  18       * @type Number
  19       * @writeOnce
  20       * @default null
  21       */
  22      categoryid : {
  23          getter : function (value, name) {
  24              if (value === null) {
  25                  value = this.get('node').getData('id');
  26                  this.set(name, value);
  27              }
  28              return value;
  29          },
  30          value : null,
  31          writeOnce : true
  32      },
  33  
  34      /**
  35       * True if this category is the currently selected category.
  36       * @attribute selected
  37       * @type Boolean
  38       * @default null
  39       */
  40      selected : {
  41          getter : function(value, name) {
  42              if (value === null) {
  43                  value = this.get('node').getData(name);
  44                  if (value === null) {
  45                      value = false;
  46                  }
  47                  this.set(name, value);
  48              }
  49              return value;
  50          },
  51          value : null
  52      },
  53  
  54      /**
  55       * An array of courses belonging to this category.
  56       * @attribute courses
  57       * @type Course[]
  58       * @default Array
  59       */
  60      courses : {
  61          validator : function(val) {
  62              return Y.Lang.isArray(val);
  63          },
  64          value : []
  65      }
  66  };
  67  Category.prototype = {
  68      /**
  69       * Initialises an instance of a Category.
  70       * @method initializer
  71       */
  72      initializer : function() {
  73          this.set('itemname', 'category');
  74      },
  75  
  76      /**
  77       * Returns the name of the category.
  78       * @method getName
  79       * @return {String}
  80       */
  81      getName : function() {
  82          return this.get('node').one('a.categoryname').get('innerHTML');
  83      },
  84  
  85      /**
  86       * Registers a course as belonging to this category.
  87       * @method registerCourse
  88       * @param {Course} course
  89       */
  90      registerCourse : function(course) {
  91          var courses = this.get('courses');
  92          courses.push(course);
  93          this.set('courses', courses);
  94      },
  95  
  96      /**
  97       * Handles a category related event.
  98       *
  99       * @method handle
 100       * @param {String} action
 101       * @param {EventFacade} e
 102       * @return {Boolean}
 103       */
 104      handle : function(action, e) {
 105          var catarg = {categoryid : this.get('categoryid')},
 106              selected = this.get('console').get('activecategoryid');
 107          if (selected && selected !== catarg.categoryid) {
 108              catarg.selectedcategory = selected;
 109          }
 110          switch (action) {
 111              case 'moveup':
 112                  e.preventDefault();
 113                  this.get('console').performAjaxAction('movecategoryup', catarg, this.moveup, this);
 114                  break;
 115              case 'movedown':
 116                  e.preventDefault();
 117                  this.get('console').performAjaxAction('movecategorydown', catarg, this.movedown, this);
 118                  break;
 119              case 'show':
 120                  e.preventDefault();
 121                  this.get('console').performAjaxAction('showcategory', catarg, this.show, this);
 122                  break;
 123              case 'hide':
 124                  e.preventDefault();
 125                  this.get('console').performAjaxAction('hidecategory', catarg, this.hide, this);
 126                  break;
 127              case 'expand':
 128                  e.preventDefault();
 129                  if (this.get('node').getData('expanded') === '0') {
 130                      this.get('node').setAttribute('data-expanded', '1').setData('expanded', 'true');
 131                      this.get('console').performAjaxAction('getsubcategorieshtml', catarg, this.loadSubcategories, this);
 132                  }
 133                  this.expand();
 134                  break;
 135              case 'collapse':
 136                  e.preventDefault();
 137                  this.collapse();
 138                  break;
 139              case 'select':
 140                  var c = this.get('console'),
 141                      movecategoryto = c.get('categorylisting').one('#menumovecategoriesto');
 142                  // If any category is selected and there are more then one categories.
 143                  if (movecategoryto) {
 144                      if (c.isCategorySelected(e.currentTarget) &&
 145                              c.get('categories').length > 1) {
 146                          movecategoryto.removeAttribute('disabled');
 147                      } else {
 148                          movecategoryto.setAttribute('disabled', true);
 149                      }
 150                      c.handleBulkSortByaction();
 151                  }
 152                  break;
 153              default:
 154                  Y.log('Invalid AJAX action requested of managed category.', 'warn', 'moodle-course-management');
 155                  return false;
 156          }
 157      },
 158  
 159      /**
 160       * Expands the category making its sub categories visible.
 161       * @method expand
 162       */
 163      expand : function() {
 164          var node = this.get('node'),
 165              action = node.one('a[data-action=expand]'),
 166              ul = node.one('ul[role=group]');
 167          node.removeClass('collapsed').setAttribute('aria-expanded', 'true');
 168          action.setAttribute('data-action', 'collapse').setAttrs({
 169              title : M.util.get_string('collapsecategory', 'moodle', this.getName())
 170          }).one('img').setAttrs({
 171              src : M.util.image_url('t/switch_minus', 'moodle'),
 172              alt : M.util.get_string('collapse', 'moodle')
 173          });
 174          if (ul) {
 175              ul.setAttribute('aria-hidden', 'false');
 176          }
 177          this.get('console').performAjaxAction('expandcategory', {categoryid : this.get('categoryid')}, null, this);
 178      },
 179  
 180      /**
 181       * Collapses the category making its sub categories hidden.
 182       * @method collapse
 183       */
 184      collapse : function() {
 185          var node = this.get('node'),
 186              action = node.one('a[data-action=collapse]'),
 187              ul = node.one('ul[role=group]');
 188          node.addClass('collapsed').setAttribute('aria-expanded', 'false');
 189          action.setAttribute('data-action', 'expand').setAttrs({
 190              title : M.util.get_string('expandcategory', 'moodle', this.getName())
 191          }).one('img').setAttrs({
 192              src : M.util.image_url('t/switch_plus', 'moodle'),
 193              alt : M.util.get_string('expand', 'moodle')
 194          });
 195          if (ul) {
 196              ul.setAttribute('aria-hidden', 'true');
 197          }
 198          this.get('console').performAjaxAction('collapsecategory', {categoryid : this.get('categoryid')}, null, this);
 199      },
 200  
 201      /**
 202       * Loads sub categories provided by an AJAX request..
 203       *
 204       * @method loadSubcategories
 205       * @protected
 206       * @param {Number} transactionid The transaction ID of the AJAX request (unique)
 207       * @param {Object} response The response from the AJAX request.
 208       * @param {Object} args The arguments given to the request.
 209       * @return {Boolean} Returns true on success - false otherwise.
 210       */
 211      loadSubcategories : function(transactionid, response, args) {
 212          var outcome = this.checkAjaxResponse(transactionid, response, args),
 213              node = this.get('node'),
 214              managementconsole = this.get('console'),
 215              ul,
 216              actionnode;
 217          if (outcome === false) {
 218              Y.log('AJAX failed to load sub categories for '+this.get('itemname'), 'warn', 'moodle-course-management');
 219              return false;
 220          }
 221          Y.log('AJAX loaded subcategories for '+this.get('itemname'), 'info', 'moodle-course-management');
 222          node.append(outcome.html);
 223          managementconsole.initialiseCategories(node);
 224          if (M.core && M.core.actionmenu && M.core.actionmenu.newDOMNode) {
 225              M.core.actionmenu.newDOMNode(node);
 226          }
 227          ul = node.one('ul[role=group]');
 228          actionnode = node.one('a[data-action=collapse]');
 229          if (ul && actionnode) {
 230              actionnode.setAttribute('aria-controls', ul.generateID());
 231          }
 232          return true;
 233      },
 234  
 235      /**
 236       * Moves the course to this category.
 237       *
 238       * @method moveCourseTo
 239       * @param {Course} course
 240       */
 241      moveCourseTo : function(course) {
 242          var self = this;
 243          Y.use('moodle-core-notification-confirm', function() {
 244              var confirm = new M.core.confirm({
 245                  title : M.util.get_string('confirm', 'moodle'),
 246                  question : M.util.get_string('confirmcoursemove', 'moodle', {
 247                      course : course.getName(),
 248                      category : self.getName()
 249                  }),
 250                  yesLabel : M.util.get_string('move', 'moodle'),
 251                  noLabel : M.util.get_string('cancel', 'moodle')
 252              });
 253              confirm.on('complete-yes', function() {
 254                  confirm.hide();
 255                  confirm.destroy();
 256                  this.get('console').performAjaxAction('movecourseintocategory', {
 257                      categoryid : this.get('categoryid'),
 258                      courseid : course.get('courseid')
 259                  }, this.completeMoveCourse, this);
 260              }, self);
 261              confirm.show();
 262          });
 263      },
 264  
 265      /**
 266       * Completes moving a course to this category.
 267       * @method completeMoveCourse
 268       * @protected
 269       * @param {Number} transactionid The transaction ID of the AJAX request (unique)
 270       * @param {Object} response The response from the AJAX request.
 271       * @param {Object} args The arguments given to the request.
 272       * @return {Boolean}
 273       */
 274      completeMoveCourse : function(transactionid, response, args) {
 275          var outcome = this.checkAjaxResponse(transactionid, response, args),
 276              managementconsole = this.get('console'),
 277              category,
 278              course,
 279              totals;
 280          if (outcome === false) {
 281              Y.log('AJAX failed to move courses into this category: '+this.get('itemname'), 'warn', 'moodle-course-management');
 282              return false;
 283          }
 284          course = managementconsole.getCourseById(args.courseid);
 285          if (!course) {
 286              Y.log('Course was moved but the course listing could not be found to reflect this', 'warn', 'moodle-course-management');
 287              return false;
 288          }
 289          Y.log('Moved the course ('+course.getName()+') into this category ('+this.getName()+')', 'info', 'moodle-course-management');
 290          this.highlight();
 291          if (course) {
 292              if (outcome.paginationtotals) {
 293                  totals = managementconsole.get('courselisting').one('.listing-pagination-totals');
 294                  if (totals) {
 295                      totals.set('innerHTML', outcome.paginationtotals);
 296                  }
 297              }
 298              if (outcome.totalcatcourses !== 'undefined') {
 299                  totals = this.get('node').one('.course-count span');
 300                  if (totals) {
 301                      totals.set('innerHTML', totals.get('innerHTML').replace(/^\d+/, outcome.totalcatcourses));
 302                  }
 303              }
 304              if (typeof outcome.fromcatcoursecount !== 'undefined') {
 305                  category = managementconsole.get('activecategoryid');
 306                  category = managementconsole.getCategoryById(category);
 307                  if (category) {
 308                      totals = category.get('node').one('.course-count span');
 309                      if (totals) {
 310                          totals.set('innerHTML', totals.get('innerHTML').replace(/^\d+/, outcome.fromcatcoursecount));
 311                      }
 312                  }
 313              }
 314              course.remove();
 315          }
 316          return true;
 317      },
 318  
 319      /**
 320       * Makes an item visible.
 321       *
 322       * @method show
 323       * @param {Number} transactionid The transaction ID of the AJAX request (unique)
 324       * @param {Object} response The response from the AJAX request.
 325       * @param {Object} args The arguments given to the request.
 326       * @return {Boolean}
 327       */
 328      show : function(transactionid, response, args) {
 329          var outcome = this.checkAjaxResponse(transactionid, response, args),
 330              hidebtn;
 331          if (outcome === false) {
 332              Y.log('AJAX request to show '+this.get('itemname')+' by outcome.', 'warn', 'moodle-course-management');
 333              return false;
 334          }
 335  
 336          this.markVisible();
 337          hidebtn = this.get('node').one('a[data-action=hide]');
 338          if (hidebtn) {
 339              hidebtn.focus();
 340          }
 341          if (outcome.categoryvisibility) {
 342              this.updateChildVisibility(outcome.categoryvisibility);
 343          }
 344          if (outcome.coursevisibility) {
 345              this.updateCourseVisiblity(outcome.coursevisibility);
 346          }
 347          this.updated();
 348          Y.log('Success: category made visible by AJAX.', 'info', 'moodle-course-management');
 349      },
 350  
 351      /**
 352       * Hides an item.
 353       *
 354       * @method hide
 355       * @param {Number} transactionid The transaction ID of the AJAX request (unique)
 356       * @param {Object} response The response from the AJAX request.
 357       * @param {Object} args The arguments given to the request.
 358       * @return {Boolean}
 359       */
 360      hide : function(transactionid, response, args) {
 361          var outcome = this.checkAjaxResponse(transactionid, response, args),
 362              showbtn;
 363          if (outcome === false) {
 364              Y.log('AJAX request to hide '+this.get('itemname')+' by outcome.', 'warn', 'moodle-course-management');
 365              return false;
 366          }
 367          this.markHidden();
 368          showbtn = this.get('node').one('a[data-action=show]');
 369          if (showbtn) {
 370              showbtn.focus();
 371          }
 372          if (outcome.categoryvisibility) {
 373              this.updateChildVisibility(outcome.categoryvisibility);
 374          }
 375          if (outcome.coursevisibility) {
 376              this.updateCourseVisiblity(outcome.coursevisibility);
 377          }
 378          this.updated();
 379          Y.log('Success: '+this.get('itemname')+' made hidden by AJAX.', 'info', 'moodle-course-management');
 380      },
 381  
 382      /**
 383       * Updates the visibility of child courses if required.
 384       * @method updateCourseVisiblity
 385       * @chainable
 386       * @param courses
 387       */
 388      updateCourseVisiblity : function(courses) {
 389          var managementconsole = this.get('console'),
 390              key,
 391              course;
 392          Y.log('Changing categories course visibility', 'info', 'moodle-course-management');
 393          try {
 394              for (key in courses) {
 395                  if (typeof courses[key] === 'object') {
 396                      course = managementconsole.getCourseById(courses[key].id);
 397                      if (course) {
 398                          if (courses[key].visible === "1") {
 399                              course.markVisible();
 400                          } else {
 401                              course.markHidden();
 402                          }
 403                      }
 404                  }
 405              }
 406          } catch (err) {
 407              Y.log('Error trying to update course visibility: ' + err.message, 'warn', 'moodle-course-management');
 408          }
 409          return this;
 410      },
 411  
 412      /**
 413       * Updates the visibility of subcategories if required.
 414       * @method updateChildVisibility
 415       * @chainable
 416       * @param categories
 417       */
 418      updateChildVisibility : function(categories) {
 419          var managementconsole = this.get('console'),
 420              key,
 421              category;
 422          Y.log('Changing categories subcategory visibility', 'info', 'moodle-course-management');
 423          try {
 424              for (key in categories) {
 425                  if (typeof categories[key] === 'object') {
 426                      category = managementconsole.getCategoryById(categories[key].id);
 427                      if (category) {
 428                          if (categories[key].visible === "1") {
 429                              category.markVisible();
 430                          } else {
 431                              category.markHidden();
 432                          }
 433                      }
 434                  }
 435              }
 436          } catch (err) {
 437              Y.log('Error trying to update category visibility: ' + err.message, 'warn', 'moodle-course-management');
 438          }
 439          return this;
 440      }
 441  };
 442  Y.extend(Category, Item, Category.prototype);


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