5 window.addEventListener('load', onload);
8 // Only proceed if appropriate
9 if (!document.getElementById('ill-batch-requests')) return;
11 // A proxy that will give us some element of reactivity to
12 // changes in our list of batches
13 batchesProxy = new Proxy(
16 get: function (obj, prop) {
19 set: function (obj, prop, value) {
26 // Initialise the Datatable, binding it to our proxy object
29 // Do the initial data population
30 window.doBatchApiRequest('', {
32 'x-koha-embed': '+strings,requests+count,patron'
35 .then(function(response) {
36 return response.json();
38 .then(function(data) {
39 batchesProxy.data = data;
42 // Clean up any event listeners we added
43 window.addEventListener('beforeunload', removeEventListeners);
46 // Initialise the Datatable
47 // FIXME: This should be a kohaTable not KohaTable
48 var initTable = function () {
49 return KohaTable("ill-batch-requests", {
50 data: batchesProxy.data,
62 data: 'requests_count',
72 render: createPatronLink,
81 render: createActions,
89 drawCallback: addEventListeners
93 // A render function for branch name
94 var createBranch = function (x, y, data) {
95 return data._strings.library_id.str;
98 // A render function for batch name
99 var createName = function (x, y, data) {
100 var a = document.createElement('a');
101 a.setAttribute('href', '/cgi-bin/koha/ill/ill-requests.pl?batch_id=' + data.ill_batch_id);
102 a.setAttribute('title', data.name);
103 a.textContent = data.name;
107 // A render function for batch status
108 var createStatus = function (x, y, data) {
109 return data._strings.status_code.str;
112 // A render function for our patron link
113 var createPatronLink = function (data) {
114 return data ? $patron_to_html(data, { display_cardnumber: true, url: true }) : '';
117 // A render function for our row action buttons
118 var createActions = function (data, type, row) {
119 var div = document.createElement('div');
120 div.setAttribute('class', 'action-buttons');
122 var editButton = document.createElement('button');
123 editButton.setAttribute('type', 'button');
124 editButton.setAttribute('class', 'editButton btn btn-xs btn-default');
125 editButton.setAttribute('data-batch-id', row.ill_batch_id);
126 editButton.appendChild(document.createTextNode(ill_batch_edit));
128 var deleteButton = document.createElement('button');
129 deleteButton.setAttribute('type', 'button');
130 deleteButton.setAttribute('class', 'deleteButton btn btn-xs btn-danger');
131 deleteButton.setAttribute('data-batch-id', row.ill_batch_id);
132 deleteButton.appendChild(document.createTextNode(ill_batch_delete));
134 div.appendChild(editButton);
135 div.appendChild(deleteButton);
137 return div.outerHTML;
140 // Add event listeners to our row action buttons
141 var addEventListeners = function () {
142 var del = document.querySelectorAll('.deleteButton');
143 del.forEach(function (el) {
144 el.addEventListener('click', handleDeleteClick);
147 var edit = document.querySelectorAll('.editButton');
148 edit.forEach(function (elEdit) {
149 elEdit.addEventListener('click', handleEditClick);
153 // Remove all added event listeners
154 var removeEventListeners = function () {
155 var del = document.querySelectorAll('.deleteButton');
156 del.forEach(function (el) {
157 el.removeEventListener('click', handleDeleteClick);
159 window.removeEventListener('load', onload);
160 window.removeEventListener('beforeunload', removeEventListeners);
163 // Handle "Delete" clicks
164 var handleDeleteClick = function(e) {
165 var el = e.srcElement;
166 if (confirm(ill_batch_confirm_delete)) {
171 // Handle "Edit" clicks
172 var handleEditClick = function(e) {
173 var el = e.srcElement;
174 var id = el.dataset.batchId;
175 window.openBatchModal(id);
179 // - Make the API call
181 // - Update our proxy data
182 var deleteBatch = function (el) {
183 var id = el.dataset.batchId;
188 .then(function (response) {
190 window.handleApiError(ill_batch_delete_fail);
192 removeBatch(el.dataset.batchId);
195 .catch(function (response) {
196 window.handleApiError(ill_batch_delete_fail);
200 // Remove a batch from our proxy data
201 var removeBatch = function(id) {
202 batchesProxy.data = batchesProxy.data.filter(function (batch) {
203 return batch.ill_batch_id != id;
208 var updateTable = function () {
211 .rows.add(batchesProxy.data)