-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathprofile-menu.js
More file actions
261 lines (214 loc) · 8.01 KB
/
profile-menu.js
File metadata and controls
261 lines (214 loc) · 8.01 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
define(function (require, exports, module) {
const Mustache = require("thirdparty/mustache/mustache");
const PopUpManager = require("widgets/PopUpManager");
// HTML templates
const loginTemplate = require("text!./html/login-dialog.html");
const profileTemplate = require("text!./html/profile-panel.html");
// for the popup DOM element
let $popup = null;
// this is to track whether the popup is visible or not
let isPopupVisible = false;
// if user is logged in we show the profile menu, otherwise we show the login menu
let isLoggedIn = false;
// this is to handle document click events to close popup
let documentClickHandler = null;
const defaultLoginData = {
welcomeTitle: "Welcome to Phoenix Code",
signInBtnText: "Sign in to your account",
supportBtnText: "Contact support"
};
const defaultProfileData = {
initials: "CA",
userName: "Charly A.",
planName: "Paid Plan",
quotaLabel: "AI quota used",
quotaUsed: "7,000",
quotaTotal: "10,000",
quotaUnit: "tokens",
quotaPercent: 70,
accountBtnText: "Account details",
supportBtnText: "Contact support",
signOutBtnText: "Sign out"
};
function _handleSignInBtnClick() {
console.log("User clicked sign in button");
closePopup(); // need to close the current popup to show the new one
isLoggedIn = true;
showProfilePopup();
}
function _handleSignOutBtnClick() {
console.log("User clicked sign out");
closePopup();
isLoggedIn = false;
showLoginPopup();
}
function _handleContactSupportBtnClick() {
Phoenix.app.openURLInDefaultBrowser(brackets.config.support_url);
}
function _handleAccountDetailsBtnClick() {
console.log("User clicked account details");
}
/**
* Close the popup if it's open
* this is called at various instances like when the user click on the profile icon even if the popup is open
* or when user clicks somewhere else on the document
*/
function closePopup() {
if ($popup) {
PopUpManager.removePopUp($popup);
$popup = null;
isPopupVisible = false;
}
// we need to remove document click handler if it already exists
if (documentClickHandler) {
$(document).off("mousedown", documentClickHandler);
documentClickHandler = null;
}
}
/**
* this function is to position the popup near the profile button
*/
function positionPopup() {
const $profileButton = $("#user-profile-button");
if ($profileButton.length && $popup) {
const buttonPos = $profileButton.offset();
const popupWidth = $popup.outerWidth();
const windowWidth = $(window).width();
// pos above the profile button
let top = buttonPos.top - $popup.outerHeight() - 10;
// If popup would go off the right edge of the window, align right edge of popup with right edge of button
let left = Math.min(
buttonPos.left - popupWidth + $profileButton.outerWidth(),
windowWidth - popupWidth - 10
);
// never go off left edge
left = Math.max(10, left);
$popup.css({
top: top + "px",
left: left + "px"
});
}
}
/**
* this function is responsible to set up a click handler to close the popup when clicking outside
*/
function _setupDocumentClickHandler() {
// remove any existing handlers
if (documentClickHandler) {
$(document).off("mousedown", documentClickHandler);
}
// add the new click handler
documentClickHandler = function (event) {
// if the click is outside the popup and not on the profile button (which toggles the popup)
if ($popup && !$popup[0].contains(event.target) && !$("#user-profile-button")[0].contains(event.target)) {
closePopup();
}
};
// this is needed so we don't close the popup immediately as the profile button is clicked
setTimeout(function() {
$(document).on("mousedown", documentClickHandler);
}, 100);
}
/**
* Shows the sign-in popup when the user is not logged in
* @param {Object} loginData - Data to populate the template (optional)
*/
function showLoginPopup(loginData) {
// If popup is already visible, just close it
if (isPopupVisible) {
closePopup();
return;
}
// Merge provided data with defaults
const templateData = $.extend({}, defaultLoginData, loginData || {});
// create the popup element
closePopup(); // close any existing popup first
// Render template with data
const renderedTemplate = Mustache.render(loginTemplate, templateData);
$popup = $(renderedTemplate);
$("body").append($popup);
isPopupVisible = true;
positionPopup();
PopUpManager.addPopUp($popup, function() {
$popup.remove();
$popup = null;
isPopupVisible = false;
}, true, { closeCurrentPopups: true });
// event handlers for buttons
$popup.find("#phoenix-signin-btn").on("click", function () {
_handleSignInBtnClick();
});
$popup.find("#phoenix-support-btn").on("click", function () {
_handleContactSupportBtnClick();
closePopup();
});
// handle window resize to reposition popup
$(window).on("resize.profilePopup", function () {
if (isPopupVisible) {
positionPopup();
}
});
_setupDocumentClickHandler();
}
/**
* Shows the user profile popup when the user is logged in
* @param {Object} profileData - Data to populate the template (optional)
*/
function showProfilePopup(profileData) {
// If popup is already visible, just close it
if (isPopupVisible) {
closePopup();
return;
}
// Merge provided data with defaults
const templateData = $.extend({}, defaultProfileData, profileData || {});
closePopup();
// Render template with data
const renderedTemplate = Mustache.render(profileTemplate, templateData);
$popup = $(renderedTemplate);
$("body").append($popup);
isPopupVisible = true;
positionPopup();
PopUpManager.addPopUp($popup, function() {
$popup.remove();
$popup = null;
isPopupVisible = false;
}, true, { closeCurrentPopups: true });
$popup.find("#phoenix-account-btn").on("click", function () {
_handleAccountDetailsBtnClick();
closePopup();
});
$popup.find("#phoenix-support-btn").on("click", function () {
_handleContactSupportBtnClick();
closePopup();
});
$popup.find("#phoenix-signout-btn").on("click", function () {
_handleSignOutBtnClick();
});
// handle window resize to reposition popup
$(window).on("resize.profilePopup", function () {
if (isPopupVisible) {
positionPopup();
}
});
_setupDocumentClickHandler();
}
/**
* Toggle the profile popup based on the user's login status
* this function is called inside the src/extensionsIntegrated/Phoenix/main.js when user clicks on the profile icon
* @param {Object} data - Data to populate the templates (optional)
*/
function init(data) {
// check if the popup is already visible or not. if visible close it
if (isPopupVisible) {
closePopup();
return;
}
if (isLoggedIn) {
showProfilePopup(data);
} else {
showLoginPopup(data);
}
}
exports.init = init;
});