-
Notifications
You must be signed in to change notification settings - Fork 0
/
liff-starter.js
139 lines (125 loc) · 4.41 KB
/
liff-starter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
window.onload = function() {
const useNodeJS = false; // if you are not using a node server, set this value to false
const defaultLiffId = "1653759747-e13y1nmz"; // change the default LIFF value if you are not using a node server
// DO NOT CHANGE THIS
let myLiffId = "";
// if node is used, fetch the environment variable and pass it to the LIFF method
// otherwise, pass defaultLiffId
if (useNodeJS) {
fetch('/send-id')
.then(function(reqResponse) {
return reqResponse.json();
})
.then(function(jsonResponse) {
myLiffId = jsonResponse.id;
initializeLiffOrDie(myLiffId);
})
.catch(function(error) {
document.getElementById("liffAppContent").classList.add('hidden');
document.getElementById("nodeLiffIdErrorMessage").classList.remove('hidden');
});
} else {
myLiffId = defaultLiffId;
initializeLiffOrDie(myLiffId);
}
};
/**
* Check if myLiffId is null. If null do not initiate liff.
* @param {string} myLiffId The LIFF ID of the selected element
*/
function initializeLiffOrDie(myLiffId) {
if (!myLiffId) {
document.getElementById("liffAppContent").classList.add('hidden');
document.getElementById("liffIdErrorMessage").classList.remove('hidden');
} else {
initializeLiff(myLiffId);
}
}
/**
* Initialize LIFF
* @param {string} myLiffId The LIFF ID of the selected element
*/
function initializeLiff(myLiffId) {
liff
.init({
liffId: myLiffId
})
.then(() => {
// start to use LIFF's api
initializeApp();
})
.catch((err) => {
document.getElementById("liffAppContent").classList.add('hidden');
document.getElementById("liffInitErrorMessage").classList.remove('hidden');
});
}
/**
* Initialize the app by calling functions handling individual app components
*/
function initializeApp() {
displayLiffData();
displayIsInClientInfo();
registerButtonHandlers();
// check if the user is logged in/out, and disable inappropriate button
if (liff.isLoggedIn()) {
document.getElementById('liffLoginButton').disabled = true;
} else {
document.getElementById('liffLogoutButton').disabled = true;
}
}
/**
* Display data generated by invoking LIFF methods
*/
function displayLiffData() {
document.getElementById('isInClient').textContent = liff.isInClient();
document.getElementById('isLoggedIn').textContent = liff.isLoggedIn();
}
/**
* Toggle the login/logout buttons based on the isInClient status, and display a message accordingly
*/
function displayIsInClientInfo() {
if (liff.isInClient()) {
document.getElementById('liffLoginButton').classList.toggle('hidden');
document.getElementById('liffLogoutButton').classList.toggle('hidden');
document.getElementById('isInClientMessage').textContent = 'You are opening the app in the in-app browser of LINE.';
} else {
document.getElementById('isInClientMessage').textContent = 'You are opening the app in an external browser.';
}
}
function registerButtonHandlers() {
document.getElementById('openWindowButton').addEventListener('click', function() {
liff.openWindow({
url: 'https://convert-heroku.herokuapp.com/', // Isi dengan Endpoint URL aplikasi web Anda
external: true
});
});
document.getElementById('closeWindowButton').addEventListener('click', function() {
if (!liff.isInClient()) {
sendAlertIfNotInClient();
} else {
liff.closeWindow();
}
});
document.getElementById('liffLoginButton').addEventListener('click', function() {
if (!liff.isLoggedIn()) {
liff.login();
}
});
document.getElementById('liffLogoutButton').addEventListener('click', function() {
if (liff.isLoggedIn()) {
liff.logout();
window.location.reload();
}
});
}
function sendAlertIfNotInClient() {
alert('This button is unavailable as LIFF is currently being opened in an external browser.');
}
function toggleElement(elementId) {
const elem = document.getElementById(elementId);
if (elem.offsetWidth > 0 && elem.offsetHeight > 0) {
elem.style.display = 'none';
} else {
elem.style.display = 'block';
}
}