Remove code redundancy

Former-commit-id: 6bb1390785b6e1ca55be574fac5e2ff60b6b2e9d [formerly ddfb7d2cb274fc093cd006195384a73b671cd8f7]
Former-commit-id: d3217bb06963ff7d1f743b657ab0e926fe3393e8
This commit is contained in:
Jaifroid 2018-02-26 08:57:59 +00:00
parent 727c64c950
commit 4f779929d4

View File

@ -145,14 +145,6 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
return false; return false;
} }
}); });
//Also have to listen to iframe key presses
document.getElementById('articleContent').contentWindow.addEventListener('keyup', function (e) {
//Alt-F for search in article, also patches Ctrl-F for apps that do not have access to browser search
if ((e.ctrlKey || e.altKey) && e.which == 70) {
$('#findText').click();
return false;
}
}), false;
var localSearch = {}; var localSearch = {};
var firstRun = false; var firstRun = false;
@ -1084,7 +1076,6 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
*/ */
function setLocalArchiveFromArchiveList(archiveDirectory) { function setLocalArchiveFromArchiveList(archiveDirectory) {
archiveDirectory = archiveDirectory || $('#archiveList').val(); archiveDirectory = archiveDirectory || $('#archiveList').val();
document.getElementById('kiwixIcon').src = /wikivoyage/i.test(archiveDirectory) ? params.cssUITheme == "light" ? "./img/icons/wikivoyage-black-32.png" : "./img/icons/wikivoyage-white-32.png" : /medicine/i.test(archiveDirectory) ? params.cssUITheme == "light" ? "./img/icons/wikimed-blue-32.png" : "./img/icons/wikimed-lightblue-32.png" : params.cssUITheme == "light" ? "./img/icons/kiwix-blue-32.png" : "./img/icons/kiwix-32.png";
if (archiveDirectory && archiveDirectory.length > 0) { if (archiveDirectory && archiveDirectory.length > 0) {
// Now, try to find which DeviceStorage has been selected by the user // Now, try to find which DeviceStorage has been selected by the user
// It is the prefix of the archive directory // It is the prefix of the archive directory
@ -1112,9 +1103,6 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
selectedStorage = storages[0]; selectedStorage = storages[0];
} }
else { //IT'S NOT FREAKIN FFOS!!!!!!!!!! else { //IT'S NOT FREAKIN FFOS!!!!!!!!!!
//console.log("Something weird happened with the DeviceStorage API : found a directory without prefix : "
// + archiveDirectory + ", but there were " + storages.length
// + " storages found with getDeviceStorages instead of 1");
//Patched for UWP support: //Patched for UWP support:
if (params.pickedFolder && Windows && Windows.Storage) { if (params.pickedFolder && Windows && Windows.Storage) {
var query = params.pickedFolder.createFileQuery(); var query = params.pickedFolder.createFileQuery();
@ -1145,47 +1133,7 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
} }
} }
if (fileset && fileset.length) { if (fileset && fileset.length) {
//@TODO: check if this is redundant setLocalArchiveFromFileList(fileset);
//if (archiveDirectory != params.storedFile) {
// params.storedFile = String(archiveDirectory);
// cookies.setItem("lastSelectedArchive", params.storedFile, Infinity);
//}
selectedStorage = fileset;
archiveDirectory = "";
// Reset the cssDirEntryCache and cssBlobCache. Must be done when archive changes.
if (cssBlobCache)
cssBlobCache = new Map();
//if (cssDirEntryCache)
// cssDirEntryCache = new Map();
selectedArchive = zimArchiveLoader.loadArchiveFromDeviceStorage(selectedStorage, archiveDirectory, function (archive) {
// The archive is set : go back to home page to start searching
params.storedFile = archive._file._files[0].name;
cookies.setItem('lastSelectedArchive', params.storedFile, Infinity);
var reloadLink = document.getElementById('reloadPackagedArchive');
if (reloadLink) {
if (params.packagedFile != archive._file._files[0].name) {
reloadLink.style.display = 'inline';
reloadLink.removeEventListener('click', loadPackagedArchive);
reloadLink.addEventListener('click', loadPackagedArchive);
} else {
reloadLink.style.display = 'none';
document.getElementById('currentArchive').style.display = 'none';
}
}
if (params.rescan) {
$('#btnConfigure').click();
$('#btnConfigure').click();
params.rescan = false;
} else {
$('#openLocalFiles').hide();
if (params.rememberLastPage && ~params.lastPageVisit.indexOf(selectedArchive._file._files[0].name)) {
var lastPage = decodeURIComponent(params.lastPageVisit.replace(/@kiwixKey@.+/, ""));
goToArticle(lastPage);
} else {
$('#btnHome').click();
}
}
});
} else { } else {
console.error("The picked file could not be found in the selected folder!"); console.error("The picked file could not be found in the selected folder!");
var archiveList = []; var archiveList = [];
@ -1220,6 +1168,11 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
} }
} }
} }
// Reset the cssDirEntryCache and cssBlobCache. Must be done when archive changes.
if (cssBlobCache)
cssBlobCache = new Map();
//if (cssDirEntryCache)
// cssDirEntryCache = new Map();
selectedArchive = zimArchiveLoader.loadArchiveFromDeviceStorage(selectedStorage, archiveDirectory, function (archive) { selectedArchive = zimArchiveLoader.loadArchiveFromDeviceStorage(selectedStorage, archiveDirectory, function (archive) {
cookies.setItem("lastSelectedArchive", archiveDirectory, Infinity); cookies.setItem("lastSelectedArchive", archiveDirectory, Infinity);
// The archive is set : go back to home page to start searching // The archive is set : go back to home page to start searching
@ -1340,7 +1293,7 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
cookies.setItem("lastSelectedArchive", params.storedFile, Infinity); cookies.setItem("lastSelectedArchive", params.storedFile, Infinity);
var reloadLink = document.getElementById("reloadPackagedArchive"); var reloadLink = document.getElementById("reloadPackagedArchive");
if (reloadLink) { if (reloadLink) {
if (params.packagedFile != archive._file._files[0].name) { if (params.packagedFile != params.storedFile) {
reloadLink.style.display = "inline"; reloadLink.style.display = "inline";
reloadLink.removeEventListener("click", loadPackagedArchive); reloadLink.removeEventListener("click", loadPackagedArchive);
reloadLink.addEventListener("click", loadPackagedArchive); reloadLink.addEventListener("click", loadPackagedArchive);
@ -1349,11 +1302,20 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
document.getElementById('currentArchive').style.display = 'none'; document.getElementById('currentArchive').style.display = 'none';
} }
} }
//This ensures the correct icon is set for the newly loaded archive
cssUIThemeSet(params.cssUITheme);
if (params.rescan) {
$('#btnConfigure').click();
$('#btnConfigure').click();
params.rescan = false;
} else {
$('#openLocalFiles').hide();
if (params.rememberLastPage && ~params.lastPageVisit.indexOf(params.storedFile)) { if (params.rememberLastPage && ~params.lastPageVisit.indexOf(params.storedFile)) {
var lastPage = decodeURIComponent(params.lastPageVisit.replace(/@kiwixKey@.+/, "")); var lastPage = decodeURIComponent(params.lastPageVisit.replace(/@kiwixKey@.+/, ""));
goToArticle(lastPage); goToArticle(lastPage);
} else { } else {
$("#btnHome").click(); $('#btnHome').click();
}
} }
}); });
} }
@ -1711,7 +1673,7 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
//Replace inline Math TeX with dummy images //Replace inline Math TeX with dummy images
//if (params.useMathJax) htmlArticle = htmlArticle.replace(/\$\$?((?:[^$<>]|<\s|\s>)+)\$\$?([\s<.,;:?!'")\]])/g, "<img alt='$1' class='mwe-math-fallback-image' />$2"); //if (params.useMathJax) htmlArticle = htmlArticle.replace(/\$\$?((?:[^$<>]|<\s|\s>)+)\$\$?([\s<.,;:?!'")\]])/g, "<img alt='$1' class='mwe-math-fallback-image' />$2");
//Test for raw TeX in the HTML - no need to convert because configuration for inline will be set by a script in the Stackexchange HTML //Test for raw TeX in the HTML - no need to convert because configuration for inline will be set by a script in the Stackexchange HTML
containsMathTeXRaw = params.useMathJax ? /\$\$?((?:[^$<>]|<\s|\s>)+)\$\$?([\s<.,;:?!'")\]])/.test(htmlArticle) : false; containsMathTeXRaw = params.useMathJax && !/wikivoyage/.test(params.storedFile) ? /\$\$?((?:[^$<>]|<\s|\s>)+)\$\$?([\s<.,;:?!'")\]])/.test(htmlArticle) : false;
//Simplify any configuration script //Simplify any configuration script
//if (containsMathTeXRaw) htmlArticle = htmlArticle.replace(/(<script\s+[^>]*?type\s*=\s*['"]\s*text\/x-mathjax-config[^>]+>[^<]+?Hub\.Config\s*\(\s*{\s*)[^<]*?(tex2jax\s*:[^}]+?})\s*,[^<]+(<\/script>)/i, "$1$2});$3"); //if (containsMathTeXRaw) htmlArticle = htmlArticle.replace(/(<script\s+[^>]*?type\s*=\s*['"]\s*text\/x-mathjax-config[^>]+>[^<]+?Hub\.Config\s*\(\s*{\s*)[^<]*?(tex2jax\s*:[^}]+?})\s*,[^<]+(<\/script>)/i, "$1$2});$3");
//Replace all TeX SVGs with MathJax scripts //Replace all TeX SVGs with MathJax scripts
@ -1923,6 +1885,15 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
} }
checkToolbar(); checkToolbar();
//Listen to iframe key presses for in-page search
document.getElementById('articleContent').contentWindow.addEventListener('keyup', function (e) {
//Alt-F for search in article, also patches Ctrl-F for apps that do not have access to browser search
if ((e.ctrlKey || e.altKey) && e.which == 70) {
$('#findText').click();
return false;
}
}), false;
// If the ServiceWorker is not useable, we need to fallback to parse the DOM // If the ServiceWorker is not useable, we need to fallback to parse the DOM
// to inject math images, and replace some links with javascript calls // to inject math images, and replace some links with javascript calls
if (contentInjectionMode === 'jquery') { if (contentInjectionMode === 'jquery') {
@ -2045,6 +2016,7 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'q', 'module'
prepareImages(); prepareImages();
} else { } else {
console.log("There are no images to display in this article."); console.log("There are no images to display in this article.");
loadMathJax();
//TESTING //TESTING
console.timeEnd("Time to Document Ready"); console.timeEnd("Time to Document Ready");
} }