|
| 1 | + |
| 2 | +// This JavaScript is entended to work with all supported |
| 3 | +// browsers. Some polyfills may be needed. |
| 4 | +// |
| 5 | +// See https://getbootstrap.com/docs/4.0/getting-started/browsers-devices/ |
| 6 | +// for the supported browser list |
| 7 | + |
| 8 | +let DS_EG = (function(){ |
| 9 | + // globals |
| 10 | + // |
| 11 | + // NotifyJS -- see https://notifyjs.jpillora.com/ |
| 12 | + const notify_info_t = { className:"info", globalPosition: "top center" } |
| 13 | + , notify_warning_t = { className:"warn", globalPosition: "top center" } |
| 14 | + ; |
| 15 | + let library = {}; |
| 16 | + |
| 17 | + // See http://jsfiddle.net/unLSJ/ |
| 18 | + // USAGE: $(el).html(library.json.prettyPrint(json_obj)); |
| 19 | + // where el is <pre><code> el |
| 20 | + // or |
| 21 | + // $(el).html(library.json.prettyPrint2(json_obj)); |
| 22 | + // where el is any el (<pre><code> will be added) |
| 23 | + library.json = { |
| 24 | + replacer: function(match, pIndent, pKey, pVal, pEnd) { |
| 25 | + var key = '<span class=json-key>'; |
| 26 | + var val = '<span class=json-value>'; |
| 27 | + var str = '<span class=json-string>'; |
| 28 | + var r = pIndent || ''; |
| 29 | + if (pKey) |
| 30 | + r = r + key + pKey.replace(/[": ]/g, '') + '</span>: '; |
| 31 | + if (pVal) |
| 32 | + r = r + (pVal[0] == '"' ? str : val) + pVal + '</span>'; |
| 33 | + return r + (pEnd || ''); |
| 34 | + }, |
| 35 | + prettyPrint: function(obj) { |
| 36 | + var jsonLine = /^( *)("[\w]+": )?("[^"]*"|[\w.+-]*)?([,[{]|\[],|\{},|\[]|\{})?$/mg; |
| 37 | + return JSON.stringify(obj, null, 3) |
| 38 | + .replace(/&/g, '&').replace(/\\"/g, '"') |
| 39 | + .replace(/</g, '<').replace(/>/g, '>') |
| 40 | + .replace(jsonLine, library.json.replacer); |
| 41 | + }, |
| 42 | + prettyPrint2: function(obj) { |
| 43 | + var jsonLine = /^( *)("[\w]+": )?("[^"]*"|[\w.+-]*)?([,[{])?$/mg |
| 44 | + , out = JSON.stringify(obj, null, 3) |
| 45 | + .replace(/&/g, '&').replace(/\\"/g, '"') |
| 46 | + .replace(/</g, '<').replace(/>/g, '>') |
| 47 | + .replace(jsonLine, library.json.replacer); |
| 48 | + return '<pre><code>' + out + '</code></pre>'; |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + // Add on_click handlers to elements with data-busy attribute |
| 55 | + function augment_busy(){ |
| 56 | + $('a[data-busy="href"]').click(busy_href); |
| 57 | + $('form[data-busy="form"]').submit(busy_form); |
| 58 | + $('form[data-busy="form-download"]').submit(busy_form_download); |
| 59 | + } |
| 60 | + |
| 61 | + // Process flash messages from the server |
| 62 | + function process_server_flash_msgs(){ |
| 63 | + let flash_msg_raw = $("#server_data").attr("data-server-data") |
| 64 | + , flash_msg_json = flash_msg_raw ? JSON.parse(flash_msg_raw) : false |
| 65 | + , flash_msg_info = (flash_msg_json && flash_msg_json.flash && |
| 66 | + flash_msg_json.flash.info) |
| 67 | + ; |
| 68 | + _.forEach(flash_msg_info, function (msg){ |
| 69 | + $.notify(msg, notify_info_t); |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + // process json display |
| 74 | + function process_json_display() { |
| 75 | + let json_raw = $("#server_json_data").attr("data-server-json-data") |
| 76 | + , json_raw2 = json_raw ? JSON.parse(json_raw) : false |
| 77 | + , json_raw3 = json_raw2 && json_raw2.json |
| 78 | + , json = JSON.parse(json_raw3) |
| 79 | + ; |
| 80 | + |
| 81 | + if (json) { |
| 82 | + $('#json-display').html(library.json.prettyPrint(json)) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Handles clicks for elements with attribute data-busy="href" |
| 87 | + // 1. Make global feedback and busy indicators visible |
| 88 | + // 2. Change location to the element's href value |
| 89 | + let busy_href = function _busy_href(e){ |
| 90 | + e.preventDefault(); |
| 91 | + $("#feedback,#busy").show(); |
| 92 | + $("#content").hide(); |
| 93 | + const href = $(e.target).attr("href"); |
| 94 | + window.location = href; |
| 95 | + } |
| 96 | + |
| 97 | + let countdownInfo = { // using an object since it's a pointer |
| 98 | + countdown: null, // Should countdown continue? |
| 99 | + intervalId: null // id of the count down interval |
| 100 | + } |
| 101 | + // When starting a download request, how long should the spinner display? |
| 102 | + , downloadSpinnerMS = 3000; |
| 103 | + |
| 104 | + let busy_form = function _busy_form(e){ |
| 105 | + e.preventDefault(); |
| 106 | + $("#feedback,#busy").show(); |
| 107 | + $("#content").hide(); |
| 108 | + const form = $(e.target); |
| 109 | + form.get(0).submit(); |
| 110 | + countdownInfo.countdown = true; |
| 111 | + doCountdown(); |
| 112 | + } |
| 113 | + |
| 114 | + let busy_form_download = function _busy_form_download(e){ |
| 115 | + e.preventDefault(); |
| 116 | + $("#feedback,#busy").show(); |
| 117 | + $("#content").hide(); |
| 118 | + const form = $(e.target); |
| 119 | + form.get(0).submit(); |
| 120 | + countdownInfo.countdown = true; |
| 121 | + doCountdown(); |
| 122 | + let stopCount = info => { |
| 123 | + info.countdown = false; |
| 124 | + clearInterval(info.intervalId); |
| 125 | + $('#feedback h3 span').text("your download will start soon."); |
| 126 | + $("#busy").hide(); |
| 127 | + $("#download-continue").show(); |
| 128 | + } |
| 129 | + setTimeout(stopCount, downloadSpinnerMS, countdownInfo); |
| 130 | + } |
| 131 | + |
| 132 | + function doCountdown() { |
| 133 | + let value = 200 |
| 134 | + , timerMS = 300 |
| 135 | + , el = $('#feedback h3 span') |
| 136 | + , show = () => {if (countdownInfo.countdown) {el.text(value)} value -= 1} |
| 137 | + ; |
| 138 | + |
| 139 | + show(); |
| 140 | + countdownInfo.intervalId = setInterval( show, timerMS ); |
| 141 | + } |
| 142 | + |
| 143 | + let start_up = function(){ |
| 144 | + augment_busy(); |
| 145 | + process_server_flash_msgs(); |
| 146 | + process_json_display(); |
| 147 | + } |
| 148 | + |
| 149 | + function notify_info (msg) { |
| 150 | + $.notify(msg, notify_info_t); |
| 151 | + } |
| 152 | + function notify_warning (msg) { |
| 153 | + $.notify(msg, notify_warning_t); |
| 154 | + } |
| 155 | + |
| 156 | +//////////////////////////////////////////////////////////////////////////// |
| 157 | +//////////////////////////////////////////////////////////////////////////// |
| 158 | +//////////////////////////////////////////////////////////////////////////// |
| 159 | + |
| 160 | +// Return the publicly exposed items |
| 161 | + return { |
| 162 | + start_up: start_up |
| 163 | + } |
| 164 | +})(); |
| 165 | + |
| 166 | + |
| 167 | +// Main stem |
| 168 | +$( document ).ready(function() { |
| 169 | + DS_EG.start_up(); |
| 170 | +}); |
0 commit comments