[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yui/build/moodle-core-checknet/ -> moodle-core-checknet.js (source)

   1  YUI.add('moodle-core-checknet', function (Y, NAME) {
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * A utility to check whether the connection to the Moodle server is still
  20   * active.
  21   *
  22   * @module     moodle-core-checknet
  23   * @package    core
  24   * @copyright  2014 Andrew Nicols <andrew@nicols.co.uk>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   * @main       moodle-core-checknet
  27   */
  28  
  29  /**
  30   * @namespace M.core
  31   * @class checknet
  32   */
  33  
  34  function CheckNet() {
  35      CheckNet.superclass.constructor.apply(this, arguments);
  36  }
  37  
  38  Y.extend(CheckNet, Y.Base, {
  39      /**
  40       * A link to the warning dialogue.
  41       *
  42       * @property _alertDialogue
  43       * @type M.core.dialogue
  44       * @private
  45       * @default null
  46       */
  47      _alertDialogue: null,
  48  
  49      /**
  50       * Setup the checking mechanism.
  51       *
  52       * @method initializer
  53       */
  54      initializer: function() {
  55          // Perform our first check.
  56          this._scheduleCheck();
  57      },
  58  
  59      /**
  60       * Schedule a check of the checknet file.
  61       *
  62       * @method _scheduleCheck
  63       * @chainable
  64       * @private
  65       */
  66      _scheduleCheck: function() {
  67          // Schedule the next check after five seconds.
  68          Y.later(this.get('frequency'), this, this._performCheck);
  69  
  70          return this;
  71      },
  72  
  73      /**
  74       * Perform an immediate check of the checknet file.
  75       *
  76       * @method _performCheck
  77       * @private
  78       */
  79      _performCheck: function() {
  80          Y.io(this.get('uri'), {
  81              data: {
  82                  // Add the session key.
  83                  sesskey: M.cfg.sesskey,
  84                  // Add a query string to prevent older versions of IE from using the cache.
  85                  time: new Date().getTime()
  86              },
  87              timeout: this.get('timeout'),
  88              headers: {
  89                  'Cache-Control': 'no-cache',
  90                  'Expires': '-1'
  91              },
  92              context: this,
  93              on: {
  94                  complete: function(tid, response) {
  95                      // Check for failure conditions.
  96                      // We check for a valid status here because if the user is moving away from the page at the time we
  97                      // run this callback we do not want to display the error.
  98                      if (response && typeof response.status !== "undefined") {
  99                          var code = parseInt(response.status, 10);
 100  
 101                          if (code === 200) {
 102                              // This is a valid attempt - clear any existing warning dialogue and destroy it.
 103                              if (this._alertDialogue) {
 104                                  this._alertDialogue.destroy();
 105                                  this._alertDialogue = null;
 106                              }
 107                          } else if (code >= 300 && code <= 399) {
 108                              // This is a cached status - warn developers, but otherwise ignore.
 109                          } else {
 110                              if (this._alertDialogue === null || this._alertDialogue.get('destroyed')) {
 111                                  // Only create a new dialogue if it isn't already displayed.
 112                                  this._alertDialogue = new M.core.alert({
 113                                      message: M.util.get_string.apply(this, this.get('message'))
 114                                  });
 115                              } else {
 116                                  this._alertDialogue.show();
 117                              }
 118                          }
 119                      }
 120  
 121                      // Start the next check.
 122                      this._scheduleCheck();
 123                  }
 124              }
 125          });
 126      }
 127  }, {
 128      NAME: 'checkNet',
 129      ATTRS: {
 130          /**
 131           * The file to check access against.
 132           *
 133           * @attribute uri
 134           * @type String
 135           * @default M.cfg.wwwroot + '/lib/yui/build/moodle-core-checknet/assets/checknet.txt'
 136           */
 137          uri: {
 138              value: M.cfg.wwwroot + '/lib/yui/build/moodle-core-checknet/assets/checknet.txt'
 139          },
 140  
 141          /**
 142           * The timeout (in milliseconds) before the checker should give up and display a warning.
 143           *
 144           * @attribute timeout
 145           * @type Number
 146           * @value 4000
 147           */
 148          timeout: {
 149              value: 4000
 150          },
 151  
 152          /**
 153           * The frequency (in milliseconds) that checks should be run.
 154           * A new check is not begun until the previous check has completed.
 155           *
 156           * @attribute frequency
 157           * @writeOnce
 158           * @type Number
 159           * @value 10000
 160           */
 161          frequency: {
 162              value: 10000
 163          },
 164  
 165          /**
 166           * The message which should be displayed upon a test failure.
 167           *
 168           * The array values are passed directly to M.util.get_string() and arguments should match accordingly.
 169           *
 170           * @attribute message
 171           * @type Array
 172           * @value [
 173           *  'networkdropped',
 174           *  'moodle'
 175           * ]
 176           */
 177          message: {
 178              value: [
 179                  'networkdropped',
 180                  'moodle'
 181              ]
 182          }
 183      }
 184  });
 185  
 186  M.core = M.core || {};
 187  M.core.checknet = M.core.checknet || {};
 188  M.core.checknet.init = function(config) {
 189      return new CheckNet(config);
 190  };
 191  
 192  
 193  }, '@VERSION@', {"requires": ["base-base", "moodle-core-notification-alert", "io-base"]});


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