show.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //页面加载完就执行
  2. $(function(){
  3. //自动根据url把当前菜单激活
  4. var page_id = GetQueryString('page_id');
  5. //如果中没有指定page_id,则判断有没有父目录为0的页面,默认打开第一个
  6. if(!page_id) {
  7. page_id = $(".doc-left li").children("a").attr("data-page-id");
  8. };
  9. if(page_id !=null && page_id.toString().length>0)
  10. {
  11. var str = 'page_id='+page_id;
  12. $(".doc-left li").each(function(){
  13. url = $(this).children("a").attr("href");
  14. //如果链接中包含当前url的信息,两者相匹配
  15. if (url && url.indexOf(str) >= 0 ) {
  16. //激活菜单
  17. $(this).addClass("active");
  18. //如果该菜单是子菜单,则还需要把父菜单打开才行
  19. if ($(this).parent('.child-ul')) {
  20. $(this).parent('.child-ul').show();
  21. $(this).parent('.child-ul').parent('li').children("a").children('i').attr("class","icon-chevron-down");
  22. };
  23. //获取对应的page_id
  24. page_id = $(this).children("a").attr("data-page-id");
  25. if (page_id != '' && page_id !='#') {
  26. change_page(page_id)
  27. };
  28. };
  29. })
  30. }
  31. //根据屏幕宽度进行响应(应对移动设备的访问)
  32. if( isMobile()){
  33. AdaptToMobile();
  34. }
  35. $(window).resize(function(){
  36. if( isMobile()){
  37. AdaptToMobile();
  38. }
  39. else if($(window).width() < 600){
  40. AdaptToMobile();
  41. }else{
  42. window.location.reload();
  43. }
  44. });
  45. //js获取url参数
  46. function GetQueryString(name)
  47. {
  48. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  49. var r = window.location.search.substr(1).match(reg);
  50. if(r!=null)return unescape(r[2]); return null;
  51. }
  52. function AdaptToMobile(){
  53. $(".doc-left").removeClass("span3");
  54. $(".doc-left").css("width",'100%');
  55. $(".doc-left").css("height",'initial');
  56. $(".doc-left").css("min-height",'0px');
  57. $(".doc-right").removeClass("span12");
  58. $(".doc-head .right").hide();
  59. $(".page-edit-link").html('');
  60. $(".doc-left-newbar").html('');
  61. $(".iframe_content").css("padding-left","30px");
  62. $(".doc-left .nav-list li a i ").css("margin-left" , '10px');
  63. $(".search-input-append").css("width","100%");
  64. $(".search-query-input").css("width","70%");
  65. }
  66. function mScroll(id){
  67. $("html,body").stop(true);
  68. $("html,body").animate(
  69. {scrollTop: $("#"+id).offset().top},
  70. 2000);
  71. }
  72. //点击左侧菜单事件
  73. $(".doc-left li").click(function(){
  74. //先把所有菜单的激活状态取消
  75. $(".doc-left li").each(function(){
  76. $(this).removeClass("active");
  77. });
  78. //先判断是否存在子菜单
  79. if ($(this).children('.child-ul').length != 0) {
  80. //如果子菜单是隐藏的,则显示之;如果是显示状态的,则隐藏
  81. if ($(this).children('.child-ul').css("display") == "none") {
  82. $(this).children('.child-ul').show();
  83. $(this).children("a").children('i').attr("class","icon-chevron-down");
  84. }else{
  85. $(this).children('.child-ul').hide();
  86. $(this).children("a").children('i').attr("class","icon-chevron-right");
  87. }
  88. };
  89. //获取对应的page_id
  90. page_id = $(this).children("a").attr("data-page-id");
  91. page_title = $(this).children("a")[0].innerText;
  92. if (page_id != '' && page_id != null && page_id !='#') {
  93. if (page_title != '' && page_title != null) {
  94. document.title = page_title + " - ShowDoc";
  95. }
  96. change_page(page_id);
  97. //如果是移动设备的话,则滚动页面
  98. if( isMobile()){
  99. mScroll("page-content");
  100. }
  101. };
  102. return false;//禁止原有的href链接
  103. });
  104. //切换页面;
  105. function change_page(page_id){
  106. if(!page_id)return;
  107. var item_id = $("#item_id").val();
  108. var base_url = $("#base_url").val();
  109. $(".page-edit-link").show();
  110. $("#page-content").attr("src" , base_url+"/home/page/index/page_id/"+page_id);
  111. $("#edit-link").attr("href" , base_url+"/home/page/edit/page_id/"+page_id);
  112. $("#copy-link").attr("href" , base_url+"/home/page/edit/item_id/"+item_id+"/copy_page_id/"+page_id);
  113. $("#share-page-link").html("http://"+window.location.host+base_url+"/"+item_id+"&page_id="+page_id);
  114. $("#delete-link").attr("href" , base_url+"/home/page/delete/page_id/"+page_id);
  115. history.replaceState(null, null, "http://"+window.location.host+base_url+"/"+item_id+"&page_id="+page_id);
  116. }
  117. //分享项目
  118. $("#share").click(function(){
  119. $("#share-modal").modal();
  120. return false;
  121. });
  122. //分享页面
  123. $("#share-page").click(function(){
  124. $("#share-page-modal").modal();
  125. return false;
  126. });
  127. var ifr = document.getElementById('page-content')
  128. ifr.onload = function() {
  129. var iDoc = ifr.contentDocument || ifr.document
  130. var height = calcPageHeight(iDoc)
  131. ifr.style.height = height + 'px'
  132. }
  133. // 计算页面的实际高度,iframe自适应会用到
  134. function calcPageHeight(doc) {
  135. var cHeight = Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)
  136. var sHeight = Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight)
  137. var height = Math.max(cHeight, sHeight)
  138. return height
  139. }
  140. var keyMap = {
  141. // 编辑
  142. "Ctrl+E": function() {
  143. location.href = $("#edit-link").attr('href');
  144. },
  145. // 删除
  146. "Ctrl+D": function() {
  147. if (confirm('确认删除吗?'))
  148. location.href = $("#delete-link").attr('href');
  149. },
  150. // 新建页面
  151. "Ctrl+F1": function() {
  152. location.href = $("#new-like").attr('href');
  153. },
  154. // 新建目录
  155. "Ctrl+F2": function() {
  156. location.href = $("#dir-like").attr('href');
  157. }
  158. };
  159. if (!isMobile()) initKeys();
  160. function initKeys() {
  161. var $doc = $(document);
  162. $.each(keyMap, function(key, fn) {
  163. $doc.on('keydown', null, key, function(e) {
  164. e.preventDefault();
  165. fn();
  166. return false;
  167. });
  168. });
  169. }
  170. })