Commits

Anonymous committed 117538e Merge

htsql 2.0 uses the 'data-ref' for refernces (former variables)

  • Participants
  • Parent commits 586b96e, 9a12091

Comments (0)

Files changed (5)

File htraf/examples20/index.html

     <div style="width: 500px; height: 350px;" 
         data-htsql="/program{title, count(student)}
                     ?school_code=$school&count(student)>0" 
+        data-ref="school"
         data-type="pie"
         data-widget="chart"
         data-title="Percent of Students by Program"></div>
     <p>Filter by name: <input id="department_name"/></p>
     <table id="department" data-hide-column-0="yes" 
         data-htsql="/department{code, name, school.name}
-                    ?school_code=$school&name~$department_name">
+                    ?school_code=$school&name~$department_name"
+        data-ref="school department_name">
     </table>
     <p>
         The selected department: 
-        <em data-htsql="/department{name}?code=$department"></em> <br/>
+        <em data-htsql="/department{name}?code=$department"
+            data-ref="department"></em> <br/>
         The number of courses in the selected department: 
-        <strong data-htsql="/department{count(course)}?code=$department">
+        <strong data-htsql="/department{count(course)}?code=$department"
+                data-ref="department">
         </strong>
     </p>
     <h3>Courses</h3>
-    <table id="course" data-htsql="/course?department_code=$department">
+    <table id="course" data-htsql="/course?department_code=$department"
+           data-ref="department">
     </table>
 </body>
 </html>

File htraf/htraf.js

                           };
 
 var qs = location.search;
-HTSQL.param = {};
+HTRAF.param = {};
 if(qs) {
     qs = qs.substr(1, qs.length).split('&');
     for(var i = 0, l = qs.length; i < l; i++) {
         var param = qs[i].split('=');
         if(param.length == 2)
-            HTSQL.param[param[0]] = decodeURIComponent(param[1]);
+            HTRAF.param[param[0]] = decodeURIComponent(param[1]);
     }
 }
 

File htraf/js/htraf.init.js

     });
 };
 
-$.fn.linked = function() {
+$.fn.ref = function() {
     if(this.size() == 0)
         return $();
     var el = this[0];
     var widget = $(el).data('htraf');
     if(!widget)
         return $();
-    return $(el)[widget]('linked');
+    return $(el)[widget]('ref');
 };
 
 $.fn.detectCycles = function() {
     function expand(node, proceed) {
-        $(node).linked().each(function() {
+        $(node).ref().each(function() {
             assert($.inArray(this, proceed) == -1, "Cycle detected", this);
             expand(this, [].concat(proceed, [this])); 
         });
             .widgetize()
             .detectCycles()
             .filter(function() {
-                return $(this).linked().size() == 0; 
+                return $(this).ref().size() == 0; 
             })
             .loadWidget();
     });

File htraf/js/htraf.plugins.js

                     $(this).removeClass(cls);
             });
         }
+
+        // link highlighting
+        $el.find("th").each(function(i) {
+                      
+            if ( $(this).text().indexOf('link_to_') != -1 )
+            {
+                $(this).text($(this).text().substring($(this).text().indexOf('link_to_')+8));
+                selector = "td:nth-child("+(i+1)+")";
+                $el.find(selector).each(function(e) {
+                    if ( $(this).text().indexOf('|') != -1 )
+                    {
+                        $(this).html('<a href="'+$(this).text().substring($(this).text().indexOf('|')+1)+'">'+$(this).text().substring(0,$(this).text().indexOf('|'))+'</a>');
+                    }
+                });
+            }
+        });
+
         
     }
 };
 
 
 })(jQuery);
+
+
+// Misc functions
+
+// Convert table columns of named like_to_ into links when
+// data values are of form link_name|link_href
+function makeLinks(id)
+{
+    var table_selector = "#" + id;
+            
+    $(table_selector).find("th").each(function(i) {
+                  
+        if ( $(this).text().indexOf('link_to_') != -1 )
+        {
+            $(this).text($(this).text().substring($(this).text().indexOf('link_to_')+8));
+            selector = "td:nth-child("+(i+1)+")";
+            $(table_selector).find(selector).each(function(e) {
+                if ( $(this).text().indexOf('|') != -1 )
+                {
+                    $(this).html('<a href="'+$(this).text().substring($(this).text().indexOf('|')+1)+'">'+$(this).text().substring(0,$(this).text().indexOf('|'))+'</a>');
+                }
+            });
+        }
+    });
+
+}
+
+// hide column in tables
+function hideCol(id,col)
+{ 
+    selector = "#" + id + ' td:nth-child('+col+')';
+    $(selector).hide();
+    selector = "#" + id + ' th:nth-child('+col+')';
+    $(selector).hide();
+}
+    
+// right align column in tables
+function alignCol(id,col)
+{ 
+    selector = "#" + id + ' td:nth-child('+col+')';
+    $(selector).css("text-align","right");
+    selector = "#" + id + ' th:nth-child('+col+')';
+    $(selector).css("text-align","right");
+}
+
+
+// Add column totals to tables
+function colTotals(id,cols)
+{
+
+    selector = "#" + id + " tbody tr";
+    for (i in cols)
+    {
+        tot = 0;
+        dollars = false;
+        $(selector).children("td:nth-child(" + cols[i] + ")")
+        .each(function() {
+            if ($(this).html().indexOf('$')>=0)
+                dollars = true;
+            fl = isFloat( $(this).html().replace('$','').replace(/,/g,''));
+            if ( fl ) {
+                tot += parseFloat($(this).html().replace('$','').replace(/,/g,''));
+            } else {
+                tot += parseInt($(this).html().replace('$','').replace(/,/g,''));
+            }
+        });
+        
+        tot = (isFloat(tot)) ? tot.toFixed(2) : tot;
+        tot = addCommas(tot);
+        if (dollars)
+            tot = '$'+tot;
+
+        selector2 = "#" + id + " tfoot th:nth-child(" + cols[i] + ")";
+        alert(tot);
+        $(selector2).html(tot);
+
+    }
+
+}
+
+// used by colTotals
+function isFloat(x)
+{
+    if (x == parseInt(x) && x == parseFloat(x)) 
+    {
+        return false;
+    } 
+    else if (x == parseFloat(x)) 
+    {
+        return true;
+    } 
+    else 
+        return false;
+}
+
+// used by colTotals
+function addCommas(nStr)
+{
+	nStr += '';
+	x = nStr.split('.');
+	x1 = x[0];
+	x2 = x.length > 1 ? '.' + x[1] : '';
+	var rgx = /(\d+)(\d{3})/;
+	while (rgx.test(x1)) {
+		x1 = x1.replace(rgx, '$1' + ',' + '$2');
+	}
+	return x1 + x2;
+}
+
+// End add column totals to tables
+
+
+

File htraf/js/htraf.widgets.js

 }
 
 function isHtsqlResource(url) {
-    return url.match(/(^.+?\.htsql)\s*(\?|$)/);
+    return url.match(/\.htsql$/);
 }
 
 var $hidden = null;
         window.console.debug.apply(window.console, arguments);
 }
 
-function iterVars(s, callback) {
-    var reStr = /^('|").*?(\1|$)/;
-    var reNonStr = /^[^\$'"]+?(\$|'|"|$)/;
-    var proceed = '';
-    var findVar = function(s) {
-        var reQuoted = /^\s*(".+?")+/;
-        var reNonQuoted = /^\s*[a-z]\w*/i;
-        var match = s.match(reQuoted);
-        if(match) {
-            match = match[0]
-            s = s.substr(match.length, s.length);
-            match = $.trim(match.replace(/""/g, '"'));
-            match = match.substr(1, match.length - 2);
-        }
-        else {
-            match = s.match(reNonQuoted);
-            if(match) {
-                match = match[0];
-                s = s.substr(match.length, s.length);
-                match = $.trim(match);
-            }
-        }
-        if(match)
-            proceed += callback(match);
-        return s;
-    };
-
-    while(s) {
-        var match = s.match(reStr);
-        if(match) {
-            match = match[0];
-            proceed += match;
-            s = s.substr(match.length, s.length);
-            continue;
-        }
-        var match = s.match(reNonStr);
-        if(match) {
-            match = match[0];
-            var last = match.substr(match.length - 1, 1);
-            switch(last) {
-                case '$':
-                    proceed += s.substr(0, match.length - 1);
-                    s = findVar(s.substr(match.length, s.length));
-                    break;
-                case '"':
-                case "'":
-                    match = match.substr(0, match.length - 1);
-                    proceed += match;
-                    s = s.substr(match.length, s.length);
-                    break;
-                default:
-                    proceed += match
-                    s = s.substr(match.length, s.length);
-            }
-            continue;
-        }
-        assert(false, "Can't process query: " + s);
-    }
-    return proceed;
-}
-
-function replaceVars(url, vars) {
-    return iterVars(url, function(v) {
-        return escape(vars[v]);      
-    });
-}
-
-function varsSelector(s) {
-    var vars = {}, selector = [];
-    if(isHtsqlResource(s))
-        $.each(s.match(/\$\w+/g) || [], function(i, v) {
-            vars[v.substr(1, v.length)] = true;
-        });
-    else
-        iterVars(s, function(v) {
-            vars[v] = true; 
-        });
-
-    $.each(vars, function(key, value) {
-        selector.push('[id="' + key.replace(/"/g, '\\"') + '"]');
-    });
-    return selector.join(',');
-}
-
 function varsToQS(vars) {
     var ret = [];
     $.each(vars, function(key, value) {
         HTSQL: 'data-htsql',
         LOCAL: 'data-bind',
         WIDGET: 'data-widget',
+        REF: 'data-ref',
         CHANGE: 'change',
         BEFORELOAD: 'beforeload',
         AFTERLOAD: 'afterload',
     isTrue: isTrue,
     getHtml: getHtml,
     isHtsqlResource: isHtsqlResource,
-    varsSelector: varsSelector,
     escape: escape,
     require: require
 };
     _updateAttr: {},
 
     _setupSource: function() {
-        if(this._linked)
-            this._removeLinked();
+        if(this._ref)
+            this._removeRef();
         else
-            this._linked = $();
+            this._ref = $();
+        this._param  = {};
 
         var self = this;
         $.each(SOURCES, function(i, attr) {
         }); 
 
         switch(this._sourceType) {
-            case HTSQL: this._addLinked(varsSelector(this._source)); break;
-            case LOCAL: this._addLinked('#' + this._source); break;
+            case HTSQL: 
+                var refs = [],
+                    ids = this.element.attr(REF) || '';
+                $.each(ids.split(' '), function(i, id) {
+                    if(!id)
+                        return;
+                    if(id.substr(0, 1) == '_') {
+                        var value = HTRAF.param[id.substr(1, id.length)];
+                        if(value)
+                            self._param[id] = value;
+                    }
+                    else
+                        refs.push('#' + id); 
+                });
+                this._addRef(refs.join(',')); 
+                break;
+            case LOCAL: this._addRef('#' + this._source); break;
         }
     },
 
             f.call(this);
     },
 
-    linked: function() {
-        return this._linked;
+    ref: function() {
+        return this._ref;
     },
 
     isLocal: function() {
         return this._sourceType == LOCAL;       
     },
 
-    _addLinked: function(selector) {
+    _addRef: function(selector) {
         if($(selector).size() == 0)
             return;
 
         var self = this;
-        this._linked = this._linked.add(selector);
-        this._linked
+        this._ref = this._ref.add(selector);
+        this._ref
             .unbind('change.' + this.uniqueID)
             .bind('change.' + this.uniqueID, function() {
                 if(self.isLocal())
             });
     },
 
-    _removeLinked: function() {
-        this._linked.unbind('change.' + this.uniqueID)
-        this._linked = $();
+    _removeRef: function() {
+        this._ref.unbind('change.' + this.uniqueID)
+        this._ref = $();
     },
 
     getVars: function() {
-        var vars = {};
-        this.linked().each(function() {
+        var vars = $.extend({}, this._param || {});
+        this.ref().each(function() {
             if(!$(this).attr('id'))
                 return;
             var widget = $(this).data('htraf');
             && formatter.substr(formatter.length - 1, 1) != ')') {
                 formatter += '()';
             }
-            url = replaceVars(url, vars) + formatter;
+            
+            if(HTRAF.htsqlVersion == '1') {
+                url = '/htsql:let(' + url + formatter + ',' 
+                      + escape(vars) + ')';
+            }
+            else {
+                var s = [];
+                $.each(vars, function(key, value) {
+                    s.push('$' + key + ':=' + escape(value)); 
+                });
+                s = s.join(',');
+                url = url + (s ? ' :where(' + s + ')':'') + formatter;
+            }
         }
         return HTRAF.htsqlPrefix + url;
     },