show.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. //增加返回顶部按钮
  46. $.goup({
  47. trigger: 100,
  48. bottomOffset: 150,
  49. locationOffset: 100,
  50. title: '回到顶部',
  51. titleAsText: true,
  52. containerColor:"#08c",
  53. });
  54. //js获取url参数
  55. function GetQueryString(name)
  56. {
  57. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  58. var r = window.location.search.substr(1).match(reg);
  59. if(r!=null)return unescape(r[2]); return null;
  60. }
  61. function AdaptToMobile(){
  62. $(".doc-left").removeClass("span3");
  63. $(".doc-left").css("width",'100%');
  64. $(".doc-left").css("height",'initial');
  65. $(".doc-left").css("min-height",'0px');
  66. $(".doc-right").removeClass("span12");
  67. $(".doc-head .right").hide();
  68. $(".page-edit-link").html('');
  69. $(".doc-left-newbar").html('');
  70. $(".iframe_content").css("padding-left","30px");
  71. $(".doc-left .nav-list li a i ").css("margin-left" , '10px');
  72. $(".search-input-append").css("width","100%");
  73. $(".search-query-input").css("width","70%");
  74. }
  75. function mScroll(id){
  76. $("html,body").stop(true);
  77. $("html,body").animate(
  78. {scrollTop: $("#"+id).offset().top},
  79. 2000);
  80. }
  81. //点击左侧菜单事件
  82. $(".doc-left li").click(function(){
  83. //先把所有菜单的激活状态取消
  84. $(".doc-left li").each(function(){
  85. $(this).removeClass("active");
  86. });
  87. //先判断是否存在子菜单
  88. if ($(this).children('.child-ul').length != 0) {
  89. //如果子菜单是隐藏的,则显示之;如果是显示状态的,则隐藏
  90. if ($(this).children('.child-ul').css("display") == "none") {
  91. $(this).children('.child-ul').show();
  92. $(this).children("a").children('i').attr("class","icon-chevron-down");
  93. }else{
  94. $(this).children('.child-ul').hide();
  95. $(this).children("a").children('i').attr("class","icon-chevron-right");
  96. }
  97. };
  98. //获取对应的page_id
  99. page_id = $(this).children("a").attr("data-page-id");
  100. page_title = $(this).children("a")[0].innerText;
  101. if (page_id != '' && page_id != null && page_id !='#') {
  102. if (page_title != '' && page_title != null) {
  103. document.title = page_title + " - ShowDoc";
  104. }
  105. change_page(page_id);
  106. //如果是移动设备的话,则滚动页面
  107. if( isMobile()){
  108. mScroll("page-content");
  109. }
  110. };
  111. return false;//禁止原有的href链接
  112. });
  113. //切换页面;
  114. function change_page(page_id){
  115. if(!page_id)return;
  116. var item_id = $("#item_id").val();
  117. var base_url = $("#base_url").val();
  118. $(".page-edit-link").show();
  119. $("#page-content").attr("src" , base_url+"/home/page/index/page_id/"+page_id);
  120. $("#edit-link").attr("href" , base_url+"/home/page/edit/page_id/"+page_id);
  121. $("#copy-link").attr("href" , base_url+"/home/page/edit/item_id/"+item_id+"/copy_page_id/"+page_id);
  122. $("#share-page-link").html("http://"+window.location.host+base_url+"/"+item_id+"&page_id="+page_id);
  123. $("#delete-link").attr("href" , base_url+"/home/page/delete/page_id/"+page_id);
  124. history.replaceState(null, null, "http://"+window.location.host+base_url+"/"+item_id+"&page_id="+page_id);
  125. }
  126. //分享项目
  127. $("#share").click(function(){
  128. $("#share-modal").modal();
  129. return false;
  130. });
  131. //分享页面
  132. $("#share-page").click(function(){
  133. $("#share-page-modal").modal();
  134. return false;
  135. });
  136. var ifr = document.getElementById('page-content')
  137. ifr.onload = function() {
  138. var iDoc = ifr.contentDocument || ifr.document
  139. var height = calcPageHeight(iDoc)
  140. ifr.style.height = height + 'px'
  141. }
  142. // 计算页面的实际高度,iframe自适应会用到
  143. function calcPageHeight(doc) {
  144. var cHeight = Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)
  145. var sHeight = Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight)
  146. var height = Math.max(cHeight, sHeight)
  147. return height
  148. }
  149. var keyMap = {
  150. // 编辑
  151. "Ctrl+E": function() {
  152. location.href = $("#edit-link").attr('href');
  153. },
  154. // 删除
  155. "Ctrl+D": function() {
  156. if (confirm('确认删除吗?'))
  157. location.href = $("#delete-link").attr('href');
  158. },
  159. // 新建页面
  160. "Ctrl+F1": function() {
  161. location.href = $("#new-like").attr('href');
  162. },
  163. // 新建目录
  164. "Ctrl+F2": function() {
  165. location.href = $("#dir-like").attr('href');
  166. }
  167. };
  168. if (!isMobile()) initKeys();
  169. function initKeys() {
  170. var $doc = $(document);
  171. $.each(keyMap, function(key, fn) {
  172. $doc.on('keydown', null, key, function(e) {
  173. e.preventDefault();
  174. fn();
  175. return false;
  176. });
  177. });
  178. }
  179. })