Skip to content
Snippets Groups Projects
Commit 4566ee96 authored by Chris Hallberg's avatar Chris Hallberg
Browse files

Lightbox: private emit function and checkstyles.

parent 66e0510e
No related merge requests found
/*global $, document, Event, VuFind, window */ /*global $, document, CustomEvent, VuFind, window */
VuFind.lightbox = (function() { VuFind.lightbox = (function() {
// State // State
var _originalUrl = false; var _originalUrl = false;
...@@ -7,10 +7,41 @@ VuFind.lightbox = (function() { ...@@ -7,10 +7,41 @@ VuFind.lightbox = (function() {
// Elements // Elements
var _modal, _modalBody, _clickedButton = null; var _modal, _modalBody, _clickedButton = null;
// Utilities // Utilities
var _storeClickedStatus = function() {
_clickedButton = this;
};
var _html = function(html) { var _html = function(html) {
_modalBody.html(html); _modalBody.html(html);
_modal.modal('handleUpdate'); _modal.modal('handleUpdate');
}; };
var _emit = function(msg, details) {
if ('undefined' == typeof details) {
details = {};
}
document.dispatchEvent(
new CustomEvent(msg, {
detail: details,
bubbles: true,
cancelable: true
})
);
};
/**
* Reload the page without causing trouble with POST parameters while keeping hash
*/
var _refreshPage = function() {
var parts = window.location.href.split('#');
if (typeof parts[1] === 'undefined') {
window.location.href = window.location.href;
} else {
var href = parts[0];
// Force reload with a timestamp
href += href.indexOf('?') == -1 ? '?_=' : '&_=';
href += new Date().getTime() + '#' + parts[1];
window.location.href = href;
}
};
// Public: Present an alert // Public: Present an alert
var showAlert = function(message, type) { var showAlert = function(message, type) {
if ('undefined' == typeof type) { if ('undefined' == typeof type) {
...@@ -33,7 +64,9 @@ VuFind.lightbox = (function() { ...@@ -33,7 +64,9 @@ VuFind.lightbox = (function() {
* data-lightbox-ignore = do not submit this form in lightbox * data-lightbox-ignore = do not submit this form in lightbox
*/ */
var _update = function(html) { var _update = function(html) {
if (!html.match) return; if (!html.match) {
return;
}
// Isolate successes // Isolate successes
var htmlDiv = $('<div>'+html+'</div>'); var htmlDiv = $('<div>'+html+'</div>');
var alerts = htmlDiv.find('.alert-success'); var alerts = htmlDiv.find('.alert-success');
...@@ -68,10 +101,6 @@ VuFind.lightbox = (function() { ...@@ -68,10 +101,6 @@ VuFind.lightbox = (function() {
}); });
}; };
var _storeClickedStatus = function() {
_clickedButton = this;
};
var _xhr = false; var _xhr = false;
// Public: Handle AJAX in the Lightbox // Public: Handle AJAX in the Lightbox
var ajax = function(obj) { var ajax = function(obj) {
...@@ -96,26 +125,20 @@ VuFind.lightbox = (function() { ...@@ -96,26 +125,20 @@ VuFind.lightbox = (function() {
return; return;
} }
if ( // Close the lightbox after deliberate login if ( // Close the lightbox after deliberate login
obj.method // is a form obj.method // is a form
&& !html.match(/alert alert-danger/) // skip failed logins && !html.match(/alert alert-danger/) // skip failed logins
&& ((obj.url.match(/MyResearch/) && !obj.url.match(/Bulk/)) // that matches login/create account && ((obj.url.match(/MyResearch/) && !obj.url.match(/Bulk/)) // that matches login/create account
|| obj.url.match(/catalogLogin/)) // catalog login for holds || obj.url.match(/catalogLogin/)) // or catalog login for holds
) { ) {
if (_originalUrl.match(/UserLogin/) || obj.url.match(/catalogLogin/)) { if (_originalUrl.match(/UserLogin/) || obj.url.match(/catalogLogin/)) {
var event = new CustomEvent('VuFind.lightbox.login', { _refreshPage();
detail: {
originalUrl: _originalUrl,
formUrl: obj.url
},
bubbles: true,
cancelable: true
});
if (document.dispatchEvent(event)) {
_refreshPage();
}
return false; return false;
} else { } else {
VuFind.lightbox.refreshOnClose = true; VuFind.lightbox.refreshOnClose = true;
_emit('VuFind.lightbox.login', {
originalUrl: _originalUrl,
formUrl: obj.url
});
} }
} }
_update(html); _update(html);
...@@ -129,6 +152,17 @@ VuFind.lightbox = (function() { ...@@ -129,6 +152,17 @@ VuFind.lightbox = (function() {
ajax({url:_currentUrl || _originalUrl}); ajax({url:_currentUrl || _originalUrl});
}; };
/**
* Evaluate a callback
*/
var _evalCallback = function(callback, event, data) {
if ('function' === typeof window[callback]) {
return window[callback](event, data);
} else {
return eval('(function(event, data) {' + callback + '}())'); // inline code
}
};
/** /**
* Modal link data options * Modal link data options
* *
...@@ -219,33 +253,6 @@ VuFind.lightbox = (function() { ...@@ -219,33 +253,6 @@ VuFind.lightbox = (function() {
return false; return false;
}; };
/**
* Evaluate a callback
*/
var _evalCallback = function(callback, event, data) {
if ('function' === typeof window[callback]) {
return window[callback](event, data);
} else {
return eval('(function(event, data) {' + callback + '}())'); // inline code
}
};
/**
* Reload the page without causing trouble with POST parameters while keeping hash
*/
var _refreshPage = function() {
var parts = window.location.href.split('#');
if (typeof parts[1] === 'undefined') {
window.location.href = window.location.href;
} else {
var href = parts[0];
// Force reload with a timestamp
href += href.indexOf('?') == -1 ? '?_=' : '&_=';
href += new Date().getTime() + '#' + parts[1];
window.location.href = href;
}
};
// Public: Attach listeners to the page // Public: Attach listeners to the page
var bind = function(target) { var bind = function(target) {
if ('undefined' === typeof target) { if ('undefined' === typeof target) {
...@@ -289,11 +296,11 @@ VuFind.lightbox = (function() { ...@@ -289,11 +296,11 @@ VuFind.lightbox = (function() {
if (VuFind.lightbox.refreshOnClose) { if (VuFind.lightbox.refreshOnClose) {
_refreshPage(); _refreshPage();
} }
document.dispatchEvent(new Event('VuFind.lightbox.closing')); _emit('VuFind.lightbox.closing');
}); });
_modal.on('hidden.bs.modal', function() { _modal.on('hidden.bs.modal', function() {
document.dispatchEvent(new Event('VuFind.lightbox.closed'));
VuFind.lightbox.reset(); VuFind.lightbox.reset();
_emit('VuFind.lightbox.closed');
}); });
VuFind.modal = function(cmd) { _modal.modal(cmd); }; VuFind.modal = function(cmd) { _modal.modal(cmd); };
......
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