ClickHouse/website/js/docs.js

174 lines
6.2 KiB
JavaScript
Raw Normal View History

2020-03-30 08:25:29 +00:00
function onResize() {
var window_height = $(window).height();
2020-04-07 15:35:55 +00:00
var window_width = $(window).width();
var is_wide = window_width >= 768;
var docs_top_nav = $('#top-nav.bg-dark-alt');
2020-03-30 08:25:29 +00:00
$('body').attr('data-offset', window_height.toString());
var sidebar = $('#sidebar');
var languages = $('#languages-dropdown')
var edit = $('#edit-link');
2020-03-30 08:25:29 +00:00
var single_page_switch = $('#single-page-switch');
if ((sidebar.width() - single_page_switch.width() - sidebar.find('.dropdown-toggle').width()) >= 36) {
single_page_switch.addClass('float-right');
} else {
single_page_switch.removeClass('float-right');
}
2020-04-07 15:35:55 +00:00
if (is_wide) {
2020-03-30 08:25:29 +00:00
sidebar.removeClass('collapse');
edit.detach().appendTo($('#edit-wrapper'));
2020-03-30 08:25:29 +00:00
languages.detach().appendTo($('#languages-wrapper'));
} else {
sidebar.addClass('collapse');
edit.detach().insertBefore(single_page_switch);
edit.addClass('float-right');
languages.detach().insertBefore(edit);
2020-03-30 08:25:29 +00:00
languages.addClass('float-right');
single_page_switch.removeClass('float-right');
}
2020-04-07 15:35:55 +00:00
if (window_height < 800 && is_wide) {
docs_top_nav.removeClass('sticky-top');
$('#sidebar, #toc.toc-right').css({
'height': window_height,
'position': 'sticky',
'top': 0
});
} else {
var top_nav_height = docs_top_nav.height();
docs_top_nav.addClass('sticky-top');
$('#sidebar, #toc.toc-right').css({
'height': (window_height - top_nav_height) + 'px',
'position': 'fixed',
'top': top_nav_height + 16
});
}
2020-03-30 08:25:29 +00:00
}
2020-04-07 15:35:55 +00:00
2020-03-30 08:25:29 +00:00
$(document).ready(function () {
onResize();
$('#sidebar .nav-link.active').parents('.collapse').each(function() {
var current = $(this);
if (current.attr('id') !== 'sidebar') {
current.collapse('show');
}
});
$(window).resize(onResize);
$(window).on('activate.bs.scrollspy', function () {
var maxActiveOffset = 0;
$('#toc .nav-link.active').each(function() {
if (maxActiveOffset < $(this).offset().top) {
maxActiveOffset = $(this).offset().top;
}
});
$('#toc .nav-link').each(function() {
if (maxActiveOffset >= $(this).offset().top) {
$(this).addClass('toc-muted');
} else {
$(this).removeClass('toc-muted');
}
});
});
2020-04-07 15:35:55 +00:00
$('#sidebar').on('shown.bs.collapse', function () {
onResize();
$('body').on('touchmove', function (e) {
e.preventDefault();
});
});
$('#sidebar').on('hidden.bs.collapse', function () {
$('body').on('touchmove', function () {});
});
2020-03-30 08:25:29 +00:00
var headers = $('#content h1, #content h2, #content h3, #content h4, #content h5, #content h6');
headers.mouseenter(function() {
$(this).find('.headerlink').html('¶');
2020-03-30 08:25:29 +00:00
});
headers.mouseleave(function() {
$(this).find('.headerlink').html('&nbsp;');
2020-03-30 08:25:29 +00:00
});
if ($('#docsearch-input').length) {
docsearch({
apiKey: 'e239649803024433599de47a53b2d416',
indexName: 'yandex_clickhouse',
inputSelector: '#docsearch-input',
algoliaOptions: {
advancedSyntax: true,
clickAnalytics: true,
hitsPerPage: 25,
2020-04-03 13:23:32 +00:00
'facetFilters': [
'lang:' + $('html').attr('lang'),
2020-04-04 05:30:40 +00:00
'version:' + $('html').attr('data-version')
2020-04-03 13:23:32 +00:00
]
2020-03-30 08:25:29 +00:00
},
2020-04-03 19:25:20 +00:00
debug: false
2020-03-30 08:25:29 +00:00
});
}
var rating_stars = $('#rating-stars');
if (rating_stars.length) {
var key = '';
var stars_text = rating_stars.text().replace(/^\s+|\s+$/g, '');
var url_match = window.location.pathname.match(/^\/docs(?:\/v[0-9]+\.[0-9]+)?(?:\/[a-z]{2})(.*)/);
if (url_match && url_match.length > 1) {
key = url_match[1];
if (key.length > 2) {
key = key.replace(/^\/|\/$/g, '');
}
key = 'rating_' + key;
var local_stars = localStorage.getItem(key);
if (local_stars) {
stars_text = local_stars;
rating_stars.addClass('text-orange');
}
}
var titles = JSON.parse(rating_stars.attr('data-titles').replace(/'/g, '"'));
var documentation = rating_stars.attr('data-documentation');
var replacement = '';
for (var i = 0; i < 5; i++) {
var star = stars_text.charAt(i);
replacement += '<a href="#" title="' + titles[i] + ' ' + documentation +
'" data-toggle="tooltip" data-placement="bottom" role="rating" ' +
'class="rating-star text-reset text-decoration-none">' + star + '</a>';
}
rating_stars.html(replacement);
$('[data-toggle="tooltip"]').tooltip({
trigger: 'hover'
});
var rating_star_item = $('.rating-star');
rating_star_item.hover(function() {
var current = $(this);
current.text('★');
current.prevAll().text('★');
current.nextAll().text('☆');
});
rating_stars.mouseleave(function() {
for (var i = 0; i < 5; i++) {
rating_stars.children().eq(i).text(stars_text[i]);
}
});
rating_star_item.click(function(e) {
e.preventDefault();
rating_stars.addClass('text-orange');
stars_text = rating_stars.text();
localStorage.setItem(key, stars_text);
$.ajax({
url: window.location.pathname + 'rate/',
type: 'POST',
dataType: 'json',
2020-05-19 12:19:55 +00:00
data: JSON.stringify({rating: $(this).prevAll().length + 1}),
success: function () {
try {
window.yaCounter18343495.reachGoal('docs_feedback');
} catch (e) {
Sentry.captureException(e);
}
},
error: function () {
rating_stars.removeClass('text-orange');
localStorage.removeItem(key);
}
});
});
}
2020-03-30 08:25:29 +00:00
});