Add analytics
[bus.git] / busui / owa / modules / base / js / owa.resultSetExplorer.js
blob:a/busui/owa/modules/base/js/owa.resultSetExplorer.js -> blob:b/busui/owa/modules/base/js/owa.resultSetExplorer.js
  //
  // Open Web Analytics - An Open Source Web Analytics Framework
  //
  // Copyright 2010 Peter Adams. All rights reserved.
  //
  // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  //
  // $Id$
  //
   
  /**
  * Result Set Explorer Library
  *
  * @author Peter Adams <peter@openwebanalytics.com>
  * @web <a href="http://www.openwebanalytcs.com">Open Web Analytics</a>
  * @copyright Copyright &copy; 2006-2010 Peter Adams <peter@openwebanalytics.com>
  * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0
  * @category owa
  * @package owa
  * @version $Revision$
  * @since owa 1.3.0
  */
   
  OWA.resultSetExplorer = function(dom_id, options) {
   
  this.dom_id = dom_id || '';
  this.gridInit = false;
  this.init = {
  grid: false,
  pieChart: false,
  areaChart: false
  };
   
  this.columnLinks = '';
  this._columnLinksCount = 0;
  this.resultSet = [];
  this.currentView = '';
  this.currentContainerWidth = '';
  this.currentWindowWidth = '';
  this.view = '';
  this.asyncQueue = [];
   
  this.domSelectors = {
  areaChart: '',
  grid: ''
  };
   
  this.options = {
  defaultView: 'grid',
  areaChart: {
  series:[],
  showDots: true,
  showLegend: true,
  lineWidth: 4
  },
  pieChart: {
  metric: '',
  dimension: '',
  metrics: [],
  numSlices: 5
  },
  sparkline: {
  metric: ''
  },
  grid: {
  showRowNumbers: true,
  excludeColumns: [],
  columnFormatters: {}
  },
  template: {
  template: '',
  params: '',
  mode: 'append',
  dom_id: ''
  },
  metricBoxes: {
  width: ''
  },
  chart: {showGrid: true},
  chartHeight: 125,
  chartWidth:700,
  autoResizeCharts: true,
  views:['grid', 'areaChart','pie', 'sparkline']
  };
   
  this.viewObjects = {};
  this.loadUrl = '';
  };
   
  OWA.resultSetExplorer.prototype = {
   
  //remove
  viewMethods: {
  grid: 'refreshGrid',
  areaChart: 'makeAreaChart',
  pie: 'makePieChart',
  sparkline: 'makeSparkline',
  template: 'renderTemplate'
  },
   
  setDataLoadUrl : function(url) {
   
  this.loadUrl = url;
  },
   
  getOption : function(name) {
   
  return this.options[name];
  },
   
  setView : function(name) {
   
  this.view = name;
  },
   
  getAggregates : function() {
   
  return this.resultSet.aggregates;
  },
   
  // called after data is rendered for a view
  setCurrentView : function(name) {
   
  jQuery(that.domSelectors[that.currentView]).toggle();
  this.currentView = name;
  },
   
  getRowValues : function(old) {
   
  var row = {};
   
  for (var item in old) {
   
  if (old.hasOwnProperty(item)) {
  row[item] = old[item].value;
  }
  }
   
  return row;
  },
   
  // makesa unqiue idfor each row
  makeRowGuid : function(row) {
   
  },
   
  loadFromArray : function(json, view) {
   
  if (view) {
  this.view = view;
  }
   
  this.loader(json);
   
  },
   
  load : function(url) {
   
  url = url || this.loadUrl;
  this.getResultSet(url);
  },
   
  displayGrid : function () {
  this.injectDomElements();
  this.setGridOptions();
  this.addAllRowsToGrid();
  this.makeGridPagination();
  this.gridInit = true;
  this.currentView = 'grid';
   
  },
   
  makeGrid : function(dom_id) {
   
  dom_id = dom_id || this.dom_id;
   
  //if (typeof this.viewObjects[dom_id] != 'undefined') {
  this.viewObjects[dom_id] = new OWA.dataGrid(dom_id);
  //}
   
  this.viewObjects[dom_id].makeGrid(this.resultSet);
  },
   
  refreshGrid : function() {
   
  var that = this;
   
  // custom formattter functions.
  jQuery.extend(jQuery.fn.fmatter , {
  // urlFormatter allows for a single param substitution.
  urlFormatter : function(cellvalue, options, rowdata) {
  var sub_value = options.rowId;
  //alert(options.rowId);
  var name = options.colModel.realColName;
  //var name = 'actionName';
  //alert(that.columnLinks[name].template);
  OWA.debug(options.rowId-1+' '+name);
  //var new_url = that.columnLinks[name].template.replace('%s', escape(sub_value));
  var new_url = that.resultSet.resultsRows[options.rowId-1][name].link;
  var link = '<a href="' + new_url + '">' + cellvalue + '</a>';
  return link;
  },
   
  useServerFormatter : function(cellvalue, options, rowdata) {
  var name = options.colModel.realColName;
  return that.resultSet.resultsRows[options.rowId-1][name].formatted_value;
  }
   
  });
   
   
   
  if (this.resultSet.resultsReturned > 0) {
   
  // happens with first results set when loading from URL.
  if (this.gridInit !== true) {
  this.displayGrid();
  }
   
  jQuery("#"+that.dom_id + ' _grid').jqGrid('clearGridData',true);
  this.addAllRowsToGrid();
   
  // hide the built in jqgrid loading divs.
  jQuery("#load_"+that.dom_id+"_grid").hide();
  jQuery("#load_"+that.dom_id+"_grid").css("z-index", 101);
   
  // check to see if we need ot hide the previous page control.
  if (this.resultSet.page == 1) {
  jQuery("#"+that.dom_id +' > .owa_resultsExplorerBottomControls > UL > .owa_previousPageControl').hide();
  } else if (this.resultSet.page == this.resultSet.total_pages) {
  jQuery("#"+that.dom_id +' > .owa_resultsExplorerBottomControls > UL > .owa_nextPageControl').hide();
  } else {
  jQuery("#"+that.dom_id +' > .owa_resultsExplorerBottomControls > UL > .owa_previousPageControl').show();
  }
   
  } else {
  jQuery("#"+that.dom_id).html("No data is available for this time period.");
  }
  },
   
  loader : function(data) {
   
  if (data) {
  // check to see if resultSet is new
  if (this.resultSet.length > 0) {
  // if not new then return. nothing to do.
  if (data.resultSet.guid === this.resultSet.guid) {
  return;
  }
   
  }
   
  this.setResultSet(data);
   
  if (this.view) {
  var method_name = this.viewMethods[this.view];
  this[method_name]();
  }
   
  if (this.asyncQueue.length > 0) {
   
  for(var i=0;i< this.asyncQueue.length;i++) {
   
  this.dynamicFunc(this.asyncQueue[i]);
  }
  }
  }
  },
   
  dynamicFunc : function (func){
  //alert(func[0]);
  var args = Array.prototype.slice.call(func, 1);
  //alert(args);
  this[func[0]].apply(this, args);
  },
   
  // fetch the result set from the server
  getResultSet : function(url) {
   
  var that = this;
   
  // uses the built in jqgrid loading divs. just giveit a message and show it.
  jQuery("#load_"+that.dom_id+"_grid").html('Loading...');
  jQuery("#load_"+that.dom_id+"_grid").show();
  jQuery("#load_"+that.dom_id+"_grid").css("z-index", 1000);
  jQuery.getJSON(url, '', function (data) {that.loader(data);});
  },
   
  injectDomElements : function() {
   
  var p = '';
   
  p += '<table id="'+ this.dom_id + '_grid"></table>';
  p += '<div class="owa_genericHorizontalList owa_resultsExplorerBottomControls"><ul></ul></div>';
  p += '<div style="clear:both;"></div>';
   
  var that = this;
  jQuery('#'+that.dom_id).append(p);
  },
   
  setGridOptions : function() {
   
  var that = this;
   
  var columns = [];
   
  var columnDef = '';
   
  for (var column in this.resultSet.resultsRows[0]) {
   
  // check to see if we should exclude any columns
  if (this.options.grid.excludeColumns.length > 0) {
   
  for (var i=0;i<=this.options.grid.excludeColumns.length -1;i++) {
  // if column name is not on the exclude list then add it.
  if (this.options.grid.excludeColumns[i] != column) {
  // add column
  columnDef = this.makeGridColumnDef(this.resultSet.resultsRows[0][column]);
  columns.push(columnDef);
  }
  }
   
  } else {
  // add column
  columnDef = this.makeGridColumnDef(this.resultSet.resultsRows[0][column]);
  columns.push(columnDef);
  }
   
  }
   
  jQuery('#' + that.dom_id + '_grid').jqGrid({
  jsonReader: {
  repeatitems: false,
  root: "resultsRows",
  cell: '',
  id: '',
  page: 'page',
  total: 'total_pages',
  records: 'resultsReturned'
  },
  afterInsertRow: function(rowid, rowdata, rowelem) {return;},
  datatype: 'local',
  colModel: columns,
  rownumbers: that.options.grid.showRowNumbers,
  viewrecords: true,
  rowNum: that.resultSet.resultsReturned,
  height: '100%',
  autowidth: true,
  hoverrows: false,
  sortname: that.resultSet.sortColumn + '.value',
  sortorder: that.resultSet.sortOrder
  });
   
  // set header css
  for (var y=0;y < columns.length;y++) {
  var css = {};
  //if dimension column then left align
  if ( columns[y].classes == 'owa_dimensionGridCell' ) {
  css['text-align'] = 'left';
  } else {
  css['text-align'] = 'right';
  }
  // if sort column then bold.
  if (this.resultSet.sortColumn +'.value' === columns[y].name) {
  //css.fontWeight = 'bold';
  }
  // set the css. no way to just set a class...
  jQuery('#' + that.dom_id + '_grid').jqGrid('setLabel', columns[y].name, '',css);
  }
   
  /*
  // custom formattter functions.
  jQuery.extend(jQuery.fn.fmatter , {
  // urlFormatter allows for a single param substitution.
  urlFormatter : function(cellvalue, options, rowdata) {
  var sub_value = options.rowId;
  //alert(options.rowId);
  var name = options.colModel.realColName;
  //var name = 'actionName';
  //alert(that.columnLinks[name].template);
  OWA.debug(options.rowId-1+' '+name);
  //var new_url = that.columnLinks[name].template.replace('%s', escape(sub_value));
  var new_url = that.resultSet.resultsRows[options.rowId-1][name].link;
  var link = '<a href="' + new_url + '">' + cellvalue + '</a>';
  return link;
  }
  });
  */
  },
   
  makeGridColumnDef : function(column) {
   
  var _sort_type = '';
  var _align = '';