|
| 1 | +/*========================================== |
| 2 | +======= Master reveal.js ======== |
| 3 | +============================================ |
| 4 | +=== This file contains the JS for === |
| 5 | +=== the Runestone reval component. === |
| 6 | +============================================ |
| 7 | +=== Created by === |
| 8 | +=== Isaiah Mayerchak === |
| 9 | +=== 06/12/15 === |
| 10 | +==========================================*/ |
| 11 | +var RevealList = {}; // Dictionary that contains all instances of Reveal objects |
| 12 | + |
| 13 | +Reveal.prototype = new RunestoneBase(); |
| 14 | + |
| 15 | +// Define Reveal object |
| 16 | +function Reveal (opts) { |
| 17 | + if (opts) { |
| 18 | + this.init(opts); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/*====================================== |
| 23 | +== Initialize basic Reveal attributes == |
| 24 | +========================================*/ |
| 25 | +Reveal.prototype.init = function (opts) { |
| 26 | + RunestoneBase.apply(this, arguments); |
| 27 | + var orig = opts.orig; // entire <div> element that will be replaced by new HTML |
| 28 | + this.origElem = orig; |
| 29 | + this.divid = orig.id; |
| 30 | + this.dataModal = false; // is a model dialog vs. inline |
| 31 | + if ($(this.origElem).is("[data-modal]")) { |
| 32 | + this.dataModal = true; |
| 33 | + } |
| 34 | + this.modalTitle = null; |
| 35 | + this.showtitle = null; // Title of button that shows the concealed data |
| 36 | + this.hidetitle = null; |
| 37 | + this.origContent = $(this.origElem).html(); |
| 38 | + this.children = []; |
| 39 | + this.adoptChildren(); |
| 40 | + |
| 41 | + this.checkForTitle(); |
| 42 | + |
| 43 | + this.getButtonTitles(); |
| 44 | + this.createShowButton(); |
| 45 | + |
| 46 | + if (!this.dataModal) { |
| 47 | + this.createHideButton(); // Hide button is already implemented in modal dialog |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +/*==================================== |
| 52 | +== Get text for buttons/modal title == |
| 53 | +====================================*/ |
| 54 | + |
| 55 | +Reveal.prototype.adoptChildren = function () { |
| 56 | + for (var i = 0; i < this.origElem.childNodes.length; i++) { |
| 57 | + this.children.push(this.origElem.childNodes[i]); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +Reveal.prototype.getButtonTitles = function () { // to support old functionality |
| 62 | + this.showtitle = $(this.origElem).data("showtitle"); |
| 63 | + if (this.showtitle === undefined) { |
| 64 | + this.showtitle = "Show"; // default |
| 65 | + } |
| 66 | + this.hidetitle = $(this.origElem).data("hidetitle"); |
| 67 | + if (this.hidetitle === undefined) { |
| 68 | + this.hidetitle = "Hide"; // default |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +Reveal.prototype.checkForTitle = function () { |
| 73 | + this.modalTitle = $(this.origElem).data("title"); |
| 74 | + if (this.modalTitle === undefined) { |
| 75 | + this.modalTitle = "Message from the author"; // default |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +/*============================ |
| 80 | +== Create show/hide buttons == |
| 81 | +============================*/ |
| 82 | +Reveal.prototype.createShowButton = function () { |
| 83 | + var _this = this; |
| 84 | + this.wrapDiv = document.createElement("div"); // wrapper div |
| 85 | + if (!this.dataModal) { |
| 86 | + this.revealDiv = document.createElement("div"); // Div that is hidden that contains content |
| 87 | + this.revealDiv.id = this.divid; |
| 88 | + |
| 89 | + // Get original content, put it inside revealDiv and replace original div with revealDiv |
| 90 | + //$(this.revealDiv).html(this.origContent); |
| 91 | + |
| 92 | + for (var i = 0; i < this.children.length; i++) { |
| 93 | + this.revealDiv.appendChild(this.children[i]); |
| 94 | + } |
| 95 | + $(this.revealDiv).hide(); |
| 96 | + this.wrapDiv.appendChild(this.revealDiv); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + this.sbutt = document.createElement("button"); |
| 101 | + $(this.sbutt).addClass("btn btn-default reveal_button"); |
| 102 | + $(this.sbutt).css("margin-bottom","10px"); |
| 103 | + this.sbutt.textContent = this.showtitle; |
| 104 | + this.sbutt.id = this.divid + "_show"; |
| 105 | + if (!this.dataModal) { |
| 106 | + this.sbutt.onclick = function () { |
| 107 | + _this.showInline(); |
| 108 | + $(this).hide(); |
| 109 | + }; |
| 110 | + } else { |
| 111 | + this.createModal(); |
| 112 | + $(this.sbutt).attr({"data-toggle":"modal", |
| 113 | + "data-target":"#" + this.divid + "_modal"}); |
| 114 | + } |
| 115 | + this.wrapDiv.appendChild(this.sbutt); |
| 116 | + $(this.origElem).replaceWith(this.wrapDiv); |
| 117 | +}; |
| 118 | + |
| 119 | +Reveal.prototype.createHideButton = function () { |
| 120 | + var _this = this; |
| 121 | + this.hbutt = document.createElement("button"); |
| 122 | + $(this.hbutt).hide(); |
| 123 | + this.hbutt.textContent = this.hidetitle; |
| 124 | + this.hbutt.className = "btn btn-default reveal_button"; |
| 125 | + $(this.hbutt).css("margin-bottom","10px"); |
| 126 | + |
| 127 | + this.hbutt.id = this.divid + "_hide"; |
| 128 | + this.hbutt.onclick = function () { |
| 129 | + _this.hideInline(); |
| 130 | + $(this).hide(); |
| 131 | + }; |
| 132 | + this.wrapDiv.appendChild(this.hbutt); |
| 133 | + |
| 134 | +}; |
| 135 | + |
| 136 | +/*================= |
| 137 | +=== Modal logic === |
| 138 | +=================*/ |
| 139 | +Reveal.prototype.createModal = function () { // Displays popup dialog modal window |
| 140 | + this.modalContainerDiv = document.createElement("div"); |
| 141 | + $(this.modalContainerDiv).addClass("modal fade"); |
| 142 | + this.modalContainerDiv.id = this.divid + "_modal"; |
| 143 | + $(this.modalContainerDiv).attr("role", "dialog"); |
| 144 | + document.body.appendChild(this.modalContainerDiv); |
| 145 | + |
| 146 | + this.modalDialogDiv = document.createElement("div"); |
| 147 | + $(this.modalDialogDiv).addClass("modal-dialog compare-modal"); |
| 148 | + this.modalContainerDiv.appendChild(this.modalDialogDiv); |
| 149 | + |
| 150 | + this.modalContentDiv = document.createElement("div"); |
| 151 | + $(this.modalContentDiv).addClass("modal-content"); |
| 152 | + this.modalDialogDiv.appendChild(this.modalContentDiv); |
| 153 | + |
| 154 | + this.modalHeaderDiv = document.createElement("div"); |
| 155 | + $(this.modalHeaderDiv).addClass("modal-header"); |
| 156 | + this.modalContentDiv.appendChild(this.modalHeaderDiv); |
| 157 | + |
| 158 | + this.modalButton = document.createElement("button"); |
| 159 | + this.modalButton.type = "button"; |
| 160 | + $(this.modalButton).addClass("close"); |
| 161 | + $(this.modalButton).attr({"aria-hidden":"true", |
| 162 | + "data-dismiss":"modal"}); |
| 163 | + this.modalButton.innerHTML = "×"; |
| 164 | + this.modalHeaderDiv.appendChild(this.modalButton); |
| 165 | + |
| 166 | + this.modalTitleE = document.createElement("h4"); |
| 167 | + $(this.modalTitleE).addClass("modal-title"); |
| 168 | + this.modalTitleE.innerHTML = this.modalTitle; |
| 169 | + this.modalHeaderDiv.appendChild(this.modalTitleE); |
| 170 | + |
| 171 | + this.modalBody = document.createElement("div"); |
| 172 | + $(this.modalBody).addClass("modal-body"); |
| 173 | + for (var i = 0; i < this.children.length; i++) { |
| 174 | + this.modalBody.appendChild(this.children[i]); |
| 175 | + } |
| 176 | + this.modalContentDiv.appendChild(this.modalBody); |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + /*var html = "<div class='modal fade'>" + |
| 182 | + " <div class='modal-dialog compare-modal'>" + |
| 183 | + " <div class='modal-content'>" + |
| 184 | + " <div class='modal-header'>" + |
| 185 | + " <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button>" + |
| 186 | + " <h4 class='modal-title'>" + this.modalTitle + "</h4>" + |
| 187 | + " </div>" + |
| 188 | + " <div class='modal-body'>" + |
| 189 | + this.origContent + |
| 190 | + " </div>" + |
| 191 | + " </div>" + |
| 192 | + " </div>" + |
| 193 | + "</div>";*/ |
| 194 | + //var el = $(this.modalContainerDiv); |
| 195 | + //el.modal(); |
| 196 | +}; |
| 197 | + |
| 198 | +/*================== |
| 199 | +=== Inline logic === |
| 200 | +==================*/ |
| 201 | +Reveal.prototype.showInline = function () { // Displays inline version of reveal |
| 202 | + $(this.revealDiv).show(); |
| 203 | + $(this.hbutt).show(); |
| 204 | + $(this.revealDiv).find(".CodeMirror").each(function (i, el) {el.CodeMirror.refresh(); }); |
| 205 | + |
| 206 | +}; |
| 207 | + |
| 208 | +Reveal.prototype.hideInline = function () { |
| 209 | + $(this.revealDiv).hide(); |
| 210 | + $(this.sbutt).show(); |
| 211 | +}; |
| 212 | + |
| 213 | +/*================================= |
| 214 | +== Find the custom HTML tags and == |
| 215 | +== execute our code on them == |
| 216 | +=================================*/ |
| 217 | +$(document).ready(function () { |
| 218 | + $("[data-component=reveal]").each(function (index) { |
| 219 | + RevealList[this.id] = new Reveal({"orig": this}); |
| 220 | + }); |
| 221 | +}); |
0 commit comments