Skip to content

Commit

Permalink
Merge pull request #439 from Doublemine/pisces
Browse files Browse the repository at this point in the history
Feature:notes and show number of visitors to each article
  • Loading branch information
iissnan committed Oct 22, 2015
2 parents 24bdd17 + 28aa90b commit 9d01d7b
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 1 deletion.
7 changes: 7 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ facebook_sdk:
like_button: #true
webmaster: #true

# Show number of visitors to each article.
# You can visit https://leancloud.cn get AppID and AppKey.
leancloud_visitors:
enable: false
app_id: #<app_id>
app_key: #<app_key>


## DO NOT EDIT THE FOLLOWING SETTINGS
## UNLESS YOU KNOW WHAT YOU ARE DOING
Expand Down
1 change: 1 addition & 0 deletions languages/zh-Hans.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sidebar:
post:
posted: 发表于
in: 分类于
visitors: 阅读次数
read_more: 阅读全文
untitled: 未命名
toc_empty: 此文章未包含目录
Expand Down
1 change: 1 addition & 0 deletions languages/zh-hk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sidebar:
post:
posted: 發表於
in: 分類於
visitors: 閱讀次數
read_more: 閱讀全文
untitled: 未命名
toc_empty: 此文章未包含目錄
Expand Down
1 change: 1 addition & 0 deletions languages/zh-tw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sidebar:
post:
posted: 發表於
in: 分類於
visitors: 閱讀次數
read_more: 閱讀全文
untitled: 未命名
toc_empty: 此文章未包含目錄
Expand Down
3 changes: 3 additions & 0 deletions layout/_layout.swig
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,8 @@
});
});
</script>
{% if theme.leancloud_visitors.enable %}
{% include '_scripts/lean-analytics.swig' %}
{% endif %}
</body>
</html>
6 changes: 5 additions & 1 deletion layout/_macro/post.swig
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@
&nbsp; | &nbsp;
<div class="fb-like" data-layout="button_count" data-share="true"></div>
{% endif %}

{% if theme.leancloud_visitors.enable %}
<span id="{{ url_for(post.path) }}"class="leancloud_visitors" data-flag-title="{{ post.title }}">
&nbsp; | &nbsp; {{__('post.visitors')}}
</span>
{% endif %}
</div>
</header>
{% endif %}
Expand Down
81 changes: 81 additions & 0 deletions layout/_scripts/lean-analytics.swig
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("{{theme.leancloud_visitors.app_id}}", "{{theme.leancloud_visitors.app_key}}");</script>
<script>
function showTime(Counter) {
var query = new AV.Query(Counter);
$(".leancloud_visitors").each(function() {
var url = $(this).attr("id").trim();
query.equalTo("url", url);
query.find({
success: function(results) {
if (results.length == 0) {
var content = $(document.getElementById(url)).text() + ': 0';
$(document.getElementById(url)).text(content);
return;
}
for (var i = 0; i < results.length; i++) {
var object = results[i];
var content = $(document.getElementById(url)).text() + ': ' + object.get('time');
$(document.getElementById(url)).text(content);
}
},
error: function(object, error) {
console.log("Error: " + error.code + " " + error.message);
}
});

});
}

function addCount(Counter) {
var Counter = AV.Object.extend("Counter");
url = $(".leancloud_visitors").attr('id').trim();
title = $(".leancloud_visitors").attr('data-flag-title').trim();
var query = new AV.Query(Counter);
query.equalTo("url", url);
query.find({
success: function(results) {
if (results.length > 0) {
var counter = results[0];
counter.fetchWhenSave(true);
counter.increment("time");
counter.save(null, {
success: function(counter) {
var content = $(document.getElementById(url)).text() + ': ' + counter.get('time');
$(document.getElementById(url)).text(content);
},
error: function(counter, error) {
console.log('Failed to save Visitor num, with error message: ' + error.message);
}
});
} else {
var newcounter = new Counter();
newcounter.set("title", title);
newcounter.set("url", url);
newcounter.set("time", 1);
newcounter.save(null, {
success: function(newcounter) {
var content = $(document.getElementById(url)).text() + ': ' + newcounter.get('time');
$(document.getElementById(url)).text(content);
},
error: function(newcounter, error) {
console.log('Failed to create');
}
});
}
},
error: function(error) {
console.log('Error:' + error.code + " " + error.message);
}
});
}
$(function() {
var Counter = AV.Object.extend("Counter");
if ($('.leancloud_visitors').length == 1) {
addCount(Counter);
} else if ($('.post-title-link').length > 1) {
showTime(Counter);
}
});
</script>

0 comments on commit 9d01d7b

Please sign in to comment.