")
.attr('id', 'recent')
.appendTo("#languages");
// Initialize API results
t.initAPI();
// Done
t.hideLoading();
},
error: function(){
t.hideLoading();
alert("Error getting index. Try reloading page, or just give up.");
}
});
// ===== Loading functionality =====
// Initialize the module list
this.initModules = function(lang){
t.state.index['languages'][lang] = t.state.index['languages'][lang].sort();
$("#modules").empty();
for (var i in t.state.index['languages'][lang]) {
var module = t.state.index['languages'][lang][i];
if (!module) continue;
$('
')
.html(module)
.attr('href', "#"+lang+"/"+module+".gf")
.appendTo("#modules");
}
}
// Load both scope & source for a file
this.loadFile = function(lang, module, lineNo){
t.setTitle(lang+"/"+module);
t.state.current.set(lang, module);
t.loadTagsFile(module);
t.loadSourceFile(lang, module, lineNo);
if ($('.tab.api').hasClass('active'))
t.showPanel("#scope");
t.addRecent(lang, module);
}
// Add item to recent list
this.addRecent = function(lang, module) {
var full_module = lang+'/'+module;
// If already there, do nothing
if ($('#recent').text().indexOf(full_module) > -1) return;
// Delete oldest if at limit
if ($('#recent a').length >= t.state.recentCount) {
$('#recent a').last().remove();
}
// Add it
$('')
.html(full_module)
.attr('href', "#"+lang+"/"+module+".gf")
.prependTo("#recent");
}
// Load a tags file
this.loadTagsFile = function(module) {
t.clearScope();
t.showLoading();
$.ajax({
url: "tags/"+module+".gf-tags",
type: "GET",
dataType: "text",
success: function(data){
data = data.replace(/^(\S+)\s(\S+)\s(.+)?$/gm, function(a,b,c,d){
var s = d.split("\t");
if (c == "indir") {
var module = s[2].substring(s[2].lastIndexOf('/')+1, s[2].lastIndexOf('.'));
var lang = t.lookupModuleLanguage(module);
var name = lang+"/"+module;
var url = "#"+lang+"/"+module;
var anchor = ''+name+'';
return '
| '+b+' | '+c+' | '+s[0]+' | '+s[1]+' | '+anchor+' | |
|---|
'
} 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 = '
'+name+'';
return '
| '+b+' | '+c+' | | | '+anchor+' | '+s[1]+' |
|---|
'
}
});
t.setScope(data);
t.runFilter();
t.hideLoading();
},
error: function(data){
t.clearScope("No scope available");
t.hideLoading();
},
});
}
// Load a source module
this.loadSourceFile = function(lang, module, lineNo) {
t.clearCode();
t.showLoading();
$.ajax({
url: t.state.urlPrefix + "lib/src/"+lang+"/"+module+".gf",
type: "GET",
dataType: "text",
success: function(data, status, xhr){
t.setCode(data);
t.hideLoading();
if (lineNo) {
t.scrollToCodeLine(lineNo);
}
},
error: function(data){
t.clearCode("No code available");
t.hideLoading();
}
});
}
// Which modules do we include for API?
this.apiModules = [
// api
"Syntax",
"Constructors", "Cat", "Structural", "Combinators",
// abstract
// "Adjective",
// "Adverb",
// "Backward",
// "Cat",
// "Common",
// "Compatibility",
// "Conjunction",
// "Extra",
// "Grammar",
// "Idiom",
// "Lang",
// "Lexicon",
// "Noun",
// "Numeral",
// "NumeralTransfer",
// "Phrase",
// "Question",
// "Relative",
// "Sentence",
// "Structural",
// "Symbol",
// "Tense",
// "Text",
// "Transfer",
// "Verb",
];
this.initAPI = function() {
t.showLoading();
$('#api #results').empty();
for (var i in t.apiModules) {
var module = t.apiModules[i];
$.ajax({
url: "tags/"+module+".gf-tags",
type: "GET",
dataType: "text",
success: function(data){
data = data.replace(/^(\S+)\s(\S+)\s(.+)?$/gm, function(a,b,c,d){
var out = '';
var s = d.split("\t");
if (c != "indir") {
var type = s[1];
if (type) {
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 = '
'+name+'';
out += '
| '+b+' | '+c+' | '+anchor+' | '+s[1]+' |
|---|
'
}
}
return out;
});
$('#api #results').append($(data));
$("#api #results tr").removeClass('odd');
$("#api #results tr:odd").addClass('odd');
$('#api #count').text( $("#api #results tr").length );
},
error: function(data){
console.log("Error loading tags file: " + module);
},
});
}
t.hideLoading();
}
// ===== Module search =====
this.searchModule = function(s) {
if (!s) {
return t.clearSearchModule();
}
$('#language_select').hide();
$("#modules").empty();
for (var lang in t.state.index['languages']) {
var modules = t.state.index['languages'][lang];
for (var j in modules) {
var module = modules[j];
var full_module = lang+'/'+module;
if (!module) continue;
if (full_module.toLowerCase().indexOf(s.toLowerCase())==-1) continue;
$('
')
.html(full_module)
.attr('href', "#"+lang+"/"+module+".gf")
.appendTo("#modules");
}
}
};
this.clearSearchModule = function() {
$('#module_search').val('');
$('#language_select').show();
t.setLanguage(t.state.language);
return false;
};
// ===== Filtering of scope info =====
// Custom selector
$.expr[':'].match = function(a,b,c) {
var obj = $(a);
var needle = c[3];
var haystack = obj.attr('name');
if (haystack == undefined)
return false;
if ($("#scope #case_sensitive").is(":checked"))
return haystack.indexOf(needle)>=0;
else
return haystack.toLowerCase().indexOf(needle.toLowerCase())>=0;
};
this.runFilter = function() {
t.showLoading();
$("#scope #results tr").removeClass('odd');
var s = $("#scope #search").val();
try {
if (s) {
$("#scope #results tr").hide();
$("#scope #results tr:match(\""+s+"\")").show();
} else {
$("#scope #results tr").show();
}
if ($("#scope #show_local").is(":checked") ) {
$("#scope #results tr.indir").hide();
}
} catch (error) {
alert(error.message);
}
t.updateScopeCount();
$("#scope #results tr:visible:odd").addClass('odd');
t.hideLoading();
}
// Instant results
this.prevSearch = $("#scope #search").val();
$("#scope #search").keyup(function(){
var s = $("#scope #search").val();
if (s!=t.prevSearch) {
t.runFilter();
t.prevSearch = s;
}
});
$("#scope #search").keypress(function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { // Enter
t.runFilter();
}
});
$("#scope #clear").click(function(){
$("#scope #search")
.val('')
.focus()
t.runFilter();
});
$("#scope #case_sensitive").change(t.runFilter);
$("#scope #show_all").change(t.runFilter);
$("#scope #show_local").change(t.runFilter);
// ===== API search =====
// Custom selector
$.expr[':'].matchAPI = function(a,b,c) {
var obj = $(a); // tr
var ident = $(obj.children().get(0)).text();
var type = $(obj.children().get(3)).text();
var needle = c[3];
var match_ident = ident.toLowerCase().indexOf(needle.toLowerCase())>=0;
var match_type = type.toLowerCase().indexOf(needle.toLowerCase())>=0;
// if ($("#scope #case_sensitive").is(":checked"))
// return haystack.indexOf(needle)>=0;
// else
return match_ident || match_type ;
};
this.runFilterAPI = function() {
t.showLoading();
$("#api #results tr").removeClass('odd');
var s = $("#api #search").val();
try {
if (s) {
$("#api #results tr").hide();
$("#api #results tr:matchAPI(\""+s+"\")").show();
} else {
$("#api #results tr").show();
}
} catch (error) {
alert(error.message);
}
t.updateAPICount();
$("#api #results tr:visible:odd").addClass('odd');
t.hideLoading();
}
// Instant results
this.prevAPISearch = $("#api #search").val();
$("#api #search").keyup(function(){
var s = $("#api #search").val();
if (s!=t.prevAPISearch) {
t.runFilterAPI();
t.prevAPISearch = s;
}
});
$("#api #search").keypress(function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { // Enter
t.runFilterAPI();
}
});
$("#api #clear").click(function(){
$("#api #search")
.val('')
.focus();
t.runFilterAPI();
});
};