Commit 6c8bfeb2 authored by Vadim's avatar Vadim

Version 2.6.3

Первичная загрузка счетчиков
parent d0f71b3a
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
Plugin Name: Bg Az-Counter Plugin Name: Bg Az-Counter
Plugin URI: https://bogaiskov.ru Plugin URI: https://bogaiskov.ru
Description: Подсчет количества посещений страниц на базе stat.azbyka.ru Description: Подсчет количества посещений страниц на базе stat.azbyka.ru
Version: 2.6.2 Version: 2.6.3
Author: VBog Author: VBog
Author URI: https://bogaiskov.ru Author URI: https://bogaiskov.ru
License: GPL2 License: GPL2
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
if ( !defined('ABSPATH') ) { if ( !defined('ABSPATH') ) {
die( 'Sorry, you are not allowed to access this page directly.' ); die( 'Sorry, you are not allowed to access this page directly.' );
} }
define('BG_COUNTER_VERSION', '2.6.2'); define('BG_COUNTER_VERSION', '2.6.3');
define('BG_COUNTER_LOG', dirname(__FILE__ ).'/bg_counter.log'); define('BG_COUNTER_LOG', dirname(__FILE__ ).'/bg_counter.log');
define('BG_COUNTER_STAT_COUNTERS','https://stat.azbyka.ru/counters'); define('BG_COUNTER_STAT_COUNTERS','https://stat.azbyka.ru/counters');
......
...@@ -31,7 +31,10 @@ jQuery( document ).ready(function() { ...@@ -31,7 +31,10 @@ jQuery( document ).ready(function() {
} }
} else GetAllCounters(); } else GetAllCounters();
bg_counter_elements_reloaded_on_scroll(); // Обновляет счетчики после прокрутки страницы, если добавлены элементы.
jQuery(window).on('scroll', function() {
GetAllCounters();
});
/********************************************************************************* /*********************************************************************************
Просомтр счетчиков читающих в реальном времени. Просомтр счетчиков читающих в реальном времени.
...@@ -190,51 +193,59 @@ GET /counters/project/test/author/1/book/3 ...@@ -190,51 +193,59 @@ GET /counters/project/test/author/1/book/3
**********************************************************************************/ **********************************************************************************/
function GetAllCounters() { function GetAllCounters() {
jQuery('span.bg-az-counter').each (function () { var elem = jQuery('span.bg-az-counter');
var el = jQuery(this);
// bg_counter_elements = el.length; if( typeof elem == 'undefined' ) {
var type = el.attr('data-type'); return;
var id = el.attr('data-ID'); }
var project = el.attr('data-project'); if (elem.length > bg_counter_elements) {
if (project == "") request = bg_counter.counterurl; bg_counter_elements = elem.length;
else { jQuery('span.bg-az-counter').each (function () {
if (project) project = '/project/'+project; var el = jQuery(this);
else project = bg_counter.project; // bg_counter_elements = el.length;
var type = el.attr('data-type');
if (!type || !id) var request = bg_counter.counterurl+project; var id = el.attr('data-ID');
else var request = bg_counter.counterurl+project+"/"+type+"/"+id; var project = el.attr('data-project');
if (project == "") request = bg_counter.counterurl;
} else {
var xhr = new XMLHttpRequest(); if (project) project = '/project/'+project;
xhr.open("GET", request, true); else project = bg_counter.project;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) { if (!type || !id) var request = bg_counter.counterurl+project;
if (xhr.responseText) { else var request = bg_counter.counterurl+project+"/"+type+"/"+id;
var response = JSON.parse(xhr.responseText);
if (response.success) { }
if (bg_counter.debug) console.log('GET REQUEST: '+request); var xhr = new XMLHttpRequest();
if (bg_counter.debug) console.log(JSON.stringify(response.data)); xhr.open("GET", request, true);
el.find('span.bg-az-counter-views').text(bg_counter_number_format(response.data.total)); xhr.onreadystatechange = function() {
el.find('span.bg-az-counter-now').text(addDelimiter(response.data.now)); if (xhr.readyState == 4 && xhr.status == 200) {
if (xhr.responseText) {
var response = JSON.parse(xhr.responseText);
if (response.success) {
if (bg_counter.debug) console.log('GET REQUEST: '+request);
if (bg_counter.debug) console.log(JSON.stringify(response.data));
el.find('span.bg-az-counter-views').text(bg_counter_number_format(response.data.total));
el.find('span.bg-az-counter-now').text(addDelimiter(response.data.now));
} else {
if (bg_counter.debug) console.log('GET REQUEST: '+request+' ERROR '+xhr.status+': '+response.error);
el.find('span.bg-az-counter-views').text('0');
el.find('span.bg-az-counter-now').text('0');
}
} else { } else {
if (bg_counter.debug) console.log('GET REQUEST: '+request+' ERROR '+xhr.status+': '+response.error); if (bg_counter.debug) console.warn('GET REQUEST: '+request+' Warning: responseText is empty!');
el.find('span.bg-az-counter-views').text('0'); el.find('span.bg-az-counter-views').text(' - ');
el.find('span.bg-az-counter-now').text('0'); el.find('span.bg-az-counter-now').text(' - ');
} }
} else {
if (bg_counter.debug) console.warn('GET REQUEST: '+request+' Warning: responseText is empty!');
el.find('span.bg-az-counter-views').text(' - ');
el.find('span.bg-az-counter-now').text(' - ');
} }
} }
} xhr.onerror = function(err) {
xhr.onerror = function(err) { console.warn(err.type +" "+ err.target.status + ". Check if the server is running!");
console.warn(err.type +" "+ err.target.status + ". Check if the server is running!"); el.find('span.bg-az-counter-views').text(' - ');
el.find('span.bg-az-counter-views').text(' - '); el.find('span.bg-az-counter-now').text(' - ');
el.find('span.bg-az-counter-now').text(' - '); }
} xhr.send();
xhr.send(); });
}); }
} }
/********************************************************************************* /*********************************************************************************
Отображает значения счетчика на странице Отображает значения счетчика на странице
...@@ -280,22 +291,3 @@ function bg_counter_number_format (num) { ...@@ -280,22 +291,3 @@ function bg_counter_number_format (num) {
return num; return num;
} }
/*********************************************************************************
Обновляет счетчики после прокрутки страницы,
если добавлены элементы.
**********************************************************************************/
function bg_counter_elements_reloaded_on_scroll() {
jQuery(window).on('scroll', function() {
var elem = jQuery('span.bg-az-counter');
if( typeof elem == 'undefined' ) {
return;
}
if (elem.length > bg_counter_elements) {
bg_counter_elements = elem.length;
GetAllCounters();
// getAllRates();
}
});
}
...@@ -6,43 +6,46 @@ var bg_counter_ratings = 0; ...@@ -6,43 +6,46 @@ var bg_counter_ratings = 0;
jQuery( document ).ready(function() { jQuery( document ).ready(function() {
getAllRates();
// Обновлять рейтинги после прокрутки страницы, если добавлены элементы.
jQuery(window).on('scroll', function() {
getAllRates();
});
// getAllRates(); if (!bg_counter.ID) return; // У объекта нет ID
bg_counter_ratings_reloaded_on_scroll(); if (jQuery("div").is(".bg_counter_rating") == false) return; // На странице нет счетчика
if (!bg_counter.ID) return; // У объекта нет ID
if (jQuery("div").is(".bg_counter_rating") == false) return; // На странице нет счетчика start_rate_index = parseFloat(jQuery( "#bg_counter_score" ).html());
rating_voted = (jQuery( "#bg_counter_score" ).attr("data-voted")=='true')?true:false;
start_rate_index = parseFloat(jQuery( "#bg_counter_score" ).html()); iniRatingState(start_rate_index, rating_voted);
rating_voted = (jQuery( "#bg_counter_score" ).attr("data-voted")=='true')?true:false;
iniRatingState(start_rate_index, rating_voted); getRate(bg_counter.type, bg_counter.ID);
getRate(bg_counter.type, bg_counter.ID); jQuery( "#bg_counter_rate_box li" ).mouseover(function() {
if(!rating_voted){
jQuery( "#bg_counter_rate_box li" ).mouseover(function() { var index = jQuery( this ).index();
if(!rating_voted){ iniRatingState(index+1, rating_voted);
var index = jQuery( this ).index(); jQuery('#bg_counter_popup_help').text(bg_counter.price[index]);
iniRatingState(index+1, rating_voted); } else {
jQuery('#bg_counter_popup_help').text(bg_counter.price[index]); jQuery('#bg_counter_popup_help').text(bg_counter.voted);
} else { }
jQuery('#bg_counter_popup_help').text(bg_counter.voted); jQuery('#bg_counter_popup_help').show();
} });
jQuery('#bg_counter_popup_help').show();
});
jQuery( "#bg_counter_rate_box" ).mouseout(function() { jQuery( "#bg_counter_rate_box" ).mouseout(function() {
if(!rating_voted){ if(!rating_voted){
iniRatingState(start_rate_index, rating_voted); iniRatingState(start_rate_index, rating_voted);
} }
jQuery('#bg_counter_popup_help').hide(); jQuery('#bg_counter_popup_help').hide();
}); });
jQuery( "#bg_counter_rate_box li" ).click(function() { jQuery( "#bg_counter_rate_box li" ).click(function() {
if(!rating_voted){ if(!rating_voted){
rating_voted = true; rating_voted = true;
jQuery( "#bg_counter_rate_box li" ).css('cursor', 'default'); jQuery( "#bg_counter_rate_box li" ).css('cursor', 'default');
var sindex = jQuery( this ).index()+1; var sindex = jQuery( this ).index()+1;
sendRate(bg_counter.type, bg_counter.ID, sindex); sendRate(bg_counter.type, bg_counter.ID, sindex);
} }
}); });
}); });
function iniRatingState(sindex, voted){ function iniRatingState(sindex, voted){
if(!voted) jQuery( "#bg_counter_rate_box li" ).css('cursor', 'pointer'); if(!voted) jQuery( "#bg_counter_rate_box li" ).css('cursor', 'pointer');
...@@ -130,41 +133,48 @@ function getRate(type, id) { ...@@ -130,41 +133,48 @@ function getRate(type, id) {
function getAllRates() { function getAllRates() {
jQuery('span.bg-az-counter').each (function () { var elem = jQuery('span.bg-az-counter');
var el = jQuery(this);
// bg_counter_ratings = el.length; if( typeof elem == 'undefined' ) {
var type = el.attr('data-type'); return;
var id = el.attr('data-ID'); }
var project = el.attr('data-project'); if (elem.length > bg_counter_ratings) {
if (project) project = '/project/'+project; bg_counter_ratings = elem.length;
else project = bg_counter.project; jQuery('span.bg-az-counter').each (function () {
var el = jQuery(this);
if (!type || !id) return; // bg_counter_ratings = el.length;
var request = bg_counter.scoreurl+bg_counter.project+"/"+type+"/"+id; var type = el.attr('data-type');
var xhr = new XMLHttpRequest(); var id = el.attr('data-ID');
xhr.open("GET", request, true); var project = el.attr('data-project');
if (bg_counter.debug) console.log('GET REQUEST: '+request); if (project) project = '/project/'+project;
xhr.onreadystatechange = function() { else project = bg_counter.project;
if (xhr.readyState == 4 && xhr.status == 200) {
if (xhr.responseText) { if (!type || !id) return;
var response = JSON.parse(xhr.responseText); var request = bg_counter.scoreurl+bg_counter.project+"/"+type+"/"+id;
if (response.success) { var xhr = new XMLHttpRequest();
// Вывод данных на экран xhr.open("GET", request, true);
if (bg_counter.debug) console.log(JSON.stringify(response.data)); if (bg_counter.debug) console.log('GET REQUEST: '+request);
el.find('span.bg-az-counter-score').text(parseFloat(response.data.score).toFixed(1)); xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
if (xhr.responseText) {
var response = JSON.parse(xhr.responseText);
if (response.success) {
// Вывод данных на экран
if (bg_counter.debug) console.log(JSON.stringify(response.data));
el.find('span.bg-az-counter-score').text(parseFloat(response.data.score).toFixed(1));
} else {
if (bg_counter.debug) console.log('GET REQUEST: '+request+' ERROR: '+response.error);
el.find('span.bg-az-counter-score').text('0');
}
} else { } else {
if (bg_counter.debug) console.log('GET REQUEST: '+request+' ERROR: '+response.error); if (bg_counter.debug) console.warn('GET REQUEST: '+request+' Warning: responseText is empty!');
el.find('span.bg-az-counter-score').text('0'); el.find('span.bg-az-counter-score').text(' - ');
} }
} else {
if (bg_counter.debug) console.warn('GET REQUEST: '+request+' Warning: responseText is empty!');
el.find('span.bg-az-counter-score').text(' - ');
} }
} }
} xhr.send();
xhr.send(); });
}); }
} }
...@@ -226,21 +236,3 @@ function sendRate(type, id, number) { ...@@ -226,21 +236,3 @@ function sendRate(type, id, number) {
xhr.send('{"rating": '+number+'}'); xhr.send('{"rating": '+number+'}');
} }
/*********************************************************************************
Обновляет рейтинги после прокрутки страницы,
если добавлены элементы.
**********************************************************************************/
function bg_counter_ratings_reloaded_on_scroll() {
jQuery(window).on('scroll', function() {
var elem = jQuery('span.bg-az-counter');
if( typeof elem == 'undefined' ) {
return;
}
if (elem.length > bg_counter_ratings) {
bg_counter_ratings = elem.length;
getAllRates();
}
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment