Vlad Kurkin (b_vladi) wrote in changelog,
Vlad Kurkin
b_vladi
changelog

[livejournal] r21107: LJSUP-10937: Self promo for journals and...

Committer: vkurkin
LJSUP-10937: Self promo for journals and communities
U   trunk/htdocs/js/jquery_fn.js
Modified: trunk/htdocs/js/jquery_fn.js
===================================================================
--- trunk/htdocs/js/jquery_fn.js	2012-02-06 11:37:20 UTC (rev 21106)
+++ trunk/htdocs/js/jquery_fn.js	2012-02-06 14:31:54 UTC (rev 21107)
@@ -184,33 +184,90 @@
 	tab container: ul>li
 	tab container current: ul>li.current
 */
-jQuery.fn.tabsChanger = function(container){
-	var links = this.children("li").children("a");
+(function ($) {
+	var supportHistoryAPI = !!window.history.pushState;
+	var dataHistory = {};
 
-	if(container){
-		container = jQuery(container);
-	} else {
-		// next sibling of links
-		container = links.parent().parent().next();
+	function changeTab(containers, links, index) {
+		links
+			.parent()
+			.removeClass('current')
+			.eq(index)
+			.addClass('current');
+
+		containers.removeClass('current')
+			.eq(index)
+			.addClass('current');
 	}
 
-	links.click(function(e){
-		var item = jQuery(this).parent(),
+	function onClick(evt) {
+		var item = $(this).parent(),
 			index = item.index(),
-			containers = container.children("li");
+			data = evt.data;
 
-		if(containers[index]){
-			links.parent().removeClass("current");
-			item.addClass("current");
+		if (data.containers[index]) {
+			changeTab(data.containers, data.links, index);
 
-			containers.removeClass("current").eq(index).addClass("current");
+			if (supportHistoryAPI) {
+				window.history.pushState(null, '', this.href);
+			}
 
-			e.preventDefault();
+			evt.preventDefault();
 		}
-	});
+	}
 
-	return this;
-};
+	if (supportHistoryAPI) {
+		$(window).bind('popstate', function () {
+			var data = dataHistory[location.href];
+
+			if (data && data.length) {
+				var length = data.length;
+				while (length) {
+					var itemData = data[--length];
+					changeTab(itemData.containers, itemData.links, itemData.index);
+				}
+			}
+		});
+	}
+
+	$.fn.tabsChanger = function(container) {
+		var links = this.children('li').children('a');
+
+		if (container) {
+			container = $(container);
+		} else {
+			// next sibling of links
+			container = links.parent().parent().next();
+		}
+
+		container = container.children('li');
+
+		if (supportHistoryAPI) {
+			links.each(function (index) {
+				var urlData = dataHistory[this.href];
+
+				if (!urlData) {
+					urlData = dataHistory[this.href] = [];
+				}
+
+				urlData.push({
+					index: index,
+					links: links,
+					containers: container
+				});
+			});
+		}
+
+		links.bind('click', {
+			containers: container,
+			links: links
+		}, onClick);
+
+		return this;
+	};
+
+})(jQuery);
+
 /** jQuery overlay plugin
  * After creation overlay visibility can be toggled with
  * $( '#selector' ).overlay( 'show' ) and $( '#selector' ).overlay( 'hide' )

Tags: b_vladi, js, livejournal, vkurkin
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments