[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/availability/condition/grade/yui/build/moodle-availability_grade-form/ -> moodle-availability_grade-form.js (source)

   1  YUI.add('moodle-availability_grade-form', function (Y, NAME) {
   2  
   3  /**
   4   * JavaScript for form editing grade conditions.
   5   *
   6   * @module moodle-availability_grade-form
   7   */
   8  M.availability_grade = M.availability_grade || {};
   9  
  10  /**
  11   * @class M.availability_grade.form
  12   * @extends M.core_availability.plugin
  13   */
  14  M.availability_grade.form = Y.Object(M.core_availability.plugin);
  15  
  16  /**
  17   * Grade items available for selection.
  18   *
  19   * @property grades
  20   * @type Array
  21   */
  22  M.availability_grade.form.grades = null;
  23  
  24  /**
  25   * Initialises this plugin.
  26   *
  27   * @method initInner
  28   * @param {Array} grades Array of objects containing gradeid => name
  29   */
  30  M.availability_grade.form.initInner = function(grades) {
  31      this.grades = grades;
  32      this.nodesSoFar = 0;
  33  };
  34  
  35  M.availability_grade.form.getNode = function(json) {
  36      // Increment number used for unique ids.
  37      this.nodesSoFar++;
  38  
  39      // Create HTML structure.
  40      var html = '<label>' + M.util.get_string('title', 'availability_grade') + ' <span class="availability-group">' +
  41              '<select name="id"><option value="0">' + M.util.get_string('choosedots', 'moodle') + '</option>';
  42      for (var i = 0; i < this.grades.length; i++) {
  43          var grade = this.grades[i];
  44          // String has already been escaped using format_string.
  45          html += '<option value="' + grade.id + '">' + grade.name + '</option>';
  46      }
  47      html += '</select></span></label> <span class="availability-group">' +
  48              '<label><input type="checkbox" name="min"/>' + M.util.get_string('option_min', 'availability_grade') +
  49              '</label> <label><span class="accesshide">' + M.util.get_string('label_min', 'availability_grade') +
  50              '</span><input type="text" name="minval" title="' +
  51              M.util.get_string('label_min', 'availability_grade') + '"/></label>%</span>' +
  52              '<span class="availability-group">' +
  53              '<label><input type="checkbox" name="max"/>' + M.util.get_string('option_max', 'availability_grade') +
  54              '</label> <label><span class="accesshide">' + M.util.get_string('label_max', 'availability_grade') +
  55              '</span><input type="text" name="maxval" title="' +
  56              M.util.get_string('label_max', 'availability_grade') + '"/></label>%</span>';
  57      var node = Y.Node.create('<span>' + html + '</span>');
  58  
  59      // Set initial values.
  60      if (json.id !== undefined &&
  61              node.one('select[name=id] > option[value=' + json.id + ']')) {
  62          node.one('select[name=id]').set('value', '' + json.id);
  63      }
  64      if (json.min !== undefined) {
  65          node.one('input[name=min]').set('checked', true);
  66          node.one('input[name=minval]').set('value', json.min);
  67      }
  68      if (json.max !== undefined) {
  69          node.one('input[name=max]').set('checked', true);
  70          node.one('input[name=maxval]').set('value', json.max);
  71      }
  72  
  73      // Disables/enables text input fields depending on checkbox.
  74      var updateCheckbox = function(check, focus) {
  75          var input = check.ancestor('label').next('label').one('input');
  76          var checked = check.get('checked');
  77          input.set('disabled', !checked);
  78          if (focus && checked) {
  79              input.focus();
  80          }
  81          return checked;
  82      };
  83      node.all('input[type=checkbox]').each(updateCheckbox);
  84  
  85      // Add event handlers (first time only).
  86      if (!M.availability_grade.form.addedEvents) {
  87          M.availability_grade.form.addedEvents = true;
  88  
  89          var root = Y.one('#fitem_id_availabilityconditionsjson');
  90          root.delegate('change', function() {
  91              // For the grade item, just update the form fields.
  92              M.core_availability.form.update();
  93          }, '.availability_grade select[name=id]');
  94  
  95          root.delegate('click', function() {
  96              updateCheckbox(this, true);
  97              M.core_availability.form.update();
  98          }, '.availability_grade input[type=checkbox]');
  99  
 100          root.delegate('valuechange', function() {
 101              // For grade values, just update the form fields.
 102              M.core_availability.form.update();
 103          }, '.availability_grade input[type=text]');
 104      }
 105  
 106      return node;
 107  };
 108  
 109  M.availability_grade.form.fillValue = function(value, node) {
 110      value.id = parseInt(node.one('select[name=id]').get('value'), 10);
 111      if (node.one('input[name=min]').get('checked')) {
 112          value.min = this.getValue('minval', node);
 113      }
 114      if (node.one('input[name=max]').get('checked')) {
 115          value.max = this.getValue('maxval', node);
 116      }
 117  };
 118  
 119  /**
 120   * Gets the numeric value of an input field. Supports decimal points (using
 121   * dot or comma).
 122   *
 123   * @method getValue
 124   * @return {Number|String} Value of field as number or string if not valid
 125   */
 126  M.availability_grade.form.getValue = function(field, node) {
 127      // Get field value.
 128      var value = node.one('input[name=' + field + ']').get('value');
 129  
 130      // If it is not a valid positive number, return false.
 131      if (!(/^[0-9]+([.,][0-9]+)?$/.test(value))) {
 132          return value;
 133      }
 134  
 135      // Replace comma with dot and parse as floating-point.
 136      var result = parseFloat(value.replace(',', '.'));
 137      if (result < 0 || result > 100) {
 138          return value;
 139      } else {
 140          return result;
 141      }
 142  };
 143  
 144  M.availability_grade.form.fillErrors = function(errors, node) {
 145      var value = {};
 146      this.fillValue(value, node);
 147  
 148      // Check grade item id.
 149      if (value.id === 0) {
 150          errors.push('availability_grade:error_selectgradeid');
 151      }
 152  
 153      // Check numeric values.
 154      if ((value.min !== undefined && typeof (value.min) === 'string') ||
 155              (value.max !== undefined && typeof (value.max) === 'string')) {
 156          errors.push('availability_grade:error_invalidnumber');
 157      } else if (value.min !== undefined && value.max !== undefined &&
 158              value.min >= value.max) {
 159          errors.push('availability_grade:error_backwardrange');
 160      }
 161  };
 162  
 163  
 164  }, '@VERSION@', {"requires": ["base", "node", "event", "moodle-core_availability-form"]});


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