| 1 | // For osh-to-oil.html.
 | 
| 2 | 
 | 
| 3 | 'use strict';
 | 
| 4 | 
 | 
| 5 | // Append a message to an element.  Used for errors.
 | 
| 6 | function appendMessage(elem, msg) {
 | 
| 7 |   elem.innerHTML += msg + '<br />';
 | 
| 8 | }
 | 
| 9 | 
 | 
| 10 | // jQuery-like AJAX helper, but simpler.
 | 
| 11 | 
 | 
| 12 | // Requires an element with id "status" to show errors.
 | 
| 13 | //
 | 
| 14 | // Args:
 | 
| 15 | //   errElem: optional element to append error messages to.  If null, then
 | 
| 16 | //     alert() on error.
 | 
| 17 | //   success: callback that is passed the xhr object.
 | 
| 18 | function ajaxGet(url, errElem, success) {
 | 
| 19 |   var xhr = new XMLHttpRequest();
 | 
| 20 |   xhr.open('GET', url, true /*async*/);
 | 
| 21 |   xhr.onreadystatechange = function() {
 | 
| 22 |     if (xhr.readyState != 4 /*DONE*/) {
 | 
| 23 |       return;
 | 
| 24 |     }
 | 
| 25 | 
 | 
| 26 |     if (xhr.status != 200) {
 | 
| 27 |       var msg = 'ERROR requesting ' + url + ': ' + xhr.status + ' ' +
 | 
| 28 |                 xhr.statusText;
 | 
| 29 |       if (errElem) {
 | 
| 30 |         appendMessage(errElem, msg);
 | 
| 31 |       } else {
 | 
| 32 |         alert(msg);
 | 
| 33 |       }
 | 
| 34 |       return;
 | 
| 35 |     }
 | 
| 36 | 
 | 
| 37 |     success(xhr);
 | 
| 38 |   };
 | 
| 39 |   xhr.send();
 | 
| 40 | }
 | 
| 41 | 
 | 
| 42 | // Fill in title and iframe src attributes.
 | 
| 43 | function loadSource(sourceName, statusElem) {
 | 
| 44 |   document.getElementById('title').innerHTML = sourceName;
 | 
| 45 | 
 | 
| 46 |   document.getElementById('orig').src = sourceName + '.txt';
 | 
| 47 |   document.getElementById('oil').src = sourceName + '__oil.txt';
 | 
| 48 |   document.getElementById('ast').src = sourceName + '__ast.html';
 | 
| 49 | 
 | 
| 50 |   document.getElementById('link-orig').href = sourceName + '.txt';
 | 
| 51 |   document.getElementById('link-oil').href = sourceName + '__oil.txt';
 | 
| 52 |   document.getElementById('link-ast').href = sourceName + '__ast.html';
 | 
| 53 | 
 | 
| 54 |   // NOTE: There is no error checking here.  They will just see a 404 in the
 | 
| 55 |   // iframe pane.
 | 
| 56 |   // http://stackoverflow.com/questions/16499117/how-to-detect-an-error-404-in-an-iframe
 | 
| 57 |   //appendMessage(statusElem, "Loaded contents for " + sourceName);
 | 
| 58 | }
 | 
| 59 | 
 | 
| 60 | function getNameFromHash(urlHash, statusElem) {
 | 
| 61 |   var h = urlHash.substring(1);  // without leading #
 | 
| 62 |   if (h.length < 1) {
 | 
| 63 |     appendMessage(statusElem, "Invalid URL hash: [" + urlHash + "]");
 | 
| 64 |     return null;
 | 
| 65 |   }
 | 
| 66 |   return h;
 | 
| 67 | }
 | 
| 68 | 
 | 
| 69 | function onLoad(urlHash, globals, statusElem) {
 | 
| 70 |   onHashChange(urlHash, globals, statusElem);
 | 
| 71 | }
 | 
| 72 | 
 | 
| 73 | // This is the onhashchange handler.
 | 
| 74 | function onHashChange(urlHash, globals, statusElem) {
 | 
| 75 |   var sourceName = getNameFromHash(urlHash, statusElem);
 | 
| 76 |   if (sourceName === null) return;
 | 
| 77 | 
 | 
| 78 |   loadSource(sourceName, statusElem)
 | 
| 79 | }
 |