inkstitch/print/resources/inkstitch.js

274 wiersze
8.2 KiB
JavaScript
Czysty Zwykły widok Historia

$.postJSON = function(url, data, success=null) {
return $.ajax(url, {
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
success: success
});
};
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);
});
}
2018-04-09 17:20:02 +00:00
// Scale SVG (fit || full size)
function scaleSVG(element, scale = 'fit') {
// always center svg
transform = "translate(-50%, -50%)";
if(scale == 'fit') {
var scale = Math.min(
2018-04-09 17:20:02 +00:00
element.width() / element.find('svg').width(),
element.height() / element.find('svg').height()
);
2018-04-09 17:20:02 +00:00
// Do not scale to more than 100%
scale = (scale <= 1) ? scale : 1;
}
transform += " scale(" + scale + ")";
var label = parseInt(scale*100);
2018-04-09 17:20:02 +00:00
element.find('svg').css({ transform: transform });
element.find('figcaption span').text(label);
}
// set preview svg scale to fit into its box if transform is not set
function scaleAllSvg() {
$('.page').each(function() {
if( $(this).css('display') != 'none' ) {
if( $(this).find('.inksimulation svg').css('transform') == 'none') {
if( $(this).find('.inksimulation span').text() == '') {
scaleSVG($(this).find('.inksimulation'));
}
else {
var transform = $(this).find('.inksimulation span').text();
var scale = transform.match(/-?[\d\.]+/g)[0];
$(this).find('.inksimulation svg').css({ transform: transform });
$(this).find('.inksimulation span').text(parseInt(scale*100));
}
}
}
2018-04-09 17:20:02 +00:00
});
}
$(function() {
setTimeout(ping, 1000);
setPageNumbers();
2018-04-09 17:20:02 +00:00
scaleAllSvg();
2018-04-09 17:20:02 +00:00
/* SCALING AND MOVING SVG */
/* Mousewheel scaling */
$('figure.inksimulation').on( 'DOMMouseScroll mousewheel', function (e) {
if(event.ctrlKey == true) {
2018-04-09 17:20:02 +00:00
var svg = $(this).find('svg');
var transform = svg.css('transform').match(/-?[\d\.]+/g);
var scale = parseFloat(transform[0]);
if( scale > 0.01 && (e.originalEvent.detail > 0 || e.originalEvent.wheelDelta < 0)) {
// scroll down
scale -= 0.01;
} else {
//scroll up
scale += 0.01;
}
// set modified scale
transform[0] = scale;
transform[3] = scale;
svg.css({ transform: 'matrix(' + transform + ')' });
// set scale caption text
$(this).find("span").text(parseInt(scale*100));
//prevent page fom scrolling
return false;
}
});
2018-04-09 17:20:02 +00:00
/* Fit SVG */
$('button.svg-fit').click(function() {
var svgfigure = $(this).closest('figure');
scaleSVG(svgfigure, 'fit');
});
/* Full Size SVG */
2018-04-09 17:20:02 +00:00
$('button.svg-full').click(function() {
var svgfigure = $(this).closest('figure');
scaleSVG(svgfigure, '1');
});
/* Drag SVG */
$('figure.inksimulation').on('mousedown', function(e) {
$(this).data('p0', { x: e.pageX, y: e.pageY });
$(this).css({cursor: 'move'});
}).on('mouseup', function(e) {
$(this).css({cursor: 'auto'});
var p0 = $(this).data('p0'),
p1 = { x: e.pageX, y: e.pageY },
d = Math.sqrt(Math.pow(p1.x - p0.x, 2) + Math.pow(p1.y - p0.y, 2));
if (d > 4) {
var transform = $(this).find('svg').css('transform').match(/-?[\d\.]+/g);
transform[4] = parseFloat(transform[4]) + parseFloat(p1.x-p0.x);
transform[5] = parseFloat(transform[5]) + parseFloat(p1.y-p0.y);
// set modified translate
$(this).find('svg').css({ transform: 'matrix(' + transform + ')' });
}
})
/* Apply transforms to All */
$('button.svg-apply').click(function() {
var transform = $(this).parent().siblings('svg').css('transform');
var scale = transform.match(/-?[\d\.]+/g)[0];
$('.inksimulation').each(function() {
$(this).find('svg').css({ transform: transform });
$(this).find("span").text(parseInt(scale*100));
})
});
2018-04-14 01:23:00 +00:00
/* Contendeditable Fields */
2018-04-14 01:23:00 +00:00
$('[contenteditable="true"]').on('focusout', function() {
/* change svg scale */
2018-04-14 01:23:00 +00:00
var content = $(this).html();
var field_name = $(this).attr('data-field-name');
if(field_name == 'svg-scale') {
var scale = parseInt(content);
var svg = $(this).parent().siblings('svg');
var transform = svg.css('transform').match(/-?[\d\.]+/g);
transform[0] = scale / 100;
transform[3] = scale / 100;
svg.css({ transform: 'matrix(' + transform + ')' });
} else {
/* When we focus out from a contenteditable field, we want to
* set the same content to all fields with the same classname */
$('[data-field-name="' + field_name + '"]').html(content);
$.postJSON('/settings/' + field_name, {value: content});
}
});
2018-04-14 01:23:00 +00:00
2018-04-14 03:39:49 +00:00
// load up initial metadata values
2018-04-17 00:17:07 +00:00
$.getJSON('/settings', function(settings) {
$.each(settings, function(field_name, value) {
$('[data-field-name="' + field_name + '"]').each(function(i, item) {
2018-04-17 00:17:07 +00:00
var item = $(item);
if (item.is(':checkbox')) {
item.prop('checked', value).trigger('change');
} else if (item.is('img')) {
item.attr('src', value);
} else if (item.is('select')) {
item.val(value).trigger('change');
} else {
2018-04-17 00:17:07 +00:00
item.text(value);
}
});
2018-04-14 03:39:49 +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
/* Settings Bar */
2018-04-14 01:23:00 +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
/* Settings */
2018-04-14 01:23:00 +00:00
// Paper Size
$('select#printing-size').change(function(){
2018-04-17 00:17:07 +00:00
var size = $(this).find(':selected').val();
$('.page').toggleClass('a4', size == 'a4');
$.postJSON('/settings/paper-size', {value: size});
});
2018-04-14 01:23:00 +00:00
//Checkbox
$(':checkbox').change(function() {
var checked = $(this).prop('checked');
var field_name = $(this).attr('data-field-name');
$('.' + field_name).toggle(checked);
setPageNumbers();
2018-04-09 17:20:02 +00:00
scaleAllSvg();
2018-04-17 00:17:07 +00:00
$.postJSON('/settings/' + field_name, {value: checked});
});
2018-04-14 01:23:00 +00:00
2018-04-17 00:17:07 +00:00
// Logo
$('#logo-picker').change(function(e) {
var file = e.originalEvent.srcElement.files[0];
var reader = new FileReader();
reader.onloadend = function() {
var data = reader.result;
$('figure.brandlogo img').attr('src', data);
$.postJSON('/settings/logo', {value: data});
};
reader.readAsDataURL(file);
});
// "save as defaults" button
$('#save-settings').click(function(e) {
var settings = {};
settings["client-overview"] = $("[data-field-name='client-overview']").is(':checked');
settings["client-detailedview"] = $("[data-field-name='client-detailedview']").is(':checked');
settings["operator-overview"] = $("[data-field-name='operator-overview']").is(':checked');
settings["operator-detailedview"] = $("[data-field-name='operator-detailedview']").is(':checked');
settings["paper-size"] = $('select#printing-size').find(':selected').val();
var logo = $("figure.brandlogo img").attr('src');
if (logo.startsWith("data:")) {
settings["logo"] = logo;
}
$.postJSON('/defaults', {'value': settings});
});
});