Last active
June 26, 2016 17:16
-
-
Save kyleschmolze/57b1baa0f850467d4a80 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
### | |
This super simple script loads SlimFAQ content into any page. | |
Specify a category from SlimFAQ by it's URL, and put a simple | |
template into the HTML, and this script will auto-load those questions | |
into the page. When clicked, the links will open inside the SlimFAQ sidebar. | |
Sample html template: | |
<div data-slimfaq-category='http://faq.groupmuse.com/323-hosting' data-slimfaq-limit=2> | |
<h2 data-slimfaq-category-name></h2> | |
<p data-slimfaq-question-template></p> | |
</div> | |
### | |
$(document).ready -> | |
if $("[data-slimfaq-category]").length | |
$("[data-slimfaq-category]").each -> | |
endpoint = $(this).attr 'data-slimfaq-category' | |
endpoint += '.json' unless endpoint.match /\.json^/ | |
$.get(endpoint) | |
.success (json) => | |
questions = json.questions | |
# if it doesn't have questions, it's not a category | |
return unless questions? | |
# Insert the category name, if it exists | |
$(this).find("[data-slimfaq-category-name]").text json.name | |
# Use this template for questions | |
question_template = $(this).find("[data-slimfaq-question-template]") | |
# Clone template, insert <a> tag for each question | |
for question in questions | |
clone = question_template.clone() | |
# find the first <a> tag in the template, or insert a new one | |
link = clone.find('a').eq(0) | |
if !link.length | |
link = $("<a>") | |
clone.prepend link | |
link.text question.name | |
link.attr 'href', question.url | |
# attach a click handler so that the link opens the Slimfaq sidebar | |
# (if it's loaded on the page) instead of redirecting | |
link.click (e) -> | |
# if Slimfaq isn't defined, don't do interupt | |
return true unless Slimfaq? | |
e.preventDefault() | |
Slimfaq.SlimfaqButton.hide() | |
Slimfaq.SlimfaqSidebar.show() | |
$(Slimfaq.SlimfaqContainer).find('iframe').attr 'src', $(this).attr('href') | |
clone.insertBefore question_template | |
# remove template | |
question_template.remove() | |
.error => # If we don't get a 200 from SlimFaq | |
$(this).text "Sorry, we couldn't load our FAQ." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment