[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/availability/condition/grade/yui/src/form/js/ -> form.js (source)

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


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