123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- /*
- * Diaspora
- * @author LoeiFy
- * @url http://lorem.in
- */
- var Home = location.href,
- Pages = 4,
- xhr, xhrUrl = '';
- var Diaspora = {
- L: function(url, f, err) {
- if (url == xhrUrl) {
- return false
- }
- xhrUrl = url;
- if (xhr) {
- xhr.abort()
- }
- xhr = $.ajax({
- type: 'GET',
- url: url,
- timeout: 10000,
- success: function(data) {
- f(data) xhrUrl = '';
- },
- error: function(a, b, c) {
- if (b == 'abort') {
- err && err()
- } else {
- window.location.href = url
- }
- xhrUrl = '';
- }
- })
- },
- P: function() {
- return !! ('ontouchstart' in window);
- },
- PS: function() {
- if (! (window.history && history.pushState)) return;
- history.replaceState({
- u: Home,
- t: document.title
- },
- document.title, Home)
- window.addEventListener('popstate',
- function(e) {
- var state = e.state;
- if (!state) return;
- document.title = state.t;
- if (state.u == Home) {
- $('#preview').css('position', 'fixed') setTimeout(function() {
- $('#preview').removeClass('show').addClass('trans') $('#container').show() window.scrollTo(0, parseInt($('#container').data('scroll'))) setTimeout(function() {
- $('#preview').html('') $(window).trigger('resize')
- },
- 300)
- },
- 0)
- } else {
- Diaspora.loading()
- Diaspora.L(state.u,
- function(data) {
- document.title = state.t;
- $('#preview').html($(data).filter('#single'))
- Diaspora.preview()
- setTimeout(function() {
- Diaspora.player()
- },
- 0)
- })
- }
- })
- },
- HS: function(tag, flag) {
- var id = tag.data('id') || 0,
- url = tag.attr('href'),
- title = tag.attr('title') || tag.text();
- if (!$('#preview').length || !(window.history && history.pushState)) location.href = url;
- Diaspora.loading()
- var state = {
- d: id,
- t: title,
- u: url
- };
- Diaspora.L(url,
- function(data) {
- if (!$(data).filter('#single').length) {
- location.href = url;
- return
- }
- switch (flag) {
- case 'push':
- history.pushState(state, title, url) break;
- case 'replace':
- history.replaceState(state, title, url) break;
- }
- document.title = title;
- $('#preview').html($(data).filter('#single'))
- switch (flag) {
- case 'push':
- Diaspora.preview() break;
- case 'replace':
- window.scrollTo(0, 0) Diaspora.loaded() break;
- }
- setTimeout(function() {
- Diaspora.player()
- if (flag == 'replace') {
- $('#top').show()
- }
- },
- 0)
- })
- },
- preview: function() {
- setTimeout(function() {
- $('#preview').addClass('show') $('#container').data('scroll', window.scrollY) setTimeout(function() {
- $('#container').hide() setTimeout(function() {
- $('#preview').css({
- 'position': 'static',
- 'overflow-y': 'auto'
- }).removeClass('trans') $('#top').show()
- Diaspora.loaded()
- },
- 500)
- },
- 300)
- },
- 0)
- },
- player: function() {
- var p = $('#audio');
- if (!p.length) {
- $('.icon-play').css({
- 'color': '#dedede',
- 'cursor': 'not-allowed'
- }) return
- }
- if (p.eq(0).data("autoplay") == true) {
- p[0].play();
- }
- p.on({
- 'timeupdate': function() {
- var progress = p[0].currentTime / p[0].duration * 100;
- $('.bar').css('width', progress + '%');
- if (progress / 5 <= 1) {
- p[0].volume = progress / 5;
- } else {
- p[0].volume = 1;
- }
- },
- 'ended': function() {
- $('.icon-pause').removeClass('icon-pause').addClass('icon-play')
- },
- 'playing': function() {
- $('.icon-play').removeClass('icon-play').addClass('icon-pause')
- }
- })
- },
- loading: function() {
- var w = window.innerWidth;
- 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>';
- $('.loaderstyle').remove() $('head').append(css)
- $('#loader').removeClass().addClass('loader' + w).show()
- },
- loaded: function() {
- $('#loader').removeClass().hide()
- },
- F: function(id, w, h) {
- var _height = $(id).parent().height(),
- _width = $(id).parent().width(),
- ratio = h / w;
- if (_height / _width > ratio) {
- id.style.height = _height + 'px';
- id.style.width = _height / ratio + 'px';
- } else {
- id.style.width = _width + 'px';
- id.style.height = _width * ratio + 'px';
- }
- id.style.left = (_width - parseInt(id.style.width)) / 2 + 'px';
- id.style.top = (_height - parseInt(id.style.height)) / 2 + 'px';
- }
- }
- $(function() {
- if (Diaspora.P()) {
- $('body').addClass('touch')
- }
- if ($('#preview').length) {
- var cover = {};
- cover.t = $('#cover');
- cover.w = cover.t.attr('width');
- cover.h = cover.t.attr('height');
- ; (cover.o = function() {
- $('#mark').height(window.innerHeight)
- })();
- if (cover.t.prop('complete')) {
- // why setTimeout ?
- setTimeout(function() {
- cover.t.load()
- },
- 0)
- }
- cover.t.on('load',
- function() {
- ; (cover.f = function() {
- var _w = $('#mark').width(),
- _h = $('#mark').height(),
- x,
- y,
- i,
- e;
- e = (_w >= 1000 || _h >= 1000) ? 1000 : 500;
- if (_w >= _h) {
- i = _w / e * 50;
- y = i;
- x = i * _w / _h;
- } else {
- i = _h / e * 50;
- x = i;
- y = i * _h / _w;
- }
- $('.layer').css({
- 'width': _w + x,
- 'height': _h + y,
- 'marginLeft': -0.5 * x,
- 'marginTop': -0.5 * y
- })
- if (!cover.w) {
- cover.w = cover.t.width();
- cover.h = cover.t.height();
- }
- Diaspora.F($('#cover')[0], cover.w, cover.h)
- })();
- setTimeout(function() {
- $('html, body').removeClass('loading')
- },
- 1000)
- $('#mark').parallax()
- var vibrant = new Vibrant(cover.t[0]);
- var swatches = vibrant.swatches()
- if (swatches['DarkVibrant']) {
- $('#vibrant polygon').css('fill', swatches['DarkVibrant'].getHex()) $('#vibrant div').css('background-color', swatches['DarkVibrant'].getHex())
- }
- if (swatches['Vibrant']) {
- $('.icon-menu').css('color', swatches['Vibrant'].getHex())
- }
- })
- if (!cover.t.attr('src')) {
- alert('Please set the post thumbnail')
- }
- $('#preview').css('min-height', window.innerHeight)
- Diaspora.PS()
- $('.pview a').addClass('pviewa')
- var T;
- $(window).on('resize',
- function() {
- clearTimeout(T)
- T = setTimeout(function() {
- if (!Diaspora.P() && location.href == Home) {
- cover.o() cover.f()
- }
- if ($('#loader').attr('class')) {
- Diaspora.loading()
- }
- },
- 500)
- })
- } else {
- $('#single').css('min-height', window.innerHeight)
- setTimeout(function() {
- $('html, body').removeClass('loading')
- },
- 1000)
- window.addEventListener('popstate',
- function(e) {
- if (e.state) location.href = e.state.u;
- })
- Diaspora.player();
- $('.icon-icon, .image-icon').attr('href', '/')
- $('#top').show()
- }
- $(window).on('scroll',
- function() {
- if ($('.scrollbar').length && !Diaspora.P() && !$('.icon-images').hasClass('active')) {
- var st = $(window).scrollTop(),
- ct = $('.content').height();
- if (st > ct) {
- st = ct
- }
- $('.scrollbar').width((50 + st) / ct * 100 + '%')
- if (st > 80 && window.innerWidth > 800) {
- $('.subtitle').fadeIn()
- } else {
- $('.subtitle').fadeOut()
- }
- }
- })
- $(window).on('touchmove',
- function(e) {
- if ($('body').hasClass('mu')) {
- e.preventDefault()
- }
- })
- $('body').on('click',
- function(e) {
- var tag = $(e.target).attr('class') || '',
- rel = $(e.target).attr('rel') || '';
- if (!tag && !rel) return;
- switch (true) {
- // nav menu
- case(tag.indexOf('switchmenu') != -1) : window.scrollTo(0, 0) $('html, body').toggleClass('mu');
- break;
- // next page
- case (tag.indexOf('more') != -1) : tag = $('.more');
- if (tag.data('status') == 'loading') {
- return false
- }
- var num = parseInt(tag.data('page')) || 1;
- if (num == 1) {
- tag.data('page', 1)
- }
- if (num >= Pages) {
- return
- }
- tag.html('åŠ è½½ä¸..').data('status', 'loading') Diaspora.loading()
- Diaspora.L(tag.attr('href'),
- function(data) {
- var link = $(data).find('.more').attr('href');
- if (link != undefined) {
- tag.attr('href', link).html('åŠ è½½æ›´å¤š').data('status', 'loaded') tag.data('page', parseInt(tag.data('page')) + 1)
- } else {
- $('#pager').remove()
- }
- $('#primary').append($(data).find('.post'))
- Diaspora.loaded()
- },
- function() {
- tag.html('åŠ è½½æ›´å¤š').data('status', 'loaded')
- })
- return false;
- break;
- // home
- case (tag.indexOf('icon-home') != -1) : if ($('#preview').hasClass('show')) {
- history.back();
- } else {
- location.href = "/";
- }
- break;
- // qrcode
- case (tag.indexOf('icon-scan') != -1) : if ($('.icon-scan').hasClass('tg')) {
- $('#qr').toggle()
- } else {
- $('.icon-scan').addClass('tg') $('#qr').qrcode({
- width: 128,
- height: 128,
- text: location.href
- }).toggle()
- }
- break;
- // audio play
- case (tag.indexOf('icon-play') != -1) : $('#audio')[0].play() $('.icon-play').removeClass('icon-play').addClass('icon-pause') break;
- // audio pause
- case (tag.indexOf('icon-pause') != -1) : $('#audio')[0].pause() $('.icon-pause').removeClass('icon-pause').addClass('icon-play') break;
- // history state
- case (tag.indexOf('cover') != -1) : Diaspora.HS($(e.target).parent(), 'push') return false;
- break;
- // history state
- case (tag.indexOf('posttitle') != -1) : Diaspora.HS($(e.target), 'push') return false;
- break;
- // prev, next post
- case (rel == 'prev' || rel == 'next') : if (rel == 'prev') {
- var t = $('#prev_next a')[0].text
- } else {
- var t = $('#prev_next a')[1].text
- }
- $(e.target).attr('title', t)
- Diaspora.HS($(e.target), 'replace') return false;
- break;
- // quick view
- case (tag.indexOf('pviewa') != -1) : $('body').removeClass('mu')
- setTimeout(function() {
- Diaspora.HS($(e.target), 'push')
- },
- 300)
- return false;
- break;
- // comment
- case - 1 != tag.indexOf("comment") && -1 != tag.indexOf("link") : Diaspora.loading();
- $(".comment").removeClass("link").html("");
- var id = $(".comment").data("id"),
- title = $(".comment").data("title"),
- url = $(".comment").data("url"),
- shortname = $(".comment").data("shortname");
- window.duoshuoQuery = {
- short_name: shortname
- };
- var f = function() {
- var a = document.createElement("div");
- a.setAttribute("data-thread-key", id);
- a.setAttribute("data-title", title);
- a.setAttribute("data-url", url);
- DUOSHUO.EmbedThread(a);
- $(".comment").html(a);
- Diaspora.loaded();
- };
- window.DUOSHUO ? f() : $.getScript("http://static.duoshuo.com/embed.js",
- function() {
- f()
- });
- return false;
- break;
- default:
- return;
- break;
- }
- })
- console.log("%c Github %c", "background:#24272A; color:#ffffff", "", "https://github.com/Fechin/hexo-theme-diaspora")
- })
|