diaspora.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Diaspora
  3. * @author LoeiFy
  4. * @url http://lorem.in
  5. */
  6. var Home = location.href,
  7. Pages = 4,
  8. xhr, xhrUrl = '';
  9. var Diaspora = {
  10. L: function(url, f, err) {
  11. if (url == xhrUrl) {
  12. return false
  13. }
  14. xhrUrl = url;
  15. if (xhr) {
  16. xhr.abort()
  17. }
  18. xhr = $.ajax({
  19. type: 'GET',
  20. url: url,
  21. timeout: 10000,
  22. success: function(data) {
  23. f(data) xhrUrl = '';
  24. },
  25. error: function(a, b, c) {
  26. if (b == 'abort') {
  27. err && err()
  28. } else {
  29. window.location.href = url
  30. }
  31. xhrUrl = '';
  32. }
  33. })
  34. },
  35. P: function() {
  36. return !! ('ontouchstart' in window);
  37. },
  38. PS: function() {
  39. if (! (window.history && history.pushState)) return;
  40. history.replaceState({
  41. u: Home,
  42. t: document.title
  43. },
  44. document.title, Home)
  45. window.addEventListener('popstate',
  46. function(e) {
  47. var state = e.state;
  48. if (!state) return;
  49. document.title = state.t;
  50. if (state.u == Home) {
  51. $('#preview').css('position', 'fixed') setTimeout(function() {
  52. $('#preview').removeClass('show').addClass('trans') $('#container').show() window.scrollTo(0, parseInt($('#container').data('scroll'))) setTimeout(function() {
  53. $('#preview').html('') $(window).trigger('resize')
  54. },
  55. 300)
  56. },
  57. 0)
  58. } else {
  59. Diaspora.loading()
  60. Diaspora.L(state.u,
  61. function(data) {
  62. document.title = state.t;
  63. $('#preview').html($(data).filter('#single'))
  64. Diaspora.preview()
  65. setTimeout(function() {
  66. Diaspora.player()
  67. },
  68. 0)
  69. })
  70. }
  71. })
  72. },
  73. HS: function(tag, flag) {
  74. var id = tag.data('id') || 0,
  75. url = tag.attr('href'),
  76. title = tag.attr('title') || tag.text();
  77. if (!$('#preview').length || !(window.history && history.pushState)) location.href = url;
  78. Diaspora.loading()
  79. var state = {
  80. d: id,
  81. t: title,
  82. u: url
  83. };
  84. Diaspora.L(url,
  85. function(data) {
  86. if (!$(data).filter('#single').length) {
  87. location.href = url;
  88. return
  89. }
  90. switch (flag) {
  91. case 'push':
  92. history.pushState(state, title, url) break;
  93. case 'replace':
  94. history.replaceState(state, title, url) break;
  95. }
  96. document.title = title;
  97. $('#preview').html($(data).filter('#single'))
  98. switch (flag) {
  99. case 'push':
  100. Diaspora.preview() break;
  101. case 'replace':
  102. window.scrollTo(0, 0) Diaspora.loaded() break;
  103. }
  104. setTimeout(function() {
  105. Diaspora.player()
  106. if (flag == 'replace') {
  107. $('#top').show()
  108. }
  109. },
  110. 0)
  111. })
  112. },
  113. preview: function() {
  114. setTimeout(function() {
  115. $('#preview').addClass('show') $('#container').data('scroll', window.scrollY) setTimeout(function() {
  116. $('#container').hide() setTimeout(function() {
  117. $('#preview').css({
  118. 'position': 'static',
  119. 'overflow-y': 'auto'
  120. }).removeClass('trans') $('#top').show()
  121. Diaspora.loaded()
  122. },
  123. 500)
  124. },
  125. 300)
  126. },
  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. }) return
  136. }
  137. if (p.eq(0).data("autoplay") == true) {
  138. p[0].play();
  139. }
  140. p.on({
  141. 'timeupdate': function() {
  142. var progress = p[0].currentTime / p[0].duration * 100;
  143. $('.bar').css('width', progress + '%');
  144. if (progress / 5 <= 1) {
  145. p[0].volume = progress / 5;
  146. } else {
  147. p[0].volume = 1;
  148. }
  149. },
  150. 'ended': function() {
  151. $('.icon-pause').removeClass('icon-pause').addClass('icon-play')
  152. },
  153. 'playing': function() {
  154. $('.icon-play').removeClass('icon-play').addClass('icon-pause')
  155. }
  156. })
  157. },
  158. loading: function() {
  159. var w = window.innerWidth;
  160. var css = '<style class="loaderstyle" id="loaderstyle' + w + '">' + '@-moz-keyframes loader' + w + '{100%{background-position:' + w + 'px 0}}' + '@-webkit-keyframes loader' + w + '{100%{background-position:' + w + 'px 0}}' + '.loader' + w + '{-webkit-animation:loader' + w + ' 3s linear infinite;-moz-animation:loader' + w + ' 3s linear infinite;}' + '</style>';
  161. $('.loaderstyle').remove() $('head').append(css)
  162. $('#loader').removeClass().addClass('loader' + w).show()
  163. },
  164. loaded: function() {
  165. $('#loader').removeClass().hide()
  166. },
  167. F: function(id, w, h) {
  168. var _height = $(id).parent().height(),
  169. _width = $(id).parent().width(),
  170. ratio = h / w;
  171. if (_height / _width > ratio) {
  172. id.style.height = _height + 'px';
  173. id.style.width = _height / ratio + 'px';
  174. } else {
  175. id.style.width = _width + 'px';
  176. id.style.height = _width * ratio + 'px';
  177. }
  178. id.style.left = (_width - parseInt(id.style.width)) / 2 + 'px';
  179. id.style.top = (_height - parseInt(id.style.height)) / 2 + 'px';
  180. }
  181. }
  182. $(function() {
  183. if (Diaspora.P()) {
  184. $('body').addClass('touch')
  185. }
  186. if ($('#preview').length) {
  187. var cover = {};
  188. cover.t = $('#cover');
  189. cover.w = cover.t.attr('width');
  190. cover.h = cover.t.attr('height');
  191. ; (cover.o = function() {
  192. $('#mark').height(window.innerHeight)
  193. })();
  194. if (cover.t.prop('complete')) {
  195. // why setTimeout ?
  196. setTimeout(function() {
  197. cover.t.load()
  198. },
  199. 0)
  200. }
  201. cover.t.on('load',
  202. function() {
  203. ; (cover.f = function() {
  204. var _w = $('#mark').width(),
  205. _h = $('#mark').height(),
  206. x,
  207. y,
  208. i,
  209. e;
  210. e = (_w >= 1000 || _h >= 1000) ? 1000 : 500;
  211. if (_w >= _h) {
  212. i = _w / e * 50;
  213. y = i;
  214. x = i * _w / _h;
  215. } else {
  216. i = _h / e * 50;
  217. x = i;
  218. y = i * _h / _w;
  219. }
  220. $('.layer').css({
  221. 'width': _w + x,
  222. 'height': _h + y,
  223. 'marginLeft': -0.5 * x,
  224. 'marginTop': -0.5 * y
  225. })
  226. if (!cover.w) {
  227. cover.w = cover.t.width();
  228. cover.h = cover.t.height();
  229. }
  230. Diaspora.F($('#cover')[0], cover.w, cover.h)
  231. })();
  232. setTimeout(function() {
  233. $('html, body').removeClass('loading')
  234. },
  235. 1000)
  236. $('#mark').parallax()
  237. var vibrant = new Vibrant(cover.t[0]);
  238. var swatches = vibrant.swatches()
  239. if (swatches['DarkVibrant']) {
  240. $('#vibrant polygon').css('fill', swatches['DarkVibrant'].getHex()) $('#vibrant div').css('background-color', swatches['DarkVibrant'].getHex())
  241. }
  242. if (swatches['Vibrant']) {
  243. $('.icon-menu').css('color', swatches['Vibrant'].getHex())
  244. }
  245. })
  246. if (!cover.t.attr('src')) {
  247. alert('Please set the post thumbnail')
  248. }
  249. $('#preview').css('min-height', window.innerHeight)
  250. Diaspora.PS()
  251. $('.pview a').addClass('pviewa')
  252. var T;
  253. $(window).on('resize',
  254. function() {
  255. clearTimeout(T)
  256. T = setTimeout(function() {
  257. if (!Diaspora.P() && location.href == Home) {
  258. cover.o() cover.f()
  259. }
  260. if ($('#loader').attr('class')) {
  261. Diaspora.loading()
  262. }
  263. },
  264. 500)
  265. })
  266. } else {
  267. $('#single').css('min-height', window.innerHeight)
  268. setTimeout(function() {
  269. $('html, body').removeClass('loading')
  270. },
  271. 1000)
  272. window.addEventListener('popstate',
  273. function(e) {
  274. if (e.state) location.href = e.state.u;
  275. })
  276. Diaspora.player();
  277. $('.icon-icon, .image-icon').attr('href', '/')
  278. $('#top').show()
  279. }
  280. $(window).on('scroll',
  281. function() {
  282. if ($('.scrollbar').length && !Diaspora.P() && !$('.icon-images').hasClass('active')) {
  283. var st = $(window).scrollTop(),
  284. ct = $('.content').height();
  285. if (st > ct) {
  286. st = ct
  287. }
  288. $('.scrollbar').width((50 + st) / ct * 100 + '%')
  289. if (st > 80 && window.innerWidth > 800) {
  290. $('.subtitle').fadeIn()
  291. } else {
  292. $('.subtitle').fadeOut()
  293. }
  294. }
  295. })
  296. $(window).on('touchmove',
  297. function(e) {
  298. if ($('body').hasClass('mu')) {
  299. e.preventDefault()
  300. }
  301. })
  302. $('body').on('click',
  303. function(e) {
  304. var tag = $(e.target).attr('class') || '',
  305. rel = $(e.target).attr('rel') || '';
  306. if (!tag && !rel) return;
  307. switch (true) {
  308. // nav menu
  309. case(tag.indexOf('switchmenu') != -1) : window.scrollTo(0, 0) $('html, body').toggleClass('mu');
  310. break;
  311. // next page
  312. case (tag.indexOf('more') != -1) : tag = $('.more');
  313. if (tag.data('status') == 'loading') {
  314. return false
  315. }
  316. var num = parseInt(tag.data('page')) || 1;
  317. if (num == 1) {
  318. tag.data('page', 1)
  319. }
  320. if (num >= Pages) {
  321. return
  322. }
  323. tag.html('åŠ è½½ä¸­..').data('status', 'loading') Diaspora.loading()
  324. Diaspora.L(tag.attr('href'),
  325. function(data) {
  326. var link = $(data).find('.more').attr('href');
  327. if (link != undefined) {
  328. tag.attr('href', link).html('åŠ è½½æ›´å¤š').data('status', 'loaded') tag.data('page', parseInt(tag.data('page')) + 1)
  329. } else {
  330. $('#pager').remove()
  331. }
  332. $('#primary').append($(data).find('.post'))
  333. Diaspora.loaded()
  334. },
  335. function() {
  336. tag.html('åŠ è½½æ›´å¤š').data('status', 'loaded')
  337. })
  338. return false;
  339. break;
  340. // home
  341. case (tag.indexOf('icon-home') != -1) : if ($('#preview').hasClass('show')) {
  342. history.back();
  343. } else {
  344. location.href = "/";
  345. }
  346. break;
  347. // qrcode
  348. case (tag.indexOf('icon-scan') != -1) : if ($('.icon-scan').hasClass('tg')) {
  349. $('#qr').toggle()
  350. } else {
  351. $('.icon-scan').addClass('tg') $('#qr').qrcode({
  352. width: 128,
  353. height: 128,
  354. text: location.href
  355. }).toggle()
  356. }
  357. break;
  358. // audio play
  359. case (tag.indexOf('icon-play') != -1) : $('#audio')[0].play() $('.icon-play').removeClass('icon-play').addClass('icon-pause') break;
  360. // audio pause
  361. case (tag.indexOf('icon-pause') != -1) : $('#audio')[0].pause() $('.icon-pause').removeClass('icon-pause').addClass('icon-play') break;
  362. // history state
  363. case (tag.indexOf('cover') != -1) : Diaspora.HS($(e.target).parent(), 'push') return false;
  364. break;
  365. // history state
  366. case (tag.indexOf('posttitle') != -1) : Diaspora.HS($(e.target), 'push') return false;
  367. break;
  368. // prev, next post
  369. case (rel == 'prev' || rel == 'next') : if (rel == 'prev') {
  370. var t = $('#prev_next a')[0].text
  371. } else {
  372. var t = $('#prev_next a')[1].text
  373. }
  374. $(e.target).attr('title', t)
  375. Diaspora.HS($(e.target), 'replace') return false;
  376. break;
  377. // quick view
  378. case (tag.indexOf('pviewa') != -1) : $('body').removeClass('mu')
  379. setTimeout(function() {
  380. Diaspora.HS($(e.target), 'push')
  381. },
  382. 300)
  383. return false;
  384. break;
  385. // comment
  386. case - 1 != tag.indexOf("comment") && -1 != tag.indexOf("link") : Diaspora.loading();
  387. $(".comment").removeClass("link").html("");
  388. var id = $(".comment").data("id"),
  389. title = $(".comment").data("title"),
  390. url = $(".comment").data("url"),
  391. shortname = $(".comment").data("shortname");
  392. window.duoshuoQuery = {
  393. short_name: shortname
  394. };
  395. var f = function() {
  396. var a = document.createElement("div");
  397. a.setAttribute("data-thread-key", id);
  398. a.setAttribute("data-title", title);
  399. a.setAttribute("data-url", url);
  400. DUOSHUO.EmbedThread(a);
  401. $(".comment").html(a);
  402. Diaspora.loaded();
  403. };
  404. window.DUOSHUO ? f() : $.getScript("http://static.duoshuo.com/embed.js",
  405. function() {
  406. f()
  407. });
  408. return false;
  409. break;
  410. default:
  411. return;
  412. break;
  413. }
  414. })
  415. console.log("%c Github %c", "background:#24272A; color:#ffffff", "", "https://github.com/Fechin/hexo-theme-diaspora")
  416. })