Forked from Mikhail-Zakharov/Collapse categories_Ver2.js
Last active
December 15, 2015 04:49
-
-
Save mozz100/5203829 to your computer and use it in GitHub Desktop.
Add this as a global javascript widget in Zendesk to achieve collapsible category headers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var collapsed_by_default = false; // set this to true if you prefer collapsed by default | |
function HideCategory(id, IsAgent, animation) { | |
// Function hides category | |
$j("div#category_" + id).hide(animation); | |
SetDownArrow(id, IsAgent); | |
$j.cookie('hide_category_' + id, 'yes', { expires: 180 }); | |
$j("div#category_header_" + id).attr("onclick", "ShowCategory(" + id + "," +IsAgent+ ",'blind')"); | |
}; | |
function ShowCategory(id, IsAgent, animation) { | |
// Function shows category | |
$j("div#category_" + id).show(animation); | |
SetUpArrow(id, IsAgent); | |
$j.cookie('hide_category_' + id, 'no', { expires: 180 }); | |
$j("div#category_header_" + id).attr("onclick", "HideCategory(" + id + "," +IsAgent+ ",'blind')"); | |
}; | |
function SetUpArrow(id, IsAgent){ | |
// Show Up arrow. | |
// Script uses image from Zendesk resources https://www.zendesk.com/wp-content/themes/zendesk-twentyeleven/img/support/icons-toggle.png | |
// Better to store it in other place | |
// Trick: agent's markup is different than end-users. We should insert arrow by different ways | |
if (IsAgent==1) | |
{ | |
$j("div#category_header_" + id).children('div.category-name').css('background','#f8f8f8 url(https://www.zendesk.com/wp-content/themes/zendesk-twentyeleven/img/support/icons-toggle.png) no-repeat right 5px'); | |
} else { | |
$j("div#category_header_" + id).children('div.category-header').css('background','#f8f8f8 url(https://www.zendesk.com/wp-content/themes/zendesk-twentyeleven/img/support/icons-toggle.png) no-repeat 90% 18px'); | |
} | |
} | |
function SetDownArrow(id, IsAgent){ | |
// Show Down arrow. | |
// Script uses image from Zendesk resources https://www.zendesk.com/wp-content/themes/zendesk-twentyeleven/img/support/icons-toggle.png | |
// Better to store it in other place | |
// Trick: agent's markup is different than end-users. We should insert arrow by different ways | |
if (IsAgent==1) | |
{ | |
$j("div#category_header_" + id).children('div.category-name').css('background','#f8f8f8 url(https://www.zendesk.com/wp-content/themes/zendesk-twentyeleven/img/support/icons-toggle.png) no-repeat right -50px'); | |
} else { | |
$j("div#category_header_" + id).children('div.category-header').css('background','#f8f8f8 url(https://www.zendesk.com/wp-content/themes/zendesk-twentyeleven/img/support/icons-toggle.png) no-repeat 90% -37px'); | |
} | |
} | |
// Get current url | |
var s_url = window.location.pathname; | |
var s_urlparts = s_url.split('/'); | |
var s_section = s_urlparts[1]; | |
// Run if we in the "forums" section | |
if (s_section == 'forums' || s_section == 'home') { | |
$j("span.follow_link").hide(); //remove follow link | |
// Get all sections from page | |
var cat_headers = $j("div.category-header"); | |
$j.each(cat_headers, function () { | |
var base = $j(this).children("div.category-name"); | |
var IsAgent = 1; | |
if (base.length == 0) { | |
// Is non agent if that | |
base = $j(this); | |
IsAgent=0; | |
} | |
var child = base.children("h2").children("a"); | |
if (child.length != 0) { | |
// "0" means No Category Section | |
var id = child.attr("href") | |
var id_splitted = id.split("/"); | |
if (id_splitted.length == 3) { | |
var id_value = id_splitted[2].split("-")[0]; | |
base.parent().attr("onclick","HideCategory(" + id_value + "," +IsAgent+ ")"); | |
if (IsAgent==0) { | |
// Trick: we should add ID because it absent in end-user markup | |
base.parent().attr("id","category_header_" + id_value); | |
} | |
// underline links | |
child.mouseover(function(){$j(this).css("text-decoration","underline")}); | |
child.mouseleave(function(){$j(this).css("text-decoration","none")}); | |
base.parent().css("cursor","pointer"); | |
var ckie = $j.cookie('hide_category_' + id_value); | |
if ((ckie == 'yes') || (collapsed_by_default && !(ckie))) { | |
HideCategory(id_value, IsAgent); | |
} else { | |
ShowCategory(id_value, IsAgent); | |
} | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment