Commits

Anonymous committed f548789

making jquery.htraf.js usable without htraf.js

Comments (0)

Files changed (2)

     } 
 }
 
-
-var HTRAF = window.HTRAF = {};
-
-HTRAF.prefix = getAttr('src').replace(/\/htraf\.js$/, '');
-HTRAF.htsqlPrefix = getAttr('data-htsql-prefix') || '';
-
-var htsqlVersion = (getAttr('data-htsql-version') || '1').substr(0,1);
-HTRAF.htsqlVersion = /1|2/.test(htsqlVersion) ? htsqlVersion:'1';
-
-HTRAF.addClass = getAttr('data-htraf-class') || 'htraf';
-
-HTRAF.convert = {};
-HTRAF.convert.htsql = HTRAF.htsqlVersion == '1' ?
-    function(data) {
-        var ret = {headers: [], data: []}; 
-        $.each(data.meta[0].segment[0].element, function(i, element) {
-            ret.headers.push({
-                title: element.title,
-                domain: element.domain
-            });
-        });
-
-        $.each(data.data.branches[0], function(i, row) {
-            ret.data.push(row.fields);     
-        });
-        return ret;
-    }
-    :
-    function(data) {
-        return {
-            headers: data.meta,
-            data: data.data
-        };
-    };
-
-HTRAF.htsqlFormatter = HTRAF.htsqlVersion == 1 ? 'jsonex':':json';
-
-var onerror = getAttr('data-onerror'); 
-HTRAF.onerror = onerror ? new Function(onerror) :
-                          function (e, info) {
-                              alert('Error loading element\n\n'
-                                    + info.reason + '\n'
-                                    + info.detail + '\n\n'
-                                    + 'Element:\n' 
-                                    + $.htraf.util.getHtml(info.element) 
-                                    + '\n');
-                          };
-
-var qs = location.search;
-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)
-            HTRAF.param[param[0]] = decodeURIComponent(param[1]);
-    }
-}
-
-
-var _jqueryVersion = (window.$ || function() {return {}}).call().jquery 
-                       || '0.0.0';
+var prefix = getAttr('src').replace(/(^|\/)htraf\.js$/, ''),
+    _jqueryVersion = (window.$ || function() {return {}}).call().jquery 
+                     || '0.0.0';
 if(_jqueryVersion < '1.5.1')
     document.write('<script type="text/javascript" src="' 
-        + HTRAF.prefix + '/lib/jquery-1.5.1.min.js"></script>');
+        + prefix + '/lib/jquery-1.5.1.min.js"></script>');
 
 if(!(window.JSON && window.JSON.parse && window.JSON.stringify))
     document.write('<script type="text/javascript" src="' 
-        + HTRAF.prefix + '/lib/json2.js"></script>');
+        + prefix + '/lib/json2.js"></script>');
 
-// Always include jquery.ui since already included may lack some widgets
-// TODO: optimize size later: all-in-one or part by part?
 document.write('<script type="text/javascript" src="' 
-    + HTRAF.prefix + '/lib/jquery.blockUI.js"></script>');
-document.write('<script type="text/javascript" src="' + HTRAF.prefix 
-    + '/lib/jqueryui/jquery-ui-1.8.13.custom.min.js"></script>');
+    + prefix + '/lib/jquery.blockUI.js"></script>');
+
+if(!window.$ || !window.$.Widget || !window.ui || !window.ui.position)
+    document.write('<script type="text/javascript" src="' + prefix 
+        + '/lib/jqueryui/jquery-ui-1.8.13.custom.min.js"></script>');
 
 // HTRAF files
 document.write('<script type="text/javascript" src="' 
-    + HTRAF.prefix + '/jquery.htraf.js"></script>');
+    + prefix + '/jquery.htraf.js"></script>');
 document.write('<link rel="stylesheet" type="text/css" href="'
-    + HTRAF.prefix + '/htraf.css"/>');
+    + prefix + '/htraf.css"/>');
 
 })();

htraf/jquery.htraf.js

 (function($, undefined) {
 
+// {{{ Setting up the base htraf parameters
+var HTRAF = window.HTRAF = {},
+    selector = $.map(['htraf.js', 'jquery.htraf.js'], function(f) {
+        return 'script[src="' + f + '"],script[src$="/' + f + '"]';
+    }).join(','),
+    $script = $(selector);
+
+HTRAF.prefix = $script.attr('src').replace(/(^|\/)htraf\.js$/, '');
+HTRAF.htsqlPrefix = $script.attr('data-htsql-prefix') || '';
+
+var htsqlVersion = ($script.attr('data-htsql-version') || '1').substr(0,1);
+HTRAF.htsqlVersion = /1|2/.test(htsqlVersion) ? htsqlVersion:'1';
+
+HTRAF.addClass = $script.attr('data-htraf-class') || 'htraf';
+
+HTRAF.convert = {};
+HTRAF.convert.htsql = HTRAF.htsqlVersion == '1' ?
+    function(data) {
+        var ret = {headers: [], data: []}; 
+        $.each(data.meta[0].segment[0].element, function(i, element) {
+            ret.headers.push({
+                title: element.title,
+                domain: element.domain
+            });
+        });
+
+        $.each(data.data.branches[0], function(i, row) {
+            ret.data.push(row.fields);     
+        });
+        return ret;
+    }
+    :
+    function(data) {
+        return {
+            headers: data.meta,
+            data: data.data
+        };
+    };
+
+HTRAF.htsqlFormatter = HTRAF.htsqlVersion == 1 ? 'jsonex':':json';
+
+var onerror = $script.attr('data-onerror'); 
+HTRAF.onerror = onerror ? new Function(onerror) :
+                          function (e, info) {
+                              alert('Error loading element\n\n'
+                                    + info.reason + '\n'
+                                    + info.detail + '\n\n'
+                                    + 'Element:\n' 
+                                    + $.htraf.util.getHtml(info.element) 
+                                    + '\n');
+                          };
+
+var qs = location.search;
+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)
+            HTRAF.param[param[0]] = decodeURIComponent(param[1]);
+    }
+}
+
 HTRAF.$ = $;
+// }}}
+
 
 $.htraf = $.htraf || {};
 $.htraf.plugin = $.htraf.plugin || {};
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.