63 lines
1.9 KiB
JavaScript
Vendored
63 lines
1.9 KiB
JavaScript
Vendored
(function ($) {
|
|
/**
|
|
* @param {object} _options
|
|
*/
|
|
$.fn.loadMoreNews = function (_options) {
|
|
var isLoading = false;
|
|
var page = 1;
|
|
var options = {
|
|
loadingElementId: '#loading',
|
|
container: '',
|
|
url: document.location.pathname,
|
|
nextPage: 2,
|
|
dateStart: null,
|
|
dateEnd:null
|
|
};
|
|
|
|
$.extend(options, _options);
|
|
var $isLoading = $(options.loadingElementId, this);
|
|
$isLoading.hide();
|
|
|
|
this.unbind('click').click(function (e) {
|
|
e.preventDefault();
|
|
if (!isLoading) {
|
|
// Set flag and update UI
|
|
isLoading = 1;
|
|
$isLoading.show();
|
|
var $button = $(this).attr("disabled", "disabled");
|
|
var $container = $(options.container)
|
|
|
|
// Fire request for the next page
|
|
$.ajax({url: options.url + (options.url.indexOf('?') >= 0 ? '&' : '?') + 'pagina=' + options.nextPage + (options.dateStart ? '&dateStart=' + options.dateStart : '') + (options.dateEnd ? '&dateEnd=' + options.dateEnd : '')})
|
|
.always(function () {
|
|
// Whether success or failure, update the UI again
|
|
isLoading = 0;
|
|
$isLoading.hide();
|
|
$button.removeAttr("disabled");
|
|
})
|
|
.done(function (data) {
|
|
if (!data) {
|
|
// When no data was returned, disable the button permanently
|
|
page = -1;
|
|
$button.attr("disabled", "disabled").text("Geen nieuws meer.");
|
|
return;
|
|
}
|
|
|
|
$container.each(function () {
|
|
var id = this.toString();
|
|
$(id).append($('<div>'+data+'</div>').find(id).length ? $('<div>'+data+'</div>').find(id).children() : $(data));
|
|
});
|
|
++options.nextPage;
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
}(jQuery));
|
|
|
|
$(function () {
|
|
$('[data-loadmorenews]').each(function () {
|
|
$(this).loadMoreNews($(this).data('loadmorenews'));
|
|
});
|
|
});
|