diaspora.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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');
  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. // preview toggle
  110. $("#preview").one('transitionend webkitTransitionEnd oTransitionEnd otransitionend MSTransitionEnd', function() {
  111. var left = $('#preview').css('left');
  112. if (left == '0px') {
  113. $('#container').hide();
  114. }else{
  115. $('#container').show();
  116. }
  117. Diaspora.loaded();
  118. });
  119. setTimeout(function() {
  120. $('#preview').addClass('show');
  121. $('#container').data('scroll', window.scrollY);
  122. setTimeout(function() {
  123. $('#preview').css({
  124. 'position': 'static',
  125. 'overflow-y': 'auto'
  126. });
  127. }, 500);
  128. }, 0);
  129. },
  130. player: function() {
  131. var p = $('#audio');
  132. if (!p.length) {
  133. $('.icon-play').css({
  134. 'color': '#dedede',
  135. 'cursor': 'not-allowed'
  136. })
  137. return
  138. }
  139. if (p.eq(0).data("autoplay") == true) {
  140. p[0].play();
  141. }
  142. p.on({
  143. 'timeupdate': function() {
  144. var progress = p[0].currentTime / p[0].duration * 100;
  145. $('.bar').css('width', progress + '%');
  146. if (progress / 5 <= 1) {
  147. p[0].volume = progress / 5;
  148. }else {
  149. p[0].volume = 1;
  150. }
  151. },
  152. 'ended': function() {
  153. $('.icon-pause').removeClass('icon-pause').addClass('icon-play')
  154. },
  155. 'playing': function() {
  156. $('.icon-play').removeClass('icon-play').addClass('icon-pause')
  157. }
  158. })
  159. },
  160. loading: function() {
  161. var w = window.innerWidth;
  162. var css = '<style class="loaderstyle" id="loaderstyle'+ w +'">'+
  163. '@-moz-keyframes loader'+ w +'{100%{background-position:'+ w +'px 0}}'+
  164. '@-webkit-keyframes loader'+ w +'{100%{background-position:'+ w +'px 0}}'+
  165. '.loader'+ w +'{-webkit-animation:loader'+ w +' 3s linear infinite;-moz-animation:loader'+ w +' 3s linear infinite;}'+
  166. '</style>';
  167. $('.loaderstyle').remove()
  168. $('head').append(css)
  169. $('#loader').removeClass().addClass('loader'+ w).show()
  170. },
  171. loaded: function() {
  172. $('#loader').removeClass().hide()
  173. },
  174. F: function(id, w, h) {
  175. var _height = $(id).parent().height(),
  176. _width = $(id).parent().width(),
  177. ratio = h / w;
  178. if (_height / _width > ratio) {
  179. id.style.height = _height +'px';
  180. id.style.width = _height / ratio +'px';
  181. } else {
  182. id.style.width = _width +'px';
  183. id.style.height = _width * ratio +'px';
  184. }
  185. id.style.left = (_width - parseInt(id.style.width)) / 2 +'px';
  186. id.style.top = (_height - parseInt(id.style.height)) / 2 +'px';
  187. }
  188. }
  189. $(function() {
  190. if (Diaspora.P()) {
  191. $('body').addClass('touch')
  192. }
  193. if ($('#preview').length) {
  194. var cover = {};
  195. cover.t = $('#cover');
  196. cover.w = cover.t.attr('width');
  197. cover.h = cover.t.attr('height');
  198. ;(cover.o = function() {
  199. $('#mark').height(window.innerHeight)
  200. })();
  201. if (cover.t.prop('complete')) {
  202. // why setTimeout ?
  203. setTimeout(function() { cover.t.load() }, 0)
  204. }
  205. cover.t.on('load', function() {
  206. ;(cover.f = function() {
  207. var _w = $('#mark').width(), _h = $('#mark').height(), x, y, i, e;
  208. e = (_w >= 1000 || _h >= 1000) ? 1000 : 500;
  209. if (_w >= _h) {
  210. i = _w / e * 50;
  211. y = i;
  212. x = i * _w / _h;
  213. } else {
  214. i = _h / e * 50;
  215. x = i;
  216. y = i * _h / _w;
  217. }
  218. $('.layer').css({
  219. 'width': _w + x,
  220. 'height': _h + y,
  221. 'marginLeft': - 0.5 * x,
  222. 'marginTop': - 0.5 * y
  223. })
  224. if (!cover.w) {
  225. cover.w = cover.t.width();
  226. cover.h = cover.t.height();
  227. }
  228. Diaspora.F($('#cover')[0], cover.w, cover.h)
  229. })();
  230. setTimeout(function() {
  231. $('html, body').removeClass('loading')
  232. }, 1000)
  233. $('#mark').parallax()
  234. var vibrant = new Vibrant(cover.t[0]);
  235. var swatches = vibrant.swatches()
  236. if (swatches['DarkVibrant']) {
  237. $('#vibrant polygon').css('fill', swatches['DarkVibrant'].getHex())
  238. $('#vibrant div').css('background-color', swatches['DarkVibrant'].getHex())
  239. }
  240. if (swatches['Vibrant']) {
  241. $('.icon-menu').css('color', swatches['Vibrant'].getHex())
  242. }
  243. })
  244. if (!cover.t.attr('src')) {
  245. alert('Please set the post thumbnail')
  246. }
  247. $('#preview').css('min-height', window.innerHeight)
  248. Diaspora.PS()
  249. $('.pview a').addClass('pviewa')
  250. var T;
  251. $(window).on('resize', function() {
  252. clearTimeout(T)
  253. T = setTimeout(function() {
  254. if (!Diaspora.P() && location.href == Home) {
  255. cover.o()
  256. cover.f()
  257. }
  258. if ($('#loader').attr('class')) {
  259. Diaspora.loading()
  260. }
  261. }, 500)
  262. })
  263. } else {
  264. $('#single').css('min-height', window.innerHeight)
  265. setTimeout(function() {
  266. $('html, body').removeClass('loading')
  267. }, 1000)
  268. window.addEventListener('popstate', function(e) {
  269. if (e.state) location.href = e.state.u;
  270. })
  271. Diaspora.player();
  272. $('.icon-icon, .image-icon').attr('href', '/')
  273. $('#top').show()
  274. }
  275. $(window).on('scroll', function() {
  276. if ($('.scrollbar').length && !Diaspora.P() && !$('.icon-images').hasClass('active')) {
  277. var st = $(window).scrollTop(),
  278. ct = $('.content').height();
  279. if (st > ct) {
  280. st = ct
  281. }
  282. $('.scrollbar').width((50 + st) / ct * 100 +'%')
  283. if (st > 80 && window.innerWidth > 800) {
  284. $('.subtitle').fadeIn()
  285. } else {
  286. $('.subtitle').fadeOut()
  287. }
  288. }
  289. })
  290. $(window).on('touchmove', function(e) {
  291. if ($('body').hasClass('mu')) {
  292. e.preventDefault()
  293. }
  294. })
  295. $('body').on('click', function(e) {
  296. var tag = $(e.target).attr('class') || '',
  297. rel = $(e.target).attr('rel') || '';
  298. if (!tag && !rel) return;
  299. switch (true) {
  300. // nav menu
  301. case (tag.indexOf('switchmenu') != -1):
  302. window.scrollTo(0, 0)
  303. $('html, body').toggleClass('mu');
  304. break;
  305. // next page
  306. case (tag.indexOf('more') != -1):
  307. tag = $('.more');
  308. if (tag.data('status') == 'loading') {
  309. return false
  310. }
  311. var num = parseInt(tag.data('page')) || 1;
  312. if (num == 1) {
  313. tag.data('page', 1)
  314. }
  315. if (num >= Pages) {
  316. return
  317. }
  318. tag.html('åŠ è½½ä¸­..').data('status', 'loading')
  319. Diaspora.loading()
  320. Diaspora.L(tag.attr('href'), function(data) {
  321. var link = $(data).find('.more').attr('href');
  322. if (link != undefined) {
  323. tag.attr('href', link).html('åŠ è½½æ›´å¤š').data('status', 'loaded')
  324. tag.data('page', parseInt(tag.data('page')) + 1)
  325. } else {
  326. $('#pager').remove()
  327. }
  328. $('#primary').append($(data).find('.post'))
  329. Diaspora.loaded()
  330. }, function() {
  331. tag.html('åŠ è½½æ›´å¤š').data('status', 'loaded')
  332. })
  333. return false;
  334. break;
  335. // home
  336. case (tag.indexOf('icon-home') != -1):
  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. // quick view
  384. case (tag.indexOf('pviewa') != -1):
  385. $('body').removeClass('mu')
  386. setTimeout(function() {
  387. Diaspora.HS($(e.target), 'push')
  388. }, 300)
  389. return false;
  390. break;
  391. default:
  392. return;
  393. break;
  394. }
  395. })
  396. console.log("%c Github %c","background:#24272A; color:#ffffff","","https://github.com/Fechin/hexo-theme-diaspora")
  397. })