Commits

Anonymous committed 6931256

add segmentedlist widget; we need to look at how segmented data is handled in htraf

Comments (0)

Files changed (1)

htraf/jquery.htraf.js

 });
 // }}}
 
+ 
+// {{{ Widgets: htraf.segmentedlist
+// produces indented list of segmented data 
+$.widget("htraf.segmentedlist", $.htraf.Base, {
+    _create: function() {
+        $.htraf.Base.prototype._create.call(this);
+    },
+
+    render: function() {
+        var data = this.data.data;
+        var id = this.element.attr('id');
+        // data not is right format and doesn't include multiple segments
+        // so use our own get data function
+        // we must fix htraf to support multiple segments
+        this.getJSONdata( id );
+
+        if(!data.length)
+            this._trigger('empty');
+        this._trigger('change');
+    },
+
+    // Put the results of a segmented query into a html object, usually a div
+    getJSONdata: function( id ) 
+    {
+        var htsql = this._source + '/json()';
+        var outerthis = this;
+	    $.getJSON(htsql)
+	    .success(function(data) {
+	        $("#"+id).append( outerthis.arrayToStr(data) );
+	    })
+	    .error(function() {  
+	        alert("Error retrieving HTSQL Statement information") 
+	    });
+    },
+
+    // Test if object is an array
+    isArray: function(what) 
+    {
+        return Object.prototype.toString.call(what) === '[object Array]';
+    },
+
+    // Make every element of an array into an html line
+    arrayToStr: function(arr, indent) 
+    {
+        var tab =  '      ';  // used for indenting and spacing columns
+        
+        if ( indent == undefined)
+            indent = 0;
+        else 
+            indent = indent + 1;
+        
+        // here is where we wrap each line
+        // just using span for now, but we can make it a table row or whatever  
+        var str = '<span class="htraf_seg_'+indent+'">';
+
+        // for each element in the array                 
+        for(var i in arr)
+		{
+		    for ( var j=0; j<indent; j++)
+		        str = str + tab;
+
+            // for each column n the row
+            // skip the special htsql: columns and passover lower segments				        
+			for (var key in arr[i]) 
+			{
+			  if (arr[i].hasOwnProperty(key)) 
+			  {
+			      if (key == "htsql:id" | key == "htsql:idtag" | key == "htsql:tag")
+			          continue;
+			      if ( this.isArray( arr[i][key] ) )
+			           continue;
+			      else 
+			           // here is where we format each column
+			           // just using string for now
+			           str = str + key + " :" + arr[i][key] + tab;
+			  }
+			}
+			str = str + "</span><br/>";
+			
+			// now, for each sub record
+			for (var key in arr[i]) 
+			{
+			  if (arr[i].hasOwnProperty(key)) 
+			  {
+			      if ( this.isArray( arr[i][key] ) )
+			           // make recursive call to arrayToStr
+			           str = str + this.arrayToStr(arr[i][key], indent);
+			  }
+			}
+        }
+        return str;
+    },
+
+    removeUserSelect: function() {
+        this.element.unbind('change.userselect');                  
+    },
+
+    addUserSelect: function() {
+        var self = this;
+        this.element.bind('change.userselect', function() {
+            self._trigger(USERSELECT); 
+        });               
+    }
+});
+// }}}
+
 // {{{ Widgets: htraf.table
 $.widget("htraf.table", $.htraf.Base, {
     _create: function() {