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

Improve the style of firebase analytics and post-meta #1056

Merged
merged 6 commits into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,6 @@ firestore:
collection: articles # Required, a string collection name to access firestore database
apiKey: # Required
projectId: # Required
bluebird: false # Enable this if you want to include bluebird 3.5.1 (core version) Promise polyfill

# Show Views / Visitors of the website / page with busuanzi.
# Get more information on http://ibruce.info/2015/04/04/busuanzi
Expand Down
75 changes: 34 additions & 41 deletions layout/_macro/post.swig
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,23 @@
#}{# Link posts #}{#
#}{%- if post.link %}
{%- if post.sticky > 0 %}
{{ post.sticky }}
<span class="post-sticky-flag" title="{{ __('post.sticky') }}">
<i class="fa fa-thumb-tack"></i>
</span>
{%- endif %}
{%- set postTitleIcon = '<i class="fa fa-external-link"></i>' %}
{%- set postText = post.title or post.link %}
{{ next_url(post.link, postText + postTitleIcon, {class: 'post-title-link post-title-link-external', itemprop: 'url' }) }}
{% else %}{#
#}{%- if is_index %}
{%- if post.sticky > 0 %}
<span class="post-sticky-flag" title="{{ __('post.sticky') }}">
<i class="fa fa-thumb-tack"></i>
</span>
{%- endif %}
{# Need to delete maybe? #}
{{ next_url(post.path, post.title | default(__('post.untitled')), {class: 'post-title-link', itemprop: 'url' }) }}
{% else -%}
{{- post.title -}}
{% include '../_partials/post-edit.swig' %}
{% elif is_index %}
{%- if post.sticky > 0 %}
<span class="post-sticky-flag" title="{{ __('post.sticky') }}">
<i class="fa fa-thumb-tack"></i>
</span>
{%- endif %}
{{ next_url(post.path, post.title | default(__('post.untitled')), {class: 'post-title-link', itemprop: 'url' }) }}
{% else -%}
{{- post.title -}}
{% include '../_partials/post-edit.swig' %}
{%- endif %}
</{%- if theme.seo %}h2{% else %}h1{%- endif %}>
{%- endif %}
Expand All @@ -74,9 +70,7 @@
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
{%- if theme.post_meta.item_text %}
<span class="post-meta-item-text">{{ __('post.posted') }}</span>
{%- endif %}
<span class="post-meta-item-text">{{ __('post.posted') }}</span>

{%- if !date_diff && time_diff && theme.post_meta.updated_at.enable && theme.post_meta.updated_at.another_day %}
{%- set create_title = __('post.created') + __('symbol.colon') + full_date(post.date) + ' / ' + __('post.modified') + __('symbol.colon') + time(post.updated) %}
Expand All @@ -98,9 +92,7 @@
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
{%- if theme.post_meta.item_text %}
<span class="post-meta-item-text">{{ __('post.edited') }}</span>
{%- endif %}
<span class="post-meta-item-text">{{ __('post.edited') }}</span>
<time title="{{ __('post.modified') + __('symbol.colon') + full_date(post.updated) }}" itemprop="dateModified" datetime="{{ moment(post.updated).format() }}">{#
#}{{ date(post.updated) -}}
</time>
Expand All @@ -113,9 +105,7 @@
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
{%- if theme.post_meta.item_text %}
<span class="post-meta-item-text">{{ __('post.in') }}</span>
{%- endif %}
<span class="post-meta-item-text">{{ __('post.in') }}</span>
{%- for cat in post.categories %}
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">{#
#}<a href="{{ url_for(cat.path) }}" itemprop="url" rel="index">{#
Expand All @@ -133,58 +123,61 @@

{# LeanCloud PageView #}
{%- if theme.leancloud_visitors.enable or (theme.valine.enable and theme.valine.appid and theme.valine.appkey and theme.valine.visitor) %}
<span id="{{ url_for(post.path) }}" class="post-meta-item leancloud_visitors" data-flag-title="{{ post.title }}">
<span id="{{ url_for(post.path) }}" class="post-meta-item leancloud_visitors" data-flag-title="{{ post.title }}" title="{{ __('post.views') }}">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
{%- if theme.post_meta.item_text %}
<span class="post-meta-item-text">{{ __('post.views') + __('symbol.colon') }}</span>
{%- endif %}
<span class="leancloud-visitors-count"></span>
<span class="post-meta-item-text">{{ __('post.views') + __('symbol.colon') }}</span>
<span class="leancloud-visitors-count"></span>
</span>
{%- endif %}

{%- if theme.firestore.enable %}
<span class="post-meta-item" title="{{ __('post.views') }}">
<span class="post-meta-item-icon">
<i class="fa fa-users"></i>
</span>
<span class="post-meta-item-text">{{ __('post.views') + __('symbol.colon') }}</span>
<span class="firestore-visitors-count"></span>
</span>
{%- endif %}

{%- if not is_index and theme.busuanzi_count.enable and theme.busuanzi_count.post_views %}
<span class="post-meta-item">
<span class="post-meta-item-icon"
{%- if not theme.post_meta.item_text %} title="{{ __('post.views') }}" {%- endif %}>
<span class="post-meta-item" title="{{ __('post.views') }}">
<span class="post-meta-item-icon">
<i class="fa fa-{{ theme.busuanzi_count.post_views_icon }}"></i>
{%- if theme.post_meta.item_text %} {{ __('post.views') + __('symbol.colon') }} {%- endif %}
<span class="busuanzi-value" id="busuanzi_value_page_pv"></span>
</span>
<span class="post-meta-item-text">{{ __('post.views') + __('symbol.colon') }}</span>
<span class="busuanzi-value" id="busuanzi_value_page_pv"></span>
</span>
{%- endif %}

{%- for inject_item in theme.injects.postMeta %}
{{ partial(inject_item.layout, inject_item.locals, inject_item.options) }}
{%- endfor %}

{%- if theme.symbols_count_time.separated_meta %}<br>{%- endif %}
{%- if config.symbols_count_time.symbols %}
<span class="post-meta-item">
{%- if theme.symbols_count_time.separated_meta %}<br>{%- endif %}
<span class="post-meta-item" title="{{ __('symbols_count_time.count') }}">
<span class="post-meta-item-icon">
<i class="fa fa-file-word-o"></i>
</span>
{%- if theme.symbols_count_time.item_text_post %}
<span class="post-meta-item-text">{{ __('symbols_count_time.count') + __('symbol.colon') }}</span>
{%- endif %}
<span title="{{ __('symbols_count_time.count') }}">{#
#}{{ symbolsCount(post) }}{#
#}</span>
<span>{{ symbolsCount(post) }}</span>
</span>
{%- endif %}

{%- if config.symbols_count_time.time %}
<span class="post-meta-item">
<span class="post-meta-item" title="{{ __('symbols_count_time.time') }}">
<span class="post-meta-item-icon">
<i class="fa fa-clock-o"></i>
</span>
{%- if theme.symbols_count_time.item_text_post %}
<span class="post-meta-item-text">{{ __('symbols_count_time.time') }} &asymp;</span>
{%- endif %}
<span title="{{ __('symbols_count_time.time') }}">{#
#}{{ symbolsTime(post, theme.symbols_count_time.awl, theme.symbols_count_time.wpm, __('symbols_count_time.time_minutes')) }}{#
#}</span>
<span>{{ symbolsTime(post, theme.symbols_count_time.awl, theme.symbols_count_time.wpm, __('symbols_count_time.time_minutes')) }}</span>
</span>
{%- endif %}

Expand Down
2 changes: 1 addition & 1 deletion layout/_partials/footer.swig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<i class="fa fa-coffee"></i>
</span>
{%- if theme.symbols_count_time.item_text_total %}
<span class="post-meta-item-text">{{ __('symbols_count_time.time_total')}} &asymp;</span>
<span class="post-meta-item-text">{{ __('symbols_count_time.time_total') }} &asymp;</span>
{%- endif %}
<span title="{{ __('symbols_count_time.time_total') }}">{#
#}{{ symbolsTimeTotal(site, theme.symbols_count_time.awl, theme.symbols_count_time.wpm, __('symbols_count_time.time_minutes')) }}{#
Expand Down
94 changes: 40 additions & 54 deletions layout/_third-party/analytics/firestore.swig
Original file line number Diff line number Diff line change
@@ -1,88 +1,74 @@
{%- if theme.firestore.enable %}
<script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-firestore.js"></script>
{%- if theme.firestore.bluebird %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.5.1/bluebird.core.min.js"></script>
{%- endif %}
<script{{ pjax }}>
(function() {

<script>
firebase.initializeApp({
apiKey: '{{ theme.firestore.apiKey }}',
projectId: '{{ theme.firestore.projectId }}'
});

function getCount(doc, increaseCount) {
//increaseCount will be false when not in article page
// IncreaseCount will be false when not in article page

return doc.get().then(d => {
var count;
if (!d.exists) { //has no data, initialize count
var count = 0;
if (!d.exists) { // Has no data, initialize count
if (increaseCount) {
doc.set({
count: 1
});
count = 1;
} else {
count = 0;
}
} else { //has data
} else { // Has data
count = d.data().count;
if (increaseCount) {
if (!(window.localStorage && window.localStorage.getItem(title))) { //if first view this article
doc.set({ //increase count
count: count + 1
});
count++;
}
// If first view this article
doc.set({ // Increase count
count: count + 1
});
count++;
}
}
if (window.localStorage && increaseCount) { //mark as visited
localStorage.setItem(title, true);
}

return count;
});
}

function appendCountTo(el) {
return function(count) {
$(el).append(
$('<span>').addClass('post-meta-item')
.append(
$('<span>').addClass('post-meta-item-icon').append(
$('<i>').addClass('fa fa-users')
)
)
.append(
$('<span>').text('{{ __("post.views")}} ' + count)
)
);
$(el).text(count);
}
}
</script>
<script{{ pjax }}>
(function() {
var db = firebase.firestore();
var articles = db.collection('{{ theme.firestore.collection }}');

var db = firebase.firestore();
var articles = db.collection('{{ theme.firestore.collection }}');

if (CONFIG.page.isPost) { //is article page
var title = $('.post-title').text().trim();
var doc = articles.doc(title);

getCount(doc, true).then(appendCountTo($('.post-meta')));
} else if (CONFIG.page.isHome) { //is index page
var promises = $('.post-title').map((i, o) => {
var title = $(o).text().trim();
return articles.doc(title);
}).map(doc => {
return getCount(doc);
});
Promise.all(promises).then(counts => {
var metas = $('.post-meta')
counts.forEach((val, idx) => {
appendCountTo(metas[idx])(val)
if (CONFIG.page.isPost) { // Is article page
var title = $('.post-title').text().trim();
var doc = articles.doc(title);
var increaseCount = true;
if (localStorage.getItem(title)) {
increaseCount = false;
} else {
// Mark as visited
localStorage.setItem(title, true);
}
getCount(doc, increaseCount).then(appendCountTo($('.firestore-visitors-count')));
} else if (CONFIG.page.isHome) { // Is index page
var promises = $('.post-title').map((i, o) => {
var title = $(o).text().trim();
var doc = articles.doc(title);
return getCount(doc);
});
});
}
})();
Promise.all(promises).then(counts => {
var metas = $('.firestore-visitors-count');
counts.forEach((val, idx) => {
appendCountTo(metas[idx])(val);
});
});
}
})();
</script>
{%- endif %}
50 changes: 25 additions & 25 deletions layout/_third-party/analytics/lean-analytics.swig
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,32 @@
}
}

$(function() {
$.get('https://app-router.leancloud.cn/2/route?appId=' + '{{ theme.leancloud_visitors.app_id }}')
.done(function({ api_server }) {
var Counter = function(method, url, data) {
return $.ajax({
method: method,
url: `https://${api_server}/1.1${url}`,
headers: {
'X-LC-Id': '{{ theme.leancloud_visitors.app_id }}',
'X-LC-Key': '{{ theme.leancloud_visitors.app_key }}',
'Content-Type': 'application/json',
},
data: data
});
};
if (CONFIG.page.isPost) {
const localhost = /http:\/\/(localhost|127.0.0.1|0.0.0.0)/;
if (localhost.test(document.URL)) return;
addCount(Counter);
} else {
if ($('.post-title-link').length >= 1) {
showTime(Counter);
}
$(function() {
$.get('https://app-router.leancloud.cn/2/route?appId=' + '{{ theme.leancloud_visitors.app_id }}')
.done(function({ api_server }) {
var Counter = function(method, url, data) {
return $.ajax({
method: method,
url: `https://${api_server}/1.1${url}`,
headers: {
'X-LC-Id': '{{ theme.leancloud_visitors.app_id }}',
'X-LC-Key': '{{ theme.leancloud_visitors.app_key }}',
'Content-Type': 'application/json',
},
data: data
});
};
if (CONFIG.page.isPost) {
const localhost = /http:\/\/(localhost|127.0.0.1|0.0.0.0)/;
if (localhost.test(document.URL)) return;
addCount(Counter);
} else {
if ($('.post-title-link').length >= 1) {
showTime(Counter);
}
});
});
}
});
});
</script>

{%- endif %}
4 changes: 1 addition & 3 deletions scripts/filters/comment/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ module.exports = {
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
{% if theme.post_meta.item_text %}
<span class="post-meta-item-text">{{ __('post.comments_count') + __('symbol.colon') }}</span>
{% endif %}
<span class="post-meta-item-text">{{ __('post.comments_count') + __('symbol.colon') }}</span>
`
};
4 changes: 4 additions & 0 deletions source/css/_common/components/post/post-meta.styl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
}

.post-meta-item-text {
if (!hexo-config('post_meta.item_text')) {
display: none;
}

+tablet-mobile() {
display: none;
}
Expand Down
2 changes: 1 addition & 1 deletion source/css/_common/components/post/post-title.styl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
}

.fa {
font-size: $font-size-larger;
font-size: $font-size-small;
margin-left: 5px;
}
}