-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
185 lines (175 loc) · 4.39 KB
/
script.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Check the checkboxes of all printouts in results
function select_all() {
$('input[name="slip_id"]').prop('checked', true);
}
// Uncheck the checkboxes of all printouts in results
function deselect_all() {
$('input[name="slip_id"]').prop('checked', false);
}
// Get printouts from the queue and display
function get_slips() {
$('#loading').show();
$('#slip-results').hide();
var printer = $('#printer').val();
$.ajax({
url: 'slips.php',
method: 'post',
data: {printer: printer},
success: function (results) {
$('#slip-results').html(results);
var columns = $(results).find('#columns').val();
update_columns(columns);
$('#slip-results').show();
$('#loading').hide();
}
});
}
// Update the number of columns displayed
function update_columns(columns) {
var percentage = Math.floor(100/columns);
$('.slip').css('max-width', percentage + '%');
}
// Trigger bulk action on printouts (print or cancel)
function do_action() {
var printout_ids = get_printout_ids();
if (printout_ids.length > 0) {
var action = $('#action').val();
switch (action) {
case 'print':
print_slips();
break;
case 'cancel':
cancel_slips();
break;
}
return false;
}
else {
$('#dialog-no-selections').dialog('open');
}
}
// Get IDs of checked printouts
function get_printout_ids() {
var printout_ids = [];
$.each($('input[name="slip_id"]:checked'), function() {
printout_ids.push($(this).val());
});
return printout_ids;
}
// Get string of "X printout(s)"
function get_printout_string(printout_ids) {
var printout_string = printout_ids.length + ' ' + ' printout';
if (printout_ids.length > 1) {
printout_string += 's';
}
return printout_string;
}
// Send checked printouts to new window for printing
function print_slips() {
var columns = $('#columns').val();
var print_contents = '<!DOCTYPE html><html><head><link rel="stylesheet" href="print.css" /><style>td.slip {width: ' + (100/columns).toFixed(2) + '%;}</style><head><body><table id="print-slips"><tbody><tr>';
var checked_html = $('input[name="slip_id"]:checked').next('.slip-html');
var last_html = checked_html.last();
var t = 0;
$.each(checked_html, function(index) {
print_contents += '<td class="slip">' + $(this).html() + '</td>';
t++;
if (t == columns) {
print_contents += '</tr>';
if (!$(this).is(last_html)) {
print_contents += '<tr>';
}
t = 0;
}
});
if (t > 0 && t < columns) {
for (var c = 0; c <= (columns - t); c++) {
print_contents += '<td> </td>';
}
}
print_contents += '</tr></tbody></table></body></html>';
var print_window = window.open('', 'printed_slips');
print_window.document.write(print_contents);
print_window.document.close();
print_window.focus();
print_window.onload = function() {
print_window.print();
print_window.close();
}
$('#dialog-mark-printed').dialog('open');
}
// Mark a printout printed or canceled
function mark_printouts(type) {
printout_ids = get_printout_ids();
$.ajax({
url: 'mark.php',
method: 'post',
data: {type: type, ids: printout_ids},
success: function (results) {
if (results != 'success') {
$('#dialog-error').dialog('open');
}
get_slips();
}
});
}
// Cancel printouts
function cancel_slips() {
$('#dialog-cancel').dialog('open');
}
$(document).ready(function() {
$('#dialog-error').dialog({
autoOpen: false,
buttons: [{
text: 'OK',
click: function() {
$(this).dialog('close');
}
}]
});
$('#dialog-no-selections').dialog({
autoOpen: false,
buttons: [{
text: 'OK',
click: function() {
$(this).dialog('close');
}
}]
});
$('#dialog-mark-printed').dialog({
autoOpen: false,
buttons: [
{
text: 'Yes',
click: function() {
mark_printouts('printed');
$(this).dialog('close');
}
},
{
text: 'No',
click: function() {
$(this).dialog('close');
}
}
]
});
$('#dialog-cancel').dialog({
autoOpen: false,
buttons: [
{
text: 'Yes',
click: function() {
mark_printouts('canceled');
$(this).dialog('close');
}
},
{
text: 'No',
click: function() {
$(this).dialog('close');
}
}
]
});
});