common-js.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php if(!defined('__TYPECHO_ADMIN__')) exit; ?>
  2. <script src="<?php $options->adminStaticUrl('js', 'jquery.js'); ?>"></script>
  3. <script src="<?php $options->adminStaticUrl('js', 'jquery-ui.js'); ?>"></script>
  4. <script src="<?php $options->adminStaticUrl('js', 'typecho.js'); ?>"></script>
  5. <script>
  6. (function () {
  7. $(document).ready(function() {
  8. // 处理消息机制
  9. (function () {
  10. var prefix = '<?php echo \Typecho\Cookie::getPrefix(); ?>',
  11. cookies = {
  12. notice : $.cookie(prefix + '__typecho_notice'),
  13. noticeType : $.cookie(prefix + '__typecho_notice_type'),
  14. highlight : $.cookie(prefix + '__typecho_notice_highlight')
  15. },
  16. path = '<?php echo \Typecho\Cookie::getPath(); ?>',
  17. domain = '<?php echo \Typecho\Cookie::getDomain(); ?>',
  18. secure = <?php echo json_encode(\Typecho\Cookie::getSecure()); ?>;
  19. if (!!cookies.notice && 'success|notice|error'.indexOf(cookies.noticeType) >= 0) {
  20. var head = $('.typecho-head-nav'),
  21. p = $('<div class="message popup ' + cookies.noticeType + '">'
  22. + '<ul><li>' + $.parseJSON(cookies.notice).join('</li><li>')
  23. + '</li></ul></div>'), offset = 0;
  24. if (head.length > 0) {
  25. p.insertAfter(head);
  26. offset = head.outerHeight();
  27. } else {
  28. p.prependTo(document.body);
  29. }
  30. function checkScroll () {
  31. if ($(window).scrollTop() >= offset) {
  32. p.css({
  33. 'position' : 'fixed',
  34. 'top' : 0
  35. });
  36. } else {
  37. p.css({
  38. 'position' : 'absolute',
  39. 'top' : offset
  40. });
  41. }
  42. }
  43. $(window).scroll(function () {
  44. checkScroll();
  45. });
  46. checkScroll();
  47. p.slideDown(function () {
  48. var t = $(this), color = '#C6D880';
  49. if (t.hasClass('error')) {
  50. color = '#FBC2C4';
  51. } else if (t.hasClass('notice')) {
  52. color = '#FFD324';
  53. }
  54. t.effect('highlight', {color : color})
  55. .delay(5000).fadeOut(function () {
  56. $(this).remove();
  57. });
  58. });
  59. $.cookie(prefix + '__typecho_notice', null, {path : path, domain: domain, secure: secure});
  60. $.cookie(prefix + '__typecho_notice_type', null, {path : path, domain: domain, secure: secure});
  61. }
  62. if (cookies.highlight) {
  63. $('#' + cookies.highlight).effect('highlight', 1000);
  64. $.cookie(prefix + '__typecho_notice_highlight', null, {path : path, domain: domain, secure: secure});
  65. }
  66. })();
  67. // 导航菜单 tab 聚焦时展开下拉菜单
  68. const menuBar = $('.menu-bar').click(function () {
  69. const nav = $(this).next('#typecho-nav-list');
  70. if (!$(this).toggleClass('focus').hasClass('focus')) {
  71. nav.removeClass('expanded noexpanded');
  72. }
  73. });
  74. $('.main, .typecho-foot').on('click touchstart', function () {
  75. if (menuBar.hasClass('focus')) {
  76. menuBar.trigger('click');
  77. }
  78. });
  79. $('#typecho-nav-list ul.root').each(function () {
  80. const ul = $(this), nav = ul.parent();
  81. let focused = false;
  82. ul.on('click touchend', '.parent a', function (e) {
  83. nav.removeClass('noexpanded').addClass('expanded');
  84. if ($(window).width() < 576 && e.type == 'click') {
  85. return false;
  86. }
  87. }).find('.child')
  88. .append($('<li class="return"><a><?php _e('返回'); ?></a></li>').click(function () {
  89. nav.removeClass('expanded').addClass('noexpanded');
  90. return false;
  91. }));
  92. $('a', ul).focus(function () {
  93. ul.addClass('expanded');
  94. focused = true;
  95. }).blur(function () {
  96. focused = false;
  97. setTimeout(function () {
  98. if (!focused) {
  99. ul.removeClass('expanded');
  100. }
  101. });
  102. });
  103. });
  104. if ($('.typecho-login').length == 0) {
  105. $('a').each(function () {
  106. var t = $(this), href = t.attr('href');
  107. if ((href && href[0] == '#')
  108. || /^<?php echo preg_quote($options->adminUrl, '/'); ?>.*$/.exec(href)
  109. || /^<?php echo substr(preg_quote(\Typecho\Common::url('s', $options->index), '/'), 0, -1); ?>action\/[_a-zA-Z0-9\/]+.*$/.exec(href)) {
  110. return;
  111. }
  112. t.attr('target', '_blank')
  113. .attr('rel', 'noopener noreferrer');
  114. });
  115. }
  116. $('.main form').submit(function () {
  117. $('button[type=submit]', this).attr('disabled', 'disabled');
  118. });
  119. });
  120. })();
  121. </script>