|
| 1 | +/*========================================== |
| 2 | +======= Master tabbedstuff.js ======== |
| 3 | +============================================ |
| 4 | +=== This file contains the JS for === |
| 5 | +=== the Runestone tabbedStuff component. === |
| 6 | +============================================ |
| 7 | +=== Created by === |
| 8 | +=== Isaiah Mayerchak === |
| 9 | +=== 06/15/15 === |
| 10 | +=== Brad Miller === |
| 11 | +=== 06/15/15 === |
| 12 | +==========================================*/ |
| 13 | +"use strict"; |
| 14 | + |
| 15 | +var TSList = {}; // Dictionary that contains all instances of TabbedStuff objects |
| 16 | + |
| 17 | +import RunestoneBase from "../../common/js/runestonebase"; |
| 18 | +import "../css/tabbedstuff.css"; |
| 19 | + |
| 20 | +// Define TabbedStuff object |
| 21 | +class TabbedStuff extends RunestoneBase { |
| 22 | + constructor(opts) { |
| 23 | + super(opts); |
| 24 | + var orig = opts.orig; |
| 25 | + this.origElem = orig; // entire original <div> element that will be replaced by new HTML |
| 26 | + this.divid = orig.id; |
| 27 | + this.inactive = false; |
| 28 | + if ($(this.origElem).is("[data-inactive]")) { |
| 29 | + this.inactive = true; |
| 30 | + } |
| 31 | + this.togglesList = []; // For use in Codemirror/Disqus |
| 32 | + this.childTabs = []; |
| 33 | + this.populateChildTabs(); |
| 34 | + this.activeTab = 0; // default value--activeTab is the index of the tab that starts open |
| 35 | + this.findActiveTab(); |
| 36 | + this.createTabContainer(); |
| 37 | + this.indicate_component_ready(); |
| 38 | + } |
| 39 | + /*=========================================== |
| 40 | + == Update attributes of instance variables == |
| 41 | + == variables according to specifications == |
| 42 | + ===========================================*/ |
| 43 | + populateChildTabs() { |
| 44 | + for (var i = 0; i < this.origElem.childNodes.length; i++) { |
| 45 | + if ($(this.origElem.childNodes[i]).data("component") === "tab") { |
| 46 | + this.childTabs.push(this.origElem.childNodes[i]); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + findActiveTab() { |
| 51 | + for (var i = 0; i < this.childTabs.length; i++) { |
| 52 | + if ($(this.childTabs[i]).is("[data-active]")) { |
| 53 | + this.activeTab = i; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + /*========================================== |
| 58 | + == Creating/appending final HTML elements == |
| 59 | + ==========================================*/ |
| 60 | + createTabContainer() { |
| 61 | + this.containerDiv = document.createElement("div"); |
| 62 | + this.containerDiv.id = this.divid; |
| 63 | + $(this.containerDiv).addClass(this.origElem.getAttribute("class")); |
| 64 | + $(this.containerDiv).attr({ role: "tabpanel" }); |
| 65 | + this.tabsUL = document.createElement("ul"); |
| 66 | + this.tabsUL.id = this.divid + "_tab"; |
| 67 | + $(this.tabsUL).addClass("nav nav-tabs"); |
| 68 | + $(this.tabsUL).attr({ role: "tablist" }); |
| 69 | + this.tabContentDiv = document.createElement("div"); // Create tab content container that holds tab panes w/content |
| 70 | + $(this.tabContentDiv).addClass("tab-content"); |
| 71 | + this.createTabs(); // create and append tabs to the <ul> |
| 72 | + this.containerDiv.appendChild(this.tabsUL); |
| 73 | + this.containerDiv.appendChild(this.tabContentDiv); |
| 74 | + this.addCMD(); // Adds fuctionality for Codemirror/Disqus |
| 75 | + $(this.origElem).replaceWith(this.containerDiv); |
| 76 | + } |
| 77 | + createTabs() { |
| 78 | + // Create tabs in format <li><a><span></span></a></li> to be appended to the <ul> |
| 79 | + for (var i = 0; i < this.childTabs.length; i++) { |
| 80 | + // First create tabname and tabfriendly name that has no spaces to be used for the id |
| 81 | + var tabListElement = document.createElement("li"); |
| 82 | + $(tabListElement).attr({ |
| 83 | + role: "presentation", |
| 84 | + "aria-controls": this.divid + "-" + i |
| 85 | + }); |
| 86 | + // Using bootstrap tabs functionality |
| 87 | + var tabElement = document.createElement("a"); |
| 88 | + $(tabElement).attr({ |
| 89 | + "data-toggle": "tab", |
| 90 | + href: "#" + this.divid + "-" + i, |
| 91 | + role: "tab" |
| 92 | + }); |
| 93 | + var tabTitle = document.createElement("span"); // Title of tab--what the user will see |
| 94 | + tabTitle.textContent = $(this.childTabs[i]).data("tabname"); |
| 95 | + tabElement.appendChild(tabTitle); |
| 96 | + tabListElement.appendChild(tabElement); |
| 97 | + this.tabsUL.appendChild(tabListElement); |
| 98 | + // tabPane is what holds the contents of the tab |
| 99 | + var tabPaneDiv = document.createElement("div"); |
| 100 | + tabPaneDiv.id = this.divid + "-" + i; |
| 101 | + $(tabPaneDiv).addClass("tab-pane"); |
| 102 | + $(tabPaneDiv).attr({ |
| 103 | + role: "tabpanel" |
| 104 | + }); |
| 105 | + //var tabHTML = $(this.childTabs[i]).html(); |
| 106 | + //$(tabPaneDiv).html(tabHTML); |
| 107 | + tabPaneDiv.appendChild(this.childTabs[i]); |
| 108 | + if (!this.inactive) { |
| 109 | + if (this.activeTab === i) { |
| 110 | + $(tabListElement).addClass("active"); |
| 111 | + $(tabPaneDiv).addClass("active"); |
| 112 | + } |
| 113 | + } |
| 114 | + this.togglesList.push(tabElement); |
| 115 | + this.tabContentDiv.appendChild(tabPaneDiv); |
| 116 | + } |
| 117 | + } |
| 118 | + /*=================================== |
| 119 | + == Codemirror/Disqus functionality == |
| 120 | + ===================================*/ |
| 121 | + addCMD() { |
| 122 | + $(this.togglesList).on("shown.bs.tab", function(e) { |
| 123 | + var content_div = $(e.target.attributes.href.value); |
| 124 | + content_div.find(".disqus_thread_link").each(function() { |
| 125 | + $(this).click(); |
| 126 | + }); |
| 127 | + content_div.find(".CodeMirror").each(function(i, el) { |
| 128 | + el.CodeMirror.refresh(); |
| 129 | + }); |
| 130 | + }); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/*================================= |
| 135 | +== Find the custom HTML tags and == |
| 136 | +== execute our code on them == |
| 137 | +=================================*/ |
| 138 | +$("load", function() { |
| 139 | + $("[data-component=tabbedStuff]").each(function(index) { |
| 140 | + TSList[this.id] = new TabbedStuff({ orig: this }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments