[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/report/loglive/yui/src/fetchlogs/js/ -> fetchlogs.js (source)

   1  /**
   2   * A module to manage ajax requests.
   3   *
   4   * @module moodle-report_loglive-fetchlogs
   5   */
   6  
   7  /**
   8   * A module to manage ajax requests.
   9   *
  10   * @class moodle-report_loglive.fetchlogs
  11   * @extends Base
  12   * @constructor
  13   */
  14  function FetchLogs() {
  15      FetchLogs.superclass.constructor.apply(this, arguments);
  16  }
  17  
  18  
  19  var CSS = {
  20          NEWROW: 'newrow',
  21          SPINNER: 'spinner',
  22          ICONSMALL: 'iconsmall'
  23      },
  24      SELECTORS = {
  25          NEWROW: '.' + CSS.NEWROW,
  26          TBODY: '.flexible tbody',
  27          PAUSEBUTTON: '#livelogs-pause-button',
  28          SPINNER: '.' + CSS.SPINNER
  29      };
  30  
  31  Y.extend(FetchLogs, Y.Base, {
  32      /**
  33       * Reference to the callBack object generated by Y.later
  34       *
  35       * @property callBack
  36       * @type Object
  37       * @default {}
  38       * @protected
  39       */
  40      callBack: {},
  41  
  42      /**
  43       * Reference to the spinner node.
  44       *
  45       * @property callBack
  46       * @type Object
  47       * @default {}
  48       * @protected
  49       */
  50      spinner: {},
  51  
  52      /**
  53       * Reference to the toggleButton node.
  54       *
  55       * @property pauseButton
  56       * @type Object
  57       * @default {}
  58       * @protected
  59       */
  60      pauseButton: {},
  61  
  62      /**
  63       * Initializer.
  64       * Basic setup and delegations.
  65       *
  66       * @method initializer
  67       */
  68      initializer: function() {
  69          // We don't want the pages to be updated when viewing a specific page in the chain.
  70          if (this.get('page') === 0) {
  71              this.callBack = Y.later(this.get('interval') * 1000, this, this.fetchRecentLogs, null, true);
  72          }
  73  
  74          this.spinner = Y.one(SELECTORS.SPINNER);
  75          this.pauseButton = Y.one(SELECTORS.PAUSEBUTTON);
  76          this.spinner.hide();
  77          Y.delegate('click', this.toggleUpdate, 'button', SELECTORS.PAUSEBUTTON, this);
  78      },
  79  
  80      /**
  81       * Method to fetch recent logs.
  82       *
  83       * @method fetchRecentLogs
  84       */
  85      fetchRecentLogs: function() {
  86          this.spinner.show(); // Show a loading icon.
  87          var data = {
  88              logreader: this.get('logreader'),
  89              since: this.get('since'),
  90              page: this.get('page'),
  91              id: this.get('courseid')
  92          };
  93          var cfg = {
  94              method: 'get',
  95              context: this,
  96              on: {
  97                  complete: this.updateLogTable
  98              },
  99              data: data
 100          };
 101          var url = M.cfg.wwwroot + '/report/loglive/loglive_ajax.php';
 102          Y.io(url, cfg);
 103      },
 104  
 105      /**
 106       * Method to update the log table, populate the table with new entries and remove old entries if needed.
 107       *
 108       * @method updateLogTable
 109       */
 110      updateLogTable: function(tid, response) {
 111          Y.later(600, this, 'hideLoadingIcon'); // Hide loading icon, give sometime to people to actually see it. We should do it, event in case of an error.
 112          var responseobject;
 113          // Attempt to parse the response into an object.
 114          try {
 115              responseobject = Y.JSON.parse(response.responseText);
 116              if (responseobject.error) {
 117                  Y.use('moodle-core-notification-ajaxexception', function() {
 118                      return new M.core.ajaxException(responseobject);
 119                  });
 120                  return this;
 121              }
 122          } catch (error) {
 123              Y.use('moodle-core-notification-exception', function() {
 124                  return new M.core.exception(error);
 125              });
 126              return this;
 127          }
 128          this.set('since' , responseobject.until);
 129          var logs = responseobject.logs;
 130          var tbody = Y.one(SELECTORS.TBODY);
 131          var firstTr = null;
 132          if (tbody && logs) {
 133              firstTr = tbody.get('firstChild');
 134              if (firstTr) {
 135                  tbody.insertBefore(logs, firstTr);
 136              }
 137              // @Todo, when no data is present our library should generate an empty table. so data can be added dynamically (MDL-44525).
 138  
 139              // Let us chop off some data from end of table to prevent really long pages.
 140              var oldChildren = tbody.get('children').slice(this.get('perpage'));
 141              oldChildren.remove();
 142              Y.later(5000, this, 'removeHighlight', responseobject.until); // Remove highlighting from new rows.
 143          }
 144      },
 145  
 146      /**
 147       * Remove background highlight from the newly added rows.
 148       *
 149       * @method removeHighlight
 150       */
 151      removeHighlight: function(timeStamp) {
 152          Y.all('.time' + timeStamp).removeClass(CSS.NEWROW);
 153      },
 154  
 155      /**
 156       * Hide the spinning icon.
 157       *
 158       * @method hideLoadingIcon
 159       */
 160      hideLoadingIcon: function() {
 161          this.spinner.hide();
 162      },
 163  
 164      /**
 165       * Toggle live update.
 166       *
 167       * @method toggleUpdate
 168       */
 169      toggleUpdate: function() {
 170          if (this.callBack) {
 171              this.callBack.cancel();
 172              this.callBack = '';
 173              this.pauseButton.setContent(M.util.get_string('resume', 'report_loglive'));
 174          } else {
 175              this.callBack = Y.later(this.get('interval') * 1000, this, this.fetchRecentLogs, null, true);
 176              this.pauseButton.setContent(M.util.get_string('pause', 'report_loglive'));
 177          }
 178      }
 179  }, {
 180      NAME: 'fetchLogs',
 181      ATTRS: {
 182          /**
 183           * time stamp from where the new logs needs to be fetched.
 184           *
 185           * @attribute since
 186           * @default null
 187           * @type String
 188           */
 189          since: null,
 190  
 191          /**
 192           * courseid for which the logs are shown.
 193           *
 194           * @attribute courseid
 195           * @default 0
 196           * @type int
 197           */
 198          courseid: 0,
 199  
 200          /**
 201           * Page number shown to user.
 202           *
 203           * @attribute page
 204           * @default 0
 205           * @type int
 206           */
 207          page: 0,
 208  
 209          /**
 210           * Items to show per page.
 211           *
 212           * @attribute perpage
 213           * @default 100
 214           * @type int
 215           */
 216          perpage: 100,
 217  
 218          /**
 219           * Refresh interval.
 220           *
 221           * @attribute interval
 222           * @default 60
 223           * @type int
 224           */
 225          interval: 60,
 226  
 227          /**
 228           * Which logstore is being used.
 229           *
 230           * @attribute logreader
 231           * @default logstore_standard
 232           * @type String
 233           */
 234          logreader: 'logstore_standard'
 235      }
 236  });
 237  
 238  Y.namespace('M.report_loglive.FetchLogs').init = function(config) {
 239      return new FetchLogs(config);
 240  };


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