diaspora.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. var Home = location.href,
  2. Pages = 4,
  3. xhr,
  4. xhrUrl = '';
  5. var Diaspora = {
  6. L: function(url, f, err) {
  7. if (url == xhrUrl) {
  8. return false;
  9. }
  10. xhrUrl = url;
  11. if (xhr) {
  12. xhr.abort();
  13. }
  14. xhr = $.ajax({
  15. type: 'GET',
  16. url: url,
  17. timeout: 10000,
  18. success: function(data) {
  19. f(data);
  20. xhrUrl = '';
  21. },
  22. error: function(a, b, c) {
  23. if (b == 'abort') {
  24. err && err()
  25. } else {
  26. window.location.href = url;
  27. }
  28. xhrUrl = '';
  29. }
  30. });
  31. },
  32. P: function() {
  33. return !!('ontouchstart' in window);
  34. },
  35. PS: function() {
  36. if (!(window.history && history.pushState)){
  37. return;
  38. }
  39. history.replaceState({u: Home, t: document.title}, document.title, Home);
  40. window.addEventListener('popstate', function(e) {
  41. var state = e.state;
  42. if (!state) return;
  43. document.title = state.t;
  44. if (state.u == Home) {
  45. $('#preview').css('position', 'fixed');
  46. setTimeout(function() {
  47. $('#preview').removeClass('show');
  48. $('#container').show();
  49. window.scrollTo(0, parseInt($('#container').data('scroll')));
  50. setTimeout(function() {
  51. $('#preview').html('');
  52. $(window).trigger('resize');
  53. }, 300);
  54. }, 0);
  55. } else {
  56. Diaspora.loading();
  57. Diaspora.L(state.u, function(data) {
  58. document.title = state.t;
  59. $('#preview').html($(data).filter('#single'));
  60. Diaspora.preview();
  61. setTimeout(function() { Diaspora.player(); }, 0);
  62. });
  63. }
  64. });
  65. },
  66. HS: function(tag, flag) {
  67. var id = tag.data('id') || 0,
  68. url = tag.attr('href'),
  69. title = tag.attr('title') || tag.text();
  70. if (!$('#preview').length || !(window.history && history.pushState)) location.href = url;
  71. Diaspora.loading()
  72. var state = {d: id, t: title, u: url};
  73. Diaspora.L(url, function(data) {
  74. if (!$(data).filter('#single').length) {
  75. location.href = url;
  76. return
  77. }
  78. switch (flag) {
  79. case 'push':
  80. history.pushState(state, title, url)
  81. break;
  82. case 'replace':
  83. history.replaceState(state, title, url)
  84. break;
  85. }
  86. document.title = title;
  87. $('#preview').html($(data).filter('#single'))
  88. switch (flag) {
  89. case 'push':
  90. Diaspora.preview()
  91. break;
  92. case 'replace':
  93. window.scrollTo(0, 0)
  94. Diaspora.loaded()
  95. break;
  96. }
  97. setTimeout(function() {
  98. Diaspora.player();
  99. $('#top').show();
  100. comment = $("#gitalk-container");
  101. if (comment.data('ae') == true){
  102. comment.click();
  103. }
  104. }, 0)
  105. })
  106. },
  107. preview: function() {
  108. // preview toggle
  109. $("#preview").one('transitionend webkitTransitionEnd oTransitionEnd otransitionend MSTransitionEnd', function() {
  110. var left = $('#preview').css('left');
  111. if (left == '0px') {
  112. $('#container').hide();
  113. }else{
  114. $('#container').show();
  115. }
  116. Diaspora.loaded();
  117. });
  118. setTimeout(function() {
  119. $('#preview').addClass('show');
  120. $('#container').data('scroll', window.scrollY);
  121. setTimeout(function() {
  122. $('#preview').css({
  123. 'position': 'static',
  124. 'overflow-y': 'auto'
  125. });
  126. }, 500);
  127. }, 0);
  128. },
  129. player: function() {
  130. var p = $('#audio');
  131. if (!p.length) {
  132. $('.icon-play').css({
  133. 'color': '#dedede',
  134. 'cursor': 'not-allowed'
  135. })
  136. return
  137. }
  138. if (p.eq(0).data("autoplay") == true) {
  139. p[0].play();
  140. }
  141. p.on({
  142. 'timeupdate': function() {
  143. var progress = p[0].currentTime / p[0].duration * 100;
  144. $('.bar').css('width', progress + '%');
  145. if (progress / 5 <= 1) {
  146. p[0].volume = progress / 5;
  147. }else {
  148. p[0].volume = 1;
  149. }
  150. },
  151. 'ended': function() {
  152. $('.icon-pause').removeClass('icon-pause').addClass('icon-play')
  153. },
  154. 'playing': function() {
  155. $('.icon-play').removeClass('icon-play').addClass('icon-pause')
  156. }
  157. })
  158. },
  159. loading: function() {
  160. var w = window.innerWidth;
  161. var css = '<style class="loaderstyle" id="loaderstyle'+ w +'">'+
  162. '@-moz-keyframes loader'+ w +'{100%{background-position:'+ w +'px 0}}'+
  163. '@-webkit-keyframes loader'+ w +'{100%{background-position:'+ w +'px 0}}'+
  164. '.loader'+ w +'{-webkit-animation:loader'+ w +' 3s linear infinite;-moz-animation:loader'+ w +' 3s linear infinite;}'+
  165. '</style>';
  166. $('.loaderstyle').remove()
  167. $('head').append(css)
  168. $('#loader').removeClass().addClass('loader'+ w).show()
  169. },
  170. loaded: function() {
  171. $('#loader').removeClass().hide()
  172. },
  173. F: function(id, w, h) {
  174. var _height = $(id).parent().height(),
  175. _width = $(id).parent().width(),
  176. ratio = h / w;
  177. if (_height / _width > ratio) {
  178. id.style.height = _height +'px';
  179. id.style.width = _height / ratio +'px';
  180. } else {
  181. id.style.width = _width +'px';
  182. id.style.height = _width * ratio +'px';
  183. }
  184. id.style.left = (_width - parseInt(id.style.width)) / 2 +'px';
  185. id.style.top = (_height - parseInt(id.style.height)) / 2 +'px';
  186. }
  187. };
  188. $(function() {
  189. if (Diaspora.P()) {
  190. $('body').addClass('touch')
  191. }
  192. if ($('#preview').length) {
  193. var cover = {};
  194. cover.t = $('#cover');
  195. cover.w = cover.t.attr('width');
  196. cover.h = cover.t.attr('height');
  197. ;(cover.o = function() {
  198. $('#mark').height(window.innerHeight)
  199. })();
  200. if (cover.t.prop('complete')) {
  201. // why setTimeout ?
  202. setTimeout(function() { cover.t.load() }, 0)
  203. }
  204. cover.t.on('load', function() {
  205. ;(cover.f = function() {
  206. var _w = $('#mark').width(), _h = $('#mark').height(), x, y, i, e;
  207. e = (_w >= 1000 || _h >= 1000) ? 1000 : 500;
  208. if (_w >= _h) {
  209. i = _w / e * 50;
  210. y = i;
  211. x = i * _w / _h;
  212. } else {
  213. i = _h / e * 50;
  214. x = i;
  215. y = i * _h / _w;
  216. }
  217. $('.layer').css({
  218. 'width': _w + x,
  219. 'height': _h + y,
  220. 'marginLeft': - 0.5 * x,
  221. 'marginTop': - 0.5 * y
  222. })
  223. if (!cover.w) {
  224. cover.w = cover.t.width();
  225. cover.h = cover.t.height();
  226. }
  227. Diaspora.F($('#cover')[0], cover.w, cover.h)
  228. })();
  229. setTimeout(function() {
  230. $('html, body').removeClass('loading')
  231. }, 1000)
  232. $('#mark').parallax()
  233. var vibrant = new Vibrant(cover.t[0]);
  234. var swatches = vibrant.swatches()
  235. if (swatches['DarkVibrant']) {
  236. $('#vibrant polygon').css('fill', swatches['DarkVibrant'].getHex())
  237. $('#vibrant div').css('background-color', swatches['DarkVibrant'].getHex())
  238. }
  239. if (swatches['Vibrant']) {
  240. $('.icon-menu').css('color', swatches['Vibrant'].getHex())
  241. }
  242. })
  243. if (!cover.t.attr('src')) {
  244. alert('Please set the post thumbnail')
  245. }
  246. $('#preview').css('min-height', window.innerHeight)
  247. Diaspora.PS()
  248. $('.pview a').addClass('pviewa')
  249. var T;
  250. $(window).on('resize', function() {
  251. clearTimeout(T)
  252. T = setTimeout(function() {
  253. if (!Diaspora.P() && location.href == Home) {
  254. cover.o()
  255. cover.f()
  256. }
  257. if ($('#loader').attr('class')) {
  258. Diaspora.loading()
  259. }
  260. }, 500)
  261. })
  262. } else {
  263. $('#single').css('min-height', window.innerHeight)
  264. setTimeout(function() {
  265. $('html, body').removeClass('loading')
  266. }, 1000)
  267. window.addEventListener('popstate', function(e) {
  268. if (e.state) location.href = e.state.u;
  269. })
  270. Diaspora.player();
  271. $('.icon-icon, .image-icon').attr('href', '/')
  272. $('#top').show()
  273. }
  274. $(window).on('scroll', function() {
  275. if ($('.scrollbar').length && !Diaspora.P() && !$('.icon-images').hasClass('active')) {
  276. var wt = $(window).scrollTop(),
  277. tw = $('#top').width(),
  278. dh = document.body.scrollHeight,
  279. wh = $(window).height();
  280. var width = tw / (dh - wh) * wt;
  281. $('.scrollbar').width(width)
  282. if (wt > 80 && window.innerWidth > 800) {
  283. $('.subtitle').fadeIn()
  284. } else {
  285. $('.subtitle').fadeOut()
  286. }
  287. }
  288. })
  289. $(window).on('touchmove', function(e) {
  290. if ($('body').hasClass('mu')) {
  291. e.preventDefault()
  292. }
  293. })
  294. $('body').on('click', function(e) {
  295. var tag = $(e.target).attr('class') || '',
  296. rel = $(e.target).attr('rel') || '';
  297. // .content > p > img
  298. if (e.target.nodeName == "IMG" && $(e.target).parent().get(0).nodeName == "P") {
  299. tag = 'pimg';
  300. }
  301. if (!tag && !rel) return;
  302. switch (true) {
  303. // nav menu
  304. case (tag.indexOf('switchmenu') != -1):
  305. window.scrollTo(0, 0)
  306. $('html, body').toggleClass('mu');
  307. break;
  308. // next page
  309. case (tag.indexOf('more') != -1):
  310. tag = $('.more');
  311. if (tag.data('status') == 'loading') {
  312. return false
  313. }
  314. var num = parseInt(tag.data('page')) || 1;
  315. if (num == 1) {
  316. tag.data('page', 1)
  317. }
  318. if (num >= Pages) {
  319. return
  320. }
  321. tag.html('加载中...').data('status', 'loading')
  322. Diaspora.loading()
  323. Diaspora.L(tag.attr('href'), function(data) {
  324. var link = $(data).find('.more').attr('href');
  325. if (link != undefined) {
  326. tag.attr('href', link).html('加载更多').data('status', 'loaded')
  327. tag.data('page', parseInt(tag.data('page')) + 1)
  328. } else {
  329. $('#pager').remove()
  330. }
  331. var tempScrollTop = $(window).scrollTop();
  332. $('#primary').append($(data).find('.post'))
  333. $(window).scrollTop(tempScrollTop);
  334. Diaspora.loaded()
  335. $('html,body').animate({ scrollTop: tempScrollTop + 400 }, 500);
  336. }, function() {
  337. tag.html('加载更多').data('status', 'loaded')
  338. })
  339. return false;
  340. break;
  341. // home
  342. case (tag.indexOf('icon-home') != -1):
  343. $('.toc').fadeOut(100);
  344. if ($('#preview').hasClass('show')) {
  345. history.back();
  346. } else {
  347. location.href = "/";
  348. }
  349. break;
  350. // qrcode
  351. case (tag.indexOf('icon-scan') != -1):
  352. if ($('.icon-scan').hasClass('tg')) {
  353. $('#qr').toggle()
  354. } else {
  355. $('.icon-scan').addClass('tg')
  356. $('#qr').qrcode({ width: 128, height: 128, text: location.href}).toggle()
  357. }
  358. break;
  359. // audio play
  360. case (tag.indexOf('icon-play') != -1):
  361. $('#audio')[0].play()
  362. $('.icon-play').removeClass('icon-play').addClass('icon-pause')
  363. break;
  364. // audio pause
  365. case (tag.indexOf('icon-pause') != -1):
  366. $('#audio')[0].pause()
  367. $('.icon-pause').removeClass('icon-pause').addClass('icon-play')
  368. break;
  369. // history state
  370. case (tag.indexOf('cover') != -1):
  371. Diaspora.HS($(e.target).parent(), 'push')
  372. return false;
  373. break;
  374. // history state
  375. case (tag.indexOf('posttitle') != -1):
  376. Diaspora.HS($(e.target), 'push')
  377. return false;
  378. break;
  379. // prev, next post
  380. case (rel == 'prev' || rel == 'next'):
  381. if (rel == 'prev') {
  382. var t = $('#prev_next a')[0].text
  383. } else {
  384. var t = $('#prev_next a')[1].text
  385. }
  386. $(e.target).attr('title', t)
  387. Diaspora.HS($(e.target), 'replace')
  388. return false;
  389. break;
  390. // toc
  391. case (tag.indexOf('toc-text') != -1 || tag.indexOf('toc-link') != -1
  392. || tag.indexOf('toc-number') != -1):
  393. hash = '';
  394. if (e.target.nodeName == 'SPAN'){
  395. hash = $(e.target).parent().attr('href')
  396. }else{
  397. hash = $(e.target).attr('href')
  398. }
  399. to = $("a.headerlink[href='" + hash + "']")
  400. $("html,body").animate({
  401. scrollTop: to.offset().top - 50
  402. }, 300);
  403. return false;
  404. break;
  405. // quick view
  406. case (tag.indexOf('pviewa') != -1):
  407. $('body').removeClass('mu')
  408. setTimeout(function() {
  409. Diaspora.HS($(e.target), 'push')
  410. $('.toc').fadeIn(1000);
  411. }, 300)
  412. return false;
  413. break;
  414. // photoswipe
  415. case (tag.indexOf('pimg') != -1):
  416. var pswpElement = $('.pswp').get(0);
  417. if (pswpElement) {
  418. var items = [];
  419. var index = 0;
  420. var imgs = [];
  421. $('.content img').each(function(i, v){
  422. // get index
  423. if (e.target.src == v.src) {
  424. index = i;
  425. }
  426. var item = {
  427. src: v.src,
  428. w: v.naturalWidth,
  429. h: v.naturalHeight
  430. };
  431. imgs.push(v);
  432. items.push(item);
  433. });
  434. var options = {
  435. index: index,
  436. shareEl: false,
  437. zoomEl: false,
  438. allowRotationOnUserZoom: true,
  439. history: false,
  440. getThumbBoundsFn: function(index) {
  441. // See Options -> getThumbBoundsFn section of documentation for more info
  442. var thumbnail = imgs[index],
  443. pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
  444. rect = thumbnail.getBoundingClientRect();
  445. return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
  446. }
  447. };
  448. var lightBox= new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
  449. lightBox.init();
  450. }
  451. return false;
  452. break;
  453. // comment
  454. case - 1 != tag.indexOf("comment"):
  455. Diaspora.loading(),
  456. comment = $('#gitalk-container');
  457. gitalk = new Gitalk({
  458. clientID: comment.data('ci'),
  459. clientSecret: comment.data('cs'),
  460. repo: comment.data('r'),
  461. owner: comment.data('o'),
  462. admin: comment.data('a'),
  463. id: location.pathname,
  464. distractionFreeMode: comment.data('d')
  465. })
  466. $(".comment").removeClass("link")
  467. gitalk.render('gitalk-container')
  468. Diaspora.loaded();
  469. return false;
  470. break;
  471. default:
  472. return true;
  473. break;
  474. }
  475. })
  476. // 是否自动展开评论
  477. comment = $("#gitalk-container");
  478. if (comment.data('ae') == true){
  479. comment.click();
  480. }
  481. console.log("%c Github %c","background:#24272A; color:#ffffff","","https://github.com/Fechin/hexo-theme-diaspora")
  482. })