diaspora.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. }, 0)
  101. })
  102. },
  103. preview: function() {
  104. // preview toggle
  105. $("#preview").one('transitionend webkitTransitionEnd oTransitionEnd otransitionend MSTransitionEnd', function() {
  106. var left = $('#preview').css('left');
  107. if (left == '0px') {
  108. $('#container').hide();
  109. }else{
  110. $('#container').show();
  111. }
  112. Diaspora.loaded();
  113. });
  114. setTimeout(function() {
  115. $('#preview').addClass('show');
  116. $('#container').data('scroll', window.scrollY);
  117. setTimeout(function() {
  118. $('#preview').css({
  119. 'position': 'static',
  120. 'overflow-y': 'auto'
  121. });
  122. }, 500);
  123. }, 0);
  124. },
  125. player: function() {
  126. var p = $('#audio');
  127. if (!p.length) {
  128. $('.icon-play').css({
  129. 'color': '#dedede',
  130. 'cursor': 'not-allowed'
  131. })
  132. return
  133. }
  134. if (p.eq(0).data("autoplay") == true) {
  135. p[0].play();
  136. }
  137. p.on({
  138. 'timeupdate': function() {
  139. var progress = p[0].currentTime / p[0].duration * 100;
  140. $('.bar').css('width', progress + '%');
  141. if (progress / 5 <= 1) {
  142. p[0].volume = progress / 5;
  143. }else {
  144. p[0].volume = 1;
  145. }
  146. },
  147. 'ended': function() {
  148. $('.icon-pause').removeClass('icon-pause').addClass('icon-play')
  149. },
  150. 'playing': function() {
  151. $('.icon-play').removeClass('icon-play').addClass('icon-pause')
  152. }
  153. })
  154. },
  155. loading: function() {
  156. var w = window.innerWidth;
  157. var css = '<style class="loaderstyle" id="loaderstyle'+ w +'">'+
  158. '@-moz-keyframes loader'+ w +'{100%{background-position:'+ w +'px 0}}'+
  159. '@-webkit-keyframes loader'+ w +'{100%{background-position:'+ w +'px 0}}'+
  160. '.loader'+ w +'{-webkit-animation:loader'+ w +' 3s linear infinite;-moz-animation:loader'+ w +' 3s linear infinite;}'+
  161. '</style>';
  162. $('.loaderstyle').remove()
  163. $('head').append(css)
  164. $('#loader').removeClass().addClass('loader'+ w).show()
  165. },
  166. loaded: function() {
  167. $('#loader').removeClass().hide()
  168. },
  169. F: function(id, w, h) {
  170. var _height = $(id).parent().height(),
  171. _width = $(id).parent().width(),
  172. ratio = h / w;
  173. if (_height / _width > ratio) {
  174. id.style.height = _height +'px';
  175. id.style.width = _height / ratio +'px';
  176. } else {
  177. id.style.width = _width +'px';
  178. id.style.height = _width * ratio +'px';
  179. }
  180. id.style.left = (_width - parseInt(id.style.width)) / 2 +'px';
  181. id.style.top = (_height - parseInt(id.style.height)) / 2 +'px';
  182. }
  183. };
  184. $(function() {
  185. if (Diaspora.P()) {
  186. $('body').addClass('touch')
  187. }
  188. if ($('#preview').length) {
  189. var cover = {};
  190. cover.t = $('#cover');
  191. cover.w = cover.t.attr('width');
  192. cover.h = cover.t.attr('height');
  193. ;(cover.o = function() {
  194. $('#mark').height(window.innerHeight)
  195. })();
  196. if (cover.t.prop('complete')) {
  197. // why setTimeout ?
  198. setTimeout(function() { cover.t.load() }, 0)
  199. }
  200. cover.t.on('load', function() {
  201. ;(cover.f = function() {
  202. var _w = $('#mark').width(), _h = $('#mark').height(), x, y, i, e;
  203. e = (_w >= 1000 || _h >= 1000) ? 1000 : 500;
  204. if (_w >= _h) {
  205. i = _w / e * 50;
  206. y = i;
  207. x = i * _w / _h;
  208. } else {
  209. i = _h / e * 50;
  210. x = i;
  211. y = i * _h / _w;
  212. }
  213. $('.layer').css({
  214. 'width': _w + x,
  215. 'height': _h + y,
  216. 'marginLeft': - 0.5 * x,
  217. 'marginTop': - 0.5 * y
  218. })
  219. if (!cover.w) {
  220. cover.w = cover.t.width();
  221. cover.h = cover.t.height();
  222. }
  223. Diaspora.F($('#cover')[0], cover.w, cover.h)
  224. })();
  225. setTimeout(function() {
  226. $('html, body').removeClass('loading')
  227. }, 1000)
  228. $('#mark').parallax()
  229. var vibrant = new Vibrant(cover.t[0]);
  230. var swatches = vibrant.swatches()
  231. if (swatches['DarkVibrant']) {
  232. $('#vibrant polygon').css('fill', swatches['DarkVibrant'].getHex())
  233. $('#vibrant div').css('background-color', swatches['DarkVibrant'].getHex())
  234. }
  235. if (swatches['Vibrant']) {
  236. $('.icon-menu').css('color', swatches['Vibrant'].getHex())
  237. }
  238. })
  239. if (!cover.t.attr('src')) {
  240. alert('Please set the post thumbnail')
  241. }
  242. $('#preview').css('min-height', window.innerHeight)
  243. Diaspora.PS()
  244. $('.pview a').addClass('pviewa')
  245. var T;
  246. $(window).on('resize', function() {
  247. clearTimeout(T)
  248. T = setTimeout(function() {
  249. if (!Diaspora.P() && location.href == Home) {
  250. cover.o()
  251. cover.f()
  252. }
  253. if ($('#loader').attr('class')) {
  254. Diaspora.loading()
  255. }
  256. }, 500)
  257. })
  258. } else {
  259. $('#single').css('min-height', window.innerHeight)
  260. setTimeout(function() {
  261. $('html, body').removeClass('loading')
  262. }, 1000)
  263. window.addEventListener('popstate', function(e) {
  264. if (e.state) location.href = e.state.u;
  265. })
  266. Diaspora.player();
  267. $('.icon-icon, .image-icon').attr('href', '/')
  268. $('#top').show()
  269. }
  270. $(window).on('scroll', function() {
  271. if ($('.scrollbar').length && !Diaspora.P() && !$('.icon-images').hasClass('active')) {
  272. var wt = $(window).scrollTop(),
  273. tw = $('#top').width(),
  274. dh = document.body.scrollHeight,
  275. wh = $(window).height();
  276. var width = tw / (dh - wh) * wt;
  277. $('.scrollbar').width(width)
  278. if (wt > 80 && window.innerWidth > 800) {
  279. $('.subtitle').fadeIn()
  280. } else {
  281. $('.subtitle').fadeOut()
  282. }
  283. }
  284. })
  285. $(window).on('touchmove', function(e) {
  286. if ($('body').hasClass('mu')) {
  287. e.preventDefault()
  288. }
  289. })
  290. $('body').on('click', function(e) {
  291. var tag = $(e.target).attr('class') || '',
  292. rel = $(e.target).attr('rel') || '';
  293. // .content > p > img
  294. if (e.target.nodeName == "IMG" && $(e.target).parent().get(0).nodeName == "P") {
  295. tag = 'pimg';
  296. }
  297. if (!tag && !rel) return;
  298. switch (true) {
  299. // nav menu
  300. case (tag.indexOf('switchmenu') != -1):
  301. window.scrollTo(0, 0)
  302. $('html, body').toggleClass('mu');
  303. break;
  304. // next page
  305. case (tag.indexOf('more') != -1):
  306. tag = $('.more');
  307. if (tag.data('status') == 'loading') {
  308. return false
  309. }
  310. var num = parseInt(tag.data('page')) || 1;
  311. if (num == 1) {
  312. tag.data('page', 1)
  313. }
  314. if (num >= Pages) {
  315. return
  316. }
  317. tag.html('加载中...').data('status', 'loading')
  318. Diaspora.loading()
  319. Diaspora.L(tag.attr('href'), function(data) {
  320. var link = $(data).find('.more').attr('href');
  321. if (link != undefined) {
  322. tag.attr('href', link).html('加载更多').data('status', 'loaded')
  323. tag.data('page', parseInt(tag.data('page')) + 1)
  324. } else {
  325. $('#pager').remove()
  326. }
  327. $('#primary').append($(data).find('.post'))
  328. Diaspora.loaded()
  329. }, function() {
  330. tag.html('加载更多').data('status', 'loaded')
  331. })
  332. return false;
  333. break;
  334. // home
  335. case (tag.indexOf('icon-home') != -1):
  336. $('.toc').fadeOut(100);
  337. if ($('#preview').hasClass('show')) {
  338. history.back();
  339. } else {
  340. location.href = "/";
  341. }
  342. break;
  343. // qrcode
  344. case (tag.indexOf('icon-scan') != -1):
  345. if ($('.icon-scan').hasClass('tg')) {
  346. $('#qr').toggle()
  347. } else {
  348. $('.icon-scan').addClass('tg')
  349. $('#qr').qrcode({ width: 128, height: 128, text: location.href}).toggle()
  350. }
  351. break;
  352. // audio play
  353. case (tag.indexOf('icon-play') != -1):
  354. $('#audio')[0].play()
  355. $('.icon-play').removeClass('icon-play').addClass('icon-pause')
  356. break;
  357. // audio pause
  358. case (tag.indexOf('icon-pause') != -1):
  359. $('#audio')[0].pause()
  360. $('.icon-pause').removeClass('icon-pause').addClass('icon-play')
  361. break;
  362. // history state
  363. case (tag.indexOf('cover') != -1):
  364. Diaspora.HS($(e.target).parent(), 'push')
  365. return false;
  366. break;
  367. // history state
  368. case (tag.indexOf('posttitle') != -1):
  369. Diaspora.HS($(e.target), 'push')
  370. return false;
  371. break;
  372. // prev, next post
  373. case (rel == 'prev' || rel == 'next'):
  374. if (rel == 'prev') {
  375. var t = $('#prev_next a')[0].text
  376. } else {
  377. var t = $('#prev_next a')[1].text
  378. }
  379. $(e.target).attr('title', t)
  380. Diaspora.HS($(e.target), 'replace')
  381. return false;
  382. break;
  383. // toc
  384. case (tag.indexOf('toc-text') != -1 || tag.indexOf('toc-link') != -1
  385. || tag.indexOf('toc-number') != -1):
  386. hash = '';
  387. if (e.target.nodeName == 'SPAN'){
  388. hash = $(e.target).parent().attr('href')
  389. }else{
  390. hash = $(e.target).attr('href')
  391. }
  392. to = $("a.headerlink[href='" + hash + "']")
  393. $("html,body").animate({
  394. scrollTop: to.offset().top - 50
  395. }, 300);
  396. return false;
  397. break;
  398. // quick view
  399. case (tag.indexOf('pviewa') != -1):
  400. $('body').removeClass('mu')
  401. setTimeout(function() {
  402. Diaspora.HS($(e.target), 'push')
  403. $('.toc').fadeIn(1000);
  404. }, 300)
  405. return false;
  406. break;
  407. case (tag.indexOf('pimg') != -1):
  408. // photoswipe
  409. var pswpElement = $('.pswp').get(0);
  410. if (pswpElement) {
  411. var items = [];
  412. var index = 0;
  413. var imgs = [];
  414. $('.content img').each(function(i, v){
  415. // get index
  416. if (e.target.src == v.src) {
  417. index = i;
  418. }
  419. var item = {
  420. src: v.src,
  421. w: v.naturalWidth,
  422. h: v.naturalHeight
  423. };
  424. imgs.push(v);
  425. items.push(item);
  426. });
  427. var options = {
  428. index: index,
  429. shareEl: false,
  430. zoomEl: false,
  431. allowRotationOnUserZoom: true,
  432. history: false,
  433. getThumbBoundsFn: function(index) {
  434. // See Options -> getThumbBoundsFn section of documentation for more info
  435. var thumbnail = imgs[index],
  436. pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
  437. rect = thumbnail.getBoundingClientRect();
  438. return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
  439. }
  440. };
  441. var lightBox= new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
  442. lightBox.init();
  443. }
  444. return false;
  445. break;
  446. default:
  447. return true;
  448. break;
  449. }
  450. })
  451. console.log("%c Github %c","background:#24272A; color:#ffffff","","https://github.com/Fechin/hexo-theme-diaspora")
  452. })