[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/availability/condition/date/yui/build/moodle-availability_date-form/ -> moodle-availability_date-form-debug.js (source)

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


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