jQuery(function($) { var help = null; $(window).on('hashchange.start_help', function(e) { if(help == null && window.location.hash == '#help') { help = new ace.Onpage_Help($) help.init() help.disable(); //add #help tag to links to enable help $(document).on('click.start_help', '.sidebar .nav-list a', function() { var href = $(this).attr('href'); if( !href.match(/\#help$/) ) $(this).attr('href', href+'#help'); }); } }).triggerHandler('hashchange.start_help'); //some buttons inside demo pages to launch help $(document).on(ace.click_event, '.btn-display-help', function(e) { e.preventDefault(); if(help == null) { help = new ace.Onpage_Help($) help.init() help.disable(); // $('#ace-toggle-onpage-help').trigger('click'); } var section = $(this).attr('href'); help.show_help(section); }); }); ace.Onpage_Help = function($) { if( !window.Node ) window.Node = { ELEMENT_NODE: 1, ATTRIBUTE_NODE: 2, TEXT_NODE: 3, COMMENT_NODE: 8 }; var $base = ace.vars['base'] || '../..'; var section_start = {}; var section_end = {}; var rects = {}; var created = false; var active = false; var self = this; var settings = {} var ovfx = ''; var container = null; var body_h, body_w; var captureFocus = function() { if(!container) return; var scroll = -1; //like bootstrap modal $(document) .off('focusin.ace.help') //remove any previously attached handler .on('focusin.ace.help', function (e) { if (!( container[0] == e.target || $.contains(container[0], e.target) )) { container.focus(); } if(e.target == document && scroll > -1) { //when window regains focus and container is focused, it scrolls to bottom //so we put it back to its place $('body,html').scrollTop(scroll); scroll = -1; } }) $(window).on('blur.ace.help', function(){ scroll = $(window).scrollTop(); }); } var releaseFocus = function() { $(document).off('focusin.ace.help'); $(window).off('blur.ace.help'); } this.enable = function() { if(active) return; active = true; settings['navbar'] = ace.settings.is('navbar', 'fixed') settings['sidebar'] = ace.settings.is('sidebar', 'fixed') settings['breadcrumbs'] = ace.settings.is('breadcrumbs', 'fixed') ace.settings.navbar_fixed(false , false);//disable fixed navbar, sidebar, etc if( !created ) { this.init(); created = true; } $('.ace-onpage-help-backdrop, .ace-onpage-help').removeClass('hidden'); ovfx = document.body.style.overflowX; document.body.style.overflowX = 'hidden';//hide body:overflow-x $('#btn-scroll-up').css('z-index', 1000000); $(window).triggerHandler('resize.onpage_help'); captureFocus(); } this.disable = function() { active = false; $('.ace-onpage-help-backdrop, .ace-onpage-help').addClass('hidden'); document.body.style.overflowX = ovfx;//restore body:overflow-x $('#btn-scroll-up').css('z-index', ''); //restore fixed state of navbar, sidebar, etc if( settings['breadcrumbs'] ) { ace.settings.breadcrumbs_fixed(true, false, false); } if( settings['sidebar'] ) { ace.settings.sidebar_fixed(true, false, false); } if( settings['navbar'] ) { ace.settings.navbar_fixed(true, false, false); } releaseFocus(); } this.is_active = function() { return active; } this.show_help = function(section) { launch_help_modal(section, true); } this.init = function() { container = $('
').appendTo('body'); container .append('') .append('\([\S|\s]+?)\<\/pre\>/ig, function(a, b){ return a.replace(b , b.replace(/\/g , '>')); }); content.empty().append(result); content .find('.info-section').each(function() { var header = $(this).prevAll('.info-title').eq(0); if(header.length == 0) return false; header = header.addClass('widget-title').wrap('') .parent().append('').closest('.widget-header'); $(this).wrap(' '); $(this).closest('.widget-box').prepend(header); }); content.removeClass('hidden'); content.find('span.thumbnail img').each(function() { var src = $(this).attr('src'); $(this) .attr('src', $base+"/docs/" + src) .one('load', function() { mbody.ace_scroll('reset'); }); }); Rainbow.color(content.get(0), function(){ mbody.ace_scroll('reset'); }); //save history list add_to_nav_list(name, save_to_list); var pos = -1; if((pos = name.lastIndexOf('.')) > -1) { name = name.substr(0, pos); modal.find('button[data-goup=modal]').removeClass('disabled').attr('data-url', name); } else { modal.find('button[data-goup=modal]').addClass('disabled').blur(); } }) .fail(function() { modal.find('.modal-title').find('.fa-spin').remove().end().find('.hidden').children().unwrap(); mbody.ace_scroll('reset'); }); }//launch_help_modal $(document).on(ace.click_event, '.help-content > .widget-box > .widget-header > .info-title', function(e) { var widget_box = $(this).closest('.widget-box').widget_box('toggle'); }); $(document).on(ace.click_event, '.help-more', function(e) { e.preventDefault(); var href = $(this).attr('href'); launch_help_modal(href); }); function add_to_nav_list(name, save_to_list) { if(save_to_list !== false) { if(nav_list.length > 0) { nav_list = nav_list.slice(0, nav_pos + 1); } if(nav_list[nav_list.length - 1] != name) { nav_list.push(name); nav_pos = nav_list.length - 1; } } var modal = $('#onpage-help-modal'); if(nav_pos == 0){ modal.find('button[data-goback=modal]').addClass('disabled').blur(); } else { modal.find('button[data-goback=modal]').removeClass('disabled'); } if(nav_pos == nav_list.length - 1){ modal.find('button[data-goforward=modal]').addClass('disabled').blur(); } else { modal.find('button[data-goforward=modal]').removeClass('disabled'); } } $(document).on(ace.click_event, 'button[data-goforward=modal]', function() { if(nav_pos < nav_list.length - 1) { nav_pos++; launch_help_modal(nav_list[nav_pos], false); } }); $(document).on(ace.click_event, 'button[data-goback=modal]', function() { if(nav_pos > 0) { nav_pos--; launch_help_modal(nav_list[nav_pos], false); } }); $(document).on(ace.click_event, 'button[data-goup=modal]', function() { var $this = $(this), url; if( $this.hasClass('disabled') || !(url = $this.attr('data-url')) ) return; launch_help_modal(url , true) }); $(document).on(ace.click_event, '.open-file[data-open-file]', function() { $('#onpage-help-modal').find('.modal-title').wrapInner("").append(''); $('.onpage-help-content').addClass('hidden') var url = $(this).text(); var language = $(this).attr('data-open-file'); display_codeview(url, language, true); }); function display_codeview(url, language, save_to_list) { $.ajax({url: $base+'/'+url, dataType:'text'}) .done(function(content) { if(language != 'json') { if(language != 'css') { //replace each tab character with two spaces (only those that start at a new line) content = content.replace(/\n[\t]{1,}/g, function(p, q) { return p.replace(/\t/g, " "); }); } else { content = content.replace(/\t/g , " ") } } else { language = ''; content = JSON.stringify(JSON.parse(content), null, 2); } var modal = $('#onpage-help-modal'); add_to_nav_list('file:'+language+':'+url, save_to_list); modal.find('button[data-goup=modal]').addClass('disabled').blur(); content = content.replace(/\>/g, '>').replace(/\');; $('.onpage-help-content').removeClass('hidden').empty().html(highlighted_code).wrapInner(''); modal.find('.modal-body').ace_scroll('reset'); }); }); } }