[ Index ]

PHP Cross Reference of Unnamed Project

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          // Hide loading icon, give sometime to people to actually see it. We should do it, event in case of an error.
 112          Y.later(600, this, 'hideLoadingIcon');
 113          var responseobject;
 114          // Attempt to parse the response into an object.
 115          try {
 116              responseobject = Y.JSON.parse(response.responseText);
 117              if (responseobject.error) {
 118                  Y.use('moodle-core-notification-ajaxexception', function() {
 119                      return new M.core.ajaxException(responseobject);
 120                  });
 121                  return this;
 122              }
 123          } catch (error) {
 124              Y.use('moodle-core-notification-exception', function() {
 125                  return new M.core.exception(error);
 126              });
 127              return this;
 128          }
 129          this.set('since', responseobject.until);
 130          var logs = responseobject.logs;
 131          var tbody = Y.one(SELECTORS.TBODY);
 132          var firstTr = null;
 133          if (tbody && logs) {
 134              firstTr = tbody.get('firstChild');
 135              if (firstTr) {
 136                  tbody.insertBefore(logs, firstTr);
 137              }
 138              // @Todo, when no data is present our library should generate an empty table. so data can be added
 139              // dynamically (MDL-44525).
 140  
 141              // Let us chop off some data from end of table to prevent really long pages.
 142              var oldChildren = tbody.get('children').slice(this.get('perpage'));
 143              oldChildren.remove();
 144              Y.later(5000, this, 'removeHighlight', responseobject.until); // Remove highlighting from new rows.
 145          }
 146      },
 147  
 148      /**
 149       * Remove background highlight from the newly added rows.
 150       *
 151       * @method removeHighlight
 152       */
 153      removeHighlight: function(timeStamp) {
 154          Y.all('.time' + timeStamp).removeClass(CSS.NEWROW);
 155      },
 156  
 157      /**
 158       * Hide the spinning icon.
 159       *
 160       * @method hideLoadingIcon
 161       */
 162      hideLoadingIcon: function() {
 163          this.spinner.hide();
 164      },
 165  
 166      /**
 167       * Toggle live update.
 168       *
 169       * @method toggleUpdate
 170       */
 171      toggleUpdate: function() {
 172          if (this.callBack) {
 173              this.callBack.cancel();
 174              this.callBack = '';
 175              this.pauseButton.setContent(M.util.get_string('resume', 'report_loglive'));
 176          } else {
 177              this.callBack = Y.later(this.get('interval') * 1000, this, this.fetchRecentLogs, null, true);
 178              this.pauseButton.setContent(M.util.get_string('pause', 'report_loglive'));
 179          }
 180      }
 181  }, {
 182      NAME: 'fetchLogs',
 183      ATTRS: {
 184          /**
 185           * time stamp from where the new logs needs to be fetched.
 186           *
 187           * @attribute since
 188           * @default null
 189           * @type String
 190           */
 191          since: null,
 192  
 193          /**
 194           * courseid for which the logs are shown.
 195           *
 196           * @attribute courseid
 197           * @default 0
 198           * @type int
 199           */
 200          courseid: 0,
 201  
 202          /**
 203           * Page number shown to user.
 204           *
 205           * @attribute page
 206           * @default 0
 207           * @type int
 208           */
 209          page: 0,
 210  
 211          /**
 212           * Items to show per page.
 213           *
 214           * @attribute perpage
 215           * @default 100
 216           * @type int
 217           */
 218          perpage: 100,
 219  
 220          /**
 221           * Refresh interval.
 222           *
 223           * @attribute interval
 224           * @default 60
 225           * @type int
 226           */
 227          interval: 60,
 228  
 229          /**
 230           * Which logstore is being used.
 231           *
 232           * @attribute logreader
 233           * @default logstore_standard
 234           * @type String
 235           */
 236          logreader: 'logstore_standard'
 237      }
 238  });
 239  
 240  Y.namespace('M.report_loglive.FetchLogs').init = function(config) {
 241      return new FetchLogs(config);
 242  };


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