]> git.proxmox.com Git - pmg-gui.git/blob - js/mobile/utils.js
js/mobile: fix eslint and enable linting on build
[pmg-gui.git] / js / mobile / utils.js
1 PMG = {
2 Utils: {
3 getCookie(name) {
4 let cookies = document.cookie.split(/;\s*/);
5 for (let i = 0; i < cookies.length; i++) {
6 let cookie = cookies[i].split('=');
7 if (cookie[0] === name && cookie.length > 1) {
8 return cookie[1];
9 }
10 }
11 return undefined;
12 },
13 setCookie(name, value, expires) {
14 value = encodeURIComponent(value);
15 let cookie = `${name}=${value}`;
16 if (expires) {
17 cookie += `; expires=${expires}`;
18 }
19 document.cookie = cookie;
20 },
21 deleteCookie(name) {
22 PMG.Utils.setCookie(name, "", "Thu, 01 Jan 1970 00:00:00 UTC");
23 },
24 authOK(options) {
25 var authCookie = PMG.Utils.getCookie('PMGAuthCookie') || "";
26 return authCookie.substr(0, 7) === 'PMGQUAR' && Proxmox.UserName !== '';
27 },
28 isoToUnix(iso) {
29 let fields = iso.split('-').map((field) => parseInt(field, 10));
30 // monthIndex starts at 0
31 let date = new Date(fields[0], fields[1]-1, fields[2]);
32 return Math.round(date.getTime()/1000);
33 },
34 unixToIso(unix) {
35 let date = new Date(unix*1000);
36 let year = date.getFullYear().toString();
37 let month = (date.getMonth()+1).toString().padStart(2, "0");
38 let day = date.getDate().toString().padStart(2, "0");
39 return `${year}-${month}-${day}`;
40 },
41 showError(xhr) {
42 let statusText = "", errorText = "";
43 if (xhr instanceof Error) {
44 statusText = gettext("Error");
45 errorText = xhr.message;
46 } else if (xhr.error instanceof Error) {
47 statusText = gettext("Error");
48 errorText = xhr.error.message;
49 } else {
50 statusText = xhr.status.toString() + ' ' + xhr.statusText;
51 try {
52 let errorObj = JSON.parse(xhr.responseText);
53 if (errorObj.errors) {
54 let errors = Object.keys(errorObj.errors).map((key) => key + ": " + errorObj.errors[key]);
55 errorText = errors.join('<br>');
56 }
57 } catch (e) {
58 statusText = gettext("Error");
59 errorText = e.message;
60 }
61 }
62 app.toast.show({
63 text: `Error:<br>
64 ${statusText}<br>
65 ${errorText}
66 `,
67 closeButton: true,
68 destroyOnClose: true,
69 });
70 },
71 extractParams() {
72 let queryObj = app.utils.parseUrlQuery(location.search);
73 let mail, action, date, username, ticket;
74 if (queryObj.ticket) {
75 let tocheck = decodeURIComponent(queryObj.ticket);
76 let match = tocheck.match(/^PMGQUAR:([^\s:]+):/);
77 if (match) {
78 ticket = tocheck;
79 username = match[1];
80 }
81 delete queryObj.ticket;
82 }
83
84 if (queryObj.date) {
85 date =queryObj.date;
86 delete queryObj.date;
87 }
88
89 if (queryObj.cselect) {
90 mail = queryObj.cselect;
91 action = queryObj.action;
92 delete queryObj.cselect;
93 delete queryObj.action;
94 }
95
96 if (mail || action || date || ticket) {
97 let queryString = app.utils.serializeObject(queryObj);
98 window.history.replaceState(
99 window.history.state,
100 document.title,
101 location.pathname + (queryString? "?" + queryString : ''),
102 );
103 }
104
105 return { mail, action, date, username, ticket };
106 },
107 setLoginInfo(result) {
108 PMG.Utils.setCookie('PMGAuthCookie', result.data.ticket);
109 Proxmox.CSRFPreventionToken = result.data.CSRFPreventionToken;
110 },
111 getSubscriptionInfo() {
112 return new Promise(function(resolve, reject) {
113 app.request({
114 url: '/api2/json/nodes/localhost/subscription',
115 dataType: 'json',
116 success: (result, status, xhr) => {
117 resolve(result.data);
118 },
119 error: (xhr, status) => {
120 reject(xhr);
121 },
122 });
123 });
124 },
125 checkSubscription(data, showPopup) {
126 return new Promise(function(resolve, reject) {
127 if (data.status !== 'Active') {
128 let url = data.url || 'https://wwww.proxmox.com';
129 let err = `You do not have a valid subscription for this server.
130 Please visit
131 <a target="_blank" href="${url}">www.proxmox.com</a>
132 to get a list of available options.`;
133 app.toolbar.show('.toolbar.subscription');
134 $$('.button.subscription').on('click', () => {
135 app.dialog.alert(
136 err,
137 gettext("No valid subscription"),
138 );
139 });
140 if (showPopup) {
141 app.dialog.alert(
142 err,
143 gettext("No valid subscription"),
144 () => {
145 resolve(data);
146 },
147 );
148 } else {
149 resolve();
150 }
151 } else {
152 $$('.toolbar.subscription').remove();
153 resolve();
154 }
155 });
156 },
157 },
158 };
159