[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

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

   1  /**
   2   * JavaScript for form editing date conditions.
   3   *
   4   * @module moodle-availability_date-form
   5   */
   6  M.availability_date = M.availability_date || {};
   7  
   8  /**
   9   * @class M.availability_date.form
  10   * @extends M.core_availability.plugin
  11   */
  12  M.availability_date.form = Y.Object(M.core_availability.plugin);
  13  
  14  /**
  15   * Initialises this plugin.
  16   *
  17   * Because the date fields are complex depending on Moodle calendar settings,
  18   * we create the HTML for these fields in PHP and pass it to this method.
  19   *
  20   * @method initInner
  21   * @param {String} html HTML to use for date fields
  22   * @param {Number} defaultTime Time value that corresponds to initial fields
  23   */
  24  M.availability_date.form.initInner = function(html, defaultTime) {
  25      this.html = html;
  26      this.defaultTime = defaultTime;
  27  };
  28  
  29  M.availability_date.form.getNode = function(json) {
  30      var strings = M.str.availability_date;
  31      var html = strings.direction_before + ' <span class="availability-group">' +
  32              '<label><span class="accesshide">' + strings.direction_label + ' </span>' +
  33              '<select name="direction">' +
  34              '<option value="&gt;=">' + strings.direction_from + '</option>' +
  35              '<option value="&lt;">' + strings.direction_until + '</option>' +
  36              '</select></label></span> ' + this.html;
  37      var node = Y.Node.create('<span>' + html + '</span>');
  38  
  39      // Set initial value if non-default.
  40      if (json.t !== undefined) {
  41          node.setData('time', json.t);
  42          // Disable everything.
  43          node.all('select:not([name=direction])').each(function(select) {
  44              select.set('disabled', true);
  45          });
  46  
  47          var url = M.cfg.wwwroot + '/availability/condition/date/ajax.php?action=fromtime' +
  48              '&time=' + json.t;
  49          Y.io(url, { on : {
  50              success : function(id, response) {
  51                  var fields = Y.JSON.parse(response.responseText);
  52                  for (var field in fields) {
  53                      var select = node.one('select[name=x\\[' + field + '\\]]');
  54                      select.set('value', '' + fields[field]);
  55                      select.set('disabled', false);
  56                  }
  57              },
  58              failure : function() {
  59                  window.alert(M.str.availability_date.ajaxerror);
  60              }
  61          }});
  62      } else {
  63          // Set default time that corresponds to the HTML selectors.
  64          node.setData('time', this.defaultTime);
  65      }
  66      if (json.d !== undefined) {
  67          node.one('select[name=direction]').set('value', json.d);
  68      }
  69  
  70      // Add event handlers (first time only).
  71      if (!M.availability_date.form.addedEvents) {
  72          M.availability_date.form.addedEvents = true;
  73  
  74          var root = Y.one('#fitem_id_availabilityconditionsjson');
  75          root.delegate('change', function() {
  76              // For the direction, just update the form fields.
  77              M.core_availability.form.update();
  78          }, '.availability_date select[name=direction]');
  79  
  80          root.delegate('change', function() {
  81              // Update time using AJAX call from root node.
  82              M.availability_date.form.updateTime(this.ancestor('span.availability_date'));
  83          }, '.availability_date select:not([name=direction])');
  84      }
  85  
  86      if (node.one('a[href=#]')) {
  87          // Add the date selector magic.
  88          M.form.dateselector.init_single_date_selector(node);
  89  
  90          // This special handler detects when the date selector changes the year.
  91          var yearSelect = node.one('select[name=x\\[year\\]]');
  92          var oldSet = yearSelect.set;
  93          yearSelect.set = function(name, value) {
  94              oldSet.call(yearSelect, name, value);
  95              if (name === 'selectedIndex') {
  96                  // Do this after timeout or the other fields haven't been set yet.
  97                  setTimeout(function() {
  98                      M.availability_date.form.updateTime(node);
  99                  }, 0);
 100              }
 101          };
 102      }
 103  
 104      return node;
 105  };
 106  
 107  /**
 108   * Updates time from AJAX. Whenever the field values change, we recompute the
 109   * actual time via an AJAX request to Moodle.
 110   *
 111   * This will set the 'time' data on the node and then update the form, once it
 112   * gets an AJAX response.
 113   *
 114   * @method updateTime
 115   * @param {Y.Node} component Node for plugin controls
 116   */
 117  M.availability_date.form.updateTime = function(node) {
 118      // After a change to the date/time we need to recompute the
 119      // actual time using AJAX because it depends on the user's
 120      // time zone and calendar options.
 121      var url = M.cfg.wwwroot + '/availability/condition/date/ajax.php?action=totime' +
 122              '&year=' + node.one('select[name=x\\[year\\]]').get('value') +
 123              '&month=' + node.one('select[name=x\\[month\\]]').get('value') +
 124              '&day=' + node.one('select[name=x\\[day\\]]').get('value') +
 125              '&hour=' + node.one('select[name=x\\[hour\\]]').get('value') +
 126              '&minute=' + node.one('select[name=x\\[minute\\]]').get('value');
 127      Y.io(url, { on : {
 128          success : function(id, response) {
 129              node.setData('time', response.responseText);
 130              M.core_availability.form.update();
 131          },
 132          failure : function() {
 133              window.alert(M.str.availability_date.ajaxerror);
 134          }
 135      }});
 136  };
 137  
 138  M.availability_date.form.fillValue = function(value, node) {
 139      value.d = node.one('select[name=direction]').get('value');
 140      value.t = parseInt(node.getData('time'), 10);
 141  };


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