Skip to content

Instantly share code, notes, and snippets.

@vagnerolliver2
Created April 7, 2015 21:48
Show Gist options
  • Save vagnerolliver2/0d6939482c66d4d62d67 to your computer and use it in GitHub Desktop.
Save vagnerolliver2/0d6939482c66d4d62d67 to your computer and use it in GitHub Desktop.
Javascript do Template Zendesk
/*
* jQuery v1.9.1 included
*/
$(document).ready(function() {
// social share popups
$(".share a").click(function(e) {
e.preventDefault();
window.open(this.href, "", "height = 500, width = 500");
});
// toggle the share dropdown in communities
$(".share-label").on("click", function(e) {
e.stopPropagation();
var isSelected = this.getAttribute("aria-selected") == "true";
this.setAttribute("aria-selected", !isSelected);
$(".share-label").not(this).attr("aria-selected", "false");
});
$(document).on("click", function() {
$(".share-label").attr("aria-selected", "false");
});
// show form controls when the textarea receives focus or backbutton is used and value exists
var $answerbodyTextarea = $(".answer-body textarea"),
$answerFormControls = $(".answer-form-controls"),
$commentContainerTextarea = $(".comment-container textarea"),
$commentContainerFormControls = $(".comment-form-controls");
$answerbodyTextarea.one("focus", function() {
$answerFormControls.show();
});
$commentContainerTextarea.one("focus", function() {
$commentContainerFormControls.show();
});
if($commentContainerTextarea.val() !== "") {
$commentContainerFormControls.show();
}
if($answerbodyTextarea.val() !== "") {
$answerFormControls.show();
}
// Custom
$(".search-box .search input[type=submit]").val("")
$(".category-tree .category").each(function(){
$(this).find("section").css({ 'display':'none' })
$("<a class='toggle'>+</a>").appendTo($(this).find("h2"))
})
$(".category-tree .category h2").click(function(){
$(this).parent().find("section").slideToggle( "fast", function() {
console.log("as")
if($( this ).parent().find( 'h2 a.toggle' ).html() == '+'){
$( this ).parent().find( 'h2 a.toggle' ).html("-")
}else{
$( this ).parent().find( 'h2 a.toggle' ).html("+")
}
});
})
$(".info-box-body p span.toggle").click(function(){
$(this).closest(".info-box-body").find(".phone ").slideToggle( "fast", function() {
if( $( this ).closest(".info-box-body").find( 'span.toggle' ).html() == '+') {
$( this ).closest(".info-box-body").find( 'span.toggle' ).html("-")
} else {
$( this ).closest(".info-box-body").find( 'span.toggle' ).html("+")
}
});
})
//load side bar
$(".category-tree").ready(function(){
$(".category-tree").html('');
$.getJSON("/api/v2/help_center/categories", function(response){
$.each(response.categories, function(index, category){
var title1 = category.name;
var container = $(".category-tree").append('<section class="category" id="cat_'+category.id+'"></section>');
var last_category = container.find(".category").last();
last_category.append('<h2><a href="#" class="category_title">'+title1+'</a><a class="toggle">+</a></h2>');
$.getJSON("/api/v2/help_center/categories/"+category.id+"/sections", function(response){
$.each(response.sections, function(index, section){
last_category.append('<section class="section-menu" id="sec_'+section.id+'" style="display: none;"><h3> <a href="'+section.html_url+'">'+section.name+'</a> </h3></section>');
});
}).done(function() {
var section_id = document.URL.split("/").pop();
$("#sec_"+section_id).closest(".category").find(".section-menu").show();
});
});
});
});
$('.category-tree').on('click', '.category h2 a', function(evt) {
evt.preventDefault();
var $toggle = $(evt.currentTarget).parents('h2').find('a.toggle');
$toggle.html($toggle.html() === '+' ? '-' : '+');
var items = $(this).closest(".category").find(".section-menu");
if ($toggle.html() === '+') {
items.hide();
} else {
items.show();
}
});
//load articles
$(".faq-content").ready(function(){
var section_id = document.URL.split("/").pop();
$(".faq-list").html('');
$.getJSON("/api/v2/help_center/sections/"+section_id+"/articles", function(response){
$.each(response.articles, function(index, article){
$(".faq-list").append('<section class="article" id="art_'+article.id+'"><h3><a href="'+article.html_url+'" class="article_title">'+article.name+'</a><a class="toggle">+</a> </h3><section class="article-body">'+article.body+'</section></section>');
});
});
});
$('.faq-content').on('click', '.article > h3 > a', function(evt) {
evt.preventDefault();
var $toggle = $(evt.currentTarget).parents('h3').find('a.toggle');
$toggle.html($toggle.html() === '+' ? '-' : '+');
var article = $(this).closest('.article').find(".article-body");
if ($toggle.html() === '+') {
article.hide();
} else {
article.show();
}
});
//right panel
$(".info-boxes").ready(function(){
$(".info-boxes").load("/hc/pt-br .info-boxes");
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment