I have a pretty complicated page where I have multiple instances of CodeMirror in hidden tabs in tabs. To make it even more complex, I remember the last active tabs.
I managed to get it half the work ( http://codepen.io/anon/pen/LheaF ), problems with the tabs of the second editor:
- Download two tabs before clicking the main tabs of the Code Mirror. When you click the Code Mirror tab, it will not load the editor correctly until you double-click.
- I want the second tab to call the
refresh() method if it has already been started, as for the main editor. - Error when duplicating secondary editors
(function($) { var mainEditor; function initMainCodeEditor() { if (mainEditor instanceof CodeMirror) { mainEditor.refresh(); } else { // Load main editor var el = document.getElementById("codifyme"); mainEditor = CodeMirror.fromTextArea(el, { lineNumbers: true }); mainEditor.setSize('100%', 50); } } function initSecondaryCodeEditor() { var $active = $('#code_mirror_editors > .active > a'); var $sec_tab = $($active.data('target')); CodeMirror.fromTextArea($sec_tab.find('textarea')[0], { lineNumbers: true }); } $(document).ready(function() { // Only load editors if tab has been clicked $('#maintabs > li > a[data-target="#codemirror"]').on('shown.bs.tab', function(e) { initMainCodeEditor(); }); $('#code_mirror_editors > li > a[data-toggle="tab"]').on('shown.bs.tab', function(e) { initSecondaryCodeEditor(); }); // Remember tabs var json, tabsState; $('a[data-toggle="tab"]').on('shown.bs.tab', function(e) { tabsState = localStorage.getItem("tabs-state"); json = JSON.parse(tabsState || "{}"); json[$(e.target).parents("ul.nav.nav-pills, ul.nav.nav-tabs").attr("id")] = $(e.target).data('target'); localStorage.setItem("tabs-state", JSON.stringify(json)); }); tabsState = localStorage.getItem("tabs-state"); json = JSON.parse(tabsState || "{}"); $.each(json, function(containerId, target) { return $("#" + containerId + " a[data-target=" + target + "]").tab('show'); }); $("ul.nav.nav-pills, ul.nav.nav-tabs").each(function() { var $this = $(this); if (!json[$this.attr("id")]) { return $this.find("a[data-toggle=tab]:first, a[data-toggle=pill]:first").tab("show"); } }); }); // doc.ready })(jQuery);
source share