[ Index ]

PHP Cross Reference of Unnamed Project

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 html = '<label>' + M.util.get_string('title', 'availability_grade') + ' <span class="availability-group">' +
  39              '<select name="id"><option value="0">' + M.util.get_string('choosedots', 'moodle') + '</option>';
  40      for (var i = 0; i < this.grades.length; i++) {
  41          var grade = this.grades[i];
  42          // String has already been escaped using format_string.
  43          html += '<option value="' + grade.id + '">' + grade.name + '</option>';
  44      }
  45      html += '</select></span></label> <span class="availability-group">' +
  46              '<label><input type="checkbox" name="min"/>' + M.util.get_string('option_min', 'availability_grade') +
  47              '</label> <label><span class="accesshide">' + M.util.get_string('label_min', 'availability_grade') +
  48              '</span><input type="text" name="minval" title="' +
  49              M.util.get_string('label_min', 'availability_grade') + '"/></label>%</span>' +
  50              '<span class="availability-group">' +
  51              '<label><input type="checkbox" name="max"/>' + M.util.get_string('option_max', 'availability_grade') +
  52              '</label> <label><span class="accesshide">' + M.util.get_string('label_max', 'availability_grade') +
  53              '</span><input type="text" name="maxval" title="' +
  54              M.util.get_string('label_max', 'availability_grade') + '"/></label>%</span>';
  55      var node = Y.Node.create('<span>' + html + '</span>');
  56  
  57      // Set initial values.
  58      if (json.id !== undefined &&
  59              node.one('select[name=id] > option[value=' + json.id + ']')) {
  60          node.one('select[name=id]').set('value', '' + json.id);
  61      }
  62      if (json.min !== undefined) {
  63          node.one('input[name=min]').set('checked', true);
  64          node.one('input[name=minval]').set('value', json.min);
  65      }
  66      if (json.max !== undefined) {
  67          node.one('input[name=max]').set('checked', true);
  68          node.one('input[name=maxval]').set('value', json.max);
  69      }
  70  
  71      // Disables/enables text input fields depending on checkbox.
  72      var updateCheckbox = function(check, focus) {
  73          var input = check.ancestor('label').next('label').one('input');
  74          var checked = check.get('checked');
  75          input.set('disabled', !checked);
  76          if (focus && checked) {
  77              input.focus();
  78          }
  79          return checked;
  80      };
  81      node.all('input[type=checkbox]').each(updateCheckbox);
  82  
  83      // Add event handlers (first time only).
  84      if (!M.availability_grade.form.addedEvents) {
  85          M.availability_grade.form.addedEvents = true;
  86  
  87          var root = Y.one('#fitem_id_availabilityconditionsjson');
  88          root.delegate('change', function() {
  89              // For the grade item, just update the form fields.
  90              M.core_availability.form.update();
  91          }, '.availability_grade select[name=id]');
  92  
  93          root.delegate('click', function() {
  94              updateCheckbox(this, true);
  95              M.core_availability.form.update();
  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: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1