diaspora.js 16 KB

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