// 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'),
$menuPopup = $('.ui.main.menu .popup.item'),
$menuDropdown = $('.ui.main.menu .dropdown'),
$pageTabMenu = $('.tab.header.segment .tabular.menu'),
$pageTabs = $('.tab.header.segment .menu .item'),
$downloadDropdown = $('.download.buttons .dropdown'),
$helpPopup = $('.header .help.icon'),
$example = $('.example'),
$shownExample = $example.filter('.shown'),
$developer = $('.developer.item'),
$overview = $('.overview.item, .overview.button'),
$designer = $('.designer.item'),
$sidebarButton = $('.fixed.launch.button'),
$code = $('div.code').not('.existing'),
$existingCode = $('.existing.code'),
// alias
handler
;
// event handlers
handler = {
createIcon: function() {
$example
.each(function(){
$('')
.addClass('icon code')
.prependTo( $(this) )
;
})
;
},
getSpecification: function(callback) {
var
url = $(this).data('url') || false
;
callback = callback || function(){};
if(url) {
$.ajax({
method: 'get',
url: url,
type: 'json',
complete: callback
});
}
},
less: {
parseFile: function(content) {
var
variables = {},
lines = content.match(/^(@[\s|\S]+?;)/gm),
name,
value
;
$.each(lines, function(index, line) {
// clear whitespace
line = $.trim(line);
// match variables only
if(line[0] == '@') {
name = line.match(/^@(.+):/);
value = line.match(/:\s*([\s|\S]+?;)/);
if( ($.isArray(name) && name.length >= 2) && ($.isArray(value) && value.length >= 2) ) {
name = name[1];
value = value[1];
variables[name] = value;
}
}
});
console.log(variables);
return variables;
},
changeTheme: function(theme) {
var
variableURL = '/build/less/themes/packages/{$theme}/{$type}s/{$element}.variables',
overrideURL = '/build/less/themes/packages/{$theme}/{$type}s/{$element}.overrides',
urlData = {
theme : typeof(theme === 'string')
? theme.toLowerCase()
: theme,
type : $themeDropdown.data('type'),
element : $themeDropdown.data('element')
}
;
$themeDropdown
.api({
on : 'now',
url : variableURL,
dataType : 'text',
urlData : urlData,
success: function(content) {
less.modifyVars( handler.less.parseFile(content) );
$themeDropdown
.api({
on : 'now',
url : overrideURL,
dataType : 'text',
urlData : urlData,
success: function(content) {
if( $('style.override').size() > 0 ) {
$('style.override').remove();
}
console.log(content);
$('')
.addClass('override')
.appendTo('body')
;
}
})
;
}
})
;
}
},
create: {
examples: function(json) {
var
types = json['Types'],
text = json['Text'],
states = json['States'],
variations = json['Variations'],
$element,
html
;
$.each(types, function(name, type){
html += '
' + name + '')
.addClass('annotation')
.appendTo($example)
;
}
if( $example.find('.instructive').size() === 0) {
$code = $('')
.data('type', 'html')
.addClass('code')
.html(code)
.hide()
.appendTo($annotation)
;
$.proxy(handler.initializeCode, $code)();
}
if( ($demo.first().is(':visible') || type == 'developer') && type != 'designer' ) {
$demo.hide();
$header.show();
$annotation.fadeIn(500);
}
else {
$annotation.hide();
if($demo.size() > 1) {
$demo.show();
}
else {
$demo.fadeIn(500);
}
}
},
createAnnotation: function() {
if(!$(this).data('type')) {
$(this).data('type', 'html');
}
$(this)
.wrap('')
.parent()
.hide()
;
},
resizeCode: function() {
$('.ace_editor')
.each(function() {
var
$code = $(this),
padding = 50,
editor,
editorSession,
codeHeight
;
$code.css('height', 'auto');
editor = ace.edit($code[0]);
editorSession = editor.getSession();
codeHeight = editorSession.getScreenLength() * editor.renderer.lineHeight + padding;
$code.css('height', codeHeight);
editor.resize();
})
;
},
makeCode: function() {
if(window.ace !== undefined) {
$code
.filter(':visible')
.each(handler.initializeCode)
;
$existingCode
.each(handler.createAnnotation)
;
}
},
initializeCode: function() {
var
$code = $(this).show(),
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',
whiteSpace,
$label,
editor,
editorSession,
codeHeight
;
// trim whitespace
whiteSpace = new RegExp('\\n\\s{' + indent + '}', 'g');
code = $.trim(code).replace(whiteSpace, '\n');
if(contentType == 'html') {
$code.text(code);
}
else {
$code.html(code);
}
// evaluate if specified
if(evaluatedCode) {
eval(code);
}
// initialize
editor = ace.edit($code[0]);
editorSession = editor.getSession();
//editor.setTheme('ace/theme/tomorrow');
editor.setTheme('ace/theme/github');
editor.setShowPrintMargin(false);
editor.setReadOnly(true);
editor.renderer.setShowGutter(false);
editor.setHighlightActiveLine(false);
editorSession.setMode('ace/mode/'+ contentType);
editorSession.setUseWrapMode(true);
editorSession.setTabSize(2);
editorSession.setUseSoftTabs(true);
codeHeight = editorSession.getScreenLength() * editor.renderer.lineHeight + padding;
$(this)
.height(codeHeight + 'px')
.wrap('
')
;
// add label
if(title) {
$('
')
.addClass('ui attached top label')
.html('
' + title + '' + '
' + (displayType[contentType] || contentType) + '')
.prependTo( $(this).parent() )
;
}
if(label) {
$('
')
.addClass('ui pointing below label')
.html(displayType[contentType] || contentType)
.insertBefore ( $(this).parent() )
;
}
// add run code button
if(demo) {
$('
')
.addClass('ui pointing below label')
.html('Run Code')
.on('click', function() {
eval(code);
})
.insertBefore ( $(this).parent() )
;
}
// add preview if specified
if(preview) {
$(code)
.insertAfter( $(this).parent() )
;
}
editor.resize();
},
swapStyle: function() {
var
theme = $(this).data('theme')
;
$(this)
.addClass('active')
.siblings()
.removeClass('active')
;
$('head link.ui')
.each(function() {
var
href = $(this).attr('href'),
subDirectory = href.split('/')[3],
newLink = href.replace(subDirectory, theme)
;
$(this)
.attr('href', newLink)
;
})
;
}
};
$('.masthead')
.visibility({
once: false
})
.visibility('bottom visible', function(){
$('.main.menu').removeClass('filled');
})
.visibility('bottom passed', function(){
$('.main.menu').addClass('filled');
})
.find('.button')
.popup({
position : 'top center',
variation : 'inverted'
})
;
$(window)
.on('resize', function() {
clearTimeout(handler.timer);
handler.timer = setTimeout(handler.resizeCode, 500);
})
;
$downloadDropdown
.dropdown({
on : 'click',
transition : 'scale'
})
;
// attach events
if($.fn.tablesort !== undefined) {
$sortTable
.tablesort()
;
}
if( $pageTabs.size() > 0 ) {
$pageTabs
.tab({
context : '.main.container',
childrenOnly : true,
history : true,
onTabInit : function() {
handler.makeCode();
},
onTabLoad : function() {
$sticky.filter(':visible').sticky('refresh');
}
})
;
}
else {
handler.makeCode();
}
$menu
.sidebar({
transition: 'scale down'
})
.sidebar('attach events', '.launch.button, .view-ui.button, .launch.item')
.sidebar('attach events', $hideMenu, 'hide')
;
handler.createIcon();
$example
.one('mousemove', handler.generateCode)
.find('i.code')
.on('click', handler.createCode)
;
$themeDropdown
.dropdown({
action: 'select',
onChange: handler.less.changeTheme
})
;
$shownExample
.each(handler.createCode)
;
$helpPopup
.popup()
;
$swap
.on('click', handler.swapStyle)
;
$developer
.on('click', handler.developerMode)
;
$designer
.on('click', handler.designerMode)
;
$overview
.on('click', handler.overviewMode)
;
$menuPopup
.popup({
position : 'bottom center',
className: {
popup: 'ui popup'
}
})
;
$sortableTables
.tablesort()
;
$menuDropdown
.dropdown({
on : 'hover',
action : 'nothing'
})
;
$sticky
.sticky({
context : '.main.container',
pushing : true
})
;
};
// attach ready event
$(document)
.ready(semantic.ready)
;