forked from Way2CU/SBS-Site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faq.js
92 lines (75 loc) · 1.63 KB
/
faq.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Frequently Asked Questions
* Licemaster
*
* Copyright (c) 2013. by Way2CU
* Author: Mladen Mijatov
*/
function FAQ() {
var self = this;
self.questions = [];
self.answers = [];
self.current = null;
/**
* Complete object initialization.
*/
self.init = function() {
$('div#faq dt').each(function(index) {
var question = $(this);
var answer = question.next();
question
.data('index', index)
.click(self.__handleClick);
answer.hide();
self.questions.push(question);
self.answers.push(answer);
});
};
/**
* Handle clicking on question container.
*
* @param object event
*/
self.__handleClick = function(event) {
var question = $(this);
var index = question.data('index');
if (!question.hasClass('expanded'))
self.expandAnswer(index); else
self.collapseAnswer(index);
// prevent default click behavior
event.preventDefault();
};
/**
* Show answer for specified question.
*
* @param integer index
*/
self.expandAnswer = function(index) {
var question = self.questions[index];
var answer = self.answers[index];
// collapse existing answer
if (self.current != null)
self.collapseAnswer(self.current)
// expand current
question.addClass('expanded');
answer.slideDown();
// save current index
self.current = index;
};
/**
* Hide answer for specified question.
*
* @param integer index
*/
self.collapseAnswer = function(index) {
var question = self.questions[index];
var answer = self.answers[index];
question.removeClass('expanded')
answer.slideUp();
};
// finish object initialization
self.init();
}
$(function() {
new FAQ();
});