function trim(str){
return str.replace(/^[\s]+|[\s]+$/g, '');
}
function isMobail(){
var res = false
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent)) {
res = true
}
if ($(window).width() < 800){
res = true
}
return res
}
function isObject(value){
return Object.prototype.toString.call(value) === '[object Object]';
}
function isArray(value){
return Object.prototype.toString.call(value) === '[object Array]';
}
function empty(v){
return (v===undefined||v===false||v===null||v==='');
}
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * 62));
}
return result;
}
function toArrayKeys(obj){
return Array.from(Object.keys(obj));
}
function toArrayValues(obj){
try{
var a = [];
for (var k in obj){
a.push(obj[k]);
}
return a;
}
catch (e) {
console.error(e, JSON.stringify(obj));
return [];
}
}
function sendDebug(str){
var data = str;
data += "\n\n"+navigator.userAgent;
// data += "\n\n".JSON.stringify(localStorage);
data += "\n"+'Window: '+window.screen.width+'x'+window.screen.height;
if (document.querySelector('meta[name="node"]')){
data += "\n"+document.querySelector('meta[name="node"]').content;
}
data = data.replace(/\n/g, '<br/>');
var request = new XMLHttpRequest();
request.open('POST', '/api/front_debug', true);
request.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded; charset=utf-8'
);
request.send('str='+encodeURIComponent(data));
}
function getCookie(name) {
if (!name) return undefined;
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([.$?*|{}()\[\]\\\/+^])/g, '\\$1') + "=([^;]*)"
));
if (matches) {
return decodeURIComponent(matches[1]);
}
return undefined;
}
function setCookie(name, value, options) {
if (!name) return;
options = options || {};
if (options.path === undefined){
options.path = '/';
}
if (options.expires === undefined) {
var exp = new Date();
exp.setTime(exp.getTime()+(365*24*60*60*1000));
options.expires = exp.toUTCString();
}
if (value instanceof Object) {
value = JSON.stringify(value);
}
var updatedCookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);
for (var optionKey in options) {
updatedCookie += "; " + optionKey;
var optionValue = options[optionKey];
if (optionValue !== true) {
updatedCookie += "=" + optionValue;
}
}
document.cookie = updatedCookie;
}
function deleteCookie(name) {
setCookie(name, null, {expires: new Date(), path: '/'});
}
function time(){
return Math.floor(Date.now() / 1000);
}
function serialize(o, _name, q){
q = (q===undefined)? []: q;
if (typeof o === 'object'){
for (var name in o){
if (_name !== undefined){
_name=_name.replace(new RegExp('%5b%5d$', 'gi'), '');
}
if (typeof o[name] === 'object'){
var n = (_name !== undefined)? _name+'['+name+']': name;
serialize(o[name], n, q);
}
else{
var v = encodeURIComponent(o[name]);
name = (_name===undefined)? name: _name+'['+name+']';
q.push(name+'='+v);
}
}
if (_name !== undefined){
return q;
}
o = q.join("&");
}
return o;
}
function json(url, data, callback){
data = data === undefined ? '': data;
if (typeof data === 'object'){
data=serialize(data);
}
$.ajax({
url: url,
type: 'post',
data: '_front_=1&ww='+$(document).width()+'&'+data,
success: function(r){
try{
if (empty(r)) return callback({});
if (typeof r === 'string'){
r=JSON.parse(r);
}
if (r.reload){
return window.location.reload();
}
if (typeof callback !== 'undefined'){
return callback(r);
}
} catch (e) {
if (typeof callback !== 'undefined'){
console.log('ERROR PARSE JSON TRY: '+url+' => ' + e);
return callback({});
}
}
},
error: function(e,e1,e2){
if (typeof callback !== 'undefined'){
return callback({
code: 500,
error: e2,
result: ''
});
}
console.log(e,e1,e2);
}
});
}
function post(url, data, callback){
if (data === undefined) data='';
__http(url, 'post', data, callback);
}
function get(url, data, callback){
if (data === undefined) data='';
__http(url, 'get', data, callback);
}
function http(url, type, data, callback){
if (empty(type)){
type = 'get';
}
if (empty(data)){
type = '';
}
__http(url, type, data, callback);
}
function __http(url, type, data, callback){
if (typeof data === 'object'){
data=serialize(data);
}
$.ajax({
url: url,
type: type,
data: '_front_=1&ww='+$(document).width()+'&'+data,
success: function(r){
if (typeof callback === 'undefined' || typeof callback === 'boolean'){
return true;
}
try{
if (typeof r !== 'object'){
r=JSON.parse(r);
}
} catch (e) {
// console.error('response: '+url+': '+r);
// console.log(' ');
// console.log('__http error: '+url);
// console.log('__http error: ', e);
// console.log(' ');
}
if (typeof callback !== 'undefined' && typeof callback !== 'boolean'){
// console.log(r,url);
return callback(r);
}
},
error: function(e,e1,e2){
if (typeof callback !== 'undefined' && typeof callback !== 'boolean'){
return callback({
code: 500,
error: e2,
result: ''
});
}
console.error(e,e1,e2);
}
});
}
var BeginLoad = {
bind: function(selector, fn){
if ($(selector).length === 0){
return false;
}
var res;
$(window).resize(function (){
res = BeginLoad.inWindow(selector);
if (res.length){
return fn(res);
}
});
$(window).scroll(function (){
res = BeginLoad.inWindow(selector);
if (res.length){
return fn(res);
}
});
res = BeginLoad.inWindow(selector);
if (res.length){
return fn(res);
}
},
inWindow: function(s){
var scrollTop = $(window).scrollTop();
var windowheight = $(window).height() + 250;
var result = [], offset, el;
$(s).each(function(){
el = $(this);
offset = el.offset().top;
if(scrollTop <= offset && (el.height() + offset) < (scrollTop + windowheight))
result.push(this);
});
return $(result);
}
};
var Visible = {
task: [],
set: function(o, isVisible, isHidden){
if (typeof o === 'string') o = $(o);
var task;
for (var k=0; k < o.length; k++){
task = {
o: o.eq(k),
isVisible: isVisible,
isHidden: isHidden
};
if (Visible.run(task) === false){
Visible.task.push(task);
}
}
},
is : function(o){
if (typeof o === 'string') o = $(o);
var pageYOffset = window.pageYOffset;
var pageXOffset = window.pageXOffset;
var top = o[0].getBoundingClientRect().top;
var left = o[0].getBoundingClientRect().left;
var right = o[0].getBoundingClientRect().right;
var bottom = o[0].getBoundingClientRect().bottom;
var tpos = {
top: pageYOffset + top,
left: pageXOffset + left,
right: pageXOffset + right,
bottom: pageYOffset + bottom
};
var wpos = {
top: pageYOffset,
left: pageXOffset,
right: pageXOffset + document.documentElement.clientWidth,
bottom: pageYOffset + document.documentElement.clientHeight
};
return tpos.bottom > wpos.top && tpos.top < wpos.bottom && tpos.right > wpos.left && tpos.left < wpos.right;
},
run: function(obj){
if (Visible.is(obj.o) === true){
if (obj.isVisible !== undefined){
obj.isVisible(obj.o);
if (obj.isHidden === undefined) {
return true;
}
}
}
else{
if (obj.isHidden !== undefined) {
obj.isHidden(obj.o);
if (obj.isVisible === undefined) {
return true;
}
}
}
return false;
},
worker: function(){
var t = null;
var handler = function(){
if (t !== null) return null;
if (Visible.task.length === 0) return null;
t = setTimeout(function(){
var set = [];
while (Visible.task.length > 0){
var task = Visible.task.shift();
if (Visible.run(task) === false){
set.push(task);
}
}
for (var k in set){
Visible.task.push(set[k]);
}
t = null;
}, 200);
};
$(window).off('resize scroll click touchmove', handler);
$(window).on('resize scroll click touchmove', handler);
}
};
function IS(type, item){
this.type = type;
this.item = [];
this.res = {};
if (item !== undefined){
this.item.push(item);
}
}
IS.prototype.setItem = function(item){
this.item.push(item);
return this;
};
IS.prototype.setRes = function(k, val){
this.res['res'+k] = val;
return this;
};
IS.prototype.setCatStat = function(type,cat,item,pos,page){
this.type = type;
this.setItem(cat);
this.setRes(1, item);
this.setRes(2, pos);
this.setRes(3, page);
return this;
};
IS.prototype.showTimeout = function(o, ttl, fn){
if (typeof o === 'string') o = $(o);
if (o.length === 0) return null;
var th = this;
Visible.set(o, function(){
setTimeout(function(){
if (Visible.is(o)){
if (fn !== undefined) fn(o);
}
else{
th.showTimeout(o);
}
}, ttl * 1000);
});
return this;
};
IS.prototype.send = function(){
var uri = '/metrica/metrics/watch/'+this.type;
uri += '/'+(this.item.join(','));
var get = [];
get.push('width='+$(window).width());
for (var key in this.res) get.push(key+'='+this.res[key]);
if (get.length > 0) uri += '?'+get.join('&');
if (getCookie('noIs') !== undefined){
get.push('item='+this.item);
get.push('type='+this.type);
console.warn('NotIs', get);
return true;
}
var req = new XMLHttpRequest();
req.open('GET', uri, true);
req.send();
};
IS.prototype.toString = function() {
return 'IS > ' + IS.prototype.toString.call(this);
};
var Img = {
init: function(){
var list = $('[data-src]');
if (list.length > 0){
list.addClass('load-img');
BeginLoad.bind('.load-img', function(list){
for (var k=0; k<list.length; k++){
list.eq(k)
.attr('src', list.eq(k).attr('data-src'))
.removeClass('load-img');
}
});
}
}
};
function Abs(selector, opt, ttl){
if (ttl === undefined){
return setTimeout(function(){new Abs(selector, opt, 1);}, 20);
}
// console.log(selector, opt)
var $th = {
obj: typeof selector === 'string'? $(selector): selector,
taskVisible: false,
opt: {
id: 'abs'+makeid(6),
name: null,
code: 'ya',
class: '',
profile: 0,
interval: 45,
minWidth: 280,
minHeight: null,
maxHeight: false,
placeholder: false,
height: false,
list: null,
minList: 1,
pageNumber: 1,
func: false
},
ya: [{
id: 'R-A-13592867',
readTop: 1,
readBody: 2,
readBottom: 3,
}]
};
if ($th.obj.length === 0) return false;
$th.setTaskVisible = function(func){
$th.taskVisible = func;
};
$th.adfoxRotation = function(containerId, interval){
setInterval(function(){
window.Ya.adfoxCode.reload(containerId, {onlyIfWasVisible: true});
}, interval===undefined? 33000: interval);
};
$th.renderAdfox = function(){
// console.log($th.opt)
if ($th.adfox[$th.opt.name] !== undefined){
$th.adfox[$th.opt.name]();
}
};
$th.renderYa = function(){
// console.log(opt)
if ($th.isVisible() === false){
$th.setTaskVisible($th.renderYa);
// console.log($th.obj)
// console.log('not visible '+opt.name)
return false;
}
// console.log('$th.renderYa', opt.name)
window.yaContextCb.push(function(){
Ya.Context.AdvManager.render({
renderTo: $th.opt.id,
blockId: $th.opt.code,
pageNumber: $th.opt.pageNumber,
onRender: function(){
// console.log(data)
// console.log(opt)
},
onError: function(){
// console.error(data)
// console.error(opt)
},
},
function altCallback() {
if ($th.opt.reserv !== undefined) {
try {
// console.log('opt.reserv')
// console.log(opt.reserv)
$th.get($th.opt.reserv);
} catch (err) {
console.error(err);
}
}
}
);
});
};
$th.isVisible = function(){
var pageYOffset = window.pageYOffset;
var pageXOffset = window.pageXOffset;
var top = $th.obj[0].getBoundingClientRect().top;
var left = $th.obj[0].getBoundingClientRect().left;
var right = $th.obj[0].getBoundingClientRect().right;
var bottom = $th.obj[0].getBoundingClientRect().bottom;
var tpos = {
top: pageYOffset + top,
left: pageXOffset + left,
right: pageXOffset + right,
bottom: pageYOffset + bottom
};
var wpos = {
top: pageYOffset,
left: pageXOffset,
right: pageXOffset + document.documentElement.clientWidth,
bottom: pageYOffset + document.documentElement.clientHeight
};
tpos.top -= 300;
tpos.bottom += 300;
return tpos.bottom > wpos.top && tpos.top < wpos.bottom && tpos.right > wpos.left && tpos.left < wpos.right;
};
$th.reload = function(func){
if ($th.opt.interval < 1) return false;
$th.opt._interval = setInterval(function(){
if ($th.isVisible() === false){
// console.log('Reload not run', $th.opt.name)
clearInterval($th.opt._interval);
$th.setTaskVisible(function(){
func();
$th.reload(func);
});
return false;
}
// console.log('Reload', $th.opt.name)
func();
}, $th.opt.interval * 1000);
};
$th.get = function(opt){
$th.opt = $.extend($th.opt, opt);
$th.obj.attr('id', $th.opt.id);
if ($th.opt.maxHeight) {
$th.obj.css('max-height', $th.opt.maxHeight + 'vh');
$th.obj.css('overflow-y', 'auto');
// $th.obj.html('<div style="height:' + (0.9 * opt.maxHeight) + 'vh;"></div>');
}
else if ($th.opt.placeholder) {
if (!isNaN(Number($th.opt.placeholder))){
$th.opt.placeholder = $th.opt.placeholder+'vh';
}
$th.obj.html('<div style="height:'+$th.opt.placeholder+';"></div>');
}
// console.log($th.opt.name)
// $th.dd()
// console.log(opt)
if ($th.opt.code === 'ya'){
// console.log($th.opt)
if ($th.ya[$th.opt.profile] === undefined) return null;
$th.ya = $th.ya[$th.opt.profile];
// console.log($th.ya)
if ($th.ya[$th.opt.name] === undefined) return null;
$th.opt.code = $th.ya.id+'-'+$th.ya[$th.opt.name];
$th.reload(function(){
$th.opt.id = 'abs'+makeid(6);
$th.obj.attr('id', $th.opt.id);
// $th.obj.html('');
$th.renderYa();
});
$th.renderYa();
return $th;
}
else if ($th.opt.code === 'adfox'){
$th.renderAdfox();
return $th;
}
else if ($th.opt.func !== false){
if ($th.opt.interval > 0){
$th.opt._interval = setInterval(function(){
$th.obj.css('min-height', $th.obj.height());
$th.obj.html('');
try{
$th.opt.func();
}catch (e) {
throw Error(e);
}
setTimeout(function(){
$th.obj.css('min-height', '');
}, 1000);
}, $th.opt.interval * 1000);
}
try{
$th.opt.func();
}catch (e) {
throw Error(e);
}
}
};
$th.worker = function(){
var t = null;
var handler = function(){
if (t !== null) return null;
if ($th.taskVisible === false) return null;
t = setTimeout(function(){
// console.log('$th.worker', $th.opt.name)
if ($th.isVisible() === true){
$th.taskVisible();
$th.taskVisible = false;
}
t = null;
}, 100);
};
$(window).off('resize scroll click touchmove', handler);
$(window).on('resize scroll click touchmove', handler);
};
try {
if ($th.opt !== undefined){
$th.opt = $.extend($th.opt, opt);
if ($th.opt.list !== null && isArray($th.opt.list)){
if ($th.obj.hasClass('uk-grid') === false){
$th.obj.addClass('uk-grid abs_row');
setTimeout(function(){
var w = $th.obj.width();
var cw = Math.floor(w / 310);
var c = $th.opt.minList > cw? $th.opt.minList: cw;
c = $th.opt.list.length < c? $th.opt.list.length: c;
var size = Math.floor(12 / c);
size = cw < c? 12: size;
var b;
for (var i in $th.opt.list) {
if (i > c - 1) break;
b = $('<div/>');
// b.addClass('abs_col col-s-'+size);
b.addClass('abs_col uk-width-expand@m');
$th.obj.append(b);
new Abs(b, $th.opt.list[i]);
}
}, 100);
}
}
else{
$th.get($th.opt);
}
}
}catch (err) {
// console.error(e)
sendDebug([err, err.stack, 'from Abs'].join("\n"));
// throw Error(e)
}
$th.worker();
return $th;
}
function get_performance(s, level){
if (level === undefined) level = 0;
var data = {
ver: 9,
latency: -1,
dns: -1,
tcp: -1,
first_byte: -1,
transfer_time: -1,
transferSize: -1,
page_load_time: -1,
processing: -1,
duration: -1,
srv: -1,
error: '',
size: 0,
level: 0,
};
if (document.querySelector('meta[name="srv"]')){
data.srv = document.querySelector('meta[name="srv"]').content;
}
try {
var p = performance.getEntriesByType('navigation')[0];
// console.log(p);
if (p !== 'undefined'){
try {
if (p.duration === 0 && level < 5){
setTimeout(function(){
level++;
get_performance(s, level);
}, 500);
return false;
}
data.latency = ((p.responseStart - p.fetchStart) / 1000).toFixed(3);
data.dns = ((p.domainLookupEnd - p.domainLookupStart) / 1000).toFixed(3);
data.tcp = ((p.connectEnd - p.connectStart) / 1000).toFixed(3);
data.first_byte = ((p.responseStart - p.requestStart) / 1000).toFixed(3);
data.transfer_time = ((p.responseEnd - p.responseStart) / 1000).toFixed(3);
data.transferSize = (p.transferSize / 1024 / 1024).toFixed(3);
data.page_load_time = ((p.domContentLoadedEventEnd-p.responseStart)/ 1000).toFixed(3);
data.processing = ((p.domComplete - p.fetchStart) / 1000).toFixed(3);
data.duration = (p.duration / 1000).toFixed(3);
} catch (e) {
// console.log(e.name)
// console.log(e.message)
data.error = encodeURI(window.btoa(e.message));
}
}
} catch (e) {
// console.log(e.prototype)
data.error = encodeURI(window.btoa(e.message));
}
var w = $(window);
data.size = w.width()+'x'+w.height();
data.level = level;
if (s==='string' || s==='send'){
var str = [];
for (var k in data) str.push(data[k]);
str = str.join(':');
if (s==='send'){
return get('/api/per?'+str);
}
return str;
}
return data;
}
// API
function getAllUrlParams(url) {
var queryString = url ? url.split('?')[1] : window.location.search.slice(1);
var obj = {};
if (queryString) {
var paramNum = null;
var vReplace = function(v) {
paramNum = v.slice(1,-1);
return '';
};
queryString = queryString.split('#')[0];
var arr = queryString.split('&');
for (var i=0; i<arr.length; i++) {
var a = arr[i].split('=');
paramNum = undefined;
var paramName = a[0].replace(/\[\d*\]/, vReplace);
var paramValue = typeof(a[1])==='undefined' ? true : a[1];
paramName = paramName.toLowerCase();
paramName = paramName.replace(new RegExp('%5b%5d$', 'gi'), '');
paramValue = paramValue.toLowerCase();
if (obj[paramName]) {
if (typeof obj[paramName] === 'string') {
obj[paramName] = [obj[paramName]];
}
if (typeof paramNum === 'undefined') {
obj[paramName].push(paramValue);
}
else {
obj[paramName][paramNum] = decodeURIComponent(paramValue);
}
}
else {
obj[paramName] = decodeURIComponent(paramValue);
}
}
}
return obj;
}
$(document).ready(function(){
Visible.worker();
$('body').on('click', '.is', function(e){
try{
e = $(e.target);
var is = e.attr('data-is');
if (is !== undefined) {
is = is.split(',');
if (is.length > 0) {
var _is = new IS(is[0]);
if (is[1] !== undefined){
_is.setItem(is[1]);
}
for (var i = 2; i < 5; i++) {
if (is[i] !== undefined){
_is.setRes(i - 1, is[i]);
}
else{
break;
}
}
_is.send();
}
}
}
catch(err){
console.error(err);
}
});
});
© 2023 Quttera Ltd. All rights reserved.