[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/yui/src/checknet/js/ -> checknet.js (source)

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


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