Skip to content

Commit

Permalink
Add LoadMore and LoadMoreBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Jul 18, 2024
1 parent dc2b55e commit e44e96d
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
84 changes: 84 additions & 0 deletions asset/js/compat/LoadMoreBehavior.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
define(["../widget/LoadMore", "Icinga"],function (LoadMore, Icinga) {

"use strict";

class LoadMoreBehavior extends Icinga.EventListener {
constructor(icinga) {
super(icinga);

this.on('rendered', '#main > .container', this.onRendered, this);
this.on('load', this.onLoad, this);

/**
* Load More elements
*
* @type {WeakMap<object, LoadMore>}
* @private
*/
this._loadMoreElements = new WeakMap();
}

/**
* @param event
*/
onRendered(event)
{
let _this = event.data.self;

event.currentTarget.querySelectorAll('.load-more').forEach(element => {
_this._loadMoreElements.set(element, new LoadMore(element));
});
}

onLoad(event) {
let _this = event.data.self;
let anchor = event.target;
let showMore = anchor.parentElement;
var progressTimer = _this.icinga.timer.register(function () {
var label = anchor.innerText;

var dots = label.substr(-3);
if (dots.slice(0, 1) !== '.') {
dots = '. ';
} else {
label = label.slice(0, -3);
if (dots === '...') {
dots = '. ';
} else if (dots === '.. ') {
dots = '...';
} else if (dots === '. ') {
dots = '.. ';
}
}

anchor.innerText = label + dots;
}, null, 250);

let url = anchor.getAttribute('href');

let req = _this.icinga.loader.loadUrl(
// Add showCompact, we don't want controls in paged results
_this.icinga.utils.addUrlFlag(url, 'showCompact'),
$(showMore.parentElement),
undefined,
undefined,
'append',
false,
progressTimer
);
req.addToHistory = false;
req.done(function () {
showMore.remove();

// Set data-icinga-url to make it available for Icinga.History.getCurrentState()
req.$target.closest('.container').data('icingaUrl', url);

_this.icinga.history.replaceCurrentState();
});

return req;
}
}

return LoadMoreBehavior;
});
49 changes: 49 additions & 0 deletions asset/js/widget/LoadMore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
define(["../notjQuery"], function ($) {

"use strict";

class LoadMore {
/**
* @param element The element that contains the load-more anchor
*/
constructor(element) {
$(element).on('click', '.load-more[data-no-icinga-ajax] a', this.onLoadMoreClick, this);
$(element).on('keypress', '.load-more[data-no-icinga-ajax] a', this.onKeyPress, this);
}

/**
* Keypress (space) on load-more button
*
* @param event
*/
onKeyPress(event) {
if (event.key === ' ') {
this.onLoadMoreClick(event);
}
}

/**
* Click on load-more button
*
* @param event
*/
onLoadMoreClick(event) {
event.stopPropagation();
event.preventDefault();

this.loadMore(event.target);
}


/**
* Load more items based on the given anchor
*
* @param anchor
*/
loadMore(anchor) {
$(anchor).trigger('load');
}
}

return LoadMore;
});

0 comments on commit e44e96d

Please sign in to comment.