1
0
forked from GitHub/gf-core

RGL Browser: updates

This commit is contained in:
john.j.camilleri
2012-07-11 13:27:00 +00:00
parent 71098e9a73
commit 0fba4d4c5a
3 changed files with 195 additions and 130 deletions

View File

@@ -4,14 +4,60 @@
*/
$(document).ready(function() {
var loadCount = 0;
// ===== State information =====
state = {
index: undefined,
lookup: {},
loadCount: 0,
language: undefined, // lang of drop-down
current: { // current file
language: undefined,
module: undefined,
set: function(lang, module) {
state.current.language = lang;
state.current.module = module;
},
equals: function(a, b) {
if (!b)
return (a == state.current.module);
else
return (a == state.current.language) && (b == state.current.module);
}
},
title: "RGL Source Browser",
urlPrefix: "/",
defaultLangs: ['abstract','api','common','prelude']
} ;
var lookupModuleLanguage = function(module) {
var l = state.lookup[module];
if (l==undefined || l.length==0)
return null;
else if (l.length==1)
return l[0];
else {
for (i in l) {
if ($.inArray(l[i], state.defaultLangs))
return l[i];
}
return l[0]; // no preferred default, just return first...
}
}
var lookupAllModuleLanguages = function(module) {
return state.lookup[module];
}
// ===== Utility/UI functions =====
var showLoading = function(){
loadCount++;
state.loadCount++;
$("#loading").show();
}
var hideLoading = function(){
loadCount = Math.max(loadCount-1, 0);
if (loadCount == 0)
state.loadCount = Math.max(state.loadCount-1, 0);
if (state.loadCount == 0)
$("#loading").hide();
}
@@ -55,17 +101,57 @@ $(document).ready(function() {
$('#code pre').html(code);
prettyPrint();
}
var updateScopeCount = function(){
$('#scope_count').text( $("#scope_list tr:visible").length );
}
var current_language = undefined;
var urlPrefix = "/";
var index;
var setLanguage = function(lang){
state.language = lang;
$("#languages select").val(lang);
initModules(lang);
}
// obj can be just a plain selector or a jQuery object
var showPanel = function(obj, callback){
showLoading();
setTimeout(function(){
$(".panel:visible").hide();
$(obj).show(0, callback);
recalculateHeights();
updateScopeCount();
hideLoading();
}, 500); // this ensures the loading displays
}
var getPanel = function() {
return $('.panel:visible').first();
}
var setTitle = function(s){
$('#tabbar h2').html(s);
$('title').html(state.title + ": " + s);
}
// ===== Initialization =====
// Load the index file and populate language & module lists
$.ajax({
url: "index.json",
dataType: "json",
type: "GET",
success: function(data) {
index = data;
if (data['urlprefix']) urlPrefix = data['urlprefix'];
state.index = data;
if (data['urlprefix']) state.urlPrefix = data['urlprefix'];
// Build language lookup index
for (var lang in data['languages']) {
for (var i in data['languages'][lang]) {
var module = data['languages'][lang][i];
if (!module) continue;
if (!state.lookup[module]) state.lookup[module] = [];
state.lookup[module].push(lang);
}
}
// Initialize the language list
var lang_select = $("<select>")
@@ -75,7 +161,7 @@ $(document).ready(function() {
})
.appendTo("#languages")
var language_list = data['languages'];
for (i in language_list) {
for (var i in language_list) {
if (!i) continue;
var lang = i;
$('<option>')
@@ -91,41 +177,7 @@ $(document).ready(function() {
}
});
var setLanguage = function(lang){
current_language = lang;
$("#languages select").val(lang);
initModules(lang);
}
// Initialize the module list
var initModules = function(lang){
index['languages'][lang] = index['languages'][lang].sort();
$("#modules").empty();
for (i in index['languages'][lang]) {
var module = index['languages'][lang][i];
if (!module) continue;
$('<a>')
.html(module)
.addClass('button')
.attr('href', "#"+lang+"/"+module+".gf")
.appendTo("#modules");
}
};
// Initialize the panels & tabs
// obj can be just a plain selector or a jQuery object
var showPanel = function(obj, callback){
showLoading();
setTimeout(function(){
$(".panel:visible").hide();
$(obj).show(0, callback);
recalculateHeights();
hideLoading();
}, 500); // this ensures the loading displays
}
var getPanel = function() {
return $('.panel:visible').first();
}
$(".panel").each(function(a,b){
$("<a>")
.addClass('tab')
@@ -138,22 +190,60 @@ $(document).ready(function() {
})
// .appendTo("#tabbar")
.insertBefore("#tabbar *:last-child")
});
});
showPanel(".panel:first");
var setTitle = function(s){
$('#tabbar h2').html(s);
}
var updateScopeCount = function(){
$('#scope_count').text( $("#scope_list tr:visible").length );
}
// ===== URL history =====
$.History.bind(function(hash){
var s = hash.split("/");
var lang = s[0];
var module = stripExt(s[1]);
var parseLineNo = s[1].match(/:(\d+)(-(\d+))?$/);
if (state.current.equals(lang, module)) {
if (parseLineNo) {
scrollToCodeLine(parseInt(parseLineNo[1]));
}
// else there's nothing to do!
} else {
if (parseLineNo != undefined)
loadFile(lang, module, parseInt(parseLineNo[1]));
else
loadFile(lang, module);
}
});
var stripExt = function(s) {
var i = s.lastIndexOf('.');
return (i>-1) ? s.substr(0, i) : s;
};
// ===== Loading functionality =====
// Initialize the module list
var initModules = function(lang){
state.index['languages'][lang] = state.index['languages'][lang].sort();
$("#modules").empty();
for (var i in state.index['languages'][lang]) {
var module = state.index['languages'][lang][i];
if (!module) continue;
$('<a>')
.html(module)
.addClass('button')
.attr('href', "#"+lang+"/"+module+".gf")
.appendTo("#modules");
}
};
// Load both scope & source for a file
var loadFile = function(lang, module, lineNo){
setTitle(lang+"/"+module);
state.current.set(lang, module);
loadTagsFile(module);
loadSourceFile(lang, module, lineNo)
loadSourceFile(lang, module, lineNo);
}
// Load a tags file
@@ -168,19 +258,21 @@ $(document).ready(function() {
data = data.replace(/^(\S+)\s(\S+)\s(.+)?$/gm, function(a,b,c,d){
var s = d.split("\t");
if (c == "indir") {
var name = s[2].slice(s[2].lastIndexOf('/')+1);
var url = "#"+name;
var module = s[2].substring(s[2].lastIndexOf('/')+1, s[2].lastIndexOf('.'));
var lang = lookupModuleLanguage(module);
var name = lang+"/"+module;
var url = "#"+lang+"/"+module;
var anchor = '<a href="'+url+'">'+name+'</a>';
return '<tr class="indir" name="'+b+'"><th>'+b+'</th><td>'+c+'</td><td>'+s[0]+'</td><td>'+s[1]+'</td><td>'+anchor+'</td><td></td></tr>'
} else {
var bits = s[0].split("/"); // ["lib", "src", "english", "AdjectiveEng.gf:43-46"]
var name = bits[3]+"/"+bits[4];
var url = "#"+bits[3]+"/"+bits[4];
var anchor = '<a href="'+url+'">'+s[0]+'</a>';
var anchor = '<a href="'+url+'">'+name+'</a>';
return '<tr class="local" name="'+b+'"><th>'+b+'</th><td>'+c+'</td><td></td><td></td><td>'+anchor+'</td><td>'+s[1]+'</td></tr>'
}
});
setScope(data);
updateScopeCount();
runFilter();
hideLoading();
},
@@ -191,22 +283,12 @@ $(document).ready(function() {
});
}
// Just get the HTTP headers to see if a file exists
var checkSourceFile = function(args) {
$.ajax({
url: urlPrefix + "lib/src/"+args.lang+"/"+args.module+".gf",
type: "HEAD",
success: args.onsuccess,
error: args.onerror
});
}
// Load a source module
var loadSourceFile = function(lang, module, lineNo) {
clearCode();
showLoading();
$.ajax({
url: urlPrefix + "lib/src/"+lang+"/"+module+".gf",
url: state.urlPrefix + "lib/src/"+lang+"/"+module+".gf",
type: "GET",
dataType: "text",
success: function(data, status, xhr){
@@ -223,6 +305,9 @@ $(document).ready(function() {
});
}
// ===== Filtering of scope info =====
// Custom selector
$.expr[':'].match = function(a,b,c) {
var obj = $(a);
@@ -237,7 +322,7 @@ $(document).ready(function() {
};
var runFilter = function() {
showLoading()
showLoading();
var s = $("#search").val();
try {
if (s) {
@@ -286,41 +371,8 @@ $(document).ready(function() {
$("#show_all").change(runFilter);
$("#show_local").change(runFilter);
// ===== URL history =====
$.History.bind(function(state){
var s = state.split("/");
if (s[0].match(/\.gf-tags$/)) {
var module = stripExt(s[0]);
checkSourceFile({
lang: current_language,
module: module,
onsuccess: function(){
$.History.go(current_language+"/"+module);
},
onerror: function(){
// Load just tags (we don't know source)
setTitle(module+" (scope only)");
clearCode();
loadTagsFile(module);
scrollToTop();
}
});
} else {
var lang = s[0];
var module = stripExt(s[1]);
var parseLineNo = s[1].match(/:(\d+)(-(\d+))?$/);
if (parseLineNo != undefined)
loadFile(lang, module, parseInt(parseLineNo[1]));
else
loadFile(lang, module);
}
});
var stripExt = function(s) {
return s.substr(0, s.lastIndexOf('.'));
};
// ===== Resizing stuff =====
// ===== Window resizing stuff =====
// refer: http://paulirish.com/2009/throttled-smartresize-jquery-event-handler/
(function($,sr){
@@ -354,7 +406,7 @@ $(document).ready(function() {
});
var recalculateHeights = function() {
$('body').height( $(window).height()-85); //80 was just found empirically
$('body').height( $(window).height()-60); // just found empirically
$('.maxheight').each(function(){
var obj = $(this);
var parent = obj.parent();