Skip to content
Snippets Groups Projects
Commit 268c8ce1 authored by Brent Palmer's avatar Brent Palmer Committed by Robert Lange
Browse files

edited check_item_status to make it more obj-oriented (#1197)

parent cb358e8c
No related merge requests found
/*global Hunt, VuFind */
/*exported checkItemStatuses, itemStatusFail */
function linkCallnumbers(callnumber, callnumber_handler) {
if (callnumber_handler) {
var cns = callnumber.split(',\t');
for (var i = 0; i < cns.length; i++) {
cns[i] = '<a href="' + VuFind.path + '/Alphabrowse/Home?source=' + encodeURI(callnumber_handler) + '&amp;from=' + encodeURI(cns[i]) + '">' + cns[i] + '</a>';
VuFind.register('itemStatuses', function ItemStatuses() {
function linkCallnumbers(callnumber, callnumber_handler) {
if (callnumber_handler) {
var cns = callnumber.split(',\t');
for (var i = 0; i < cns.length; i++) {
cns[i] = '<a href="' + VuFind.path + '/Alphabrowse/Home?source=' + encodeURI(callnumber_handler) + '&amp;from=' + encodeURI(cns[i]) + '">' + cns[i] + '</a>';
}
return cns.join(',\t');
}
return cns.join(',\t');
return callnumber;
}
return callnumber;
}
function displayItemStatus(result, $item) {
$item.removeClass('js-item-pending');
$item.find('.status').empty().append(result.availability_message);
$item.find('.ajax-availability').removeClass('ajax-availability hidden');
if (typeof(result.error) != 'undefined'
&& result.error.length > 0
) {
// Only show error message if we also have a status indicator active:
if ($item.find('.status').length > 0) {
$item.find('.callnumAndLocation').empty().addClass('text-danger').append(result.error);
} else {
$item.find('.callnumAndLocation').addClass('hidden');
}
$item.find('.callnumber,.hideIfDetailed,.location').addClass('hidden');
} else if (typeof(result.full_status) != 'undefined'
&& result.full_status.length > 0
&& $item.find('.callnumAndLocation').length > 0
) {
// Full status mode is on -- display the HTML and hide extraneous junk:
$item.find('.callnumAndLocation').empty().append(result.full_status);
$item.find('.callnumber,.hideIfDetailed,.location,.status').addClass('hidden');
} else if (typeof(result.missing_data) != 'undefined'
&& result.missing_data
) {
// No data is available -- hide the entire status area:
$item.find('.callnumAndLocation,.status').addClass('hidden');
} else if (result.locationList) {
// We have multiple locations -- build appropriate HTML and hide unwanted labels:
$item.find('.callnumber,.hideIfDetailed,.location').addClass('hidden');
var locationListHTML = "";
for (var x = 0; x < result.locationList.length; x++) {
locationListHTML += '<div class="groupLocation">';
if (result.locationList[x].availability) {
locationListHTML += '<span class="text-success"><i class="fa fa-ok" aria-hidden="true"></i> '
+ result.locationList[x].location + '</span> ';
} else if (typeof(result.locationList[x].status_unknown) !== 'undefined'
&& result.locationList[x].status_unknown
) {
if (result.locationList[x].location) {
locationListHTML += '<span class="text-warning"><i class="fa fa-status-unknown" aria-hidden="true"></i> '
+ result.locationList[x].location + '</span> ';
}
function displayItemStatus(result, $item) {
$item.removeClass('js-item-pending');
$item.find('.status').empty().append(result.availability_message);
$item.find('.ajax-availability').removeClass('ajax-availability hidden');
if (typeof(result.error) != 'undefined'
&& result.error.length > 0
) {
// Only show error message if we also have a status indicator active:
if ($item.find('.status').length > 0) {
$item.find('.callnumAndLocation').empty().addClass('text-danger').append(result.error);
} else {
locationListHTML += '<span class="text-danger"><i class="fa fa-remove" aria-hidden="true"></i> '
+ result.locationList[x].location + '</span> ';
$item.find('.callnumAndLocation').addClass('hidden');
}
locationListHTML += '</div>';
locationListHTML += '<div class="groupCallnumber">';
locationListHTML += (result.locationList[x].callnumbers)
? linkCallnumbers(result.locationList[x].callnumbers, result.locationList[x].callnumber_handler) : '';
locationListHTML += '</div>';
$item.find('.callnumber,.hideIfDetailed,.location').addClass('hidden');
} else if (typeof(result.full_status) != 'undefined'
&& result.full_status.length > 0
&& $item.find('.callnumAndLocation').length > 0
) {
// Full status mode is on -- display the HTML and hide extraneous junk:
$item.find('.callnumAndLocation').empty().append(result.full_status);
$item.find('.callnumber,.hideIfDetailed,.location,.status').addClass('hidden');
} else if (typeof(result.missing_data) !== 'undefined'
&& result.missing_data
) {
// No data is available -- hide the entire status area:
$item.find('.callnumAndLocation,.status').addClass('hidden');
} else if (result.locationList) {
// We have multiple locations -- build appropriate HTML and hide unwanted labels:
$item.find('.callnumber,.hideIfDetailed,.location').addClass('hidden');
var locationListHTML = "";
for (var x = 0; x < result.locationList.length; x++) {
locationListHTML += '<div class="groupLocation">';
if (result.locationList[x].availability) {
locationListHTML += '<span class="text-success"><i class="fa fa-ok" aria-hidden="true"></i> '
+ result.locationList[x].location + '</span> ';
} else if (typeof(result.locationList[x].status_unknown) !== 'undefined'
&& result.locationList[x].status_unknown
) {
if (result.locationList[x].location) {
locationListHTML += '<span class="text-warning"><i class="fa fa-status-unknown" aria-hidden="true"></i> '
+ result.locationList[x].location + '</span> ';
}
} else {
locationListHTML += '<span class="text-danger"><i class="fa fa-remove" aria-hidden="true"></i> '
+ result.locationList[x].location + '</span> ';
}
locationListHTML += '</div>';
locationListHTML += '<div class="groupCallnumber">';
locationListHTML += (result.locationList[x].callnumbers)
? linkCallnumbers(result.locationList[x].callnumbers, result.locationList[x].callnumber_handler) : '';
locationListHTML += '</div>';
}
$item.find('.locationDetails').removeClass('hidden');
$item.find('.locationDetails').html(locationListHTML);
} else {
// Default case -- load call number and location into appropriate containers:
$item.find('.callnumber').empty().append(linkCallnumbers(result.callnumber, result.callnumber_handler) + '<br/>');
$item.find('.location').empty().append(
result.reserve === 'true'
? result.reserve_message
: result.location
);
}
$item.find('.locationDetails').removeClass('hidden');
$item.find('.locationDetails').html(locationListHTML);
} else {
// Default case -- load call number and location into appropriate containers:
$item.find('.callnumber').empty().append(linkCallnumbers(result.callnumber, result.callnumber_handler) + '<br/>');
$item.find('.location').empty().append(
result.reserve === 'true'
? result.reserve_message
: result.location
);
}
}
function itemStatusFail(response, textStatus) {
if (textStatus === 'abort' || typeof response.responseJSON === 'undefined') {
return;
}
// display the error message on each of the ajax status place holder
$('.js-item-pending .callnumAndLocation').addClass('text-danger').empty().removeClass('hidden')
.append(typeof response.responseJSON.data === 'string' ? response.responseJSON.data : VuFind.translate('error_occurred'));
}
var itemStatusIds = [];
var itemStatusEls = {};
var itemStatusTimer = null;
var itemStatusDelay = 200;
var itemStatusRunning = false;
function runItemAjaxForQueue() {
// Only run one item status AJAX request at a time:
if (itemStatusRunning) {
itemStatusTimer = setTimeout(runItemAjaxForQueue, itemStatusDelay);
return;
}
itemStatusRunning = true;
$.ajax({
var itemStatusObserver = null;
var ItemStatusHandler = {
name: "default",
//array to hold IDs and elements
itemStatusIds: [], itemStatusEls: [],
url: '/AJAX/JSON?method=getItemStatuses',
itemStatusRunning: false,
dataType: 'json',
method: 'POST',
url: VuFind.path + '/AJAX/JSON?method=getItemStatuses',
data: { 'id': itemStatusIds }
})
.done(function checkItemStatusDone(response) {
for (var j = 0; j < response.data.statuses.length; j++) {
var status = response.data.statuses[j];
displayItemStatus(status, itemStatusEls[status.id]);
itemStatusIds.splice(itemStatusIds.indexOf(status.id), 1);
itemStatusTimer: null,
itemStatusDelay: 200,
checkItemStatusDone: function checkItemStatusDone(response) {
var data = response.data;
for (var j = 0; j < data.statuses.length; j++) {
var status = data.statuses[j];
displayItemStatus(status, this.itemStatusEls[status.id]);
this.itemStatusIds.splice(this.itemStatusIds.indexOf(status.id), 1);
}
itemStatusRunning = false;
})
.fail(function checkItemStatusFail(response, textStatus) {
itemStatusFail(response, textStatus);
itemStatusRunning = false;
});
}
},
itemStatusFail: function itemStatusFail(response, textStatus) {
if (textStatus === 'error' || textStatus === 'abort' || typeof response.responseJSON === 'undefined') {
return;
}
// display the error message on each of the ajax status place holder
$('.js-item-pending .callnumAndLocation').addClass('text-danger').empty().removeClass('hidden')
.append(typeof response.responseJSON.data === 'string' ? response.responseJSON.data : VuFind.translate('error_occurred'));
},
itemQueueAjax: function itemQueueAjax(id, el){
clearTimeout(this.itemStatusTimer);
this.itemStatusIds.push(id);
this.itemStatusEls[id] = el;
this.itemStatusTimer = setTimeout(this.runItemAjaxForQueue.bind(this), this.itemStatusDelay);
el.addClass('js-item-pending').removeClass('hidden');
el.find('.callnumAndLocation').removeClass('hidden');
el.find('.callnumAndLocation .ajax-availability').removeClass('hidden');
el.find('.status').removeClass('hidden');
},
function itemQueueAjax(id, el) {
if (el.hasClass('js-item-pending')) {
return;
}
clearTimeout(itemStatusTimer);
itemStatusIds.push(id);
itemStatusEls[id] = el;
itemStatusTimer = setTimeout(runItemAjaxForQueue, itemStatusDelay);
el.addClass('js-item-pending').removeClass('hidden');
el.find('.callnumAndLocation').removeClass('hidden');
el.find('.callnumAndLocation .ajax-availability').removeClass('hidden');
el.find('.status').removeClass('hidden');
}
runItemAjaxForQueue: function runItemAjaxForQueue(){
if (this.itemStatusRunning) {
this.itemStatusTimer = setTimeout(this.runItemAjaxForQueue.bind(this), this.itemStatusDelay);
return;
}
$.ajax({
dataType: this.dataType,
method: this.method,
url: VuFind.path + this.url,
context: this,
data: { 'id': this.itemStatusIds }
})
.done(this.checkItemStatusDone)
.fail( this.itemStatusFail)
.always(function queueAjaxAlways(){
this.itemStatusRunning = false;
});
}//end runItemAjax
};
function checkItemStatus(el) {
var $item = $(el);
if ($item.find('.hiddenId').length === 0) {
return false;
}
var id = $item.find('.hiddenId').val();
itemQueueAjax(id + '', $item);
}
//store the handlers in a "hash" obj
//add you own overridden handler here
var checkItemHandlers = {
'ils': ItemStatusHandler,
};
var itemStatusObserver = null;
function checkItemStatuses(_container) {
var container = typeof _container === 'undefined'
? document.body
: _container;
function checkItemStatus(el) {
var $item = $(el);
if ($item.hasClass('js-item-pending')) {
return;
}
if ($item.find('.hiddenId').length === 0) {
return false;
}
var id = $item.find('.hiddenId').val();
var handlerName = 'ils';
if ($item.find('.handler-name').length > 0) {
handlerName = $item.find('.handler-name').val();
}
var ajaxItems = $(container).find('.ajaxItem');
for (var i = 0; i < ajaxItems.length; i++) {
var id = $(ajaxItems[i]).find('.hiddenId').val();
itemQueueAjax(id, $(ajaxItems[i]));
//queue the element into the handler
checkItemHandlers[handlerName].itemQueueAjax(id, $item);
}
// Stop looking for a scroll loader
if (itemStatusObserver) {
itemStatusObserver.disconnect();
function checkItemStatuses(_container) {
var container = typeof _container === 'undefined'
? document.body
: _container;
var ajaxItems = $(container).find('.ajaxItem');
for (var i = 0; i < ajaxItems.length; i++) {
var id = $(ajaxItems[i]).find('.hiddenId').val();
var handlerName = 'ils';
if ($(ajaxItems[i]).find('.handler-name').length > 0) {
handlerName = $(ajaxItems[i]).find('.handler-name').val();
}
if ($(ajaxItems[i]).data("handler-name")) {
handlerName = $(ajaxItems[i]).data("handler-name");
}
checkItemHandlers[handlerName].itemQueueAjax(id, $(ajaxItems[i]));
}
// Stop looking for a scroll loader
if (itemStatusObserver) {
itemStatusObserver.disconnect();
}
}
}
$(document).ready(function checkItemStatusReady() {
if (typeof Hunt === 'undefined') {
checkItemStatuses();
} else {
itemStatusObserver = new Hunt(
$('.ajaxItem').toArray(),
{ enter: checkItemStatus }
);
function init() {
if (typeof Hunt === 'undefined') {
checkItemStatuses();
} else {
itemStatusObserver = new Hunt(
$('.ajaxItem').toArray(),
{ enter: checkItemStatus }
);
}
}
return { init: init, check: checkItemStatuses };
});
/*global VuFind, checkItemStatuses, checkSaveStatuses */
/*global VuFind, checkSaveStatuses */
VuFind.combinedSearch = (function CombinedSearch() {
var init = function init(container, url) {
container.load(url, '', function containerLoad(responseText) {
......@@ -6,7 +6,7 @@ VuFind.combinedSearch = (function CombinedSearch() {
container.hide();
} else {
VuFind.openurl.init(container);
checkItemStatuses(container);
VuFind.itemStatuses.check(container);
checkSaveStatuses(container);
}
});
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment