repoindex/web/js/pagemanagement.js

356 lines
15 KiB
JavaScript

var repoindex = (function(repoindex) {
repoindex.Pages = {
Packages: "packages",
Groups:"groups",
Repositories: "repositories"
};
var checkVisibility = function(elm, evalType) {
evalType = evalType || "visible";
var vpH = $(window).height(),
st = $(window).scrollTop(),
y = $(elm).offset().top,
elementHeight = $(elm).height();
if (evalType === "visible") return ((y < (vpH + st)) && (y > (st - elementHeight)));
if (evalType === "above") return ((y < (vpH + st)));
};
var PageManager = function() {
// basic intialization
this.currentPage = undefined;
this.currentManager = undefined;
this.repoManager = new repoindex.RepoEntryManager(new repoindex.Pagination("repos_pagination"));
this.packageManager = new repoindex.PackageEntryManager(new repoindex.Pagination("packages_pagination"), this.repoManager.entries);
this.groupManager = new repoindex.GroupEntryManager(new repoindex.Pagination("groups_pagination"), this.repoManager.entries);
this.internalHashChange = false;
// get document elements
this.errorBox = document.getElementById("error_box");
this.errorList = document.getElementById("error_list");
this.leftColumnContainerPackages = document.getElementById("left_column_container_packages");
this.rightColumnContainerPackages = document.getElementById("right_column_container_packages");
this.leftColumnContainerRepos = document.getElementById("left_column_container_repos");
this.rightColumnContainerRepos = document.getElementById("right_column_container_repos");
this.packageInfoTabbing = undefined;
this.repoInfoTabbing = undefined;
// provide a function to add an error message (shown in red box)
this.addError = function(msg) {
this.errorBox.style.display = "block";
var errorLiElement = document.createElement('li');
errorLiElement.appendChild(document.createTextNode(msg));
this.errorList.appendChild(errorLiElement);
};
// provide a function to remove the error log
this.hideErrors = function() {
this.errorBox.style.display = "none";
this.errorList.wipeChildren();
};
// provide a function to set the connection status (to be called by the web socket client)
this.setConnectionStatus = function(status) {
var addClass, removeClass, animation = false;
switch(status) {
case repoindex.ConnectionStatus.Disconnected:
addClass = "btn-danger";
removeClass = "btn-info btn-success";
break;
case repoindex.ConnectionStatus.Connecting:
addClass = "btn-info";
removeClass = "btn-danger btn-success";
animation = true;
break;
case repoindex.ConnectionStatus.Connected:
addClass = "btn-success";
removeClass = "btn-info btn-danger";
break;
}
$("#nav_connect").removeClass(removeClass).addClass(addClass);
$("#connection_status").text(status);
$("#connection_glyphicon").toggleClass("glyphicon-refresh-animate", animation);
};
// provide a function to change the current page
this.setPage = function(page, params, toggleNavbar) {
if(page !== this.currentPage) {
// display the specified page
for(var pageType in repoindex.Pages) {
if(pageType !== undefined && repoindex.Pages.hasOwnProperty(pageType)) {
var pageName = repoindex.Pages[pageType];
if(page === pageName) {
$("#page_" + pageName).css("display", "block");
$("#nav_" + pageName).addClass("active");
} else {
$("#page_" + pageName).css("display", "none");
$("#nav_" + pageName).removeClass("active");
}
}
}
// toggle navbar
if(toggleNavbar) {
if($(".navbar-toggle").css("display") !== "none") {
$(".navbar-toggle").click();
}
}
// determine manager and params for current page
var mgr;
switch(page) {
case repoindex.Pages.Packages:
mgr = this.packageManager;
break;
case repoindex.Pages.Groups:
mgr = this.groupManager;
break;
case repoindex.Pages.Repositories:
mgr = this.repoManager;
break;
}
this.currentManager = mgr;
}
this.initPage(this.currentPage = page, params);
};
// initializes a page
// initialization consits of:
// - request required information from ALPM client
// - apply specified params
// - denote current configuration if no params specified
this.initPage = function(pageName, params) {
// variables to store the current configuration
var currentRepo, currentPkg;
// basic repo info is required for all pages
if(!repoindex.client.hasBasicRepoInfo) {
repoindex.client.requestBasicRepoInfo();
}
switch(pageName) {
case repoindex.Pages.Packages:
this.repoManager.buttonRow.style.display = "block";
this.packageManager.update();
if(params && params.repo && params.pkg) {
if(params.down) {
switch(params.down) {
case "pkg":
this.packageManager.showMirrors(params.repo, params.pkg);
break;
default:
; // ignore unknown download target
}
} else {
this.packageManager.showPackageInfo(params.repo, params.pkg);
}
} else if(this.packageManager.currentInfo) {
currentRepo = this.packageManager.currentInfo.repo;
currentPkg = this.packageManager.currentInfo.name;
}
break;
case repoindex.Pages.Groups:
this.repoManager.buttonRow.style.display = "block";
if(!repoindex.client.hasGroupInfo) {
repoindex.client.requestGroupInfo();
}
this.groupManager.update();
break;
case repoindex.Pages.Repositories:
this.repoManager.buttonRow.style.display = "none";
if(params && params.repo) {
this.repoManager.showRepoInfo(params.repo);
if(params.invoke) {
if(params.invoke === "upgradelookup") {
// check for upgrades
this.repoManager.showAvailableUpgrades(params.repo);
}
}
} else if(this.repoManager.currentInfo) {
currentRepo = this.repoManager.currentInfo.name;
}
break;
default:
;
}
if(params) {
if(this.currentManager) {
this.currentManager.pagination.selectPage(params ? params.p : 0);
}
} else {
this.rehash(function(hash) {
hash[0] = pageName;
var params = hash[1];
params.p = repoindex.pageManager.currentManager ? repoindex.pageManager.currentManager.pagination.currentPageIndex() : undefined;
params.repo = currentRepo;
params.pkg = currentPkg;
delete params.invoke;
});
}
};
this.reinitCurrentPage = function() {
if(this.currentPage) {
this.initPage(this.currentPage);
}
};
// provide a function to set the current page according the #hash
this.gotoHash = function(toggleNavbar) {
var hash = window.location.hash;
if(hash.length < 2) {
this.setPage(repoindex.Pages.Packages, undefined, toggleNavbar);
} else {
var hashParts = repoindex.parseHash(hash);
this.setPage(hashParts[0], hashParts[1], toggleNavbar);
}
};
// provide a method to denote a hash change internally
this.denoteHash = function(pageName, params) {
var newHash = repoindex.makeHash(pageName, params);
if("#" + newHash !== window.location.hash) {
this.internalHashChange = true;
window.location.hash = newHash;
}
};
this.rehash = function(func) {
var currentHash = repoindex.parseHash(window.location.hash);
if(!currentHash[1]) {
currentHash[1] = {};
}
func(currentHash);
repoindex.pageManager.denoteHash(currentHash[0], currentHash[1]);
};
// remove the download parameter from the hash when the mirror dlg has been closed
$("#dlg_mirror_selection").on('hidden.bs.modal', function () {
repoindex.pageManager.rehash(function(hash) { delete hash[1].down; });
});
// goto hash when it has changed non-internally
window.onhashchange = function() {
if(!repoindex.pageManager.internalHashChange) {
repoindex.pageManager.gotoHash(true);
} else {
repoindex.pageManager.internalHashChange = undefined;
}
};
// provide a function to apply an entered search term
this.applySearchTerm = function(searchTerm, exact, explcit, reset) {
if(this.currentManager) {
if(reset) {
this.currentManager.customSelection = undefined;
}
this.currentManager.filterName = searchTerm;
this.currentManager.filterNameExact = exact;
this.currentManager.filterDescr = searchTerm ? (exact ? "with the name" : "for the search term") : undefined;
this.currentManager.invalidate(true);
if(searchTerm && !this.currentManager.customSelection && this.repoManager.entryByName("AUR").enabled) {
repoindex.client.requestSuggestions(["AUR"], searchTerm);
}
}
};
this.hideRightColumn = function(leftColumnContainer, rightColumnContainer) {
if(rightColumnContainer.style.display !== "none") {
rightColumnContainer.style.display = "none";
leftColumnContainer.className = rightColumnContainer.className = "";
return true;
}
return false;
};
this.showRightColumn = function(leftColumnContainer, rightColumnContainer) {
if(rightColumnContainer.style.display !== "block") {
rightColumnContainer.style.display = "block";
leftColumnContainer.className = "col-md-8";
rightColumnContainer.className = "col-md-4";
return true;
}
return false;
};
this.createPackageInfoPane = function(entry, newTab) {
if(!this.packageInfoTabbing) {
this.packageInfoTabbing = new repoindex.Tabbing(this.rightColumnContainerPackages);
this.packageInfoTabbing.tabRemoved = function(tab) {
if(this.tabs.length === 0) {
repoindex.pageManager.hidePackageInfo();
}
};
}
var tabElement = this.packageInfoTabbing.addTab(repoindex.makeElementId([entry.info.repo, entry.name].join("--sep--")), entry.name, true, newTab !== true);
tabElement.activate();
return tabElement.paneElement;
};
this.hidePackageInfo = function() {
this.rehash(function(hash) {
hash[1].repo = hash[1].pkg = undefined;
});
this.packageManager.currentInfo = undefined;
this.hideRightColumn(this.leftColumnContainerPackages, this.rightColumnContainerPackages);
};
this.showPackageInfo = function(scroll) {
// ensure packages page is visible and hash is set
this.setPage(repoindex.Pages.Packages);
// ensure right column is visible
this.showRightColumn(this.leftColumnContainerPackages, this.rightColumnContainerPackages);
//var tb = document.getElementById("pkg_tbody");
//if(tb.lastChild) {
// if(scroll && !checkVisibility(tb.lastChild)) {
// $('html, body').animate({
// scrollTop: $("#" + this.rightColumnContainerPackages.id).offset().top
// }, 500);
// }
//}
};
this.createRepoInfoPane = function(entry, newTab) {
if(!this.repoInfoTabbing) {
this.repoInfoTabbing = new repoindex.Tabbing(this.rightColumnContainerRepos);
this.repoInfoTabbing.tabRemoved = function(tab) {
if(this.tabs.length === 0) {
repoindex.pageManager.hideRepoInfo();
}
};
}
var tabElement = this.repoInfoTabbing.addTab(repoindex.makeElementId(entry.name), entry.name, true, newTab !== true);
tabElement.activate();
return tabElement.paneElement;
};
this.hideRepoInfo = function() {
this.rehash(function(hash) {
hash[1].repo = hash[1].pkg = undefined;
});
this.repoManager.currentInfo = undefined;
this.hideRightColumn(this.leftColumnContainerRepos, this.rightColumnContainerRepos);
};
this.showRepoInfo = function(name, scroll) {
// ensure repo page is visible and has is set
this.setPage(repoindex.Pages.Repositories);
// ensure right column is visible
this.showRightColumn(this.leftColumnContainerRepos, this.rightColumnContainerRepos);
};
this.msgboxCritical = function(title, msg, htmlTags) {
repoindex.setText("msgbox_critical_title", title, htmlTags);
repoindex.setText("msgbox_critical_msg", msg, htmlTags);
$("#msgbox_critical").modal("show");
};
$("#link_settings").popover();
};
// create a page manager object used within the entire document
repoindex.pageManager = new PageManager();
repoindex.client.init();
repoindex.pageManager.gotoHash(false);
return repoindex;
})(repoindex || {});