feat: Remembering preferred code langauge in code tabstrip.

This commit is contained in:
imtodor 2018-08-17 14:27:19 +03:00
Родитель 9f8e5f1061
Коммит 6984ddc5bd
2 изменённых файлов: 76 добавлений и 43 удалений

Просмотреть файл

@ -1,5 +1,80 @@
$(function(){
const selectedLanguageKey = "Selected_TabStrip_Language_Key";
function saveLanguage(language) {
localStorage.setItem(selectedLanguageKey, language);
}
$(function () {
$("pre").addClass("prettyprint");
prettyPrint();
/* START TabStrip logic */
var selectedLanguage = localStorage.getItem(selectedLanguageKey);
var onTabActivated = function (e) {
var language = e.item.innerText;
if (selectedLanguage !== language) {
saveLanguage(language);
}
};
$("div.tabbedCode").each(function () {
var container = $(this);
var langs = container.find("pre");
if (langs.length === 0) {
//console.log("Cannot find any languages")
return;
}
var tabs = $.map(langs, function (item) {
return $("<li>").text($(item).attr("lang"));
});
var hasActive = false;
if (!selectedLanguage) {
saveLanguage(tabs[0].innerText);
} else {
$(tabs).each(function (index) {
if ($(this).text() === selectedLanguage) {
$(this).addClass('k-state-active');
hasActive = true;
}
});
}
if (!hasActive) {
tabs[0].addClass("k-state-active");
}
var tabstrip = $("<div>")
.append($("<ul>").append(tabs))
.append(langs);
container.replaceWith(tabstrip);
langs.wrap("<div>");
tabstrip.kendoTabStrip(
{
animation: false,
activate: onTabActivated
});
});
var codeSampleMapper = {
'C#': 'cs',
'VB.NET': 'vb',
'VB': 'vb',
'JavaScript': 'js',
'ASPNET': 'html',
'XML': 'xml',
'TypeScript': 'commonjs',
}
$("pre").each(function (index) {
var langExtension = codeSampleMapper[$(this).attr('lang')];
$(this).addClass('lang-' + langExtension).addClass("prettyprint");
});
/* END TabStrip logic */
});

Просмотреть файл

@ -260,45 +260,3 @@ function onUserInputSL() {
}
/* END Developer Focused Examples Kendo Grid implementation */
/* START TabStrip logic */
$(function () {
$("div.tabbedCode").each(function () {
var container = $(this);
var langs = container.find("pre");
if (langs.length == 0) {
//console.log("Cannot find any languages")
return;
}
var tabs = $.map(langs, function (item) {
return $("<li>").text($(item).attr("lang"));
});
tabs[0].addClass("k-state-active");
var tabstrip = $("<div>")
.append($("<ul>").append(tabs))
.append(langs);
container.replaceWith(tabstrip);
langs.wrap("<div>");
tabstrip.kendoTabStrip({ animation: false });
});
var codeSampleMapper = {
'C#': 'cs',
'VB.NET': 'vb',
'VB': 'vb',
'JavaScript': 'js',
'ASPNET': 'html',
'XML': 'xml',
'TypeScript': 'commonjs',
}
$("pre").each(function (index) {
var langExtension = codeSampleMapper[$(this).attr('lang')];
$(this).addClass('lang-' + langExtension).addClass("prettyprint");
});
});
/* END TabStrip logic */