From 519a8f6316d8dc8dabf547e3d566218589baed76 Mon Sep 17 00:00:00 2001
From: Sebastien Curt <sebastien.curt@tetras-libre.fr>
Date: Wed, 4 May 2022 11:47:25 +0200
Subject: [PATCH] Fix edit popup TC for notes

---
 capsule-prototype/js/PopupPanelWidget.js | 59 +++++++++++++++++-------
 1 file changed, 42 insertions(+), 17 deletions(-)

diff --git a/capsule-prototype/js/PopupPanelWidget.js b/capsule-prototype/js/PopupPanelWidget.js
index ad296ed..3cac256 100644
--- a/capsule-prototype/js/PopupPanelWidget.js
+++ b/capsule-prototype/js/PopupPanelWidget.js
@@ -178,6 +178,10 @@ function TCPanelInputEditor(data) {
 }
 TCPanelInputEditor.prototype = Object.create(PanelInputEditor.prototype);
 TCPanelInputEditor.prototype.appendInputEditorDiv = function (parentDiv) {
+
+    function onlyNumber(event) {
+        $(this).val($(this).val().replace(/[^0-9]/g, ''));
+    }
     let that = this;
 
     this.readonlyDiv = $('<div>');
@@ -194,7 +198,7 @@ TCPanelInputEditor.prototype.appendInputEditorDiv = function (parentDiv) {
     this.popupTcInDiv
         .attr('id', 'popupTCin')
         .addClass('popupTCdisplay')
-        .html(convertToTime(that.tag.getTimeEnd()))
+        .html(convertToTime(that.tag.getTimeStart()))
     this.readonlyDiv.append(this.popupTcInDiv);
 
     this.popupTcOutDiv = $('<div>');
@@ -215,15 +219,21 @@ TCPanelInputEditor.prototype.appendInputEditorDiv = function (parentDiv) {
         })
         this.editDiv.append($('<span>').addClass('popupTClabel').html('start'));
         this.popupTCeditDivMinStart = $('<input class="popupTCeditfield" id="popupTCinMin" maxLength="2" type="text" value="">');
-        this.popupTCeditDivMinStart.click(function (event) {
-            event.stopPropagation();
-        });
+        this.popupTCeditDivMinStart
+            .val(Math.floor(that.tag.getTimeStart()/60).toString(10).padStart(2, '0'))
+            .click(function (event) {
+                event.stopPropagation();
+            })
+            .on('input', onlyNumber);
         this.editDiv.append(this.popupTCeditDivMinStart);
         this.editDiv.append(':');
         this.popupTCeditDivSecStart = $('<input class="popupTCeditfield" id="popupTCinSec" maxLength="2" type="text" value="">');
-        this.popupTCeditDivSecStart.click(function (event) {
-            event.stopPropagation();
-        });
+        this.popupTCeditDivSecStart
+            .val((that.tag.getTimeStart()%60).toString(10).padStart(2, '0'))
+            .click(function (event) {
+                event.stopPropagation();
+            })
+            .on('input', onlyNumber);
         this.editDiv.append(this.popupTCeditDivSecStart);
         let popupTCeditStartDivNow = $('<div class="nowTCbtn" id="nowTCin">now</div>');
         popupTCeditStartDivNow.click(function (event) {
@@ -232,7 +242,7 @@ TCPanelInputEditor.prototype.appendInputEditorDiv = function (parentDiv) {
             that.popupTCeditDivMinStart.val(timeCurrent.split(":")[0]);
             that.popupTCeditDivSecStart.val(timeCurrent.split(":")[1]);
         });
-        this.editDiv.append(popupTCeditStartDivNow);
+        this.editDiv.append(popupTCeditStartDivNow)
         this.editDiv.append($('<br>'));
 
         this.editDiv.append($('<span>').addClass('popupTClabel').html('end'));
@@ -242,7 +252,9 @@ TCPanelInputEditor.prototype.appendInputEditorDiv = function (parentDiv) {
             .attr('maxlength', '2')
             .attr('type', 'text')
             .addClass('popupTCeditfield')
-            .val('');
+            .val(Math.floor(that.tag.getTimeEnd()/60).toString(10).padStart(2, '0'))
+            .on('input', onlyNumber);
+
         this.popupTCeditDivMinEnd.click(function (event) {
             event.stopPropagation();
         });
@@ -254,10 +266,11 @@ TCPanelInputEditor.prototype.appendInputEditorDiv = function (parentDiv) {
             .attr('maxlength', '2')
             .attr('type', 'text')
             .addClass('popupTCeditfield')
-            .val('')
+            .val(Math.floor(that.tag.getTimeEnd()%60).toString(10).padStart(2, '0'))
             .click(function (event) {
                 event.stopPropagation();
-            });
+            })
+            .on('input', onlyNumber);
         this.editDiv.append(this.popupTCeditDivSecEnd);
         let popupTCeditEndDivNow = $('<div class="nowTCbtn" id="nowTCin">now</div>');
         popupTCeditEndDivNow.click(function (event) {
@@ -282,10 +295,18 @@ TCPanelInputEditor.prototype.appendInputEditorDiv = function (parentDiv) {
 }
 TCPanelInputEditor.prototype.closeEdition = function () {
     if (this.canEdit) {
-        let tcInMin = this.popupTCeditDivMinStart.val();
-        let tcInSec = this.popupTCeditDivSecStart.val();
-        let tcOutMin = this.popupTCeditDivMinEnd.val();
-        let tcOutSec = this.popupTCeditDivSecEnd.val();
+        let isNaN = (maybeNaN) => maybeNaN!=maybeNaN;
+        let ensureNotNaN = (text, defaultValue = 0) => {
+            let tempVal = parseInt(text);
+            if (isNaN(tempVal)) {
+                return defaultValue;
+            }
+            return tempVal;
+        }
+        let tcInMin = ensureNotNaN(this.popupTCeditDivMinStart.val());
+        let tcInSec = ensureNotNaN(this.popupTCeditDivSecStart.val());
+        let tcOutMin = ensureNotNaN(this.popupTCeditDivMinEnd.val());
+        let tcOutSec = ensureNotNaN(this.popupTCeditDivSecEnd.val());
         let tcIn = (60 * tcInMin) + tcInSec;
         let tcOut = (60 * tcOutMin) + tcOutSec;
 
@@ -306,8 +327,12 @@ TCPanelInputEditor.prototype.closeEdition = function () {
         } else {
             this.tag.setTimeStart(tcIn);
             this.tag.setTimeEnd(tcOut);
-            this.popupTcInDiv.html(tcInMin + ":" + tcInSec);
-            this.popupTcOutDiv.html(tcOutMin + ":" + tcOutSec);
+            this.popupTcInDiv.html(tcInMin.toString().padStart(2, '0') + ":" + tcInSec.toString().padStart(2, '0'));
+            this.popupTcOutDiv.html(tcOutMin.toString().padStart(2, '0') + ":" + tcOutSec.toString().padStart(2, '0'));
+            this.popupTCeditDivMinStart.val(tcInMin.toString().padStart(2, '0'));
+            this.popupTCeditDivSecStart.val(tcInSec.toString().padStart(2, '0'));
+            this.popupTCeditDivMinEnd.val(tcOutMin.toString().padStart(2, '0'));
+            this.popupTCeditDivSecEnd.val(tcOutSec.toString().padStart(2, '0'));
             window.app.rekall.Rekall('pubSub').publish('tag.tc.updated', {tag: this.tag});
         }
     }
-- 
GitLab