[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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

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


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