Просмотр исходного кода

Watch for hash change for tabs

Jessica Lawshe 7 лет назад
Родитель
Сommit
1362da710b
1 измененных файлов с 13 добавлено и 5 удалено
  1. 13 5
      services/web/public/coffee/directives/bookmarkableTabset.coffee

+ 13 - 5
services/web/public/coffee/directives/bookmarkableTabset.coffee

@@ -5,8 +5,11 @@ define [
 		restrict: "A"
 		require: "tabset"
 		link: (scope, el, attrs, tabset) ->
-			scope.$applyAsync () ->
-				hash = $location.hash()
+			linksToTabs = document.querySelectorAll(".link-to-tab");
+			_clickLinkToTab = (event) ->
+				_makeActive(event.currentTarget.getAttribute("href").replace('#', ''))
+
+			_makeActive = (hash) ->
 				if hash? and hash != ""
 					matchingTab = _.find tabset.tabs, (tab) ->
 						tab.bookmarkableTabId == hash
@@ -14,6 +17,14 @@ define [
 						matchingTab.select()
 						el.children()[0].scrollIntoView({ behavior: "smooth" })
 
+			for link in linksToTabs
+				link.addEventListener("click", _clickLinkToTab)
+
+			scope.$applyAsync () ->
+				# for page load
+				hash = $location.hash()
+				_makeActive(hash)
+
 	App.directive "bookmarkableTab", ($location) ->
 		restrict: "A"
 		require: "tab"
@@ -25,6 +36,3 @@ define [
 				tabScope.$watch "active", (isActive, wasActive) ->
 					if isActive and !wasActive and $location.hash() != tabId
 						$location.hash tabId
-
-
-