show.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. if($(this).parent('.child-ul').parent().parent('.child-ul')){
  22. $(this).parent('.child-ul').parent().parent('.child-ul').show();
  23. $(this).parent('.child-ul').parent().parent('.child-ul').parent('li').children("a").children('i').attr("class","icon-chevron-down");
  24. }
  25. };
  26. if (page_id != '' && page_id !='#') {
  27. change_page(page_id)
  28. };
  29. };
  30. })
  31. }
  32. //根据屏幕宽度进行响应(应对移动设备的访问)
  33. if( isMobile() || $(window).width() < 1000){
  34. AdaptToMobile();
  35. }
  36. $(window).resize(function(){
  37. if( isMobile()){
  38. AdaptToMobile();
  39. }
  40. else if($(window).width() < 1000){
  41. AdaptToMobile();
  42. }else{
  43. window.location.reload();
  44. }
  45. });
  46. //增加返回顶部按钮
  47. $.goup({
  48. trigger: 100,
  49. bottomOffset: 150,
  50. locationOffset: 100,
  51. title: lang["back_to_top"] ,
  52. titleAsText: true,
  53. containerColor:"#08c",
  54. });
  55. //js获取url参数
  56. function GetQueryString(name)
  57. {
  58. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  59. var r = window.location.search.substr(1).match(reg);
  60. if(r!=null)return unescape(r[2]); return null;
  61. }
  62. function AdaptToMobile(){
  63. $(".doc-left").removeClass("span3");
  64. $(".doc-left").css("width",'100%');
  65. $(".doc-left").css("height",'initial');
  66. $(".doc-left").css("min-height",'0px');
  67. $(".doc-right").removeClass("span12");
  68. $(".doc-head .right").hide();
  69. $(".page-edit-link").html('');
  70. $(".doc-left-newbar").html('');
  71. //$(".iframe_content").css("padding-left","30px");
  72. $(".iframe_content").css("width",'');
  73. $(".doc-left .nav-list li a i ").css("margin-left" , '10px');
  74. $(".search-input-append").css("width","100%");
  75. $(".search-query-input").css("width","70%");
  76. }
  77. function mScroll(id){
  78. $("html,body").stop(true);
  79. $("html,body").animate(
  80. {scrollTop: $("#"+id).offset().top},
  81. 2000);
  82. }
  83. //点击左侧菜单事件
  84. $(".doc-left li").click(function(){
  85. //先把所有菜单的激活状态取消
  86. $(".doc-left li").each(function(){
  87. $(this).removeClass("active");
  88. });
  89. //先判断是否存在子菜单
  90. if ($(this).children('.child-ul').length != 0) {
  91. //如果子菜单是隐藏的,则显示之;如果是显示状态的,则隐藏
  92. if ($(this).children('.child-ul').css("display") == "none") {
  93. $(this).children('.child-ul').show();
  94. $(this).children("a").children('i').attr("class","icon-chevron-down");
  95. }else{
  96. $(this).children('.child-ul').hide();
  97. $(this).children("a").children('i').attr("class","icon-chevron-right");
  98. }
  99. };
  100. //激活菜单
  101. $(this).addClass("active");
  102. //获取对应的page_id
  103. page_id = $(this).children("a").attr("data-page-id");
  104. page_title = $(this).children("a")[0].innerText;
  105. if (page_id != '' && page_id != null && page_id !='#') {
  106. if (page_title != '' && page_title != null) {
  107. document.title = page_title + " - ShowDoc";
  108. }
  109. change_page(page_id);
  110. //如果是移动设备的话,则滚动页面
  111. if( isMobile()){
  112. mScroll("page-content");
  113. }
  114. };
  115. return false;//禁止原有的href链接
  116. });
  117. //切换页面;
  118. function change_page(page_id){
  119. if(!page_id)return;
  120. var item_id = $("#item_id").val();
  121. var item_domain = $("#item_domain").val();
  122. var base_url = $("#base_url").val();
  123. var iframe_url = base_url+"/home/page/index/page_id/"+page_id;
  124. $(".page-edit-link").show();
  125. //$("#page-content").attr("src" , iframe_url);
  126. $("#edit-link").attr("href" , base_url+"/home/page/edit/page_id/"+page_id);
  127. $("#copy-link").attr("href" , base_url+"/home/page/edit/item_id/"+item_id+"/copy_page_id/"+page_id);
  128. $("#delete-link").attr("href" , base_url+"/home/page/delete/page_id/"+page_id);
  129. var domain = item_domain ? item_domain : item_id ;
  130. var cur_page_url = window.location.protocol +"//"+window.location.host+base_url+"/"+domain;
  131. if(base_url.length == 0){
  132. cur_page_url += "?page_id="+page_id;
  133. }else{
  134. cur_page_url += "&page_id="+page_id;
  135. }
  136. $("#share-page-link").html(cur_page_url);
  137. history.replaceState(null, null, cur_page_url);
  138. var single_page_url = window.location.protocol +"//"+window.location.host+base_url+"/page/"+page_id;
  139. $("#share-single-link").html(single_page_url);
  140. $("#qr-page-link").attr("src","?s=home/common/qrcode&size=3&url="+cur_page_url);
  141. $("#qr-single-link").attr("src","?s=home/common/qrcode&size=3&url="+single_page_url);
  142. $(".show_page_info").data("page_id",page_id);
  143. var html = '<iframe id="page-content" width="100%" scrolling="yes" height="100%" frameborder="0" style=" overflow:visible; height:100%;" name="main" seamless ="seamless"src="'+iframe_url+'"></iframe>';
  144. $(".iframe_content").html(html);
  145. iFrameHeight();
  146. }
  147. //分享项目
  148. $("#share").click(function(){
  149. $("#share-modal").modal();
  150. //延迟绑定分享事件
  151. setTimeout(function(){
  152. $('#copy-item-link').zclip(
  153. {
  154. path: DocConfig.pubile +'/jquery.zclip/ZeroClipboard.swf',
  155. copy:function()
  156. {
  157. return $('#share-item-link').html();
  158. },
  159. afterCopy: function() {
  160. show_top_msg("已经成功复制到剪切板",2000);
  161. }
  162. });
  163. },500);
  164. return false;
  165. });
  166. //分享页面
  167. $("#share-page").click(function(){
  168. $("#share-page-modal").modal();
  169. //延迟绑定分享事件
  170. setTimeout(function(){
  171. $('#copy-page-link').zclip(
  172. {
  173. path: DocConfig.pubile +'/jquery.zclip/ZeroClipboard.swf',
  174. copy:function()
  175. {
  176. return $('#share-page-link').html();
  177. },
  178. afterCopy: function() {
  179. show_top_msg("已经成功复制到剪切板",2000);
  180. }
  181. });
  182. $('#copy-single-link').zclip(
  183. {
  184. path:DocConfig.pubile +'/jquery.zclip/ZeroClipboard.swf',
  185. copy:function()
  186. {
  187. return $('#share-single-link').html();
  188. },
  189. afterCopy: function() {
  190. show_top_msg("已经成功复制到剪切板",2000);
  191. }
  192. });
  193. },500);
  194. return false;
  195. });
  196. function iFrameHeight() { 
  197. var ifr = document.getElementById('page-content');
  198. ifr.onload = function() {
  199. var iDoc = ifr.contentDocument || ifr.document;
  200. var height = calcPageHeight(iDoc);
  201. ifr.style.height = height + 'px';
  202. if(!isMobile() && $(window).width() > 1000){
  203. //调节左侧栏背景的最小高度
  204. if(height > document.body.clientHeight){
  205. $(".doc-left").css("min-height",(height+60) + 'px');
  206. }else{
  207. $(".doc-left").css("min-height",'100%');
  208. }
  209. }
  210. }
  211.  }
  212. // 计算页面的实际高度,iframe自适应会用到
  213. function calcPageHeight(doc) {
  214. var cHeight = Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)
  215. var sHeight = Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight)
  216. var height = Math.max(cHeight, sHeight)
  217. return height
  218. }
  219. var keyMap = {
  220. // 编辑
  221. "Ctrl+E": function() {
  222. location.href = $("#edit-link").attr('href');
  223. },
  224. // 删除
  225. "Ctrl+D": function() {
  226. if (confirm(lang["confirm_to_delete"]))
  227. location.href = $("#delete-link").attr('href');
  228. },
  229. // 新建页面
  230. "Ctrl+F1": function() {
  231. location.href = $("#new-like").attr('href');
  232. },
  233. // 新建目录
  234. "Ctrl+F2": function() {
  235. location.href = $("#dir-like").attr('href');
  236. }
  237. };
  238. if (!isMobile()) initKeys();
  239. function initKeys() {
  240. var $doc = $(document);
  241. $.each(keyMap, function(key, fn) {
  242. $doc.on('keydown', null, key, function(e) {
  243. e.preventDefault();
  244. fn();
  245. return false;
  246. });
  247. });
  248. }
  249. $(".show_page_info").click(function(){
  250. var page_id = $(this).data("page_id") ;
  251. $.post(
  252. DocConfig.server+"/api/page/info",
  253. {"page_id":page_id},
  254. function(data){
  255. var html = "<p>最后编辑时间:"+data.data.addtime+"</p><p>编辑人:"+data.data.author_username+"</p>";
  256. layer.alert(html);
  257. },
  258. "json"
  259. );
  260. return false;
  261. });
  262. //监听来自iframe的消息。如果传递图片url过来则默认打开之
  263. window.addEventListener('message', function(e){
  264. var img_url =e.data;
  265. var json = {
  266. "title": "", //相册标题
  267. "id": 123, //相册id
  268. "start": 0, //初始显示的图片序号,默认0
  269. "data": [ //相册包含的图片,数组格式
  270. {
  271. "alt": "",
  272. "pid": 666, //图片id
  273. "src": img_url, //原图地址
  274. "thumb": img_url //缩略图地址
  275. }
  276. ]
  277. }
  278. layer.photos({
  279. photos: json
  280. ,anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
  281. });
  282. }, false);
  283. })