friendica-addons-sekb/showmore_dyn/showmore_dyn.js

79 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-03-12 22:24:54 +00:00
$(document).ready(function(){
$("head").append('<style type="text/css"></style>');
var newStyleElement = $("head").children(':last');
newStyleElement.html('.limit-height{max-height: ' + postLimitHeight + 'px; overflow: hidden;}');
2020-03-13 21:09:21 +00:00
handleNewWallItemBodies();
2020-03-12 22:24:54 +00:00
2020-03-13 21:09:21 +00:00
document.addEventListener("postprocess_liveupdate", function() {
handleNewWallItemBodies();
});
2020-03-12 22:24:54 +00:00
});
function handleNewWallItemBodies() {
2020-03-13 21:09:21 +00:00
$('.wall-item-body:not(.showmore-done)').each(function() {
var $el = $(this);
$el.addClass('showmore-done');
if ($el.has('button.content-filter-button').length > 0) {
$el.removeClass('limitable');
return;
}
2020-03-12 22:24:54 +00:00
2020-03-13 21:09:21 +00:00
addHeightToggleHandler($el);
var limited = processHeightLimit($el);
2020-03-12 22:24:54 +00:00
2020-03-13 21:09:21 +00:00
if (!limited) {
var mutationObserver = new MutationObserver(function(mutations) {
var limited = processHeightLimit($el);
if (limited) {
mutationObserver.disconnect()
}
});
mutationObserver.observe($el[0], { attributes: true, characterData: true, childList: true, subtree: true, attributeOldValue: true, characterDataOldValue: true });
2020-03-12 22:24:54 +00:00
2020-03-13 21:09:21 +00:00
$el.imagesLoaded().then(function(){
processHeightLimit($el);
});
}
});
2020-03-12 22:24:54 +00:00
}
2020-03-13 21:09:21 +00:00
function addHeightToggleHandler($item) {
var itemId = parseInt($item.attr("id").replace("wall-item-body-", ""));
$item.data("item-id", itemId);
var wrapperId = "wall-item-body-wrapper-" + itemId;
var toggleId = "wall-item-body-toggle-" + itemId;
2020-03-12 22:24:54 +00:00
2020-03-13 21:09:21 +00:00
$item.wrap('<div id="' + wrapperId + '" class="wall-item-body-wrapper"></div>');
$("#" + wrapperId).append('<div class="wall-item-body-toggle" data-item-id="' + itemId + '" id="' + toggleId + '" ><a href="javascript:void(0)" class="wall-item-body-toggle-text">Show more ...</a></div>');
$item.addClass("limitable limit-height");
2020-03-12 22:24:54 +00:00
2020-03-13 21:09:21 +00:00
var $toggle = $("#" + toggleId);
$toggle.show();
$toggle.click(function(el) {
$item.toggleClass("limit-height");
$(this).hide();
$item.removeClass("limitable");
});
2020-03-12 22:24:54 +00:00
}
2020-03-13 21:09:21 +00:00
function processHeightLimit($item) {
if (!$item.hasClass("limitable")) {
return false;
}
2020-03-12 22:24:54 +00:00
2020-03-13 21:09:21 +00:00
var itemId = $item.data("item-id");
var $toggle = $("#wall-item-body-toggle-" + itemId);
if ($item.height() < postLimitHeight) {
2020-03-13 21:09:21 +00:00
$item.removeClass("limit-height");
$toggle.hide();
return false;
} else {
$item.addClass("limit-height");
$toggle.show();
return true;
}
2020-03-12 22:24:54 +00:00
}