kiwix-js-pwa/www/js/lib/uiUtil.js
Jaifroid 81a86d75e6 Alternate icon support
Former-commit-id: e5cb015fbe04f82fec27736a8c810bc215e4901b [formerly 9b84972b9025a3335a606d3bc7d82723ab0e1b3c]
Former-commit-id: e48ba161e919b922326632a661d0c7dc175634fa
2017-12-14 10:07:46 +00:00

151 lines
5.5 KiB
JavaScript

/**
* uiUtil.js : Utility functions for the User Interface
*
* Copyright 2013-2014 Mossroy and contributors
* License GPL v3:
*
* This file is part of Kiwix.
*
* Kiwix is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Kiwix is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kiwix (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/
'use strict';
define([], function() {
/**
* Creates a Blob from the given content, then a URL from this Blob
* And put this URL in the attribute of the DOM node
*
* This is useful to inject images (and other dependencies) inside an article
*
* @param {Object} jQueryNode
* @param {String} nodeAttribute
* @param {Uint8Array} content
* @param {String} mimeType
*/
function feedNodeWithBlob(jQueryNode, nodeAttribute, content, mimeType) {
var blob = new Blob([content], { type: mimeType }, {oneTimeOnly: true});
var url = URL.createObjectURL(blob);
/*jQueryNode.on('load', function () {
URL.revokeObjectURL(url);
});*/
jQueryNode.attr(nodeAttribute, url);
}
var regexpRemoveUrlParameters = new RegExp(/([^\?]+)\?.*$/);
function removeUrlParameters(url) {
if (regexpRemoveUrlParameters.test(url)) {
return regexpRemoveUrlParameters.exec(url)[1];
} else {
return url;
}
}
function TableOfContents(articleDoc) {
this.doc = articleDoc;
this.headings = this.doc.querySelectorAll("h1, h2, h3, h4, h5, h6");
this.getHeadingObjects = function () {
var headings = [];
for (var i = 0; i < this.headings.length; i++) {
var element = this.headings[i];
var obj = {};
obj.id = element.id;
var objectId = element.innerHTML.match(/\bid\s*=\s*["']\s*([^"']+?)\s*["']/i);
obj.id = obj.id ? obj.id : objectId && objectId.length > 1 ? objectId[1] : "";
obj.index = i;
obj.textContent = element.textContent;
obj.tagName = element.tagName;
headings.push(obj);
}
return headings;
}
}
/**
* Checks whether an element is fully or partially in view
* This is useful for progressive download of images inside an article
*
* @param {Object} el
* @param {Boolean} fully
*/
function isElementInView(el, fully) {
var elemTop = el.getBoundingClientRect().top;
var elemBottom = el.getBoundingClientRect().bottom;
var isVisible = fully ? elemTop < window.innerHeight && elemBottom >= 0 :
elemTop >= 0 && elemBottom <= window.innerHeight;
return isVisible;
}
function makeReturnLink(title) {
//Abbreviate title if necessary
var shortTitle = title.substring(0, 25);
shortTitle = shortTitle == title ? shortTitle : shortTitle + "...";
var link = '<h4 style="font-size:' + ~~(params.relativeUIFontSize * 1.4 * 0.14) + 'px;"><a href="#">&lt;&lt; Return to ' + shortTitle + '</a></h4>';
var rtnFunction = `(function () {
$('#configuration').hide();
$('#about').hide();
if (params.cssTheme == "light" && params.cssUITheme == "dark") {
document.getElementById('search-article').classList.remove("dark");
document.getElementById('findInArticle').classList.remove("dark");
document.getElementById('prefix').classList.remove("dark");
}
$('#formArticleSearch').show();
$('#articleContent').show();
$('#liHomeNav').attr('class', 'active');
$('#liConfigureNav').attr('class', '');
$('#liAboutNav').attr('class', '');
document.getElementById('btnConfigure').classList.remove('active');
document.getElementById('btnAbout').classList.remove('active');
document.getElementById('search-article').style.overflow = "hidden";
checkToolbar();
document.getElementById('search-article').scrollTop = 0;
if (params.themeChanged) {
params.themeChanged = false;
if (history.state !== null) {
var thisURL = decodeURIComponent(history.state.title);
goToArticle(thisURL);
}
}
})`;
document.getElementById("returntoArticle_top").innerHTML = link;
document.getElementById("returntoArticle_bottom").innerHTML = link;
return rtnFunction;
}
function poll(msg) {
document.getElementById("progressMessage").innerHTML += '<p>' + msg + '</p>';
}
function clear() {
document.getElementById("progressMessage").innerHTML = "<p></p>";
}
/**
* Functions and classes exposed by this module
*/
return {
feedNodeWithBlob: feedNodeWithBlob,
removeUrlParameters: removeUrlParameters,
toc: TableOfContents,
isElementInView: isElementInView,
makeReturnLink: makeReturnLink,
poll: poll,
clear: clear
};
});