diaspora.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * Diaspora
  3. * @author LoeiFy
  4. * @url http://lorem.in
  5. */
  6. var Home = location.href,
  7. Pages = 4,
  8. xhr,
  9. xhrUrl = '';
  10. var Diaspora = {
  11. L: function(url, f, err) {
  12. if (url == xhrUrl) {
  13. return false
  14. }
  15. xhrUrl = url;
  16. if (xhr) {
  17. xhr.abort()
  18. }
  19. xhr = $.ajax({
  20. type: 'GET',
  21. url: url,
  22. timeout: 10000,
  23. success: function(data) {
  24. f(data)
  25. xhrUrl = '';
  26. },
  27. error: function(a, b, c) {
  28. if (b == 'abort') {
  29. err && err()
  30. } else {
  31. window.location.href = url
  32. }
  33. xhrUrl = '';
  34. }
  35. })
  36. },
  37. P: function() {
  38. return !!('ontouchstart' in window);
  39. },
  40. PS: function() {
  41. if (!(window.history && history.pushState)) return;
  42. history.replaceState({u: Home, t: document.title}, document.title, Home)
  43. window.addEventListener('popstate', function(e) {
  44. var state = e.state;
  45. if (!state) return;
  46. document.title = state.t;
  47. if (state.u == Home) {
  48. $('#preview').css('position', 'fixed')
  49. setTimeout(function() {
  50. $('#preview').removeClass('show').addClass('trans')
  51. $('#container').show()
  52. window.scrollTo(0, parseInt($('#container').data('scroll')))
  53. setTimeout(function() {
  54. $('#preview').html('')
  55. $(window).trigger('resize')
  56. }, 300)
  57. }, 0)
  58. } else {
  59. Diaspora.loading()
  60. Diaspora.L(state.u, function(data) {
  61. document.title = state.t;
  62. $('#preview').html($(data).filter('#single'))
  63. Diaspora.preview()
  64. setTimeout(function() { Diaspora.player() }, 0)
  65. })
  66. }
  67. })
  68. },
  69. HS: function(tag, flag) {
  70. var id = tag.data('id') || 0,
  71. url = tag.attr('href'),
  72. title = tag.attr('title') || tag.text();
  73. if (!$('#preview').length || !(window.history && history.pushState)) location.href = url;
  74. Diaspora.loading()
  75. var state = {d: id, t: title, u: url};
  76. Diaspora.L(url, function(data) {
  77. if (!$(data).filter('#single').length) {
  78. location.href = url;
  79. return
  80. }
  81. switch (flag) {
  82. case 'push':
  83. history.pushState(state, title, url)
  84. break;
  85. case 'replace':
  86. history.replaceState(state, title, url)
  87. break;
  88. }
  89. document.title = title;
  90. $('#preview').html($(data).filter('#single'))
  91. switch (flag) {
  92. case 'push':
  93. Diaspora.preview()
  94. break;
  95. case 'replace':
  96. window.scrollTo(0, 0)
  97. Diaspora.loaded()
  98. break;
  99. }
  100. setTimeout(function() {
  101. Diaspora.player()
  102. if (flag == 'replace') {
  103. $('#top').show()
  104. }
  105. }, 0)
  106. })
  107. },
  108. preview: function() {
  109. setTimeout(function() {
  110. $('#preview').addClass('show')
  111. $('#container').data('scroll', window.scrollY)
  112. setTimeout(function() {
  113. $('#container').hide()
  114. setTimeout(function() {
  115. $('#preview').css({
  116. 'position': 'static',
  117. 'overflow-y': 'auto'
  118. }).removeClass('trans')
  119. $('#top').show()
  120. Diaspora.loaded()
  121. }, 500)
  122. }, 300)
  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 st = $(window).scrollTop(),
  273. ct = $('.content').height();
  274. if (st > ct) {
  275. st = ct
  276. }
  277. $('.scrollbar').width((50 + st) / ct * 100 +'%')
  278. if (st > 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. if (!tag && !rel) return;
  294. switch (true) {
  295. // nav menu
  296. case (tag.indexOf('switchmenu') != -1):
  297. window.scrollTo(0, 0)
  298. $('html, body').toggleClass('mu');
  299. break;
  300. // next page
  301. case (tag.indexOf('more') != -1):
  302. tag = $('.more');
  303. if (tag.data('status') == 'loading') {
  304. return false
  305. }
  306. var num = parseInt(tag.data('page')) || 1;
  307. if (num == 1) {
  308. tag.data('page', 1)
  309. }
  310. if (num >= Pages) {
  311. return
  312. }
  313. tag.html('加载中..').data('status', 'loading')
  314. Diaspora.loading()
  315. Diaspora.L(tag.attr('href'), function(data) {
  316. var link = $(data).find('.more').attr('href');
  317. if (link != undefined) {
  318. tag.attr('href', link).html('加载更多').data('status', 'loaded')
  319. tag.data('page', parseInt(tag.data('page')) + 1)
  320. } else {
  321. $('#pager').remove()
  322. }
  323. $('#primary').append($(data).find('.post'))
  324. Diaspora.loaded()
  325. }, function() {
  326. tag.html('加载更多').data('status', 'loaded')
  327. })
  328. return false;
  329. break;
  330. // home
  331. case (tag.indexOf('icon-home') != -1):
  332. if ($('#preview').hasClass('show')) {
  333. history.back();
  334. } else {
  335. location.href = "/";
  336. }
  337. break;
  338. // qrcode
  339. case (tag.indexOf('icon-scan') != -1):
  340. if ($('.icon-scan').hasClass('tg')) {
  341. $('#qr').toggle()
  342. } else {
  343. $('.icon-scan').addClass('tg')
  344. $('#qr').qrcode({ width: 128, height: 128, text: location.href}).toggle()
  345. }
  346. break;
  347. // audio play
  348. case (tag.indexOf('icon-play') != -1):
  349. $('#audio')[0].play()
  350. $('.icon-play').removeClass('icon-play').addClass('icon-pause')
  351. break;
  352. // audio pause
  353. case (tag.indexOf('icon-pause') != -1):
  354. $('#audio')[0].pause()
  355. $('.icon-pause').removeClass('icon-pause').addClass('icon-play')
  356. break;
  357. // history state
  358. case (tag.indexOf('cover') != -1):
  359. Diaspora.HS($(e.target).parent(), 'push')
  360. return false;
  361. break;
  362. // history state
  363. case (tag.indexOf('posttitle') != -1):
  364. Diaspora.HS($(e.target), 'push')
  365. return false;
  366. break;
  367. // relate post
  368. case (tag.indexOf('relatea') != -1):
  369. Diaspora.HS($(e.target), 'replace')
  370. return false;
  371. break;
  372. // relate post
  373. case (tag.indexOf('relateimg') != -1):
  374. Diaspora.HS($(e.target).parent(), 'replace')
  375. return false;
  376. break;
  377. // prev, next post
  378. case (rel == 'prev' || rel == 'next'):
  379. if (rel == 'prev') {
  380. var t = $('#prev_next a')[0].text
  381. } else {
  382. var t = $('#prev_next a')[1].text
  383. }
  384. $(e.target).attr('title', t)
  385. Diaspora.HS($(e.target), 'replace')
  386. return false;
  387. break;
  388. // quick view
  389. case (tag.indexOf('pviewa') != -1):
  390. $('body').removeClass('mu')
  391. setTimeout(function() {
  392. Diaspora.HS($(e.target), 'push')
  393. }, 300)
  394. return false;
  395. break;
  396. default:
  397. return;
  398. break;
  399. }
  400. })
  401. console.log("%c Github %c","background:#24272A; color:#ffffff","","https://github.com/Fechin/hexo-theme-diaspora")
  402. })