[ Index ]

PHP Cross Reference of Unnamed Project

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


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