Forum Discussion
Hoekstra_VFZ
Advisor
steffiatebay sure! We made improvements in the mean time. We now use a toggle switch component in the sidebar like this:
Below is the code of the component with some stuff edited / translated for you. The JavaScript part is at the end of the code. This only works on forums, ideas and blogs and their overview pages. It does not work on category / node level.
<@liaMarkupCache ttl="30000" variation="node" anonymousOnly="false" />
<@compress single_line=true>
<#if (conditions to exclude certain type of nodes where the toggle is not active)>
<#assign defaultLink = "/t5/user/userloginpage?redirectreason=loginToSubscribe" />
<#assign buttonClassName ="lia-link-ticket-post-action" />
<#assign actionText = "E-mail notifications" />
<#assign ToggleTextSubscribed= "Off" />
<#assign ToggleTextUnsubscribed = "On" />
<#assign altText = "" />
<#assign subscribeTarget = "" />
<#assign unsubscribeTarget = "" />
<#if page.name == "ForumTopicPage">
<#assign altText = "Receive an update on new comments in this topic." />
<#assign subscribeTarget = "#addThreadUserEmailSubscription" />
<#assign unsubscribeTarget = "#removeThreadUserEmailSubscription" />
<#elseif page.name == "BlogArticlePage">
<#assign altText = "Receive an update on new comments in this article." />
<#assign subscribeTarget = "#addThreadUserEmailSubscription" />
<#assign unsubscribeTarget = "#removeThreadUserEmailSubscription" />
<#elseif page.name == "IdeaPage">
<#assign altText = "Receive an update on new comments on this idea." />
<#assign subscribeTarget = "#addThreadUserEmailSubscription" />
<#assign unsubscribeTarget = "#removeThreadUserEmailSubscription" />
<#elseif page.name == "ForumPage">
<#assign altText = "Receive an update on new topics in ${coreNode.shortTitle}." />
<#assign subscribeTarget = "#addBoardUserEmailSubscription" />
<#assign unsubscribeTarget = "#removeBoardUserEmailSubscription" />
<#elseif page.name == "BlogPage">
<#assign altText = "Receive an update on new articles in ${coreNode.shortTitle}." />
<#assign subscribeTarget = "#addBoardUserEmailSubscription" />
<#assign unsubscribeTarget = "#removeBoardUserEmailSubscription" />
<#elseif page.name == "IdeaExchangePage">
<#assign altText = "Receive an update on new ideas in ${coreNode.shortTitle}" />
<#assign subscribeTarget = "#addBoardUserEmailSubscription" />
<#assign unsubscribeTarget = "#removeBoardUserEmailSubscription" />
</#if>
<style>
/* This is where I styled the toggle switch */
</style>
<#-- Build HTML of the component -->
<div class = "subscribe-unit">
<div id="action">
<span id="actionText">${actionText}</span>
<a href="${defaultLink}" target="_blank" id="vfz-subscribe-button" class="${buttonClassName}" alt="${altText}" title="${altText}">
<div class="vfz-toggle-container">
<div class="vfz-toggle">
<div class="vfz-toggle__helper">
<span class="vfz-toggle__slider" id="toggleSlider"></span>
<span class="vfz-toggle__value" id="toggleOn">${ToggleTextUnsubscribed}</span>
<span class="vfz-toggle__value" id="toggleOff">${ToggleTextSubscribed}</span>
</div>
</div>
</div>
</a>
</div>
<p id="altText">${altText}</p>
</div>
<#-- JavaScript link retrieval for logged in users -->
<#if !user.anonymous>
<script>
window.addEventListener('DOMContentLoaded', (event) => {
function duplicateSubscribeLinkToTopicStatus() {
let subscribeLink = document.querySelector('${subscribeTarget}');
let unsubscribeLink = document.querySelector('${unsubscribeTarget}');
var targetLink = document.getElementById('vfz-subscribe-button');
if (subscribeLink) {
var newLink = subscribeLink.cloneNode(true);
newLink.innerHTML = targetLink.innerHTML;
newLink.className = "${buttonClassName}";
}
if (unsubscribeLink) {
let a = document.querySelector('#toggleOn');
let b = document.querySelector('#toggleOff');
let c = document.querySelector('.vfz-toggle__slider');
a.style.opacity = '1';
b.style.opacity = '0';
c.style.backgroundColor = '#f48c00';
c.classList.add('vfz-toggle__slider--on');
var newLink = unsubscribeLink.cloneNode(true);
newLink.innerHTML = targetLink.innerHTML;
newLink.className = "${buttonClassName}";
}
if (targetLink) {
targetLink.parentNode.replaceChild(newLink, targetLink);
}
}
duplicateSubscribeLinkToTopicStatus();
});
</script>
</#if>
</#if>
</@compress>
Anyone who will use this code, please let us know 🙂 If you have improvements, please share.
irach15
2 years agoMaven
Hey,
we've done something like this for a standing alone subscribe and unsubscribe btns
custom subscribe button component for articles and blogs
<#import "theme-lib.common-functions.ftl" as common />
<#if !user.anonymous>
<#import "common-functions" as utility />
<#attempt>
<#if page.name == 'BlogArticlePage' || page.name == 'TkbArticlePage'>
<#assign id = page.context.thread.topicMessage.uniqueId!'' />
<#assign substype = "threads">
<#else>
<#assign id = coreNode.id!'' />
<#if page.name == "BlogPage">
<#assign substype = "blogs">
<#elseif page.name == "TkbPage">
<#assign substype = "boards">
</#if>
</#if>
<#assign checkSubscription = rest("/${substype}/id/${id}/subscriptions/users/self/type/email").subscription />
<#if checkSubscription.id?has_content>
<a class="lia-link-navigation unsubscribe-btn" <#if user.anonymous>disabled="disabled"</#if> id="unsubscribe-btn" href="javascript:void(0);">
<span class="lia-button lia-button-primary">Unsubscribe</span>
</a>
<#else>
<a class="lia-link-navigation subscribe-btn" <#if user.anonymous>disabled="disabled"</#if> id="subscribe-btn" href="javascript:void(0);">
<span class="lia-button lia-button-primary">${text.format("custom.subscribe-text")}</span>
</a>
</#if>
<#assign endpointUrl = common.getEndpointUrl("custom-subscribe-blogs") />
<@liaAddScript>
(function($) {
$(document).ready(function() {
$('.subscribe-btn, .unsubscribe-btn').click(function() {
var bId = '${id?js_string}';
var substype = '${substype?js_string}';
var classbtn = $(this).attr('id') || '';
$(this).attr('disabled', 'true');
$.ajax({
url: '${endpointUrl}',
data: {'Id': bId, 'ClassBtn': classbtn, 'substype': substype},
type: 'post',
success: function(data) {
location.reload();
}
});
});
});
})(LITHIUM.jQuery);
</@liaAddScript>
<#recover>
<!-- error in custom-subscribe-blog -->
</#attempt>
</#if>
calling endpoint component in the code above
<#compress>
<#import "theme-lib.common-functions.ftl" as common />
<#if user.registered && common.validEndpointRequest(false, true,false)>
<#setting url_escaping_charset="UTF-8" />
<#setting number_format="0.######" />
<#assign ID = http.request.parameters.name.get("Id", "") />
<#assign Class = http.request.parameters.name.get("ClassBtn", "") />
<#assign substype = http.request.parameters.name.get("substype", "") />
<#if user.anonymous || (ID?length lt 1) || (Class?length lt 1) || (substype?length lt 1)>
<p>Not permitted</p>
<#else>
<#if Class == "subscribe-btn">
<#assign subscribed = restadmin("/${substype}/id/${ID}/subscriptions/users/self/add?subscription.type=email") />
${subscribed.@status}
<p>Subscribed successfully</p>
<#elseif Class == "unsubscribe-btn">
<#assign subscribed = restadmin("/${substype}/id/${ID}/subscriptions/users/self/type/email/remove") />
${subscribed.@status}
<p>UnSubscribed successfully</p>
</#if>
</#if>
</#if>
</#compress>
definitely needs some css, we have our css and scheme , you should code yours
Related Content
- 10 months ago
- 2 years ago
- 2 years ago