show.js 10 KB

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