From 11d94ecba884511517caf267b7985a2cbd4bf764 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Tue, 15 Sep 2020 16:33:41 +0200 Subject: [PATCH 1/4] Fix `this` shenanigans --- kfet/open/static/kfetopen/kfet-open.js | 35 +++++++++++++------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/kfet/open/static/kfetopen/kfet-open.js b/kfet/open/static/kfetopen/kfet-open.js index 74f18d8a..6b7b48a8 100644 --- a/kfet/open/static/kfetopen/kfet-open.js +++ b/kfet/open/static/kfetopen/kfet-open.js @@ -2,7 +2,7 @@ var OpenWS = new KfetWebsocket({ relative_url: "open/" }); -var OpenKfet = function(force_close_url, admin) { +var OpenKfet = function (force_close_url, admin) { this.force_close_url = force_close_url; this.admin = admin; @@ -13,9 +13,9 @@ var OpenKfet = function(force_close_url, admin) { warning: $('.kfetopen .warning') }, - this.dom.force_close_btn.click( () => this.toggle_force_close() ); - setInterval( () => this.refresh(), this.refresh_interval * 1000); - OpenWS.add_handler( data => this.refresh(data) ); + this.dom.force_close_btn.click(() => this.toggle_force_close()); + setInterval(() => this.refresh(), this.refresh_interval * 1000); + OpenWS.add_handler(data => this.refresh(data)); }; @@ -53,7 +53,7 @@ OpenKfet.prototype = { return this.last_update && moment().diff(this.last_update, 'minute') <= this.time_unknown; }, - refresh: function(data) { + refresh: function (data) { if (data) { $.extend(this, data); this.last_update = moment(); @@ -63,7 +63,7 @@ OpenKfet.prototype = { this.refresh_dom(); }, - refresh_dom: function() { + refresh_dom: function () { let status = this.status; this.clear_class(); @@ -83,31 +83,32 @@ OpenKfet.prototype = { } }, - toggle_force_close: function(password) { + toggle_force_close: function (password) { $.post({ + context: this, url: this.force_close_url, - data: {force_close: !this.force_close}, + data: { force_close: !this.force_close }, beforeSend: function ($xhr) { $xhr.setRequestHeader("X-CSRFToken", csrftoken); if (password !== undefined) $xhr.setRequestHeader("KFetPassword", password); } }) - .fail(function($xhr) { - switch ($xhr.status) { - case 403: - requestAuth({'errors': {}}, this.toggle_force_close); - break; - } - }); + .fail(function ($xhr) { + switch ($xhr.status) { + case 403: + requestAuth({ 'errors': {} }, this.toggle_force_close.bind(this)); + break; + } + }); }, - clear_class: function() { + clear_class: function () { let re = new RegExp('(^|\\s)' + this.class_prefix + '\\S+', 'g'); $(this.target).attr('class', (i, c) => c ? c.replace(re, '') : ''); }, - add_class: function(status) { + add_class: function (status) { $(this.target).addClass(this.class_prefix + status); } }; From ba4cc01ed4ba77750bfab76643811e871b3b0509 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Tue, 15 Sep 2020 16:37:41 +0200 Subject: [PATCH 2/4] Fix formatting --- kfet/open/static/kfetopen/kfet-open.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/kfet/open/static/kfetopen/kfet-open.js b/kfet/open/static/kfetopen/kfet-open.js index 6b7b48a8..3cc6f397 100644 --- a/kfet/open/static/kfetopen/kfet-open.js +++ b/kfet/open/static/kfetopen/kfet-open.js @@ -11,9 +11,9 @@ var OpenKfet = function (force_close_url, admin) { status_text: $('.kfetopen .status-text'), force_close_btn: $('.kfetopen .force-close-btn'), warning: $('.kfetopen .warning') - }, + } - this.dom.force_close_btn.click(() => this.toggle_force_close()); + this.dom.force_close_btn.click(() => this.toggle_force_close()); setInterval(() => this.refresh(), this.refresh_interval * 1000); OpenWS.add_handler(data => this.refresh(data)); @@ -93,14 +93,13 @@ OpenKfet.prototype = { if (password !== undefined) $xhr.setRequestHeader("KFetPassword", password); } - }) - .fail(function ($xhr) { - switch ($xhr.status) { - case 403: - requestAuth({ 'errors': {} }, this.toggle_force_close.bind(this)); - break; - } - }); + }).fail(function ($xhr) { + switch ($xhr.status) { + case 403: + requestAuth({ 'errors': {} }, this.toggle_force_close.bind(this)); + break; + } + }); }, clear_class: function () { From 43a2f8db53bc3f601cbcbdef3fc2d62ca2ad14a7 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Tue, 15 Sep 2020 16:44:32 +0200 Subject: [PATCH 3/4] Use arrow functions everywhere for consistency --- kfet/open/static/kfetopen/kfet-open.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kfet/open/static/kfetopen/kfet-open.js b/kfet/open/static/kfetopen/kfet-open.js index 3cc6f397..869d816f 100644 --- a/kfet/open/static/kfetopen/kfet-open.js +++ b/kfet/open/static/kfetopen/kfet-open.js @@ -96,7 +96,7 @@ OpenKfet.prototype = { }).fail(function ($xhr) { switch ($xhr.status) { case 403: - requestAuth({ 'errors': {} }, this.toggle_force_close.bind(this)); + requestAuth({ 'errors': {} }, () => this.toggle_force_close); break; } }); From 9f9724b1d16bc5355a73f2e565d330a49bf90965 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Tue, 15 Sep 2020 19:52:28 +0200 Subject: [PATCH 4/4] Arrow function works now --- kfet/open/static/kfetopen/kfet-open.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kfet/open/static/kfetopen/kfet-open.js b/kfet/open/static/kfetopen/kfet-open.js index 869d816f..e92abc2f 100644 --- a/kfet/open/static/kfetopen/kfet-open.js +++ b/kfet/open/static/kfetopen/kfet-open.js @@ -96,7 +96,7 @@ OpenKfet.prototype = { }).fail(function ($xhr) { switch ($xhr.status) { case 403: - requestAuth({ 'errors': {} }, () => this.toggle_force_close); + requestAuth({ 'errors': {} }, (password) => this.toggle_force_close(password)); break; } });