global.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. jQuery(function() {
  2. var $ = jQuery,
  3. $toc = $('.post-toc');
  4. $toc.each(function() {
  5. var $this = $( this ),
  6. postion = $this.offset();
  7. $this.affix({
  8. offset: {
  9. top: postion.top - 80
  10. }
  11. });
  12. });
  13. if ( $toc.length ) {
  14. $('body').scrollspy({ target: '.post-toc' });
  15. }
  16. });
  17. // comments相关
  18. jQuery(function() {
  19. var $ = jQuery,
  20. $el = $('#ghComments'),
  21. issueId;
  22. // 不合法
  23. if ( !$el.length || !$el.attr('data-issue-id') ) {
  24. return;
  25. }
  26. issueId = $el.attr('data-issue-id') ^ 0;
  27. function formatNumber(val, len) {
  28. var num = "" + val;
  29. len = len || 2;
  30. while (num.length < len) {
  31. num = "0" + num;
  32. }
  33. return num;
  34. }
  35. function formatDate( str ) {
  36. var date = new Date( str.replace(/T/, ' ').replace(/Z/, ' UTC') );
  37. return date.getFullYear() + '-' +
  38. formatNumber( date.getMonth() + 1 ) + '-' +
  39. formatNumber( date.getDate() ) + ' ' +
  40. formatNumber( date.getHours() ) + ':' +
  41. formatNumber( date.getMinutes() ) + ':' +
  42. formatNumber( date.getSeconds() );
  43. }
  44. function loadComments( data ) {
  45. for (var i = 0; i < data.length; i++) {
  46. var cuser = data[i].user.login;
  47. var cuserlink = 'https://www.github.com/' + data[i].user.login;
  48. var clink = 'https://github.com/fex-team/webuploader/issues/' + issueId + '#issuecomment-' + data[i].url.substring(data[i].url.lastIndexOf('/') + 1);
  49. var cbody = data[i].body_html;
  50. var cavatarlink = data[i].user.avatar_url;
  51. var cdate = formatDate( data[i].created_at );
  52. $el.append('<div class="comment"><div class="commentheader"><div class="commentgravatar"><img src="' + cavatarlink + '" alt="" width="20" height="20"></div><a class="commentuser" href="' + cuserlink + '">' + cuser + '</a><a class="commentdate" href="' + clink + '">' + cdate + '</a></div><div class="commentbody">' + cbody + '</div></div>');
  53. }
  54. }
  55. $.ajax('https://api.github.com/repos/fex-team/webuploader/issues/' + issueId + '/comments?per_page=100', {
  56. headers: {
  57. Accept: 'application/vnd.github.full+json'
  58. },
  59. dataType: 'json',
  60. success: loadComments
  61. });
  62. });