// namespace window.semantic = { handler: {} }; // Allow for console.log to not break IE if (typeof window.console == "undefined" || typeof window.console.log == "undefined") { window.console = { log : function() {}, info : function(){}, warn : function(){} }; } if(typeof window.console.group == 'undefined' || typeof window.console.groupEnd == 'undefined' || typeof window.console.groupCollapsed == 'undefined') { window.console.group = function(){}; window.console.groupEnd = function(){}; window.console.groupCollapsed = function(){}; } if(typeof window.console.markTimeline == 'undefined') { window.console.markTimeline = function(){}; } window.console.clear = function(){}; // ready event semantic.ready = function() { // selector cache var $sortableTables = $('.sortable.table'), $sticky = $('.ui.sticky'), $themeDropdown = $('.theme.dropdown'), $ui = $('.ui').not('.hover, .down'), $swap = $('.theme.menu .item'), $menu = $('#menu'), $hideMenu = $('#menu .hide.item'), $sortTable = $('.sortable.table'), $demo = $('.demo'), $container = $('.main.container'), $allHeaders = $('.main.container > h2, .main.container > .tab > h2, .main.container > .tab > .examples h2'), $sectionHeaders = $container.children('h2'), $followMenu = $container.find('.following.menu'), $sectionExample = $container.find('.example'), $exampleHeaders = $sectionExample.children('h4'), $footer = $('.page > .footer'), $menuPopup = $('.ui.main.menu .popup.item'), $pageDropdown = $('.ui.main.menu .page.dropdown'), $pageTabMenu = $('.tab.header.segment .tabular.menu'), $pageTabs = $('.tab.header.segment .menu .item'), $languageDropdown = $('.language.dropdown'), $languageModal = $('.language.modal'), $downloadDropdown = $('.download.buttons .dropdown'), $helpPopup = $('.header .help.icon'), $example = $('.example'), $shownExample = $example.filter('.shown'), $overview = $('.header.segment .overview'), //$developer = $('.header .developer.item'), //$designer = $('.header .designer.item'), $sidebarButton = $('.fixed.launch.button'), $code = $('div.code').not('.existing'), $existingCode = $('.existing.code'), languageDropdownUsed = false, requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { setTimeout(callback, 0); }, // alias handler ; // event handlers handler = { createIcon: function() { $example .each(function(){ $('') .addClass('icon code') .insertAfter( $(this).children(':first-child') ) ; }) ; }, createWaypoints: function() { $sectionHeaders .visibility({ once: false, offset: 70, onTopVisible: handler.activate.accordion, onTopPassed: handler.activate.section, onBottomPassed: handler.activate.section, onTopPassedReverse: handler.activate.previous }) ; $sectionExample .visibility({ once: false, offset: 70, onTopPassed: handler.activate.example, onBottomPassedReverse: handler.activate.example }) ; $footer .visibility({ once: false, onTopVisible: function() { var $title = $followMenu.find('> .item > .title').last() ; $followMenu .accordion('open', $title) ; } }) ; }, activate: { previous: function() { var $menuItems = $followMenu.children('.item'), $section = $menuItems.filter('.active'), index = $menuItems.index($section) ; if($section.prev().size() > 0) { $section .removeClass('active') .prev('.item') .addClass('active') ; $followMenu .accordion('open', index - 1) ; } }, accordion: function() { var $section = $(this), index = $sectionHeaders.index($section), $followSection = $followMenu.children('.item'), $activeSection = $followSection.eq(index) ; $followMenu .accordion('open', index) ; }, section: function() { var $section = $(this), index = $sectionHeaders.index($section), $followSection = $followMenu.children('.item'), $activeSection = $followSection.eq(index) ; $followSection .removeClass('active') ; $activeSection .addClass('active') ; }, example: function() { var $section = $(this).children('h4').eq(0), index = $exampleHeaders.index($section), $followSection = $followMenu.find('.menu > .item'), $activeSection = $followSection.eq(index), inClosedTab = ($(this).closest('.tab:not(.active)').size() > 0), anotherExample = ($(this).filter('.another.example').size() > 0) ; if(!inClosedTab && !anotherExample) { $followSection .removeClass('active') ; $activeSection .addClass('active') ; } } }, translatePage: function(languageCode, text, $choice) { languageDropdownUsed = true; window.Transifex.live.translateTo(languageCode, true); }, showLanguageModal: function(languageCode) { var $choice = $languageDropdown.find('[data-value="' + languageCode + '"]').eq(0), percent = $choice.data('percent') || 0, text = $choice.text() ; if(percent < 100 && languageDropdownUsed) { languageDropdownUsed = false; $languageModal .modal() .find('.header .name') .html(text) .end() .find('.complete') .html(percent) .end() ; $languageModal .modal('show', function() { $('.language.modal .progress .bar').css('width', percent + '%'); }) ; } }, tryCreateMenu: function(event) { if($(window).width() > 640) { if($container.find('.following.menu').size() === 0) { handler.createMenu(); handler.createWaypoints(); $(window).off('resize.menu'); } } }, createAnchors: function() { $allHeaders .each(function() { var $section = $(this), safeName = $section.text().trim().replace(/\s+/g, '-').replace(/[^-,'A-Za-z0-9]+/g, '').toLowerCase(), id = window.escape(safeName), $anchor = $('').addClass('anchor').attr('id', id) ; $section .append($anchor) ; }) ; $example .each(function() { var $title = $(this).children('h4').eq(0), safeName = $title.text().trim().replace(/\s+/g, '-').replace(/[^-,'A-Za-z0-9]+/g, '').toLowerCase(), id = window.escape(safeName), $anchor = $('').addClass('anchor').attr('id', id) ; if($title.size() > 0) { $title.after($anchor); } }) ; }, createMenu: function() { // grab each h3 var html = '', $sticky, $rail ; $sectionHeaders .each(function(index) { var $currentHeader = $(this), $nextElements = $currentHeader.nextUntil('h2'), $examples = $nextElements.find('.example').andSelf().filter('.example'), activeClass = (index === 0) ? 'active ' : '', safeName = $currentHeader.text().trim().replace(/\s+/g, '-').replace(/[^-,'A-Za-z0-9]+/g, '').toLowerCase(), id = window.escape(safeName), $anchor = $('').addClass('anchor').attr('id', id) ; html += '
'),
code = $code.html(),
existingCode = $code.hasClass('existing'),
evaluatedCode = $code.hasClass('evaluated'),
contentType = $code.data('type') || 'javascript',
title = $code.data('title') || false,
demo = $code.data('demo') || false,
preview = $code.data('preview') || false,
label = $code.data('label') || false,
displayType = {
html : 'HTML',
javascript : 'Javascript',
css : 'CSS',
text : 'Command Line',
sh : 'Command Line'
},
indent = handler.getIndent(code) || 2,
padding = 20,
name = (evaluatedCode)
? 'existing'
: 'instructive',
formattedCode = code,
whiteSpace,
$label,
codeHeight
;
var entityMap = {
"&": "&",
"<": "<",
">": ">",
'"': '"',
"'": ''',
"/": '/'
};
function escapeHTML(string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
});
}
// evaluate if specified
if(evaluatedCode) {
eval(code);
}
// trim whitespace & escape
whiteSpace = new RegExp('\\n\\s{' + indent + '}', 'g');
formattedCode = $.trim(code).replace(whiteSpace, '\n');
formattedCode = escapeHTML(formattedCode);
// replace with
$codeTag
.addClass($code.attr('class'))
.html(formattedCode)
;
$code
.replaceWith($codeTag)
;
$code = $codeTag;
$code
.html(formattedCode)
;
// wrap
$code = $code
.wrap('')
.wrap('')
;
// color code
window.hljs.highlightBlock($code[0]);
// add label
if(title) {
$('')
.addClass('ui attached top label')
.html('' + title + '' + '' + (displayType[contentType] || contentType) + '')
.prependTo( $code.closest('.segment') )
;
}
if(label) {
$('