1 | /** |
---|
2 | * @author Jason Roy for CompareNetworks Inc. |
---|
3 | * Thanks to mikejbond for suggested udaptes |
---|
4 | * |
---|
5 | * Version 1.1 |
---|
6 | * Copyright (c) 2009 CompareNetworks Inc. |
---|
7 | * |
---|
8 | * Licensed under the MIT license: |
---|
9 | * http://www.opensource.org/licenses/mit-license.php |
---|
10 | * |
---|
11 | */ |
---|
12 | (function($) |
---|
13 | { |
---|
14 | |
---|
15 | // Private variables |
---|
16 | |
---|
17 | var _options = {}; |
---|
18 | var _container = {}; |
---|
19 | var _breadCrumbElements = {}; |
---|
20 | var _autoIntervalArray = []; |
---|
21 | var _easingEquation; |
---|
22 | |
---|
23 | // Public functions |
---|
24 | |
---|
25 | jQuery.fn.jBreadCrumb = function(options) |
---|
26 | { |
---|
27 | _options = $.extend({}, $.fn.jBreadCrumb.defaults, options); |
---|
28 | |
---|
29 | return this.each(function() |
---|
30 | { |
---|
31 | _container = $(this); |
---|
32 | setupBreadCrumb(); |
---|
33 | }); |
---|
34 | |
---|
35 | }; |
---|
36 | |
---|
37 | // Private functions |
---|
38 | |
---|
39 | function setupBreadCrumb() |
---|
40 | { |
---|
41 | //Check if easing plugin exists. If it doesn't, use "swing" |
---|
42 | if(typeof(jQuery.easing) == 'object') |
---|
43 | { |
---|
44 | _easingEquation = 'easeOutQuad' |
---|
45 | } |
---|
46 | else |
---|
47 | { |
---|
48 | _easingEquation = 'swing' |
---|
49 | } |
---|
50 | |
---|
51 | //The reference object containing all of the breadcrumb elements |
---|
52 | _breadCrumbElements = jQuery(_container).find('li'); |
---|
53 | |
---|
54 | //Keep it from overflowing in ie6 & 7 |
---|
55 | jQuery(_container).find('ul').wrap('<div style="overflow:hidden; position:relative; width: ' + jQuery(_container).css("width") + ';"><div>'); |
---|
56 | //Set an arbitrary width width to avoid float drop on the animation |
---|
57 | jQuery(_container).find('ul').width(5000); |
---|
58 | |
---|
59 | //If the breadcrumb contains nothing, don't do anything |
---|
60 | if (_breadCrumbElements.length > 0) |
---|
61 | { |
---|
62 | jQuery(_breadCrumbElements[_breadCrumbElements.length - 1]).addClass('last'); |
---|
63 | jQuery(_breadCrumbElements[0]).addClass('first'); |
---|
64 | |
---|
65 | //If the breadcrumb object length is long enough, compress. |
---|
66 | |
---|
67 | if (_breadCrumbElements.length > _options.minimumCompressionElements) |
---|
68 | { |
---|
69 | compressBreadCrumb(); |
---|
70 | }; |
---|
71 | }; |
---|
72 | }; |
---|
73 | |
---|
74 | function compressBreadCrumb() |
---|
75 | { |
---|
76 | |
---|
77 | // Factor to determine if we should compress the element at all |
---|
78 | var finalElement = jQuery(_breadCrumbElements[_breadCrumbElements.length - 1]); |
---|
79 | |
---|
80 | |
---|
81 | // If the final element is really long, compress more elements |
---|
82 | if (jQuery(finalElement).width() > _options.maxFinalElementLength) |
---|
83 | { |
---|
84 | if (_options.beginingElementsToLeaveOpen > 0) |
---|
85 | { |
---|
86 | _options.beginingElementsToLeaveOpen--; |
---|
87 | |
---|
88 | } |
---|
89 | if (_options.endElementsToLeaveOpen > 0) |
---|
90 | { |
---|
91 | _options.endElementsToLeaveOpen--; |
---|
92 | } |
---|
93 | } |
---|
94 | // If the final element is within the short and long range, compress to the default end elements and 1 less beginning elements |
---|
95 | if (jQuery(finalElement).width() < _options.maxFinalElementLength && jQuery(finalElement).width() > _options.minFinalElementLength) |
---|
96 | { |
---|
97 | if (_options.beginingElementsToLeaveOpen > 0) |
---|
98 | { |
---|
99 | _options.beginingElementsToLeaveOpen--; |
---|
100 | |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | var itemsToRemove = _breadCrumbElements.length - 1 - _options.endElementsToLeaveOpen; |
---|
105 | |
---|
106 | // We compress only elements determined by the formula setting below |
---|
107 | |
---|
108 | //TODO : Make this smarter, it's only checking the final elements length. It could also check the amount of elements. |
---|
109 | jQuery(_breadCrumbElements[_breadCrumbElements.length - 1]).css( |
---|
110 | { |
---|
111 | background: 'none' |
---|
112 | }); |
---|
113 | |
---|
114 | $(_breadCrumbElements).each(function(i, listElement) |
---|
115 | { |
---|
116 | if (i > _options.beginingElementsToLeaveOpen && i < itemsToRemove) |
---|
117 | { |
---|
118 | |
---|
119 | jQuery(listElement).find('a').wrap('<span></span>').width(jQuery(listElement).find('a').width() + 10); |
---|
120 | |
---|
121 | // Add the overlay png. |
---|
122 | // jQuery(listElement).append(jQuery('<div class="' + _options.overlayClass + '"></div>').css( |
---|
123 | // { |
---|
124 | // display: 'block' |
---|
125 | // })).css( |
---|
126 | // { |
---|
127 | // background: 'none' |
---|
128 | // }); |
---|
129 | |
---|
130 | var options = |
---|
131 | { |
---|
132 | id: i, |
---|
133 | width: jQuery(listElement).width(), |
---|
134 | listElement: jQuery(listElement).find('span'), |
---|
135 | isAnimating: false, |
---|
136 | element: jQuery(listElement).find('span') |
---|
137 | |
---|
138 | }; |
---|
139 | jQuery(listElement).bind('mouseover', options, expandBreadCrumb).bind('mouseout', options, shrinkBreadCrumb); |
---|
140 | jQuery(listElement).find('a').unbind('mouseover', expandBreadCrumb).unbind('mouseout', shrinkBreadCrumb); |
---|
141 | listElement.autoInterval = setInterval(function() |
---|
142 | { |
---|
143 | clearInterval(listElement.autoInterval); |
---|
144 | jQuery(listElement).find('span').animate( |
---|
145 | { |
---|
146 | width: _options.previewWidth |
---|
147 | }, _options.timeInitialCollapse, _options.easing); |
---|
148 | }, (150 * (i - 2))); |
---|
149 | |
---|
150 | } |
---|
151 | }); |
---|
152 | |
---|
153 | }; |
---|
154 | |
---|
155 | function expandBreadCrumb(e) |
---|
156 | { |
---|
157 | var elementID = e.data.id; |
---|
158 | var originalWidth = e.data.width; |
---|
159 | jQuery(e.data.element).stop(); |
---|
160 | jQuery(e.data.element).animate( |
---|
161 | { |
---|
162 | width: originalWidth |
---|
163 | }, |
---|
164 | { |
---|
165 | duration: _options.timeExpansionAnimation, |
---|
166 | easing: _options.easing, |
---|
167 | queue: false |
---|
168 | }); |
---|
169 | return false; |
---|
170 | |
---|
171 | }; |
---|
172 | |
---|
173 | function shrinkBreadCrumb(e) |
---|
174 | { |
---|
175 | var elementID = e.data.id; |
---|
176 | jQuery(e.data.element).stop(); |
---|
177 | jQuery(e.data.element).animate( |
---|
178 | { |
---|
179 | width: _options.previewWidth |
---|
180 | }, |
---|
181 | { |
---|
182 | duration: _options.timeCompressionAnimation, |
---|
183 | easing: _options.easing, |
---|
184 | queue: false |
---|
185 | }); |
---|
186 | return false; |
---|
187 | }; |
---|
188 | |
---|
189 | // Public global variables |
---|
190 | |
---|
191 | jQuery.fn.jBreadCrumb.defaults = |
---|
192 | { |
---|
193 | maxFinalElementLength: 400, |
---|
194 | minFinalElementLength: 200, |
---|
195 | minimumCompressionElements: 4, |
---|
196 | endElementsToLeaveOpen: 1, |
---|
197 | beginingElementsToLeaveOpen: 1, |
---|
198 | timeExpansionAnimation: 800, |
---|
199 | timeCompressionAnimation: 500, |
---|
200 | timeInitialCollapse: 600, |
---|
201 | easing: _easingEquation, |
---|
202 | overlayClass: 'chevronOverlay', |
---|
203 | previewWidth: 40 |
---|
204 | }; |
---|
205 | |
---|
206 | })(jQuery); |
---|