2013-07-02 15:51:43 +03:00
|
|
|
// util.js
|
|
|
|
// copyright Sébastien Lucas
|
|
|
|
// https://github.com/seblucas/cops
|
|
|
|
|
2013-06-28 03:07:32 +03:00
|
|
|
var templatePage, templateBookDetail, templateMain, currentData, before, filterList;
|
2013-06-15 09:27:33 +03:00
|
|
|
|
2013-09-05 09:41:40 +03:00
|
|
|
if (typeof LRUCache!='undefined') {
|
|
|
|
var cache = new LRUCache(30);
|
|
|
|
}
|
2013-06-20 22:08:50 +03:00
|
|
|
|
2013-07-16 22:57:14 +03:00
|
|
|
var DEBUG = false;
|
2013-06-20 15:43:31 +03:00
|
|
|
var isPushStateEnabled = window.history && window.history.pushState && window.history.replaceState &&
|
|
|
|
// pushState isn't reliable on iOS until 5.
|
|
|
|
!navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]|WebApps\/.+CFNetwork)/);
|
2013-06-16 08:50:14 +03:00
|
|
|
|
2013-06-20 15:34:10 +03:00
|
|
|
function debug_log(text) {
|
|
|
|
if ( DEBUG ) {
|
|
|
|
console.log(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 09:27:29 +03:00
|
|
|
function updateCookie (id) {
|
|
|
|
if($(id).prop('pattern') && !$(id).val().match(new RegExp ($(id).prop('pattern')))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var name = $(id).attr('id');
|
|
|
|
var value = $(id).val ();
|
2013-07-04 00:36:38 +03:00
|
|
|
$.cookie(name, value, { expires: 365 });
|
2013-06-27 09:27:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateCookieFromCheckbox (id) {
|
|
|
|
var name = $(id).attr('id');
|
|
|
|
if ((/^style/).test (name)) {
|
|
|
|
name = "style";
|
|
|
|
}
|
|
|
|
if ($(id).is(":checked"))
|
|
|
|
{
|
|
|
|
if ($(id).is(':radio')) {
|
2013-07-04 00:57:53 +03:00
|
|
|
$.cookie(name, $(id).val (), { expires: 365 });
|
2013-06-27 09:27:29 +03:00
|
|
|
} else {
|
2013-07-04 00:57:53 +03:00
|
|
|
$.cookie(name, '1', { expires: 365 });
|
2013-06-27 09:27:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-04 00:57:53 +03:00
|
|
|
$.cookie(name, '0', { expires: 365 });
|
2013-06-27 09:27:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 15:34:10 +03:00
|
|
|
function elapsed () {
|
2013-07-02 15:15:10 +03:00
|
|
|
var elapsedTime = new Date () - before;
|
|
|
|
return "Elapsed : " + elapsedTime;
|
2013-06-20 15:34:10 +03:00
|
|
|
}
|
|
|
|
|
2013-06-21 22:31:49 +03:00
|
|
|
function retourMail(data, textStatus, jqXHR ) {
|
2013-07-22 22:27:32 +03:00
|
|
|
$("#mailButton :first-child").removeClass ("icon-spinner icon-spin").addClass ("icon-envelope");
|
2013-06-21 22:31:49 +03:00
|
|
|
alert (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendToMailAddress (component, dataid) {
|
2013-07-02 15:15:10 +03:00
|
|
|
var email = $.cookie ('email');
|
2013-06-21 22:31:49 +03:00
|
|
|
if (!$.cookie ('email')) {
|
2013-09-06 10:18:41 +03:00
|
|
|
email = window.prompt (currentData.c.i18n.customizeEmail, "");
|
2013-07-04 00:57:53 +03:00
|
|
|
$.cookie ('email', email, { expires: 365 });
|
2013-06-21 22:31:49 +03:00
|
|
|
}
|
|
|
|
var url = 'sendtomail.php';
|
2013-07-02 15:15:10 +03:00
|
|
|
if (currentData.databaseId) {
|
|
|
|
url = url + '?db=' + currentData.databaseId;
|
|
|
|
}
|
2013-07-22 22:27:32 +03:00
|
|
|
$("#mailButton :first-child").removeClass ("icon-envelope").addClass ("icon-spinner icon-spin");
|
2013-07-02 15:15:10 +03:00
|
|
|
$.ajax ({'url': url, 'type': 'post', 'data': { 'data': dataid, 'email': email }, 'success': retourMail});
|
2013-06-21 22:31:49 +03:00
|
|
|
}
|
|
|
|
|
2013-08-22 16:12:04 +03:00
|
|
|
function str_format () {
|
2013-06-17 23:23:58 +03:00
|
|
|
var s = arguments[0];
|
2013-06-17 23:30:09 +03:00
|
|
|
for (var i = 0; i < arguments.length - 1; i++) {
|
|
|
|
var reg = new RegExp("\\{" + i + "\\}", "gm");
|
2013-06-17 23:23:58 +03:00
|
|
|
s = s.replace(reg, arguments[i + 1]);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-06-20 12:11:01 +03:00
|
|
|
function isDefined(x) {
|
2013-07-02 15:15:10 +03:00
|
|
|
var undefinedVar;
|
|
|
|
return x !== undefinedVar;
|
2013-06-20 12:11:01 +03:00
|
|
|
}
|
|
|
|
|
2013-06-16 23:01:13 +03:00
|
|
|
function getCurrentOption (option) {
|
2013-06-20 21:31:57 +03:00
|
|
|
if (!$.cookie (option)) {
|
2013-09-06 10:18:41 +03:00
|
|
|
if (currentData && currentData.c && currentData.c.config && currentData.c.config [option]) {
|
|
|
|
return currentData.c.config [option];
|
2013-06-20 21:31:57 +03:00
|
|
|
}
|
|
|
|
}
|
2013-06-16 23:01:13 +03:00
|
|
|
return $.cookie (option);
|
|
|
|
}
|
|
|
|
|
2013-08-22 16:12:04 +03:00
|
|
|
function htmlspecialchars(str) {
|
2013-06-15 09:03:22 +03:00
|
|
|
return String(str)
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>');
|
2013-06-15 09:27:33 +03:00
|
|
|
}
|
|
|
|
|
2013-07-01 23:26:32 +03:00
|
|
|
/************************************************
|
|
|
|
* All functions needed to filter the book list by tags
|
|
|
|
************************************************
|
|
|
|
*/
|
2013-06-15 09:27:33 +03:00
|
|
|
|
2013-07-01 19:14:37 +03:00
|
|
|
function getTagList () {
|
|
|
|
var tagList = {};
|
|
|
|
$(".se").each (function(){
|
2013-07-02 15:37:37 +03:00
|
|
|
if ($(this).parents (".filtered").length > 0) { return; }
|
2013-07-01 19:14:37 +03:00
|
|
|
var taglist = $(this).text();
|
|
|
|
|
2013-07-02 15:37:37 +03:00
|
|
|
var tagarray = taglist.split (",");
|
2013-07-02 15:15:10 +03:00
|
|
|
for (var i in tagarray) {
|
2013-07-01 19:14:37 +03:00
|
|
|
var tag = tagarray [i].replace(/^\s+/g,'').replace(/\s+$/g,'');
|
|
|
|
tagList [tag] = 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return tagList;
|
|
|
|
}
|
|
|
|
|
2013-06-28 03:07:32 +03:00
|
|
|
function doFilter () {
|
|
|
|
$(".books").removeClass("filtered");
|
2013-07-08 19:02:37 +03:00
|
|
|
if (jQuery.isEmptyObject(filterList)) {
|
|
|
|
updateFilters ();
|
|
|
|
return;
|
|
|
|
}
|
2013-06-28 03:07:32 +03:00
|
|
|
|
|
|
|
$(".se").each (function(){
|
|
|
|
var taglist = ", " + $(this).text() + ", ";
|
|
|
|
var toBeFiltered = false;
|
|
|
|
for (var filter in filterList) {
|
|
|
|
var onlyThisTag = filterList [filter];
|
|
|
|
filter = ', ' + filter + ', ';
|
|
|
|
var myreg = new RegExp (filter);
|
|
|
|
if (myreg.test (taglist)) {
|
|
|
|
if (onlyThisTag === false) {
|
|
|
|
toBeFiltered = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (onlyThisTag === true) {
|
|
|
|
toBeFiltered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 15:37:37 +03:00
|
|
|
if (toBeFiltered) { $(this).parents (".books").addClass ("filtered"); }
|
2013-06-28 03:07:32 +03:00
|
|
|
});
|
2013-07-01 23:26:32 +03:00
|
|
|
updateFilters ();
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateFilters () {
|
2013-07-01 19:14:37 +03:00
|
|
|
var tagList = getTagList ();
|
2013-07-01 23:26:32 +03:00
|
|
|
|
|
|
|
// If there is already some filters then let's prepare to update the list
|
2013-07-01 19:14:37 +03:00
|
|
|
$("#filter ul li").each (function () {
|
|
|
|
var text = $(this).text ();
|
|
|
|
if (isDefined (tagList [text]) || $(this).attr ('class')) {
|
|
|
|
tagList [text] = 0;
|
|
|
|
} else {
|
|
|
|
tagList [text] = -1;
|
|
|
|
}
|
|
|
|
});
|
2013-07-01 23:26:32 +03:00
|
|
|
|
|
|
|
// Update the filter -1 to remove, 1 to add, 0 already there
|
2013-07-01 19:14:37 +03:00
|
|
|
for (var tag in tagList) {
|
|
|
|
var tagValue = tagList [tag];
|
2013-07-02 15:37:37 +03:00
|
|
|
if (tagValue === -1) {
|
2013-07-01 19:14:37 +03:00
|
|
|
$("#filter ul li:contains('" + tag + "')").remove();
|
|
|
|
}
|
2013-07-02 15:37:37 +03:00
|
|
|
if (tagValue === 1) {
|
2013-07-01 19:14:37 +03:00
|
|
|
$("#filter ul").append ("<li>" + tag + "</li>");
|
|
|
|
}
|
|
|
|
}
|
2013-07-01 23:26:32 +03:00
|
|
|
|
2013-07-08 19:02:37 +03:00
|
|
|
$("#filter ul").append ("<li>_CLEAR_</li>");
|
|
|
|
|
2013-07-01 23:26:32 +03:00
|
|
|
// Sort the list alphabetically
|
2013-07-01 19:14:37 +03:00
|
|
|
$('#filter ul li').sortElements(function(a, b){
|
|
|
|
return $(a).text() > $(b).text() ? 1 : -1;
|
|
|
|
});
|
2013-06-28 03:07:32 +03:00
|
|
|
}
|
|
|
|
|
2013-07-01 23:26:32 +03:00
|
|
|
function handleFilterEvents () {
|
|
|
|
$("#filter ul").on ("click", "li", function(){
|
|
|
|
var filter = $(this).text ();
|
2013-07-08 19:02:37 +03:00
|
|
|
if (filter === "_CLEAR_") {
|
|
|
|
filterList = {};
|
|
|
|
$("#filter ul li").removeClass ("filter-exclude");
|
|
|
|
$("#filter ul li").removeClass ("filter-include");
|
|
|
|
doFilter ();
|
|
|
|
return;
|
|
|
|
}
|
2013-07-01 23:26:32 +03:00
|
|
|
switch ($(this).attr("class")) {
|
|
|
|
case "filter-include" :
|
|
|
|
$(this).attr("class", "filter-exclude");
|
|
|
|
filterList [filter] = false;
|
|
|
|
break;
|
|
|
|
case "filter-exclude" :
|
2013-07-02 15:15:10 +03:00
|
|
|
$(this).removeClass ("filter-exclude");
|
|
|
|
delete filterList [filter];
|
2013-07-01 23:26:32 +03:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
$(this).attr("class", "filter-include");
|
|
|
|
filterList [filter] = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
doFilter ();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:15:10 +03:00
|
|
|
/************************************************
|
|
|
|
* Functions to handle Ajax navigation
|
|
|
|
************************************************
|
|
|
|
*/
|
2013-07-01 23:26:32 +03:00
|
|
|
|
|
|
|
function navigateTo (url) {
|
2013-09-06 10:18:41 +03:00
|
|
|
$("h1").append (" <i class='icon-spinner icon-spin'></i>");
|
2013-07-01 23:26:32 +03:00
|
|
|
before = new Date ();
|
|
|
|
var jsonurl = url.replace ("index", "getJSON");
|
|
|
|
var cachedData = cache.get (jsonurl);
|
|
|
|
if (cachedData) {
|
|
|
|
history.pushState(jsonurl, "", url);
|
|
|
|
updatePage (cachedData);
|
|
|
|
} else {
|
|
|
|
$.getJSON(jsonurl, function(data) {
|
|
|
|
history.pushState(jsonurl, "", url);
|
|
|
|
cache.put (jsonurl, data);
|
|
|
|
updatePage (data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 09:27:33 +03:00
|
|
|
function updatePage (data) {
|
2013-06-20 15:34:10 +03:00
|
|
|
var result;
|
2013-06-28 03:07:32 +03:00
|
|
|
filterList = {};
|
2013-10-02 17:35:20 +03:00
|
|
|
data.c = currentData.c;
|
2013-07-02 15:51:43 +03:00
|
|
|
if (false && $("section").length && currentData.isPaginated === 0 && data.isPaginated === 0) {
|
2013-06-20 15:34:10 +03:00
|
|
|
// Partial update (for now disabled)
|
|
|
|
debug_log ("Partial update");
|
|
|
|
result = templateMain (data);
|
|
|
|
$("h1").html (data.title);
|
|
|
|
$("section").html (result);
|
|
|
|
} else {
|
|
|
|
// Full update
|
|
|
|
result = templatePage (data);
|
|
|
|
$("body").html (result);
|
|
|
|
}
|
2013-06-15 09:27:33 +03:00
|
|
|
document.title = data.title;
|
2013-06-20 15:34:10 +03:00
|
|
|
currentData = data;
|
2013-10-02 17:35:20 +03:00
|
|
|
setTimeout( function() { $("input[name=query]").focus(); }, 500 );
|
2013-06-20 15:34:10 +03:00
|
|
|
|
|
|
|
debug_log (elapsed ());
|
2013-06-15 09:27:33 +03:00
|
|
|
|
2013-07-04 00:36:38 +03:00
|
|
|
if ($.cookie('toolbar') === '1') { $("#tool").show (); }
|
2013-07-02 15:37:37 +03:00
|
|
|
if (currentData.containsBook === 1) {
|
2013-06-20 15:34:10 +03:00
|
|
|
$("#sortForm").show ();
|
2013-07-04 00:36:38 +03:00
|
|
|
if (getCurrentOption ("html_tag_filter") === "1") {
|
|
|
|
$("#filter ul").empty ();
|
|
|
|
updateFilters ();
|
|
|
|
handleFilterEvents ();
|
|
|
|
}
|
2013-06-20 15:34:10 +03:00
|
|
|
} else {
|
|
|
|
$("#sortForm").hide ();
|
|
|
|
}
|
2013-07-16 22:56:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleLinks () {
|
|
|
|
$("body").on ("click", "a[href^='index']", link_Clicked);
|
|
|
|
$("body").on ("submit", "#searchForm", search_Submitted);
|
|
|
|
$("body").on ("click", "#sort", function(){
|
2013-06-16 23:01:13 +03:00
|
|
|
$('.books').sortElements(function(a, b){
|
|
|
|
var test = 1;
|
2013-07-02 15:37:37 +03:00
|
|
|
if ($("#sortorder").val() === "desc")
|
2013-06-16 23:01:13 +03:00
|
|
|
{
|
|
|
|
test = -1;
|
|
|
|
}
|
|
|
|
return $(a).find ("." + $("#sortchoice").val()).text() > $(b).find ("." + $("#sortchoice").val()).text() ? test : -test;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-07-16 22:56:55 +03:00
|
|
|
$("body").on ("click", ".headright", function(){
|
2013-06-16 16:08:47 +03:00
|
|
|
if ($("#tool").is(":hidden")) {
|
|
|
|
$("#tool").slideDown("slow");
|
2013-09-11 11:26:05 +03:00
|
|
|
$("input[name=query]").focus();
|
2013-07-04 00:57:53 +03:00
|
|
|
$.cookie('toolbar', '1', { expires: 365 });
|
2013-06-16 16:08:47 +03:00
|
|
|
} else {
|
|
|
|
$("#tool").slideUp();
|
|
|
|
$.removeCookie('toolbar');
|
|
|
|
}
|
|
|
|
});
|
2013-07-16 22:56:55 +03:00
|
|
|
$("body").magnificPopup({
|
|
|
|
delegate: '.fancycover', // child items selector, by clicking on it popup will open
|
|
|
|
type: 'image',
|
|
|
|
gallery:{enabled:true, preload: [0,2]},
|
|
|
|
disableOn: function() {
|
|
|
|
if( getCurrentOption ("use_fancyapps") === "1" ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function link_Clicked (event) {
|
|
|
|
var currentLink = $(this);
|
|
|
|
if (!isPushStateEnabled ||
|
|
|
|
currentData.page === "19") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
var url = currentLink.attr('href');
|
2013-06-16 16:08:47 +03:00
|
|
|
|
2013-07-29 09:23:33 +03:00
|
|
|
if ($(".mfp-ready").length)
|
|
|
|
{
|
|
|
|
$.magnificPopup.close();
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:56:55 +03:00
|
|
|
// The bookdetail / about should be displayed in a lightbox
|
|
|
|
if (getCurrentOption ("use_fancyapps") === "1" &&
|
|
|
|
(currentLink.hasClass ("fancydetail") || currentLink.hasClass ("fancyabout"))) {
|
|
|
|
before = new Date ();
|
|
|
|
var jsonurl = url.replace ("index", "getJSON");
|
|
|
|
$.getJSON(jsonurl, function(data) {
|
2013-10-02 17:35:20 +03:00
|
|
|
data.c = currentData.c;
|
2013-07-16 22:56:55 +03:00
|
|
|
var detail = "";
|
|
|
|
if (data.page === "16") {
|
|
|
|
detail = data.fullhtml;
|
|
|
|
} else {
|
|
|
|
detail = templateBookDetail (data);
|
|
|
|
}
|
|
|
|
$.magnificPopup.open({
|
|
|
|
items: {
|
|
|
|
src: detail,
|
|
|
|
type: 'inline'
|
|
|
|
}
|
2013-06-15 17:09:37 +03:00
|
|
|
});
|
2013-07-16 22:56:55 +03:00
|
|
|
debug_log (elapsed ());
|
2013-06-15 17:09:37 +03:00
|
|
|
});
|
2013-07-16 22:56:55 +03:00
|
|
|
return;
|
2013-06-16 23:01:13 +03:00
|
|
|
}
|
2013-07-16 22:56:55 +03:00
|
|
|
navigateTo (url);
|
2013-06-15 09:27:33 +03:00
|
|
|
}
|
|
|
|
|
2013-07-16 22:56:55 +03:00
|
|
|
function search_Submitted (event) {
|
|
|
|
if (!isPushStateEnabled ||
|
|
|
|
currentData.page === "19") {
|
|
|
|
return;
|
2013-06-15 09:27:33 +03:00
|
|
|
}
|
2013-07-16 22:56:55 +03:00
|
|
|
event.preventDefault();
|
2013-08-22 16:12:04 +03:00
|
|
|
var url = str_format ("index.php?page=9¤t={0}&query={1}&db={2}", currentData.page, $("input[name=query]").val (), currentData.databaseId);
|
2013-07-16 22:56:55 +03:00
|
|
|
navigateTo (url);
|
2013-06-15 09:27:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
window.onpopstate = function(event) {
|
2013-06-20 15:34:10 +03:00
|
|
|
before = new Date ();
|
2013-07-02 15:15:10 +03:00
|
|
|
var data = cache.get (event.state);
|
2013-06-20 22:08:50 +03:00
|
|
|
updatePage (data);
|
2013-06-15 09:27:33 +03:00
|
|
|
};
|
|
|
|
|
2013-06-16 15:47:11 +03:00
|
|
|
$(document).keydown(function(e){
|
2013-07-02 15:37:37 +03:00
|
|
|
if (e.keyCode === 37 && $("#prevLink").length > 0) {
|
2013-06-15 09:27:33 +03:00
|
|
|
navigateTo ($("#prevLink").attr('href'));
|
|
|
|
}
|
2013-07-02 15:37:37 +03:00
|
|
|
if (e.keyCode === 39 && $("#nextLink").length > 0) {
|
2013-06-15 09:27:33 +03:00
|
|
|
navigateTo ($("#nextLink").attr('href'));
|
|
|
|
}
|
|
|
|
});
|