/*
GF RGL Browser
John J. Camilleri, 2013
*/
var thing = null;
$(document).ready(function(){
thing = new Thing();
// ===== URL history =====
$.history.on('load change push pushed', function(event, url, type) {
var stripExt = function(s) {
var i = s.lastIndexOf('.');
return (i>-1) ? s.substr(0, i) : s;
};
var s = url.split("/");
var lang = s[0];
var module = stripExt(s[1]);
var parseLineNo = s[1].match(/:(\d+)(-(\d+))?$/);
if (thing.state.current.equals(lang, module)) {
if (parseLineNo) {
thing.scrollToCodeLine(parseInt(parseLineNo[1]));
}
// else there's nothing to do!
} else {
if (parseLineNo != undefined)
thing.loadFile(lang, module, parseInt(parseLineNo[1]));
else
thing.loadFile(lang, module);
}
}).listen('hash');
});
function Thing() {
var t = this;
// ===== State information =====
this.state = {
index: undefined,
lookup: {},
loadCount: 0,
language: undefined, // lang of drop-down
current: { // current file
language: undefined,
module: undefined,
set: function(lang, module) {
t.state.current.language = lang;
t.state.current.module = module;
},
equals: function(a, b) {
if (!b)
return (a == t.state.current.module);
else
return (a == t.state.current.language) && (b == t.state.current.module);
}
},
title: "RGL Source Browser",
urlPrefix: "/",
defaultLangs: ['abstract','api','common','prelude']
} ;
this.lookupModuleLanguage = function(module) {
var l = t.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], t.state.defaultLangs))
return l[i];
}
return l[0]; // no preferred default, just return first...
}
}
this.lookupAllModuleLanguages = function(module) {
return t.state.lookup[module];
}
// ===== Utility/UI functions =====
this.showLoading = function(){
t.state.loadCount++;
$("#loading").show();
}
this.hideLoading = function(){
t.state.loadCount = Math.max(t.state.loadCount-1, 0);
if (t.state.loadCount == 0)
$("#loading").hide();
}
this.scrollToTop = function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
}
this.scrollToCodeLine = function(lineNo) {
t.showPanel("#code", function() {
// Find exact line, using the classes generated by google prettify
try {
var obj = $("#code pre li.L"+(lineNo%10)+":eq("+Math.floor(lineNo/10)+")").prev();
var y = Math.max(obj.offset().top - obj.parent().offset().top - 75, 0);
$("#code").parent().animate({ scrollTop: y }, "slow", function(){
t.highlight(obj);
});
} catch (e) {}
});
}
this.highlight = function(obj) {
obj.css('background-color', "yellow");
setTimeout(function(){
obj.css('background-color', "");
}, 1500);
}
this.clearScope = function(msg) {
$('#scope #results').empty();
t.updateScopeCount();
if (msg) {
$('#scope #results').html(""+msg+"");
}
}
this.setScope = function(code) {
$('#scope #results').html(code);
}
this.clearCode = function(msg) {
$('#code pre').empty();
if (msg) {
$('#codes pre').html(""+msg+"");
}
}
this.setCode = function(code) {
$('#code pre').text(code);
prettyPrint();
}
this.updateScopeCount = function(){
$('#scope #count').text( $("#scope #results tr:visible").length );
}
this.updateAPICount = function(){
$('#api #count').text( $("#api #results tr:visible").length );
}
this.setLanguage = function(lang){
t.state.language = lang;
$("#languages select").val(lang);
t.initModules(lang);
}
// hash should be "#code"
this.showPanel = function(hash, callback){
t.showLoading();
setTimeout(function(){
$(".panel:visible").hide();
$("a.tab").removeClass('active');
$("a.tab[href='"+hash+"']").addClass('active');
$(hash).show(0, callback);
t.updateScopeCount();
t.hideLoading();
}, 200); // this ensures the loading displays
}
this.getPanel = function() {
return $('.panel:visible').first();
}
this.setTitle = function(s){
$('#module_name').html(s);
$('title').html(t.state.title + ": " + s);
}
// ===== Initialization =====
// Initialize the panels, tabs
$("a.tab").click(function(){
var panel = $(this).attr("href");
t.showPanel(panel);
return false;
});
t.showPanel("#scope");
// Load the index file and populate language & module lists
$.ajax({
url: "index.json",
dataType: "json",
type: "GET",
success: function(data) {
t.state.index = data;
if (data['urlprefix']) t.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 (!t.state.lookup[module]) t.state.lookup[module] = [];
t.state.lookup[module].push(lang);
}
}
// Initialize the language list
var lang_select = $("