kiwix-js-pwa/www/js/lib/uiUtil.js
Jaifroid 0bb42e2d2a Implemented UWP future access list to remember chosen folders
Former-commit-id: 3244a046d1481905d2ad3136c715b29a9fb8e34a [formerly 0404b20e8f4633de950990fbb1ca3db59476b2f3]
Former-commit-id: 9a3d067bb2461120aecf63908d37725c6de8391c
2017-08-22 20:54:55 +01:00

125 lines
4.4 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;
obj.id = obj.id ? obj.id : element.innerHTML.match(/\bid\s*=\s*["']\s*([^"']+?)\s*["']/i)[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><a href="#" onclick="$(\'#configuration\').hide();$(\'#formArticleSearch\').show();' +
'$(\'#articleContent\').show();$(\'#liHomeNav\').attr(\'class\',\'active\');$(\'#liConfigureNav\').attr(\'class\', \'\');' +
'$(\'#liAboutNav\').attr(\'class\', \'\');document.getElementById(\'btnConfigure\').classList.remove(\'active\');">&lt;&lt; Return to ' + shortTitle + '</a></h4>';
document.getElementById("returntoArticle_top").innerHTML = link;
document.getElementById("returntoArticle_bottom").innerHTML = link;
}
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
};
});