var Main = {
CHECK_PERIOD: 400,
UPDATE_PERIOD: 1200,
FORCE_UPDATE_PERIOD: 5000,
init: function() {
Aj.onLoad(function(state) {
var cont = Aj.ajContainer;
$(window).on('resize', Main.onResize);
$('.js-logo-hoverable').on('mouseover', Main.playLogo);
$('.js-logo-clickable').on('click', Main.playLogo);
$('.js-logo-icon').on('animationend', Main.eLogoAnimEnd);
$(cont).on('click.curPage', '.js-header-menu-button', Main.eHeaderMenu);
$(cont).on('click.curPage', '.js-header-menu-close-button', Main.eHeaderMenuClose);
$(cont).on('click.curPage', '.js-btn-tonkeeper', Main.eTonkeeperOpen);
$(cont).on('click.curPage', '.js-auction-unavail', Main.eAuctionUnavailable);
$(cont).on('click.curPage', '.js-howitworks', Main.eHowitworks);
$(cont).on('click.curPage', '.logout-link', Login.logOut);
$(cont).on('click.curPage', '.ton-logout-link', Login.tonLogOut);
$(cont).on('click.curPage', '.js-copy-code', Main.copyCode);
$(cont).on('click.curPage', '.js-main-search-dd-item', Main.eMainSearchDDSelected);
state.$headerMenu = $('.js-header-menu');
state.$unavailPopup = $('.js-unavailable-popup');
state.$howitworksPopup = $('.js-howitworks-popup');
state.$mainSearchField = $('.js-main-search-field');
state.$mainSearchForm = $('.js-main-search-form');
state.$mainSearchForm.on('submit', Main.eMainSearchSubmit);
state.$mainSearchForm.field('query').on('input', Main.eMainSearchInput);
state.mainSearchCache = {};
$('.js-form-clear', state.$mainSearchForm).on('click', Main.eMainSearchClear);
Main.updateTime();
Main.initViewport();
Main.initLogo();
});
Aj.onUnload(function(state) {
clearTimeout(Aj.state.searchTimeout);
$(window).off('resize', Main.onResize);
$('.js-logo-hoverable').off('mouseover', Main.playLogo);
$('.js-logo-clickable').off('click', Main.playLogo);
$('.js-logo-icon').off('animationend', Main.eLogoAnimEnd);
state.$mainSearchForm.off('submit', Main.eMainSearchSubmit);
state.$mainSearchForm.field('query').off('input', Main.eMainSearchInput);
$('.js-form-clear', state.$mainSearchForm).off('click', Main.eMainSearchClear);
});
},
initForm: function(form) {
var $form = $(form);
$('.js-amount-input', $form).on('keyup change input', Main.eUpdateAmountField);
$('.js-amount-input', $form).trigger('input');
},
destroyForm: function(form) {
var $form = $(form);
$('.js-amount-input', $form).off('keyup change input', Main.eUpdateAmountField);
},
updateTime: function() {
var now = Math.round(+(new Date) / 1000);
if (Main._lastUpdateTime != now) {
Main._lastUpdateTime = now;
$('time[datetime]', Aj.ajContainer).each(function () {
var $time = $(this), datetime = $time.attr('datetime');
var mode = $time.attr('data-relative');
if (mode) {
var date = new Date(datetime);
var cur_date = new Date();
var time_left = Math.floor((date - cur_date) / 1000);
if (mode == 'ago-text') {
time_left = -time_left;
}
var ended = time_left < 0;
if (time_left < 0) time_left = 0;
var days = Math.floor(time_left / 86400);
var hours = Math.floor((time_left % 86400) / 3600);
var minutes = Math.floor((time_left % 3600) / 60);
var seconds = (time_left % 60);
var days_html = l('{n:# days|# day|# days}', {n: days});
if (mode == 'counter') {
var hours0 = Math.floor(hours / 10);
var hours1 = hours % 10;
var minutes0 = Math.floor(minutes / 10);
var minutes1 = minutes % 10;
var seconds0 = Math.floor(seconds / 10);
var seconds1 = seconds % 10;
Main.updateDigit($('.timer-d', this), days_html);
Main.updateDigit($('.timer-h0', this), hours0);
Main.updateDigit($('.timer-h1', this), hours1);
Main.updateDigit($('.timer-m0', this), minutes0);
Main.updateDigit($('.timer-m1', this), minutes1);
Main.updateDigit($('.timer-s0', this), seconds0);
Main.updateDigit($('.timer-s1', this), seconds1);
} else {
var arr = [];
if (days > 0) {
arr.push(days_html);
}
if (arr.length || hours > 0) {
arr.push(l('{n:# hours|# hour|# hours}', {n: hours}));
}
if (arr.length || minutes > 0) {
arr.push(l('{n:# minutes|# minute|# minutes}', {n: minutes}));
}
if (!days && !hours && (mode == 'text' || !minutes)) {
arr.push(l('{n:# seconds|# second|# seconds}', {n: seconds}));
}
arr = arr.slice(0, mode == 'short-text' ? 2 : (mode == 'ago-text' ? 1 : 3));
var text = arr.join(' ');
if (text != $time.text()) {
$time.text(text);
}
}
$(this).closest('.js-timer-wrap').toggleClass('ended', ended);
} else {
var title = $time.attr('title'),
html = $time.html(),
new_html = formatDateTime(datetime, false, !$time.hasClass('short'));
if (html != new_html) {
$time.html(new_html);
}
$time.removeAttr('datetime');
}
});
}
requestAnimationFrame(Main.updateTime);
},
updateDigit: function($el, value) {
var cur_value = $el.attr('data-val');
if (cur_value != value) {
$el.removeAttr('data-prev-val');
$el.map(function(){ this.offsetTop; });
$el.attr('data-val', value);
$el.attr('data-prev-val', cur_value);
}
},
fitUsername: function($el) {
$el.each(function(){
var init_size = $(this).data('init-size');
if (!init_size) {
init_size = parseInt($(this).css('font-size'));
$(this).data('init-size', init_size);
}
var size = parseInt($(this).css('font-size'));
size = parseInt(size);
while (this.scrollWidth > this.offsetWidth) {
size -= 0.5;
if (size >= init_size * 0.75) {
$(this).css('font-size', size + 'px');
} else {
break;
}
}
var text = $(this).attr('title') || $(this).text();
var prefix_len = text.length - 3;
while (this.scrollWidth > this.offsetWidth &&
prefix_len > 3) {
prefix_len--;
$(this).text(text.substr(0, prefix_len) + '…' + text.substr(-3));
}
});
},
initViewport: function() {
if (!window.$viewportHelper) {
window.$viewportHelper = $('
').css({position: 'absolute', left: '-100px', top: '0', height: '100vh'}).appendTo('body');
}
Main.onResize();
},
onResize: function() {
var vh = window.innerHeight;
if (window.$viewportHelper && window.$viewportHelper.height() != vh) {
document.documentElement.style.setProperty('--viewport-height', vh + 'px');
} else {
document.documentElement.style.removeProperty('--viewport-height');
}
},
initLogo: function() {
if (!Aj.globalState.logoInited) {
Aj.globalState.logoInited = true;
var main_url = '/img/TelemintLogoSprite1.svg';
var mainLogo = new Image();
mainLogo.onload = function() {
Aj.globalState.logoImageMain = main_url;
Main.playLogo(true);
};
mainLogo.src = main_url;
var url2 = '/img/TelemintLogoSprite2.svg';
var logo2 = new Image();
logo2.onload = function() {
Aj.globalState.logoImage2 = url2;
};
logo2.src = url2;
var url3 = '/img/TelemintLogoSprite3.svg';
var logo3 = new Image();
logo3.onload = function() {
Aj.globalState.logoImage3 = url3;
};
logo3.src = url3;
}
},
playLogo: function(main) {
if (!$('.js-logo').hasClass('play')) {
var url = Aj.globalState.logoImageMain;
if (!main) {
var rnd = Math.random();
if (rnd > 0.9 && Aj.globalState.logoImage3) {
url = Aj.globalState.logoImage3;
} else if (rnd > 0.8 && Aj.globalState.logoImage2) {
url = Aj.globalState.logoImage2;
}
}
$('.js-logo').each(function() {
this.style.setProperty('--image-url-logo-icon-animated', 'url(\'' + url + '\')');
});
$('.js-logo').addClass('play');
}
},
eLogoAnimEnd: function(e) {
$('.js-logo').removeClass('play');
},
eHeaderMenu: function(e) {
e.preventDefault();
openPopup(Aj.state.$headerMenu, {
noAppend: true
});
},
eHeaderMenuClose: function(e) {
e.preventDefault();
closePopup(Aj.state.$headerMenu);
},
eAuctionUnavailable: function(e) {
e.preventDefault();
var username = $(this).attr('data-username');
var later = +$(this).attr('data-later');
var onEnterPress = function(e) {
if (e.keyCode == Keys.RETURN) {
e.stopImmediatePropagation();
closePopup(Aj.state.$unavailPopup);
}
};
openPopup(Aj.state.$unavailPopup, {
onOpen: function() {
$('.js-username', this).html(username);
$('.js-unavailable-text', this).toggleClass('hide', !!later);
$('.js-available-later-text', this).toggleClass('hide', !later);
$(document).on('keydown', onEnterPress);
},
onClose: function() {
$(document).off('keydown', onEnterPress);
}
});
},
eHowitworks: function(e) {
e.preventDefault();
e.stopImmediatePropagation();
var onEnterPress = function(e) {
if (e.keyCode == Keys.RETURN) {
e.stopImmediatePropagation();
closePopup(Aj.state.$howitworksPopup);
}
};
openPopup(Aj.state.$howitworksPopup, {
onOpen: function() {
$(document).on('keydown', onEnterPress);
},
onClose: function() {
$(document).off('keydown', onEnterPress);
}
});
},
amountFieldValue: function($form, field) {
var $fieldEl = field ? $form.field(field) : $form;
var minValue = $fieldEl.attr('data-min') || null;
var maxValue = $fieldEl.attr('data-max') || null;
var decPoint = $fieldEl.attr('data-dec-point') || '.';
var value = $fieldEl.value();
var float_value = value.length ? value : '0';
if (decPoint != '.') {
float_value.split(decPoint).join('.');
}
float_value = parseFloat(float_value);
if (isNaN(float_value) || float_value >= 1e12) {
return false;
}
if (minValue !== null && float_value < minValue ||
maxValue !== null && float_value > maxValue) {
return false;
} else {
return float_value;
}
},
eUpdateAmountField: function(e) {
var $fieldEl = $(this);
var minValue = $fieldEl.attr('data-min') || null;
var maxValue = $fieldEl.attr('data-max') || null;
var decPoint = $fieldEl.attr('data-dec-point') || '.';
var decimals = $fieldEl.attr('data-decimals') || 0;
var sel_dir = this.selectionDirection;
var sel_start = this.selectionStart;
var sel_end = this.selectionEnd;
var value = this.value;
var new_sel_start = sel_start;
var new_sel_end = sel_end;
var new_value = '';
var has_decimal = false;
var chars_len = 0;
var decimal_len = 0;
for (var i = 0; i < value.length; i++) {
var char = value[i];
if ((char == '.' || char == ',') && !has_decimal && decimals > 0) {
if (!chars_len) {
new_value += '0';
if (i < sel_start) new_sel_start++;
if (i < sel_end) new_sel_end++;
}
has_decimal = true;
new_value += decPoint;
} else if (char >= '0' && char <= '9' && (!has_decimal || decimal_len < decimals)) {
new_value += char;
if (has_decimal) decimal_len++;
else chars_len++;
} else {
if (i < sel_start) new_sel_start--;
if (i < sel_end) new_sel_end--;
}
}
this.value = new_value;
this.setSelectionRange(new_sel_start, new_sel_end, sel_dir);
var float_value = new_value.length ? new_value : '0';
if (decPoint != '.') {
float_value.split(decPoint).join('.');
}
float_value = parseFloat(float_value);
var is_invalid = (isNaN(float_value) || float_value >= 1e12);
var field_value = float_value;
if (minValue !== null && float_value < minValue ||
maxValue !== null && float_value > maxValue ||
is_invalid) {
field_value = false;
// Main.showFieldError($fieldEl);
} else {
// Main.hideFieldError($fieldEl);
}
if (e.type == 'change') {
if (new_value.length && !is_invalid) {
this.value = Main.wrapTonAmount(float_value, true);
}
}
if (e.type == 'input') {
var forClass, usdForClass;
if (forClass = $fieldEl.attr('data-for')) {
$('.' + forClass).html(Main.wrapTonAmount(field_value));
}
if (usdForClass = $fieldEl.attr('data-usd-for')) {
$('.' + usdForClass).html(Main.wrapUsdAmount(field_value));
}
}
},
wrapTonAmount: function(value, field_format) {
if (!value) {
return '';
}
var dec = (Math.floor(value * 1000000) % 1000000) + '';
while (dec.substr(-1) == '0') {
dec = dec.slice(0, -1);
}
return formatNumber(value, dec.length, '.', field_format ? '' : ',');
},
wrapUsdAmount: function(value, field_format) {
if (!value) {
return '';
}
value = Math.round(value * Aj.state.tonRate * 100) / 100;
return formatNumber(value, (value % 1) && value < 1000 ? 2 : 0, '.', field_format ? '' : ',');
},
eTonkeeperOpen: function(e) {
e.stopImmediatePropagation();
e.preventDefault();
var href = $(this).attr('data-href');
if (href) {
location.href = href;
}
},
copyText: function(text) {
var $text = $('