2018-04-15 00:39:59 +00:00
|
|
|
$.postJSON = function(url, data, success=null) {
|
|
|
|
return $.ajax(url, {
|
|
|
|
type: 'POST',
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
contentType: 'application/json',
|
|
|
|
success: success
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
function ping() {
|
|
|
|
$.get("/ping")
|
|
|
|
.done(function() { setTimeout(ping, 1000) })
|
|
|
|
.fail(function() { $('#errors').attr('class', 'show') });
|
|
|
|
}
|
|
|
|
|
|
|
|
// set pagenumbers
|
|
|
|
function setPageNumbers() {
|
|
|
|
var totalPageNum = $('body').find('.page:visible').length;
|
|
|
|
$('span.total-page-num').text(totalPageNum);
|
|
|
|
$( '.page:visible span.page-num' ).each(function( index ) {
|
|
|
|
$(this).text(index + 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// set preview svg scale to fit into its box
|
|
|
|
function scaleInksimulation() {
|
|
|
|
$('.inksimulation').each(function() {
|
|
|
|
var scale = Math.min(
|
|
|
|
$(this).width() / $(this).find('svg').width(),
|
|
|
|
$(this).height() / $(this).find('svg').height()
|
|
|
|
);
|
|
|
|
|
|
|
|
// center the SVG
|
|
|
|
transform = "translate(-50%, -50%)";
|
|
|
|
|
|
|
|
if(scale <= 1) {
|
|
|
|
transform += " scale(" + scale + ")";
|
|
|
|
label = parseInt(scale*100) + '%';
|
|
|
|
} else {
|
|
|
|
label = "100%";
|
|
|
|
}
|
|
|
|
|
|
|
|
$(this).find('svg').css({ transform: transform });
|
|
|
|
$(this).find('figcaption span').text(label);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$(function() {
|
|
|
|
setTimeout(ping, 1000);
|
|
|
|
setPageNumbers();
|
|
|
|
scaleInksimulation();
|
2018-04-14 01:23:00 +00:00
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
/* Contendeditable Fields */
|
2018-04-14 01:23:00 +00:00
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
// When we focus out from a contenteditable field, we want to
|
|
|
|
// set the same content to all fields with the same classname
|
2018-04-14 01:23:00 +00:00
|
|
|
$('[contenteditable="true"]').on('focusout', function() {
|
|
|
|
var content = $(this).html();
|
|
|
|
var field_name = $(this).attr('data-field-name');
|
2018-04-14 03:39:49 +00:00
|
|
|
$('[data-field-name="' + field_name + '"]').text(content);
|
2018-04-15 00:39:59 +00:00
|
|
|
$.postJSON('/metadata/' + field_name + '/set', {value: content});
|
2018-03-31 00:37:11 +00:00
|
|
|
});
|
2018-04-14 01:23:00 +00:00
|
|
|
|
2018-04-14 03:39:49 +00:00
|
|
|
// load up initial metadata values
|
|
|
|
$.getJSON('/metadata', function(metadata) {
|
|
|
|
$.each(metadata, function(field_name, value) {
|
2018-04-15 00:39:59 +00:00
|
|
|
$('[data-field-name="' + field_name + '"]').each(function(i, item) {
|
|
|
|
console.log(item);
|
|
|
|
if ($(item).is(':checkbox')) {
|
|
|
|
console.log("is a checkbox");
|
|
|
|
$(item).prop('checked', value).trigger('change');
|
|
|
|
} else {
|
|
|
|
console.log("is not a checkbox");
|
|
|
|
$(item).text(value);
|
|
|
|
}
|
|
|
|
});
|
2018-04-14 03:39:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
$('[contenteditable="true"]').keypress(function(e) {
|
|
|
|
if (e.which == 13) {
|
|
|
|
// pressing enter defocuses the element
|
|
|
|
this.blur();
|
|
|
|
|
|
|
|
// also suppress the enter keystroke to avoid adding a new line
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2018-04-14 01:23:00 +00:00
|
|
|
|
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
/* Settings Bar */
|
2018-04-14 01:23:00 +00:00
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
$('button.close').click(function() {
|
|
|
|
$.post('/shutdown', {})
|
|
|
|
.done(function(data) {
|
|
|
|
window.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$('button.print').click(function() {
|
|
|
|
// printing halts all javascript activity, so we need to tell the backend
|
|
|
|
// not to shut down until we're done.
|
|
|
|
$.get("/printing/start")
|
|
|
|
.done(function() {
|
|
|
|
window.print();
|
|
|
|
$.get("/printing/end");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$('button.settings').click(function(){
|
|
|
|
$('#settings-ui').show();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#close-settings').click(function(){
|
|
|
|
$('#settings-ui').hide();
|
|
|
|
});
|
2018-04-14 01:23:00 +00:00
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
/* Settings */
|
2018-04-14 01:23:00 +00:00
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
// Paper Size
|
|
|
|
$('select#printing-size').change(function(){
|
|
|
|
$('.page').toggleClass('a4');
|
|
|
|
});
|
2018-04-14 01:23:00 +00:00
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
//Checkbox
|
|
|
|
$(':checkbox').change(function() {
|
2018-04-15 00:39:59 +00:00
|
|
|
var checked = $(this).prop('checked');
|
|
|
|
var field_name = $(this).attr('data-field-name');
|
|
|
|
|
|
|
|
$('.' + field_name).toggle(checked);
|
2018-03-31 00:37:11 +00:00
|
|
|
setPageNumbers();
|
|
|
|
scaleInksimulation();
|
2018-04-15 00:39:59 +00:00
|
|
|
|
|
|
|
$.postJSON('/metadata/' + field_name + '/set', {value: checked});
|
2018-03-31 00:37:11 +00:00
|
|
|
});
|
2018-04-14 01:23:00 +00:00
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
});
|
|
|
|
|