<% /*
     File:          PDA/resultset_table.inc

     $Revision$
     $Id$

    PAS OP: Soms gebruikt in combinatie met shared/data_recordset.inc.
            Zie het commentaar daarin met betrekking tot het case-sensitive
            zijn van bijvoorbeeld oRs.EOF
   */
%>

<%

function ResultsetTable(params)
{
  this.emptySetString = L("lcl_empty_rstable");
  this.sql = null;
  this.dataset = null; // of sql of dataset meegeven. Data is dan een array van objecten met properies
  this.ID = null;
  this.rowNum = 0;
  this.showAll = null;

  // Mobile params
  this.noSearch = null;
  this.inset = false;
  this.Counter = {};

  var param;
  // Neem alle meegegeven parameters mee.
  for (param in params)
      this[param] = params[param];

}

ResultsetTable.prototype.processResultset = __rsProcessResultset;
ResultsetTable.prototype.makeTableRow = __rsMakeTableRow;
ResultsetTable.prototype.countHistogram = __rsCountHistogram;

/*global*/ isData = false;

function __rsProcessResultset(processParams)
{
    this.processParams = processParams;
    var copy;

    if (this.sql)
    {
        var oRs = Oracle.Execute(this.sql);
    }
    else
    {
        /*global*/isData = true;
        oRs = new data_recordset( this.dataset );
    }

    if (oRs == null || oRs.EOF)
    {
        Response.Write("<p>" + this.emptySetString + "</p>");
        return -1;
    }

    var lastGroup, lastDivider = null;
    var html = "";
    var filterCutOff = S("pda_list_show_filter_from");


    var lines = [];
    // Heeft groupSelect een waarde, dan wordt voor deze group de lijst getoond.
    if (this.groupSelect == "")
      html += "<ul data-role='listview' data-theme='b' data-inset='" + (this.inset? "true" : "false") + "' data-content-theme='d'>";

    var cnt = 0;
    for (cnt = 0; (cnt < (S("qp_maxrows_mobile")) || this.showAll ) &&
                      (cnt < S("qp_maxrows2")) &&
                      !oRs.EOF; cnt++)
    {
      if (this.fnDataFilter && !this.fnDataFilter(oRs))
      {
        oRs.MoveNext();
        continue;
      }

      if (this.dividerColumn)
      {
        var safenextDivider = __fnContent(this.dividerColumn)(oRs);
        if (safenextDivider != lastDivider)
            lines.push("<li data-role='list-divider'>" + safenextDivider + "</li>");
        var lastDivider = safenextDivider;
      }

      lines.push(this.makeTableRow(oRs, cnt, false));
      if (this.buildHistogram)
        this.countHistogram(oRs);

      var numberInGroup = -1;
      if (this.groupColumn)
      {
        var lastGroup = __fnContent(this.groupColumn)(oRs);
        var lastLink = __fnContent(this.linkColumn)(oRs);
        if (this.numberInGroup)
          numberInGroup = __fnContent(this.numberInGroup)(oRs);
      }

      oRs.MoveNext();

      if (this.groupColumn)
      {
        var overLimit = (cnt+1 >= (S("qp_maxrows_mobile")) && !this.showAll ) ||
                        (cnt+1 >= S("qp_maxrows2"))

        if (!oRs.EOF)
            var nextGroup = __fnContent(this.groupColumn)(oRs);

        // In jquery-mobile-1.4.2 zijn geneste lijsten niet meer toegestaan.
        // De *_list.asp wordt nu geplitst: is groupSelect leeg dan worden de groepen getoond
        // Heeft groupSelect een waarde, dan wordt voor deze group de lijst getoond.
        if (this.groupSelect == "")
        { // Toon de groepen
          if (oRs.EOF || overLimit || nextGroup != lastGroup)
          {
              html += "<li>";
              html += "<a href='"+ lastLink +"'>";
              html += "<h2>" + lastGroup + "</h2><span class='ui-li-count'>" + (numberInGroup > 0? numberInGroup : lines.length) + "</span>";
              html += "</a></li>";
              lines = [];
          }
        }
        else
        { // Toon de lijst van een geselecteerde groep
          if (oRs.EOF || overLimit)
          {
            html += "<ul data-role='listview' data-theme='c' data-inset='" + (this.inset ? "true" : "false") + "' data-divider-theme='b'";
            if (!this.noSearch) {
               html += " data-filter='true' data-filter-theme='c'"; //voor het toevoegen van een filter aan de lijst.
            }
            html += ">";

            html += lines.join("\n")
                 + "</ul>";
            lines = [];
          }
        }
      }

    }
    if (this.groupSelect == "")
        html += "</ul>";
    if (!this.groupColumn)
    {
        var html = "<ul data-role='listview' data-theme='c' data-inset='" + (this.inset? "true" : "false") + "' data-divider-theme='b' ";
        if (!this.noSearch && cnt > filterCutOff) {
          html += "data-filter='true' data-filter-theme='c' "; //voor het toevoegen van een filter aan de lijst.
        }
        html += ">";
        html += lines.join("\n");
        html += ("</ul>");
    }

    if (!oRs.EOF) // en dus overLimit
    {
        var maxrows = (this.showAll ? S("qp_maxrows2") : S("qp_maxrows_mobile"));
        html += "<ul data-role='listview' data-theme='c'><li>" + L("lcl_max_lines_reached").format(maxrows) + "</li></ul>";
    }

    Response.Write(html);

    if (this.sql)
        oRs.close();

    if (this.buildHistogram)
        return this.Counter;
    else
        return this.rowNum; // simpel return aantal rows
}


function __rsMakeTableRow(oRs, cnt)
{
    // HIER START EEN NIEUWE REGEL
    var line = "";

    if (this.prefixColumn)
    {
        line += __fnContent(this.prefixColumn)(oRs, this.prefixColumn);
    }

    if (this.iconColumn)
    {
        var icon = __fnContent(this.iconColumn)(oRs, this.processParams)
      if (icon) {
        line += '<img class="rticon" src="' + safe.htmlattr(icon) + '">'
      }
    }

    var headTxt = "";
    if (this.headerColumn)
      headTxt = __fnContent(this.headerColumn)(oRs, this.processParams);

    if (this.detailColumn)
      headTxt += "<span class='cs1'>" + (__fnContent(this.detailColumn)(oRs, this.processParams) || "") + "</span>";
    line += "<h3 class='c1'>" + headTxt + "</h3>";

    if (this.asideColumn && __fnContent(this.asideColumn)(oRs))
      line += "<p class='ui-li-count'>" + __fnContent(this.asideColumn)(oRs, this.processParams) + "</p>";

    if (this.subheaderColumn)
      line += "<div class='subheader'>" + __fnContent(this.subheaderColumn)(oRs, this.processParams) +"</div>";

    if (this.linkColumn)
    {
        var lnk = __fnContent(this.linkColumn)(oRs, this.processParams);
        if (lnk)
        {
          line = "<a href='" + safe.htmlattr(lnk) + "' data-ajax='false'>"
               + line
               + "</a>";
        }
    }

    this.rowNum ++;

    var extraFilter = "";
    if (this.extraFilterColumn)
      extraFilter = " data-filtertext='" + safe.htmlattr(__fnContent(this.extraFilterColumn)(oRs) + " " + headTxt)+"'";

    return "<li" + extraFilter + ">" + line + "</li>";
}

function __rsCountHistogram(oRs)
{
    var i, kolomnaam, kolomval, cnt;
    for (i= 0; i < oRs.Fields.Count; i++)
    {
        kolomnaam = oRs.Fields(i).Name;
        if (kolomnaam.toUpperCase() == this.keyColumn.toUpperCase())
            continue;
        kolomval = oRs.Fields(i).Value;
        if (!this.Counter[kolomnaam])
            this.Counter[kolomnaam] = {};
        if (!this.Counter[kolomnaam][kolomval])
            this.Counter[kolomnaam][kolomval] = 1;
        else
            this.Counter[kolomnaam][kolomval] += 1
    }
}


// Levert een html-safe waarde op
ResultsetTable.formatValue = function (val, params) //datatype, decimals)
{
    if (val == null)
    {
        return "&nbsp;";
    }
    if (typeof val == "object" && "name" in val) // Vanuit API2
        val = val["name"];

    // Eerst de geforceerde datatypes
    switch (params.datatype)
    {
        case "date"    : return toDateString(val);
        case "datetime": return toDateTimeString(val, false);
        case "time"    : { val = toTimeString(val);
                           if (params.datatype == "time" && params.nomidnight && val == "00:00")
                             val = "";
                           return val;
                         }
        case "currency": return safe.curr(val);
        case "float"   : return safe.displayfloat(val, params.decimals);
        case "number"  : return val;
        case "html"    : return val;
    }

    // Autotypering "datum"
    if (typeof val == "date" || val instanceof Date)
        return toDateTimeString(val);   // PF: heb ik ff getweakt, moet natuurlijk niet zo (date vs datetime)

    if (!val)
        return "";

    // default datatype is "string"
    val = String(val);

    val = shared.stripbbcodes(val);

    return safe.html(val); // altijd simpelweg hele tekst
}
function __fnContent(fnContent)
{
    if (fnContent instanceof Function)
        return fnContent;

    return function (oRs)
    {
        return ResultsetTable.formatValue(oRs.Fields(fnContent).Value, {datatype: this.datatype, decimals: this.decimals, nomidnight: this.nomidnight})
    } ;
}
%>
