|
| 1 | +// Leftover code for annotation bubbles deleted from ../pytutor.ts |
| 2 | + |
| 3 | +// Annotation bubbles |
| 4 | + |
| 5 | +var qtipShared = { |
| 6 | + show: { |
| 7 | + ready: true, // show on document.ready instead of on mouseenter |
| 8 | + delay: 0, |
| 9 | + event: null, |
| 10 | + effect: function() {$(this).show();}, // don't do any fancy fading because it screws up with scrolling |
| 11 | + }, |
| 12 | + hide: { |
| 13 | + fixed: true, |
| 14 | + event: null, |
| 15 | + effect: function() {$(this).hide();}, // don't do any fancy fading because it screws up with scrolling |
| 16 | + }, |
| 17 | + style: { |
| 18 | + classes: 'ui-tooltip-pgbootstrap', // my own customized version of the bootstrap style |
| 19 | + }, |
| 20 | +}; |
| 21 | + |
| 22 | + |
| 23 | +// a speech bubble annotation to attach to: |
| 24 | +// 'codeline' - a line of code |
| 25 | +// 'frame' - a stack frame |
| 26 | +// 'variable' - a variable within a stack frame |
| 27 | +// 'object' - an object on the heap |
| 28 | +// (as determined by the 'type' param) |
| 29 | +// |
| 30 | +// domID is the ID of the element to attach to (without the leading '#' sign) |
| 31 | +function AnnotationBubble(parentViz, type, domID) { |
| 32 | + this.parentViz = parentViz; |
| 33 | + |
| 34 | + this.domID = domID; |
| 35 | + this.hashID = '#' + domID; |
| 36 | + |
| 37 | + this.type = type; |
| 38 | + |
| 39 | + if (type == 'codeline') { |
| 40 | + this.my = 'left center'; |
| 41 | + this.at = 'right center'; |
| 42 | + } |
| 43 | + else if (type == 'frame') { |
| 44 | + this.my = 'right center'; |
| 45 | + this.at = 'left center'; |
| 46 | + } |
| 47 | + else if (type == 'variable') { |
| 48 | + this.my = 'right center'; |
| 49 | + this.at = 'left center'; |
| 50 | + } |
| 51 | + else if (type == 'object') { |
| 52 | + this.my = 'bottom left'; |
| 53 | + this.at = 'top center'; |
| 54 | + } |
| 55 | + else { |
| 56 | + assert(false); |
| 57 | + } |
| 58 | + |
| 59 | + // possible states: |
| 60 | + // 'invisible' |
| 61 | + // 'edit' |
| 62 | + // 'view' |
| 63 | + // 'minimized' |
| 64 | + // 'stub' |
| 65 | + this.state = 'invisible'; |
| 66 | + |
| 67 | + this.text = ''; // the actual contents of the annotation bubble |
| 68 | + |
| 69 | + this.qtipHidden = false; // is there a qtip object present but hidden? (TODO: kinda confusing) |
| 70 | +} |
| 71 | + |
| 72 | +AnnotationBubble.prototype.showStub = function() { |
| 73 | + assert(this.state == 'invisible' || this.state == 'edit'); |
| 74 | + assert(this.text == ''); |
| 75 | + |
| 76 | + var myBubble = this; // to avoid name clashes with 'this' in inner scopes |
| 77 | + |
| 78 | + // destroy then create a new tip: |
| 79 | + this.destroyQTip(); |
| 80 | + $(this.hashID).qtip($.extend({}, qtipShared, { |
| 81 | + content: ' ', |
| 82 | + id: this.domID, |
| 83 | + position: { |
| 84 | + my: this.my, |
| 85 | + at: this.at, |
| 86 | + adjust: { |
| 87 | + x: (myBubble.type == 'codeline' ? -6 : 0), // shift codeline tips over a bit for aesthetics |
| 88 | + }, |
| 89 | + effect: null, // disable all cutesy animations |
| 90 | + }, |
| 91 | + style: { |
| 92 | + classes: 'ui-tooltip-pgbootstrap ui-tooltip-pgbootstrap-stub' |
| 93 | + } |
| 94 | + })); |
| 95 | + |
| 96 | + |
| 97 | + $(this.qTipID()) |
| 98 | + .unbind('click') // unbind all old handlers |
| 99 | + .click(function() { |
| 100 | + myBubble.showEditor(); |
| 101 | + }); |
| 102 | + |
| 103 | + this.state = 'stub'; |
| 104 | +} |
| 105 | + |
| 106 | +AnnotationBubble.prototype.showEditor = function() { |
| 107 | + assert(this.state == 'stub' || this.state == 'view' || this.state == 'minimized'); |
| 108 | + |
| 109 | + var myBubble = this; // to avoid name clashes with 'this' in inner scopes |
| 110 | + |
| 111 | + var ta = '<textarea class="bubbleInputText">' + this.text + '</textarea>'; |
| 112 | + |
| 113 | + // destroy then create a new tip: |
| 114 | + this.destroyQTip(); |
| 115 | + $(this.hashID).qtip($.extend({}, qtipShared, { |
| 116 | + content: ta, |
| 117 | + id: this.domID, |
| 118 | + position: { |
| 119 | + my: this.my, |
| 120 | + at: this.at, |
| 121 | + adjust: { |
| 122 | + x: (myBubble.type == 'codeline' ? -6 : 0), // shift codeline tips over a bit for aesthetics |
| 123 | + }, |
| 124 | + effect: null, // disable all cutesy animations |
| 125 | + } |
| 126 | + })); |
| 127 | + |
| 128 | + |
| 129 | + $(this.qTipContentID()).find('textarea.bubbleInputText') |
| 130 | + // set handler when the textarea loses focus |
| 131 | + .blur(function() { |
| 132 | + myBubble.text = $(this).val().trim(); // strip all leading and trailing spaces |
| 133 | + |
| 134 | + if (myBubble.text) { |
| 135 | + myBubble.showViewer(); |
| 136 | + } |
| 137 | + else { |
| 138 | + myBubble.showStub(); |
| 139 | + } |
| 140 | + }) |
| 141 | + .focus(); // grab focus so that the user can start typing right away! |
| 142 | + |
| 143 | + this.state = 'edit'; |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +AnnotationBubble.prototype.bindViewerClickHandler = function() { |
| 148 | + var myBubble = this; |
| 149 | + |
| 150 | + $(this.qTipID()) |
| 151 | + .unbind('click') // unbind all old handlers |
| 152 | + .click(function() { |
| 153 | + if (myBubble.parentViz.editAnnotationMode) { |
| 154 | + myBubble.showEditor(); |
| 155 | + } |
| 156 | + else { |
| 157 | + myBubble.minimizeViewer(); |
| 158 | + } |
| 159 | + }); |
| 160 | +} |
| 161 | + |
| 162 | +AnnotationBubble.prototype.showViewer = function() { |
| 163 | + assert(this.state == 'edit' || this.state == 'invisible'); |
| 164 | + assert(this.text); // must be non-empty! |
| 165 | + |
| 166 | + var myBubble = this; |
| 167 | + // destroy then create a new tip: |
| 168 | + this.destroyQTip(); |
| 169 | + $(this.hashID).qtip($.extend({}, qtipShared, { |
| 170 | + content: htmlsanitize(this.text), // help prevent HTML/JS injection attacks |
| 171 | + id: this.domID, |
| 172 | + position: { |
| 173 | + my: this.my, |
| 174 | + at: this.at, |
| 175 | + adjust: { |
| 176 | + x: (myBubble.type == 'codeline' ? -6 : 0), // shift codeline tips over a bit for aesthetics |
| 177 | + }, |
| 178 | + effect: null, // disable all cutesy animations |
| 179 | + } |
| 180 | + })); |
| 181 | + |
| 182 | + this.bindViewerClickHandler(); |
| 183 | + this.state = 'view'; |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | +AnnotationBubble.prototype.minimizeViewer = function() { |
| 188 | + assert(this.state == 'view'); |
| 189 | + |
| 190 | + var myBubble = this; |
| 191 | + |
| 192 | + $(this.hashID).qtip('option', 'content.text', ' '); //hack to "minimize" its size |
| 193 | + |
| 194 | + $(this.qTipID()) |
| 195 | + .unbind('click') // unbind all old handlers |
| 196 | + .click(function() { |
| 197 | + if (myBubble.parentViz.editAnnotationMode) { |
| 198 | + myBubble.showEditor(); |
| 199 | + } |
| 200 | + else { |
| 201 | + myBubble.restoreViewer(); |
| 202 | + } |
| 203 | + }); |
| 204 | + |
| 205 | + this.state = 'minimized'; |
| 206 | +} |
| 207 | + |
| 208 | +AnnotationBubble.prototype.restoreViewer = function() { |
| 209 | + assert(this.state == 'minimized'); |
| 210 | + $(this.hashID).qtip('option', 'content.text', htmlsanitize(this.text)); // help prevent HTML/JS injection attacks |
| 211 | + this.bindViewerClickHandler(); |
| 212 | + this.state = 'view'; |
| 213 | +} |
| 214 | + |
| 215 | +// NB: actually DESTROYS the QTip object |
| 216 | +AnnotationBubble.prototype.makeInvisible = function() { |
| 217 | + assert(this.state == 'stub' || this.state == 'edit'); |
| 218 | + this.destroyQTip(); |
| 219 | + this.state = 'invisible'; |
| 220 | +} |
| 221 | + |
| 222 | + |
| 223 | +AnnotationBubble.prototype.destroyQTip = function() { |
| 224 | + $(this.hashID).qtip('destroy'); |
| 225 | +} |
| 226 | + |
| 227 | +AnnotationBubble.prototype.qTipContentID = function() { |
| 228 | + return '#ui-tooltip-' + this.domID + '-content'; |
| 229 | +} |
| 230 | + |
| 231 | +AnnotationBubble.prototype.qTipID = function() { |
| 232 | + return '#ui-tooltip-' + this.domID; |
| 233 | +} |
| 234 | + |
| 235 | + |
| 236 | +AnnotationBubble.prototype.enterEditMode = function() { |
| 237 | + assert(this.parentViz.editAnnotationMode); |
| 238 | + if (this.state == 'invisible') { |
| 239 | + this.showStub(); |
| 240 | + |
| 241 | + if (this.type == 'codeline') { |
| 242 | + this.redrawCodelineBubble(); |
| 243 | + } |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +AnnotationBubble.prototype.enterViewMode = function() { |
| 248 | + assert(!this.parentViz.editAnnotationMode); |
| 249 | + if (this.state == 'stub') { |
| 250 | + this.makeInvisible(); |
| 251 | + } |
| 252 | + else if (this.state == 'edit') { |
| 253 | + this.text = $(this.qTipContentID()).find('textarea.bubbleInputText').val().trim(); // strip all leading and trailing spaces |
| 254 | + |
| 255 | + if (this.text) { |
| 256 | + this.showViewer(); |
| 257 | + |
| 258 | + if (this.type == 'codeline') { |
| 259 | + this.redrawCodelineBubble(); |
| 260 | + } |
| 261 | + } |
| 262 | + else { |
| 263 | + this.makeInvisible(); |
| 264 | + } |
| 265 | + } |
| 266 | + else if (this.state == 'invisible') { |
| 267 | + // this happens when, say, you first enter View Mode |
| 268 | + if (this.text) { |
| 269 | + this.showViewer(); |
| 270 | + |
| 271 | + if (this.type == 'codeline') { |
| 272 | + this.redrawCodelineBubble(); |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | +} |
| 277 | + |
| 278 | +AnnotationBubble.prototype.preseedText = function(txt) { |
| 279 | + assert(this.state == 'invisible'); |
| 280 | + this.text = txt; |
| 281 | +} |
| 282 | + |
| 283 | +AnnotationBubble.prototype.redrawCodelineBubble = function() { |
| 284 | + assert(this.type == 'codeline'); |
| 285 | + |
| 286 | + if (isOutputLineVisibleForBubbles(this.domID)) { |
| 287 | + if (this.qtipHidden) { |
| 288 | + $(this.hashID).qtip('show'); |
| 289 | + } |
| 290 | + else { |
| 291 | + $(this.hashID).qtip('reposition'); |
| 292 | + } |
| 293 | + |
| 294 | + this.qtipHidden = false; |
| 295 | + } |
| 296 | + else { |
| 297 | + $(this.hashID).qtip('hide'); |
| 298 | + this.qtipHidden = true; |
| 299 | + } |
| 300 | +} |
| 301 | + |
| 302 | +AnnotationBubble.prototype.redrawBubble = function() { |
| 303 | + $(this.hashID).qtip('reposition'); |
| 304 | +} |
0 commit comments