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

Merge pull request #114 from crowi/tab-pushstate

Location hash handling on page
Sotaro KARASAWA 9 лет назад
Родитель
Сommit
4f7f1aca84
1 измененных файлов с 35 добавлено и 4 удалено
  1. 35 4
      resource/js/crowi.js

+ 35 - 4
resource/js/crowi.js

@@ -917,8 +917,16 @@ $(function() {
     });
   } // end if pageId
 
-  // for search
-  //
+  // hash handling
+  $('a[data-toggle="tab"][href="#revision-history"]').on('show.bs.tab', function() {
+    window.history.pushState('', 'History', '#revision-history');
+  });
+  $('a[data-toggle="tab"][href="#edit-form"]').on('show.bs.tab', function() {
+    window.history.pushState('', 'Edit', '#edit-form');
+  });
+  $('a[data-toggle="tab"][href="#revision-body"]').on('show.bs.tab', function() {
+    window.history.pushState('', '',  location.href.replace(location.hash, ''));
+  });
 });
 
 Crowi.findHashFromUrl = function(url)
@@ -933,7 +941,7 @@ Crowi.findHashFromUrl = function(url)
 
 Crowi.unhighlightSelectedSection = function(hash)
 {
-  if (!hash || hash == "" || !hash.match(/^head.+/)) {
+  if (!hash || hash == "" || !hash.match(/^#head.+/)) {
     // とりあえず head* だけ (検索結果ページで副作用出た
     return true;
   }
@@ -942,7 +950,7 @@ Crowi.unhighlightSelectedSection = function(hash)
 
 Crowi.highlightSelectedSection = function(hash)
 {
-  if (!hash || hash == "" || !hash.match(/^head.+/)) {
+  if (!hash || hash == "" || !hash.match(/^#head.+/)) {
     // とりあえず head* だけ (検索結果ページで副作用出た
     return true;
   }
@@ -952,10 +960,33 @@ Crowi.highlightSelectedSection = function(hash)
 window.addEventListener('load', function(e) {
   Crowi.highlightSelectedSection(location.hash);
   Crowi.modifyScrollTop();
+
+  // hash on page
+  if (location.hash) {
+    if (location.hash == '#edit-form') {
+      $('a[data-toggle="tab"][href="#edit-form"]').tab('show');
+    }
+    if (location.hash == '#revision-history') {
+      $('a[data-toggle="tab"][href="#revision-history"]').tab('show');
+    }
+  }
 });
 
 window.addEventListener('hashchange', function(e) {
   Crowi.unhighlightSelectedSection(Crowi.findHashFromUrl(e.oldURL));
   Crowi.highlightSelectedSection(Crowi.findHashFromUrl(e.newURL));
   Crowi.modifyScrollTop();
+
+  // hash on page
+  if (location.hash) {
+    if (location.hash == '#edit-form') {
+      $('a[data-toggle="tab"][href="#edit-form"]').tab('show');
+    }
+    if (location.hash == '#revision-history') {
+      $('a[data-toggle="tab"][href="#revision-history"]').tab('show');
+    }
+  }
+  if (location.hash == '' || location.hash.match(/^#head.+/)) {
+    $('a[data-toggle="tab"][href="#revision-body"]').tab('show');
+  }
 });