summaryrefslogtreecommitdiff
path: root/print/resources/inkstitch.js
blob: ed26d3671d83e73568b917d0b047b7f01653d4c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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();

  /* Contendeditable Fields */

  // When we focus out from a contenteditable field, we want to
  // set the same content to all fields with the same classname
  $('[contenteditable="true"]').on('focusout', function() {
    var content = $(this).html();
    var field_name = $(this).attr('data-field-name');
    $('[data-field-name="' + field_name + '"]').html(content);
    $.post('/metadata/' + field_name + '/set', {value: content});
  });

  $('[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;
      }
    });


  /* Settings Bar */

  $('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();
  });

  /* Settings */

  // Paper Size
  $('select#printing-size').change(function(){
    $('.page').toggleClass('a4');
  });

  //Checkbox
  $(':checkbox').change(function() {
    $('.' + this.id).toggle();
    setPageNumbers();
    scaleInksimulation();
  });

});