1 $(document).ready(function() {
3 // Illview Datatable setup
7 // Filters that are active
8 var activeFilters = {};
11 var prefilters = $('table#ill-requests').data('prefilters');
13 // Fields we need to expand (flatten)
21 // This is auto populated
27 prep: function(tableData, oData) {
29 tableData.forEach(function(row) {
31 if (row.status_alias) {
32 resolvedName = row.status_alias.lib;
34 resolvedName = getStatusName(
35 oData[0].capabilities[row.status].name,
39 uniques[resolvedName] = 1
41 Object.keys(uniques).sort().forEach(function(unique) {
42 $('#illfilter_status').append(
43 '<option value="' + unique +
44 '">' + unique + '</option>'
48 listener: function() {
50 $('#illfilter_status').change(function() {
51 var sel = $('#illfilter_status option:selected').val();
52 if (sel && sel.length > 0) {
53 activeFilters[me] = function() {
54 table.api().column(13).search(sel);
57 if (activeFilters.hasOwnProperty(me)) {
58 delete activeFilters[me];
64 $('#illfilter_status').val('');
68 prep: function(tableData, oData) {
70 tableData.forEach(function(row) {
71 uniques[row.library_branchname] = 1
73 Object.keys(uniques).sort().forEach(function(unique) {
74 $('#illfilter_branchname').append(
75 '<option value="' + unique +
76 '">' + unique + '</option>'
80 listener: function() {
81 var me = 'pickupBranch';
82 $('#illfilter_branchname').change(function() {
83 var sel = $('#illfilter_branchname option:selected').val();
84 if (sel && sel.length > 0) {
85 activeFilters[me] = function() {
86 table.api().column(12).search(sel);
89 if (activeFilters.hasOwnProperty(me)) {
90 delete activeFilters[me];
96 $('#illfilter_branchname').val('');
100 listener: function() {
102 $('#illfilter_patron').change(function() {
103 var val = $('#illfilter_patron').val();
104 if (val && val.length > 0) {
105 activeFilters[me] = function() {
106 table.api().column(10).search(val);
109 if (activeFilters.hasOwnProperty(me)) {
110 delete activeFilters[me];
116 $('#illfilter_patron').val('');
121 $('#illfilter_datemodified_start, #illfilter_datemodified_end').val('');
126 $('#illfilter_dateplaced_start, #illfilter_dateplaced_end').val('');
129 }; //END Filterable columns
131 // Expand any fields we're expanding
132 var expandExpand = function(row) {
133 expand.forEach(function(thisExpand) {
134 if (row.hasOwnProperty(thisExpand)) {
135 if (!expanded.hasOwnProperty(thisExpand)) {
136 expanded[thisExpand] = [];
138 var expandObj = row[thisExpand];
139 Object.keys(expandObj).forEach(
140 function(thisExpandCol) {
141 var expColName = thisExpand + '_' + thisExpandCol.replace(/\s/g,'_');
142 // Keep a list of fields that have been expanded
143 // so we can create toggle links for them
144 if (expanded[thisExpand].indexOf(expColName) == -1) {
145 expanded[thisExpand].push(expColName);
147 expandObj[expColName] =
148 expandObj[thisExpandCol];
149 delete expandObj[thisExpandCol];
152 $.extend(true, row, expandObj);
153 delete row[thisExpand];
159 // Strip the expand prefix if it exists, we do this for display
160 var stripPrefix = function(value) {
161 expand.forEach(function(thisExpand) {
162 var regex = new RegExp(thisExpand + '_', 'g');
163 value = value.replace(regex, '');
168 // Our 'render' function for borrowerlink
169 var createPatronLink = function(data, type, row) {
170 var patronLink = '<a title="' + ill_borrower_details + '" ' +
171 'href="/cgi-bin/koha/members/moremember.pl?' +
172 'borrowernumber='+row.borrowernumber+'">';
173 if ( row.patron_firstname ) {
174 patronLink = patronLink + row.patron_firstname + ' ';
176 patronLink = patronLink + row.patron_surname +
177 ' (' + row.patron_cardnumber + ')' + '</a>';
181 // Our 'render' function for biblio_id
182 var createBiblioLink = function(data, type, row) {
183 return (row.biblio_id) ?
184 '<a title="' + ill_biblio_details + '" ' +
185 'href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=' +
186 row.biblio_id + '">' +
191 // Our 'render' function for title
192 var createTitle = function(data, type, row) {
194 row.hasOwnProperty('metadata_container_title') &&
195 row.metadata_container_title
196 ) ? row.metadata_container_title : row.metadata_title;
199 // Render function for request ID
200 var createRequestId = function(data, type, row) {
201 return row.id_prefix + row.illrequest_id;
204 // Render function for type
205 var createType = function(data, type, row) {
206 if (!row.hasOwnProperty('metadata_Type') || !row.metadata_Type) {
207 if (row.hasOwnProperty('medium') && row.medium) {
208 row.metadata_Type = row.medium;
210 row.metadata_Type = null;
213 return row.metadata_Type;
216 // Render function for request status
217 var createStatus = function(data, type, row, meta) {
218 if (row.status_alias) {
219 return row.status_alias.lib
220 ? row.status_alias.lib
221 : row.status_alias.authorised_value;
223 var status_name = row.capabilities[row.status].name;
224 return getStatusName(status_name, row);
228 var getStatusName = function(origName, row) {
231 return ill_statuses.new;
233 return ill_statuses.req;
234 case "Requested from partners":
235 var statStr = ill_statuses.genreq;
237 row.hasOwnProperty('requested_partners') &&
238 row.requested_partners &&
239 row.requested_partners.length > 0
241 statStr += ' (' + row.requested_partners + ')';
244 case "Request reverted":
245 return ill_statuses.rev;
246 case "Queued request":
247 return ill_statuses.que;
248 case "Cancellation requested":
249 return ill_statuses.canc;
251 return ill_statuses.comp;
252 case "Delete request":
253 return ill_statuses.del;
259 // Render function for creating a row's action link
260 var createActionLink = function(data, type, row) {
261 return '<a class="btn btn-default btn-sm" ' +
262 'href="/cgi-bin/koha/ill/ill-requests.pl?' +
263 'method=illview&illrequest_id=' +
265 '">' + ill_manage + '</a>';
268 // Columns that require special treatment
271 func: createActionLink,
275 func: createRequestId
281 name: ill_columns.biblio_id,
282 func: createBiblioLink,
292 name: ill_columns.updated
296 func: createPatronLink
300 // Display the modal containing request supplier metadata
301 $('#ill-request-display-log').on('click', function(e) {
303 $('#requestLog').modal({show:true});
306 // Toggle request attributes in Illview
307 $('#toggle_requestattributes').on('click', function(e) {
309 $('#requestattributes').toggleClass('content_hidden');
312 // Toggle new comment form in Illview
313 $('#toggle_addcomment').on('click', function(e) {
315 $('#addcomment').toggleClass('content_hidden');
318 // Filter partner list
319 // Record the list of all options
320 var ill_partner_options = $('#partners > option');
321 $('#partner_filter').keyup(function() {
322 var needle = $('#partner_filter').val();
323 var regex = new RegExp(needle, 'i');
325 ill_partner_options.each(function() {
327 needle.length == 0 ||
328 $(this).is(':selected') ||
329 $(this).text().match(regex)
331 filtered.push($(this));
334 $('#partners').empty().append(filtered);
337 // Display the modal containing request supplier metadata
338 $('#ill-request-display-metadata').on('click', function(e) {
340 $('#dataPreview').modal({show:true});
343 // Allow us to chain Datatable render helpers together, so we
344 // can use our custom functions and render.text(), which
345 // provides us with data sanitization
346 $.fn.dataTable.render.multi = function(renderArray) {
347 return function(d, type, row, meta) {
348 for(var r = 0; r < renderArray.length; r++) {
349 var toCall = renderArray[r].hasOwnProperty('display') ?
350 renderArray[r].display :
352 d = toCall(d, type, row, meta);
358 // Get our data from the API and process it prior to passing
360 var filterParam = prefilters ? '&' + prefilters : '';
361 // Only fire the request if we're on an appropriate page
364 // ILL list requests page
365 window.location.href.match(/ill\/ill-requests\.pl/) &&
366 window.location.search.length == 0
368 // Patron profile page
369 window.location.href.match(/members\/ill-requests\.pl/)
372 '/api/v1/illrequests?embed=metadata,patron,capabilities,library,status_alias,comments,requested_partners'
375 var data = JSON.parse(ajax.responseText);
376 // Make a copy, we'll be removing columns next and need
377 // to be able to refer to data that has been removed
378 var dataCopy = $.extend(true, [], data);
379 // Expand columns that need it and create an array
380 // of all column names
381 $.each(dataCopy, function(k, row) {
385 // Assemble an array of column definitions for passing
388 columns_settings.forEach(function(thisCol) {
389 var colName = thisCol.columnname;
390 // Create the base column object
391 var colObj = $.extend({}, thisCol);
392 colObj.name = colName;
393 colObj.className = colName;
394 colObj.defaultContent = '';
396 // We may need to process the data going in this
397 // column, so do it if necessary
399 specialCols.hasOwnProperty(colName) &&
400 specialCols[colName].hasOwnProperty('func')
403 specialCols[colName].func
405 if (!specialCols[colName].skipSanitize) {
407 $.fn.dataTable.render.text()
411 colObj.render = $.fn.dataTable.render.multi(
415 colObj.data = colName;
416 colObj.render = $.fn.dataTable.render.text()
418 // Make sure properties that aren't present in the API
419 // response are populated with null to avoid Datatables
420 // choking on their absence
421 dataCopy.forEach(function(thisData) {
422 if (!thisData.hasOwnProperty(colName)) {
423 thisData[colName] = null;
426 colData.push(colObj);
429 // Initialise the datatable
430 table = KohaTable("ill-requests", {
432 { // Last column shouldn't be sortable or searchable
433 'aTargets': [ 'actions' ],
437 { // When sorting 'placed', we want to use the
438 // unformatted column
439 'aTargets': [ 'placed_formatted'],
442 { // When sorting 'updated', we want to use the
443 // unformatted column
444 'aTargets': [ 'updated_formatted'],
447 { // When sorting 'completed', we want to use the
448 // unformatted column
449 'aTargets': [ 'completed_formatted'],
453 'aaSorting': [[ 16, 'desc' ]], // Default sort, updated descending
454 'processing': true, // Display a message when manipulating
455 'sPaginationType': "full_numbers", // Pagination display
456 'deferRender': true, // Improve performance on big datasets
459 'originalData': data, // Enable render functions to access
461 'initComplete': function() {
463 // Prepare any filter elements that need it
464 for (var el in filterable) {
465 if (filterable.hasOwnProperty(el)) {
466 if (filterable[el].hasOwnProperty('prep')) {
467 filterable[el].prep(dataCopy, data);
469 if (filterable[el].hasOwnProperty('listener')) {
470 filterable[el].listener();
476 }, columns_settings);
478 // Custom date range filtering
479 $.fn.dataTable.ext.search.push(function(settings, data, dataIndex) {
480 var placedStart = $('#illfilter_dateplaced_start').datepicker('getDate');
481 var placedEnd = $('#illfilter_dateplaced_end').datepicker('getDate');
482 var modifiedStart = $('#illfilter_datemodified_start').datepicker('getDate');
483 var modifiedEnd = $('#illfilter_datemodified_end').datepicker('getDate');
484 var rowPlaced = data[14] ? new Date(data[14]) : null;
485 var rowModified = data[16] ? new Date(data[16]) : null;
486 var placedPassed = true;
487 var modifiedPassed = true;
488 if (placedStart && rowPlaced && rowPlaced < placedStart) {
491 if (placedEnd && rowPlaced && rowPlaced > placedEnd) {
492 placedPassed = false;
494 if (modifiedStart && rowModified && rowModified < modifiedStart) {
495 modifiedPassed = false
497 if (modifiedEnd && rowModified && rowModified > modifiedEnd) {
498 modifiedPassed = false;
501 return placedPassed && modifiedPassed;
506 } //END if window.location.search.length == 0
508 var clearSearch = function() {
509 table.api().search('').columns().search('');
511 for (var filter in filterable) {
513 filterable.hasOwnProperty(filter) &&
514 filterable[filter].hasOwnProperty('clear')
516 filterable[filter].clear();
522 // Apply any search filters, or clear any previous
524 $('#illfilter_form').submit(function(event) {
525 event.preventDefault();
526 table.api().search('').columns().search('');
527 for (var active in activeFilters) {
528 if (activeFilters.hasOwnProperty(active)) {
529 activeFilters[active]();
536 $('#clear_search').click(function() {