// Lightstreamer Monitor Console Demo
// Table Management for the Log Section

//////////////// Utilities to Handle Splitting of Long Messages

  // Utility method, to split text at a configurable row length
  // Note: setting setPushedHtmlEnabled(true) on the visual table is needed
  // in order to render the splitting; however, any HTML markup in the original
  // text is quoted
  function getStringBreaker(limit) {
    var patternS = "(";
    for (var i = 0; i < limit; i++) {
      patternS += "\\S";
    }
    var pattern = new RegExp(patternS + ")", "g");
    // see http://www.quirksmode.org/oddsandends/wbr.html
    return function(source) {
      var target = source.replace(pattern, "$1&#8203;");
      target = target.replace(/</g, "&lt;");
      return target;
    };
  }
  
  //StringBreaker instance for CLIENT.NAME fields (Activities) and THREAD fields (Warnings and Errors)
  var sbSmall = getStringBreaker(5);
  //StringBreaker instance for MESSAGE fields (Activities, Warnings and Errors)
  var sbMessage = getStringBreaker(20);
 
//////////////// Scriptaculous Configuration for History-Limit Slider

  // number of admitted rows on scroll tables
  var limitRows = 50;

  var rowValues = [1];
  for (var x = 10; x <= 1000; x+=10) {
    rowValues.push(x);
  }
 
  new Control.Slider('handleSelectLimit','selectLimit',{sliderValue:limitRows,values:rowValues,step:10,increment:10,range:$R(1,1000),
    onSlide:function(v){
      var txt = v.toString();
      if (txt.length == 1) {
        document.getElementById("nowLimit").innerHTML = "0" + txt ;
      } else {
        document.getElementById("nowLimit").innerHTML = v;
      }
    },
    onChange:function(v){
      this.onSlide(v);
      if (limitRows != v) {
        errorsTable.setMaxDynaRows(v);
        warningsTable.setMaxDynaRows(v);
        activitiesTable.setMaxDynaRows(v);
        limitRows = v;
      }
    }
  });

//////////////// Handling of the Filtering Notification Led

  var lostPhase = 0;

  function checkUpdateCount(item, info) {
    // the "COUNTER" field is a progressive update counter provided by the Data
    // Adapter for each log-related item, which starts at subscription time.
    // If any index is missed, then some updates were lost (this is possible
    // as the log-related items are subscribed to with filtering enabled)
    
    var oldCounter = info.getOldValue("COUNTER");
    var newCounter = info.getNewValue("COUNTER");
    if (oldCounter != null && newCounter != null) {
      if (Number(newCounter) > (Number(oldCounter) + 1)) {
        lostPhase++;
        var currLed = document.getElementById("luSignal").src;
        if (currLed != null && currLed.indexOf("led_on.gif") == -1) {
          document.getElementById("luSignal").src = "images/led_on.gif";
        }
        
        var callPhase = lostPhase;
        setTimeout(function() {
          if (callPhase == lostPhase) {
            document.getElementById("luSignal").src = "images/led_off.gif";
          }
        },1000);
          // the led will be switched off in one second, provided that no more
          // filtering occurs in the meantime
      }
    }
  }

//////////////// Formatting

  function getFormatter(style,field1,breaker1,field2,breaker2) {
    // define visual effects and formatting
    return function(domNode, VisualUpdateInfo) {
      if (VisualUpdateInfo != null) {
        VisualUpdateInfo.setHotTime(0);
        VisualUpdateInfo.setRowStyle(style, style);

        // format long values
        var value = VisualUpdateInfo.getServerValue(field1);
        if (value != null) {
          value = breaker1(value);
          VisualUpdateInfo.setFormattedValue(field1, value);
        }

        value = VisualUpdateInfo.getServerValue(field2);
        if (value != null) {
          value = breaker2(value);
          VisualUpdateInfo.setFormattedValue(field2, value);
        }
      }
    };
  }  

//////////////// Subscription and Update Management for the Error Message Table
  // create a DynaScrollTable (can't use null as schema as we subscribe to more fields than we show
  var errorsTable = new DynaScrollTable(["monitor_log_error"], ["TIME","MESSAGE","THREAD","COUNTER"], "DISTINCT");

  errorsTable.setDataAdapter("MONITOR");
  errorsTable.setUpwardScroll(true);
  errorsTable.setAutoScroll("ELEMENT", "errTBody");
  errorsTable.setMaxDynaRows(limitRows);
  errorsTable.setPushedHtmlEnabled(true);
  errorsTable.setRequestedBufferSize(10);
  errorsTable.onItemUpdate = checkUpdateCount;
  errorsTable.onChangingValues = getFormatter("lscolderr","MESSAGE",sbMessage,"THREAD",sbSmall);
  
  // bind the table to the corresponding HTML template
  pushPage.addTable(errorsTable, "errors");


//////////////// Subscription and Update Management for the Warning Message Table
  // create a DynaScrollTable
  var warningsTable = new DynaScrollTable(["monitor_log_warning"], ["TIME","MESSAGE","THREAD","COUNTER"], "DISTINCT");

  warningsTable.setDataAdapter("MONITOR");
  warningsTable.setUpwardScroll(true);
  warningsTable.setAutoScroll("ELEMENT","warTBody");
  warningsTable.setMaxDynaRows(limitRows);
  warningsTable.setPushedHtmlEnabled(true);
  warningsTable.setRequestedBufferSize(10);

  warningsTable.onItemUpdate = checkUpdateCount;
  warningsTable.onChangingValues = getFormatter("lscoldwar","MESSAGE",sbMessage,"THREAD",sbSmall);

  // bind the table to the corresponding HTML template
  pushPage.addTable(warningsTable, "warnings");


//////////////// Subscription and Update Management for the Activity Message Table
  // create a DynaScrollTable
  var activitiesTable = new DynaScrollTable(["monitor_log_info"], ["TIME","CLIENT.IP","CLIENT.NAME","MESSAGE","COUNTER"], "DISTINCT");

  activitiesTable.setDataAdapter("MONITOR");
  activitiesTable.setUpwardScroll(true);
  activitiesTable.setAutoScroll("ELEMENT","actTBody");
  activitiesTable.setMaxDynaRows(limitRows);
  activitiesTable.setPushedHtmlEnabled(true);
  activitiesTable.setRequestedBufferSize(10);
  
  activitiesTable.onItemUpdate = checkUpdateCount;
  activitiesTable.onChangingValues = getFormatter("lscoldact","MESSAGE",sbMessage,"CLIENT.NAME",sbSmall);
  
  // bind the table to the corresponding HTML template
  pushPage.addTable(activitiesTable, "activities");

