notification bubble added for ajax errors, added dynamic ajax tmeo

This commit is contained in:
2016-04-01 13:55:23 +02:00
parent 7a6bdef334
commit 5376f1ba7b
21 changed files with 171 additions and 56 deletions
+6
View File
@@ -21,4 +21,10 @@ $().ready(function () {
}, 1000);
modal.init();
notify.init();
});
function errorMsg(msg) {
notify.show(msg, 3000);
}
+23 -10
View File
@@ -599,12 +599,20 @@
return cb;
};
// Basic XHR 1, no file support. Shakes fist at IE
cb.ajax = function (url, method, callback) {
cb.ajax = function (url, method, callback, options) {
var xhr,
query = serializeData(nodes),
type = (method) ? method.toUpperCase() : 'GET',
timestamp = '_ts=' + (+new Date());
var opts = Chartist.extend({}, {
nocache: true,
timeout: 5000,
loader: true,
}, options);
console.log('ajax to = '+opts.timeout);
if (query && (type === 'GET')) {
url += (url.indexOf('?') === -1) ? '?' + query : '&' + query;
query = null;
@@ -614,18 +622,23 @@
if (xhr) {
// prevent caching
url += (url.indexOf('?') === -1) ? '?' + timestamp : '&' + timestamp;
if (opts.nocache)
url += (url.indexOf('?') === -1) ? '?' + timestamp : '&' + timestamp;
$('#loader').addClass('show');
if (opts.loader)
$('#loader').addClass('show');
// Douglas Crockford: "Synchronous programming is disrespectful and should not be employed in applications which are used by people"
xhr.open(type, url, true);
xhr.timeout = 15000; // a default value. TODO way to set it for the caller
xhr.timeout = opts.timeout;
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
$('#loader').removeClass('show');
if (opts.loader)
$('#loader').removeClass('show');
if (callback && xhr.status != 0) { // xhr.status 0 means "aborted"
callback(xhr.responseText, xhr.status);
}
@@ -634,7 +647,7 @@
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
if (type === 'POST' || type === 'PUT') {
if (type === 'POST') {
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
@@ -644,12 +657,12 @@
return cb;
};
// Alias to cb.ajax(url, 'get', callback)
cb.get = function (url, callback) {
return cb.ajax(url, 'get', callback);
cb.get = function (url, callback, opts) {
return cb.ajax(url, 'get', callback, opts);
};
// Alias to cb.ajax(url, 'post', callback)
cb.post = function (url, callback) {
return cb.ajax(url, 'post', callback);
cb.post = function (url, callback, opts) {
return cb.ajax(url, 'post', callback, opts);
};
return cb;
+1
View File
@@ -21,6 +21,7 @@ var modal = (function () {
modal.init = function () {
// close modal by click outside the dialog
$('.Modal').on('click', function () {
if ($(this).hasClass('no-close')) return; // this is a no-close modal
modal.hide(this);
});
+29
View File
@@ -0,0 +1,29 @@
var notify = (function () {
var nt = {};
var sel = '#notif';
nt.show = function (message, timeout) {
$(sel).html(message);
modal.show(sel);
if (!_.isUndefined(timeout)) {
setTimeout(nt.hide, timeout);
}
};
nt.hide = function () {
var $m = $(sel);
$m.removeClass('visible');
setTimeout(function () {
$m.addClass('hidden');
}, 250); // transition time
};
nt.init = function() {
$(sel).on('click', function() {
nt.hide(this);
});
};
return nt;
})();
+34 -4
View File
@@ -1,14 +1,43 @@
var page_status = (function() {
var st = {};
st.j = {};
var updateTime = 10000;
var updateInhibited = false;
st.trigReset = function() {
var modal_sel = '#reset-modal';
$().get(_root + '/reset.cgi', function(resp, status) {
if (status == 200) {
modal.show(modal_sel);
updateInhibited = true;
var ping_i = setInterval(function() {
$().get(_root+'/ping.cgi', function(resp, code){
if (code == 200) {
// device is ready
modal.hide(modal_sel);
requestUpdate();
clearInterval(ping_i);
updateInhibited = false;
}
}, {timeout: 500});
}, 1000);
}
});
};
function onUpdate(resp, status) {
if (status != 200) {
// bad response
console.error('Update failed.');
errorMsg('Update failed.');
} else {
try {
// OK
var j = JSON.parse(resp);
st.j = j; // store for global access
$('.sta-only').toggle(j.sta);
$('.ap-only').toggle(j.ap);
@@ -38,11 +67,13 @@ var page_status = (function() {
}
// chip ID & macs don't change
} catch(e) {
console.error(e);
errorMsg(e);
}
}
setTimeout(requestUpdate, 10000);
if (!updateInhibited) {
setTimeout(requestUpdate, updateTime);
}
}
function requestUpdate() {
@@ -51,7 +82,6 @@ var page_status = (function() {
st.init = function() {
requestUpdate();
setTimeout(requestUpdate, 10000);
};
return st;
+11 -6
View File
@@ -137,7 +137,7 @@ var page_waveform = (function () {
readoutPending = false;
if (status != 200) {
console.error("Request failed.");
errorMsg("Request failed.");
if (autoReload)
toggleAutoReload(); // turn it off.
@@ -145,7 +145,7 @@ var page_waveform = (function () {
} else {
var j = JSON.parse(resp);
if (!j.success) {
console.error("Sampling / readout failed.");
errorMsg("Sampling / readout failed.");
if (autoReload)
toggleAutoReload(); // turn it off.
@@ -165,13 +165,18 @@ var page_waveform = (function () {
readoutPending = true;
var n = $('#count').val();
var fs = $('#freq').val();
var url = _root+'/api/{fmt}.json?n={n}&fs={fs}'.format({
fmt: dataFormat,
n: $('#count').val(),
fs: $('#freq').val()
fmt: dataFormat, // fft or raw
n: n,
fs: fs
});
$().get(url, onRxData);
$().get(url, onRxData, {
timeout: (1000/fs)*n+1500
});
return true;
}