$(window).load(function(){
if($("body").hasClass("p-mode")){
// Visualizes print css on screen
$("style[media='print']").attr("media", "print, screen");
$("link[media='print']").attr("media", "print, screen");
// POLYFILL HACK: move metadata and footnotes outside of div#content
footnotes = $(".footnote").detach();
footnotes.insertAfter($("#content"));
//metadata = $("#metadata").detach();
//metadata.insertAfter($(".footnote"));
// OVERRIDES PAGE FORMAT SET ON PAD
try{
var pageWidthCrop = parseInt(customPageWidth) + parseInt(crop);
var pageHeightCrop = parseInt(customPageHeight) + parseInt(crop);
var pageHeightCropx = pageHeightCrop - 1;
var styles = " \
@page { \n \
size:" + pageWidthCrop + "mm " + pageHeightCrop + "mm; \n \
} \n \
html, \n \
body { \n \
width: " + pageWidthCrop + "mm; \n \
} \n \
#master-page, \n \
.page, \n \
div.print-marks { \n \
width: " + pageWidthCrop + "mm; \n \
height: " + pageHeightCropx + "mm; \n \
} \n \
"
// IF NO CROP MARKS
if (crop == 0) {
styles += " \
.print-marks { \n \
display: none; \n \
} \n \
section.header { \n \
top: 0; \n \
left: 0; \n \
right: 0; \n \
} \n \
section.main-section { \n \
top: 0; \n \
left: 0; \n \
bottom: 0; \n \
right: 0; \n \
} \n \
section.footer { \n \
bottom: 0; \n \
left: 0; \n \
right: 0; \n \
} \n \
#master-page, \n \
.page { \n \
outline-offset: 0; \n \
} \n \
"
}
var style = document.createElement('style');
document.body.appendChild(style);
style.id = "publish-css";
style.media = "screen, print";
$("#publish-css").html(styles);
} catch(e){}
// Create pages
$("body").append('');
$("#master-page").append('\
\
');
$("#master-page").append('').append('').append('');
// OPTIONAL COLUMNS
try {
for (i = 1; i < columnNumber; i++){
$(".main-section").append("");
$(".column").css("width", 100 / columnNumber + "%");
}
} catch(e){}
height = $($(".middle")[0]).height() + $($(".middle")[1]).height();
page_height = $("#master-page").height();
// OPTIONAL CUSTOM PAGE NUMBER
try {
nb_page = customPageNumber;
} catch(e) {
nb_page = Math.ceil(height / page_height);
}
console.log("Content = " + height);
console.log("page height = " + page_height);
console.log("nb page = " + nb_page);
for (i = 1; i <= nb_page; i++){
$("#master-page").clone().addClass("page").attr("id","page-"+i).insertBefore($("#master-page"));
}
$("#master-page").hide();
$.getScript("/static/js/css-regions-polyfill.js", function(){ });
// __________________________________ DEBUG __________________________________ //
$("button#debug").click(function(e){
e.preventDefault();
$(this).toggleClass("button-active");
$("html").toggleClass("debug");
});
// __________________________________ SPREAD __________________________________ //
$("button#spread").click(function(e){
e.preventDefault();
$(this).toggleClass("button-active");
$("html").toggleClass("spread");
w = parseInt($("html").css("width")) * 2;
console.log(w);
$("html").css("width", w + "px");
});
// __________________________________ HIGH RESOLUTION __________________________________ //
$("button#hi-res").click(function(e){
e.preventDefault();
$(this).toggleClass("button-active");
$("html").toggleClass("export");
$("img").each(function(){
if ($("html").hasClass("export")){
var lores = $(this).attr("src");
hires = lores.split("/");
hires.splice(-1, 0, "HD");
hires = hires.join("/");
$(this).attr("src", hires);
console.log("Wait for hi-res images to load");
window.setTimeout(function(){
console.log("Check image resolution");
// Redlights images too small for printing
$("img").each(function(){
if (Math.ceil(this.naturalHeight / $(this).height()) < 3) {
console.log($(this).attr("src") + ": " + Math.floor(this.naturalHeight / $(this).height()) );
if($(this).parent().hasClass("moveable")) {
$(this).parent().toggleClass("lo-res");
} else {
$(this).toggleClass("lo-res");
}
}
});
}, 2000);
} else {
var hires = $(this).attr("src");
lores = hires.split("/");
lores.splice(-2, 1);
lores = lores.join("/");
$(this).attr("src", lores);
$("img").removeClass("lo-res");
}
});
});
// __________________________________ TOC __________________________________ //
$(".page:not(#master-page)").each(function(){
page = $(this).attr("id");
$("#toc-pages").append("" + page.replace("-", " ") + "")
});
$("#goto").click(function(e){
e.preventDefault();
$(this).toggleClass("button-active");
$("#toc-pages").toggle();
});
// __________________________________ ZOOM __________________________________ //
$("#zoom").click(function(e){
e.preventDefault();
$(this).toggleClass("button-active");
$("#zoom-list").toggle();
});
$("#zoom-list a").click(function(e){
e.preventDefault();
zoom = $(this).attr("title") / 100 ;
unzoom = 1 / zoom;
console.log(zoom);
console.log(unzoom);
$(".page").css("-webkit-transform", "scale(" + zoom + ")");
});
}
});