Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(load): add a javascript load queue #159

Merged
merged 3 commits into from
Jan 21, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions layout/_partial/config_css.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@
background-color: #fff;
}
</style>
<script type="text/javascript">
window.onload = function () {
setTimeout(function () {
document.body.background="https://api.i-meto.com/bing?<%= theme.background.bing.parameter %>";
}, 2000);
}
</script>
<% } else if(!theme.background.bgimg) { %>
<style>
body{
Expand Down
26 changes: 14 additions & 12 deletions layout/_partial/footer-option.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
<% if(theme.comment.use == "duoshuo") { %>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please use strict equality
  • Please use single quotes in JavaScript code

<!-- 多说公共 js 代码 start -->
<script type="text/javascript">
var duoshuoQuery = {
short_name: '<%= theme.comment.shortname %>'
};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';
ds.async = true;
ds.src = '<%= theme.comment.duoshuo_embed_js_url %>';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
queue.offer(function(){
var duoshuoQuery = {
short_name: '<%= theme.comment.shortname %>'
};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';
ds.async = true;
ds.src = '<%= theme.comment.duoshuo_embed_js_url %>';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
});
</script>
<!-- 多说公共 js 代码 end -->
<% } %>
Expand Down
12 changes: 12 additions & 0 deletions layout/_partial/head.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
<%- css("css/style.min") %>
<%- partial("_partial/config_css") %>
<%- js("js/jquery.min") %>
<%- js("js/queue") %>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please use single quotes inside JavaScript code (change it for the entire stack of import)

<%- js("js/lazyload.min") %>

<%- css("css/highlight/" + theme.reading.code_highlight ) %>

Expand Down Expand Up @@ -119,6 +121,16 @@
</script>
<% } %>

<!-- Bing Background -->
<% if(theme.background.bing.enable) { %>
<script type="text/javascript">
queue.offer(function(){
$('body').attr("data-original","https://api.i-meto.com/bing?<%= theme.background.bing.parameter %>");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please use single quotes inside JavaScript code
  • Please put a space after comma

$('body.lazy').lazyload();
});
</script>
<% } %>

<!-- Custom Head -->
<% if (site.data.head) { %>
<% for (var i in site.data.head) { %>
Expand Down
6 changes: 2 additions & 4 deletions layout/_widget/disqus.ejs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<div id="disqus_thread"></div>
<script>
$('html').ready(function() {
setTimeout(function() {
queue.offer(function() {
var disqus_config = function () {
this.page.url = '<%- config.url + url_for(path) %>'; // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
Expand All @@ -14,6 +13,5 @@
s.setAttribute('data-timestamp', + new Date());
(d.head || d.body).appendChild(s);
})();
},1500);
});
});
</script>
2 changes: 1 addition & 1 deletion layout/layout.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!-- Single Gallery Page -->
<%- partial('_widget/page-gallery') %>
<% } else { %>
<body id="scheme-<%= theme.scheme %>">
<body id="scheme-<%= theme.scheme %>" class="lazy">
<div class="material-layout mdl-js-layout has-drawer is-upgraded">
<% if(theme.scheme === 'Isolation') { %>
<!-- Isolation Header -->
Expand Down
8 changes: 8 additions & 0 deletions source/js/lazyload.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions source/js/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function Queue() {
this.dataStore = [];
this.offer = offer;
this.poll = poll;
this.execNext = execNext;
this.debug = false;
this.startDebug = startDebug;


function offer(element) {
if(this.debug) console.log("Offered a Queued Function.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please user single quote inside JavaScript code (change it for entire file)
  • Please surround if statement with brackets even for one line, and display it in several lines (change it for entire file)

if(typeof element === "function") {
this.dataStore.push(element);
} else {
console.log("You must offer a function.");
}
}

function poll() {
if(this.debug) console.log("Polled a Queued Function.");
return this.dataStore.shift();
}

function execNext() {
var nextfunc=this.poll();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please surround = by space

if(nextfunc != undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please use strict equality

if(this.debug) console.log("Run a Queued Function.");
nextfunc();
}
}

function startDebug(){
this.debug = true;
}

}

var queue = new Queue();
$(document).ready(function(){
setTimeout(function(){
setInterval(function(){
queue.execNext();
},500);
},3000);
});