luci-base: ui.js: add pingDevice() and awaitReconnect() functions
authorJo-Philipp Wich <jo@mein.io>
Mon, 7 Oct 2019 08:07:49 +0000 (10:07 +0200)
committerJo-Philipp Wich <jo@mein.io>
Mon, 7 Oct 2019 09:53:39 +0000 (11:53 +0200)
Import these functions from the flash.js for use with other views that
might trigger a device reboot.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/htdocs/luci-static/resources/ui.js

index a4050cd3f3654a03f5721f04590725e338890fb2..caae812812979158029032bd2c086e8ce92207da 100644 (file)
@@ -2214,6 +2214,44 @@ return L.Class.extend({
                }
        }),
 
+       /* Reconnect handling */
+       pingDevice: function(proto, ipaddr) {
+               var target = '%s://%s%s?%s'.format(proto || 'http', ipaddr || window.location.host, L.resource('icons/loading.gif'), Math.random());
+
+               return new Promise(function(resolveFn, rejectFn) {
+                       var img = new Image();
+
+                       img.onload = resolveFn;
+                       img.onerror = rejectFn;
+
+                       window.setTimeout(rejectFn, 1000);
+
+                       img.src = target;
+               });
+       },
+
+       awaitReconnect: function(/* ... */) {
+               var ipaddrs = arguments.length ? arguments : [ window.location.host ];
+
+               window.setTimeout(L.bind(function() {
+                       L.Poll.add(L.bind(function() {
+                               var tasks = [], reachable = false;
+
+                               for (var i = 0; i < 2; i++)
+                                       for (var j = 0; j < ipaddrs.length; j++)
+                                               tasks.push(this.pingDevice(i ? 'https' : 'http', ipaddrs[j])
+                                                       .then(function(ev) { reachable = ev.target.src.replace(/^(https?:\/\/[^\/]+).*$/, '$1/') }, function() {}));
+
+                               return Promise.all(tasks).then(function() {
+                                       if (reachable) {
+                                               L.Poll.stop();
+                                               window.location = reachable;
+                                       }
+                               });
+                       }, this));
+               }, this), 5000);
+       },
+
        /* UCI Changes */
        changes: L.Class.singleton({
                init: function() {