-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdepage-details.js
145 lines (123 loc) · 4.48 KB
/
depage-details.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* @require framework/shared/jquery-1.8.3.js
*
* @file depage-details.js
*
* adds details handler to definition-lists
*
*
* copyright (c) 2011-2015 Frank Hellenkamp [jonas@depage.net]
*/
;(function($){
if(!$.depage){
$.depage = {};
}
$.depage.details = function(el, options){
// To avoid scope issues, use 'base' instead of 'this' to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("depage.details", base);
base.$heads = null;
base.$activeDetailHead = null;
// {{{ init()
base.init = function() {
base.options = $.extend({},$.depage.details.defaultOptions, options);
base.$heads = $(base.options.head, base.el).each(function() {
var $head = $(this).css({
cursor: "pointer"
});
var $detail = $head.nextUntil(base.options.head);
$head.data("$detail", $detail);
$(this).prepend("<span class=\"opener\"></span>");
if ($head.hasClass("active")) {
base.$activeDetailHead = $head;
} else {
$detail.hide();
}
$head.on("click", base.toggleDetail);
});
};
// }}}
// {{{ toggleDetail()
base.toggleDetail = function() {
var $head = $(this);
if (!$head.hasClass("active")) {
base.showDetail($head);
} else {
base.hideDetail($head);
}
};
// }}}
// {{{ showDetail()
base.showDetail = function($head, customSpeed) {
if ($head && $head != base.$activeDetailHead) {
customSpeed = (typeof customSpeed === "undefined") ? base.options.speed : customSpeed;
var $detail = $head.data("$detail");
if (base.options.correctScroll && customSpeed !== 0) {
base.correctScroll(base.$activeDetailHead, $head);
}
base.hideDetail(base.$activeDetailHead);
$head.addClass("active");
$detail.addClass("active");
$detail.slideDown(customSpeed);
base.$activeDetailHead = $head;
base.$el.trigger("depage.detail-opened", [$head, $detail]);
}
};
// }}}
// {{{ hideDetail
base.hideDetail = function($head, customSpeed) {
if ($head) {
customSpeed = (typeof customSpeed === "undefined") ? base.options.speed : customSpeed;
var $detail = $head.data("$detail");
$detail.removeClass("active");
$detail.slideUp(customSpeed, function() {
$head.removeClass("active");
});
base.$el.trigger("depage.detail-closed", [$head, $detail]);
}
};
// }}}
// {{{ correctScroll()
base.correctScroll = function($oldHead, $newHead) {
var oldHeight = 0;
var oldIndex = -1;
var newIndex = base.$heads.index($newHead);
var scrollY = 0;
if ($(window).height() < base.$el.height()) {
if ($oldHead) {
// get height of old detail
oldHeight = $oldHead.data("$detail").height();
oldIndex = base.$heads.index($oldHead);
}
// calculate height to animate scroll
if (newIndex > oldIndex) {
scrollY = $newHead.offset().top - oldHeight - base.options.scrollOffset;
} else {
scrollY = $newHead.offset().top - base.options.scrollOffset;
}
$('html, body').animate({
scrollTop: scrollY
}, base.options.speed);
}
};
// }}}
// Run initializer
base.init();
};
$.depage.details.defaultOptions = {
head: "dt",
speed: 400,
correctScroll: false,
scrollOffset: 0
};
$.fn.depageDetails = function(options){
return this.each(function(){
(new $.depage.details(this, options));
});
};
})(jQuery);
/* vim:set ft=javascript sw=4 sts=4 fdm=marker : */