[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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

   1  /**
   2   * JavaScript for form editing profile conditions.
   3   *
   4   * @module moodle-availability_profile-form
   5   */
   6  M.availability_profile = M.availability_profile || {};
   7  
   8  /**
   9   * @class M.availability_profile.form
  10   * @extends M.core_availability.plugin
  11   */
  12  M.availability_profile.form = Y.Object(M.core_availability.plugin);
  13  
  14  /**
  15   * Groupings available for selection (alphabetical order).
  16   *
  17   * @property profiles
  18   * @type Array
  19   */
  20  M.availability_profile.form.profiles = null;
  21  
  22  /**
  23   * Initialises this plugin.
  24   *
  25   * @method initInner
  26   * @param {Array} standardFields Array of objects with .field, .display
  27   * @param {Array} customFields Array of objects with .field, .display
  28   */
  29  M.availability_profile.form.initInner = function(standardFields, customFields) {
  30      this.standardFields = standardFields;
  31      this.customFields = customFields;
  32  };
  33  
  34  M.availability_profile.form.getNode = function(json) {
  35      // Create HTML structure.
  36      var html = '<span class="availability-group"><label>' + M.util.get_string('conditiontitle', 'availability_profile') + ' ' +
  37              '<select name="field">' +
  38              '<option value="choose">' + M.util.get_string('choosedots', 'moodle') + '</option>';
  39      var fieldInfo;
  40      for (var i = 0; i < this.standardFields.length; i++) {
  41          fieldInfo = this.standardFields[i];
  42          // String has already been escaped using format_string.
  43          html += '<option value="sf_' + fieldInfo.field + '">' + fieldInfo.display + '</option>';
  44      }
  45      for (i = 0; i < this.customFields.length; i++) {
  46          fieldInfo = this.customFields[i];
  47          // String has already been escaped using format_string.
  48          html += '<option value="cf_' + fieldInfo.field + '">' + fieldInfo.display + '</option>';
  49      }
  50      html += '</select></label> <label><span class="accesshide">' + M.util.get_string('label_operator', 'availability_profile') +
  51              ' </span><select name="op" title="' + M.util.get_string('label_operator', 'availability_profile') + '">';
  52      var operators = ['isequalto', 'contains', 'doesnotcontain', 'startswith', 'endswith',
  53              'isempty', 'isnotempty'];
  54      for (i = 0; i < operators.length; i++) {
  55          html += '<option value="' + operators[i] + '">' +
  56                  M.util.get_string('op_' + operators[i], 'availability_profile') + '</option>';
  57      }
  58      html += '</select></label> <label><span class="accesshide">' + M.util.get_string('label_value', 'availability_profile') +
  59              '</span><input name="value" type="text" style="width: 10em" title="' +
  60              M.util.get_string('label_value', 'availability_profile') + '"/></label></span>';
  61      var node = Y.Node.create('<span>' + html + '</span>');
  62  
  63      // Set initial values if specified.
  64      if (json.sf !== undefined &&
  65              node.one('select[name=field] > option[value=sf_' + json.sf + ']')) {
  66          node.one('select[name=field]').set('value', 'sf_' + json.sf);
  67      } else if (json.cf !== undefined &&
  68              node.one('select[name=field] > option[value=cf_' + json.cf + ']')) {
  69          node.one('select[name=field]').set('value', 'cf_' + json.cf);
  70      }
  71      if (json.op !== undefined &&
  72              node.one('select[name=op] > option[value=' + json.op + ']')) {
  73          node.one('select[name=op]').set('value', json.op);
  74          if (json.op === 'isempty' || json.op === 'isnotempty') {
  75              node.one('input[name=value]').set('disabled', true);
  76          }
  77      }
  78      if (json.v !== undefined) {
  79          node.one('input').set('value', json.v);
  80      }
  81  
  82      // Add event handlers (first time only).
  83      if (!M.availability_profile.form.addedEvents) {
  84          M.availability_profile.form.addedEvents = true;
  85          var updateForm = function(input) {
  86              var ancestorNode = input.ancestor('span.availability_profile');
  87              var op = ancestorNode.one('select[name=op]');
  88              var novalue = (op.get('value') === 'isempty' || op.get('value') === 'isnotempty');
  89              ancestorNode.one('input[name=value]').set('disabled', novalue);
  90              M.core_availability.form.update();
  91          };
  92          var root = Y.one('#fitem_id_availabilityconditionsjson');
  93          root.delegate('change', function() {
  94               updateForm(this);
  95          }, '.availability_profile select');
  96          root.delegate('change', function() {
  97               updateForm(this);
  98          }, '.availability_profile input[name=value]');
  99      }
 100  
 101      return node;
 102  };
 103  
 104  M.availability_profile.form.fillValue = function(value, node) {
 105      // Set field.
 106      var field = node.one('select[name=field]').get('value');
 107      if (field.substr(0, 3) === 'sf_') {
 108          value.sf = field.substr(3);
 109      } else if (field.substr(0, 3) === 'cf_') {
 110          value.cf = field.substr(3);
 111      }
 112  
 113      // Operator and value
 114      value.op = node.one('select[name=op]').get('value');
 115      var valueNode = node.one('input[name=value]');
 116      if (!valueNode.get('disabled')) {
 117          value.v = valueNode.get('value');
 118      }
 119  };
 120  
 121  M.availability_profile.form.fillErrors = function(errors, node) {
 122      var value = {};
 123      this.fillValue(value, node);
 124  
 125      // Check profile item id.
 126      if (value.sf === undefined && value.cf === undefined) {
 127          errors.push('availability_profile:error_selectfield');
 128      }
 129      if (value.v !== undefined && /^\s*$/.test(value.v)) {
 130          errors.push('availability_profile:error_setvalue');
 131      }
 132  };


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