This repository was archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjquery.sapling.js
More file actions
115 lines (95 loc) · 2.65 KB
/
Copy pathjquery.sapling.js
File metadata and controls
115 lines (95 loc) · 2.65 KB
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
/*
* jQuery Sapling - http://tameraydin.github.com/jquery-sapling/
*
*/
;(function ($, window, document, undefined) {
$.sapling = function(element, options) {
var plugin = this,
$element = $(element),
defaults = {
multiexpand: true,
animation: false
};
var expand = function(el) {
el.addClass('sapling-expanded');
},
collapse = function(el) {
el.removeClass('sapling-expanded');
},
click = function(e) {
if (e.target.nodeName !== 'A') {
if ($(this).hasClass('sapling-expanded')) {
collapse($(this));
} else {
if (!plugin.settings.multiexpand) {
$element.find('.sapling-expanded').not($(this).parents()).trigger('click');
}
expand($(this));
}
}
};
plugin.settings = {};
plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
if (plugin.settings.animation) {
expand = function(el) {
el.children('ul, ol').slideDown(function() {
el.addClass('sapling-expanded');
});
};
collapse = function(el) {
el.children('ul, ol').slideUp(function() {
el.removeClass('sapling-expanded');
});
};
}
$element.addClass('sapling-list');
$element.children('li').addClass('sapling-top-level');
$element.find('li').each(function() {
var $this = $(this);
var childrenTrees = $this.children('ul, ol');
if (childrenTrees.index() !== -1) {
$this.addClass('sapling-item');
$this.bind('click.sapling', click);
childrenTrees.bind('click.sapling', function(e) {
if (e.target.nodeName != 'A') {
return false;
}
});
}
});
};
plugin.expand = function() {
expand($element.find('.sapling-item'));
};
plugin.collapse = function() {
collapse($element.find('.sapling-expanded'));
};
plugin.destroy = function() {
$element.removeClass('sapling-list');
$element.children('li').removeClass('sapling-top-level');
$element.find('li').each(function() {
var $this = $(this);
var childrenTrees = $this.children('ul, ol');
if (childrenTrees.index() !== -1) {
$this.removeClass('sapling-item');
$this.unbind('.sapling');
childrenTrees.removeAttr('style');
childrenTrees.unbind('.sapling');
}
});
$element.find('.sapling-expanded').removeClass('sapling-expanded');
$element.data('sapling', null);
};
plugin.init();
};
$.fn.sapling = function(options) {
return this.each(function() {
var saplingData = $(this).data('sapling');
if (saplingData === undefined || saplingData === null) {
var plugin = new $.sapling(this, options);
$(this).data('sapling', plugin);
}
});
};
})(jQuery, window, document);