[Service] Upgrade device home as v1.0.4
[platform/framework/web/wrtjs.git] / device_home / pincode / js / pincode.js
index 823f68c..2520121 100755 (executable)
@@ -5,18 +5,12 @@ const serverURL = window.location.protocol + '//' + window.location.hostname;
 var publicKey;
 var input = '';
 var dots = document.querySelectorAll('.dot'), numbers = document.querySelectorAll('.number');
-var tryCount = 0;
 
-function preloadKey() {
+function displayPincode() {
     var xhr = new XMLHttpRequest();
-    xhr.onload = function () {
+    xhr.onload = function() {
         if (xhr.status === 200 || xhr.status === 201) {
             publicKey = xhr.responseText;
-            if (publicKey) {
-                init();
-            } else {
-                console.error('[WT] Failed to get public key.');
-            }
         } else {
             console.error(xhr.responseText);
         }
@@ -28,8 +22,8 @@ function preloadKey() {
 function init() {
     dots = Array.prototype.slice.call(dots);
     numbers = Array.prototype.slice.call(numbers);
-    numbers.forEach(function (number, index) {
-        number.addEventListener('click', function () {
+    numbers.forEach(function(number, index) {
+        number.addEventListener('click', function() {
             number.className += ' grow';
             if (number.innerHTML === '0') {
                 input += 0;
@@ -41,17 +35,17 @@ function init() {
                 console.log(`${TAG} input: ${input}`);
                 sendPinCode(input);
 
-                setTimeout(function () {
-                    dots.forEach(function (dot, index) {
+                setTimeout(function() {
+                    dots.forEach(function(dot, index) {
                         dot.className = 'dot';
                     });
                     input = '';
                 }, 900);
-                setTimeout(function () {
+                setTimeout(function() {
                     document.body.className = '';
                 }, 1000);
             }
-            setTimeout(function () {
+            setTimeout(function() {
                 number.className = 'number';
             }, 1000);
         });
@@ -62,10 +56,14 @@ async function sendPinCode(data) {
     var encrypt = new JSEncrypt();
     encrypt.setPublicKey(publicKey);
     var xhr = new XMLHttpRequest();
-    xhr.onload = function () {
+    xhr.onload = function() {
         if (xhr.status === 200 || xhr.status === 201) {
             console.log(`result : ${xhr.responseText}`);
-            chkPinCode(xhr.responseText === 'true' ? true : false);
+            if (xhr.responseText === 'retry') {
+                retryPinCode();
+            } else {
+                chkPinCode(xhr.responseText === 'true' ? true : false);
+            }
         } else {
             console.error(xhr.responseText);
         }
@@ -77,43 +75,31 @@ async function sendPinCode(data) {
     xhr.send(JSON.stringify({ pincode: data.toString("utf8") }));
 }
 
-function generatePinCodeNumber() {
-    var xhr = new XMLHttpRequest();
-    xhr.onload = function () {
-        if (xhr.status === 200 || xhr.status === 201) {
-            console.log(xhr.responseText);
-        } else {
-            console.error(xhr.responseText);
-        }
-    };
-    xhr.open('GET', serverURL + ':' + serverPort + '/retryPinCode');
-    xhr.send();
+function retryPinCode() {
+    dots.forEach(function(dot, index) {
+        dot.className += ' wrong';
+    });
+    document.body.className += ' wrong';
+    displayPincode();
+    setTimeout(function() {
+        alert('Failed to input 5 times. A new Pincode has been generated, so check the TV notification.');
+    }, 1000);
 }
 
 function chkPinCode(returnVal) {
     if (returnVal) {
-        dots.forEach(function (dot, index) {
+        dots.forEach(function(dot, index) {
             dot.className += ' correct';
         });
         document.body.className += ' correct';
-        tryCount = 0;
-        setTimeout(function () {
+        setTimeout(function() {
             loginForm.submit();
-            //pinCode();
         }, 1000);
     } else {
-        dots.forEach(function (dot, index) {
+        dots.forEach(function(dot, index) {
             dot.className += ' wrong';
         });
         document.body.className += ' wrong';
-        tryCount++;
-        if (tryCount === 5) {
-            tryCount = 0;
-            generatePinCodeNumber();
-            setTimeout(function () {
-                alert('Failed to input 5 times. A new Pincode has been generated, so check the TV notification.');
-            }, 1000);
-        }
     }
 }
 
@@ -121,6 +107,7 @@ function pinCode() {
     window.location.href = serverURL + ':' + serverPort + '/client/client.html';
 }
 
-window.onload = function () {
-    preloadKey();
+window.onload = function() {
+    displayPincode();
+    init();
 };