<?xml version="1.0" encoding="UTF-8"?>
<Module>

  <ModulePrefs
      title="Simple Table Gadget"
      description="Official Google Gadget. A simple table presenting the data of a table query result."
      author="Google Engineering"
      author_affiliation="Google Inc."
      author_email="visualization.api+simple-table@gmail.com"
      screenshot="/ig/modules/simple-table.png"
      thumbnail="/ig/modules/simple-table-thm.png">
    <Require feature="idi" />
    <Require feature="locked-domain" />
  </ModulePrefs>

  <UserPref name="_table_query_url" display_name="Data source url" required="true"/>
  <UserPref name="_table_query_refresh_interval" display_name="Data refresh interval (minutes)" default_value="0" datatype="enum" required="false">
    <EnumValue value="0" display_value="Do not refresh"/>
    <EnumValue value="60" display_value="1"/>
    <EnumValue value="300" display_value="5"/>
    <EnumValue value="1800" display_value="30"/>
  </UserPref>

  <!--
  This is a sample gadget, that uses the Google Visualization API to read data
  from a data source, and displays it as an html table.
  -->

  <Content type="html"><![CDATA[

  <!-- Load the Google common loader, that is later used to load the Visualization API. -->
  <script src="http://www.google.com/jsapi" type="text/javascript"></script>

  <div id="tablediv" style="overflow: auto;"><img src="http://www.google.com/ig/images/spinner.gif" /></div>

  <script>
    var gadgetHelper = null;
    var table = null; 

    _IG_RegisterOnloadHandler(loadVisualizationAPI);

    /**
     * Load the Google Visualization API
     */
    function loadVisualizationAPI() {
      google.load("visualization", "1", {"packages": ["table"]});
      google.setOnLoadCallback(sendQuery);
    }

    /**
     * Create a query from the user prefs, and then send it to the data source.
     * This method is called once the visualization API is fully loaded.
     * Note that in the last line, a callback function is specified to be
     * called once the response is received from the data source.
     */
    function sendQuery() {
      var prefs = new _IG_Prefs(); // User preferences
      var tableDiv = _gel('tablediv');
      tableDiv.style.width = document.body.clientWidth + 'px';
      tableDiv.style.height = document.body.clientHeight + 'px';
      table = new google.visualization.Table(tableDiv);

      gadgetHelper = new google.visualization.GadgetHelper();
      var query = gadgetHelper.createQueryFromPrefs(prefs);
      query.send(handleQueryResponse);
    }

    /**
     * Query response handler function.
     * Called by the Google Visualization API once the response is received.
     * Takes the query response and formats it as a table.
     */
    function handleQueryResponse(response) {
      // Use the visualization GadgetHelper class to validate the data, and
      // for error handling.
      if (!gadgetHelper.validateResponse(response)) {
        // Default error handling was done, just leave.
        return;
      };
      var data = response.getDataTable();

      // Take the data table from the response, and format it.
      var options = {showRowNumber: true};
      table.draw(data, options);
    };

  </script>

  ]]>
  </Content>
</Module>

