show.js 6.1 KB

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