var app = angular.module("myApp", []);
app.controller("customersCtrl", function ($scope, $http, $sce) {
$scope.suggestions = [];
$scope.currentSuggestions = [];
$scope.engToTamilResult = [];
$scope.inputText = "";
$scope.lastWord = "";
$scope.language = "pa";
// Fetch Google API
$scope.getGoogle = function (word) {
if (!$scope.suggestions[word]) {
const apiUrl = `https://inputtools.google.com/request?text=${encodeURIComponent(
word
)}&itc=${$scope.language}-t-i0-und&num=13&cp=0&cs=0&ie=utf-8&oe=utf-8&app=demopage`;
$http
.get(apiUrl)
.success(function (response) {
var myresponse = angular.fromJson(response);
if (myresponse[0] == "SUCCESS") {
var mydata = myresponse[1];
var mydataWrap = mydata[0];
var answers_array = mydataWrap[1];
$scope.suggestions[word] = answers_array;
$scope.currentSuggestions = answers_array;
}
});
}
};
// Process Word
$scope.proccessWord = function (word) {
word = word.trim();
if (!$scope.suggestions[word]) {
$scope.getGoogle(word);
} else {
$scope.currentSuggestions = $scope.suggestions[word];
}
};
// Handle Text Area Input
$scope.getWord = function () {
var boxTextOriginal = document.getElementById("home_th_ta_box").value;
var boxText = boxTextOriginal.trim();
var boxTextArray = boxText.split(" ");
for (var i = 0; i < boxTextArray.length; i++) {
var word = boxTextArray[i];
if (word != "") {
$scope.proccessWord(word.trim());
}
}
var lastChar = boxTextOriginal.charAt(parseInt(boxTextOriginal.length) - 1);
if (" ,.-_!@#&()/".includes(lastChar)) {
$scope.setWord();
}
};
// Set Selected Word
$scope.setSelectdWord = function (selectedWord) {
var tamilWords = "";
var tamilWord = "";
var boxText = document.getElementById("home_th_ta_box").value.trim();
var boxTextArray = boxText.split(" ");
var lastlen = boxTextArray.length - 1;
for (var j = 0; j < boxTextArray.length; j++) {
var word = boxTextArray[j];
if (j == lastlen) {
tamilWord = selectedWord;
tamilWords = tamilWords ? tamilWords + " " + selectedWord : selectedWord;
$scope.suggestions[word] = [selectedWord];
} else {
tamilWord = word;
if (word != "") {
if ($scope.suggestions[word]) {
tamilWord = $scope.suggestions[word][0];
tamilWords = tamilWords ? tamilWords + " " + tamilWord : tamilWord;
} else {
$scope.getGoogle(word);
if ($scope.suggestions[word]) {
tamilWord = $scope.suggestions[word][0];
}
tamilWords = tamilWords ? tamilWords + " " + tamilWord : tamilWord;
}
}
}
$scope.suggestions[tamilWord] = $scope.suggestions[word];
}
document.getElementById("home_th_ta_box").value = tamilWords + " ";
document.getElementById("home_th_ta_box").focus();
};
// Set Words in Text Area
$scope.setWord = function () {
var tamilWords = "";
var tamilWord = "";
var boxText = document.getElementById("home_th_ta_box").value.trim();
var boxTextArray = boxText.split(" ");
for (var i = 0; i < boxTextArray.length; i++) {
var word = boxTextArray[i];
tamilWord = word;
if (word != "") {
if ($scope.suggestions[word]) {
tamilWord = $scope.suggestions[word][0];
tamilWords = tamilWords ? tamilWords + " " + tamilWord : tamilWord;
} else {
$scope.getGoogle(word);
if ($scope.suggestions[word]) {
tamilWord = $scope.suggestions[word][0];
}
tamilWords = tamilWords ? tamilWords + " " + tamilWord : tamilWord;
}
}
$scope.suggestions[tamilWord] = $scope.suggestions[word];
}
document.getElementById("home_th_ta_box").value = tamilWords + " ";
document.getElementById("home_th_ta_box").focus();
};
});