|
| 1 | +define(function (require, exports, module) { |
| 2 | + const loginTemplate = require("text!./html/login-dialog.html"); |
| 3 | + const profileTemplate = require("text!./html/profile-panel.html"); |
| 4 | + |
| 5 | + // for the popup DOM element |
| 6 | + let $popup = null; |
| 7 | + |
| 8 | + // this is to track whether the popup is visible or not |
| 9 | + let isPopupVisible = false; |
| 10 | + |
| 11 | + // if user is logged in we show the profile menu, otherwise we show the login menu |
| 12 | + const isLoggedIn = false; |
| 13 | + |
| 14 | + function _handleSignInBtnClick() { |
| 15 | + console.log("User clicked sign in button"); |
| 16 | + } |
| 17 | + |
| 18 | + function _handleSignOutBtnClick() { |
| 19 | + console.log("User clicked sign out"); |
| 20 | + } |
| 21 | + |
| 22 | + function _handleContactSupportBtnClick() { |
| 23 | + Phoenix.app.openURLInDefaultBrowser(brackets.config.support_url); |
| 24 | + } |
| 25 | + |
| 26 | + function _handleAccountDetailsBtnClick() { |
| 27 | + console.log("User clicked account details"); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Close the popup if it's open |
| 32 | + * this is called at various instances like when the user click on the profile icon even if the popup is open |
| 33 | + * or when user clicks somewhere else on the document |
| 34 | + */ |
| 35 | + function closePopup() { |
| 36 | + if ($popup) { |
| 37 | + $popup.remove(); |
| 38 | + $popup = null; |
| 39 | + isPopupVisible = false; |
| 40 | + |
| 41 | + // remove global click handler |
| 42 | + $(document).off("click.profilePopup"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * handle clicks outside the popup to close it |
| 48 | + */ |
| 49 | + function handleDocumentClick(e) { |
| 50 | + // If popup exists and click is outside the popup |
| 51 | + if ($popup && $popup.length && !$popup[0].contains(e.target)) { |
| 52 | + // If the click is not on the user-profile-button (which would toggle the popup) |
| 53 | + if (e.target.id !== "user-profile-button" && !$(e.target).closest("#user-profile-button").length) { |
| 54 | + closePopup(); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * this function is to position the popup near the profile button |
| 61 | + */ |
| 62 | + function positionPopup() { |
| 63 | + const $profileButton = $("#user-profile-button"); |
| 64 | + |
| 65 | + if ($profileButton.length && $popup) { |
| 66 | + const buttonPos = $profileButton.offset(); |
| 67 | + const popupWidth = $popup.outerWidth(); |
| 68 | + const windowWidth = $(window).width(); |
| 69 | + |
| 70 | + // pos above the profile button |
| 71 | + let top = buttonPos.top - $popup.outerHeight() - 10; |
| 72 | + |
| 73 | + // If popup would go off the right edge of the window, align right edge of popup with right edge of button |
| 74 | + let left = Math.min( |
| 75 | + buttonPos.left - popupWidth + $profileButton.outerWidth(), |
| 76 | + windowWidth - popupWidth - 10 |
| 77 | + ); |
| 78 | + |
| 79 | + // never go off left edge |
| 80 | + left = Math.max(10, left); |
| 81 | + |
| 82 | + $popup.css({ |
| 83 | + top: top + "px", |
| 84 | + left: left + "px" |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Shows the sign-in popup when the user is not logged in |
| 91 | + */ |
| 92 | + function showLoginPopup() { |
| 93 | + // If popup is already visible, just close it |
| 94 | + if (isPopupVisible) { |
| 95 | + closePopup(); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + // create the popup element |
| 100 | + closePopup(); // close any existing popup first |
| 101 | + $popup = $(loginTemplate); |
| 102 | + $("body").append($popup); |
| 103 | + isPopupVisible = true; |
| 104 | + |
| 105 | + // Position the popup |
| 106 | + positionPopup(); |
| 107 | + |
| 108 | + // event handlers for buttons |
| 109 | + $popup.find("#phoenix-signin-btn").on("click", function () { |
| 110 | + _handleSignInBtnClick(); |
| 111 | + closePopup(); |
| 112 | + }); |
| 113 | + |
| 114 | + $popup.find("#phoenix-support-btn").on("click", function () { |
| 115 | + _handleContactSupportBtnClick(); |
| 116 | + closePopup(); |
| 117 | + }); |
| 118 | + |
| 119 | + // Set up global click handler to close popup when clicking outside |
| 120 | + // Delay attaching to avoid immediate closing |
| 121 | + setTimeout(function () { |
| 122 | + $(document).on("click.profilePopup", handleDocumentClick); |
| 123 | + }, 0); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Shows the user profile popup when the user is logged in |
| 128 | + */ |
| 129 | + function showProfilePopup() { |
| 130 | + // If popup is already visible, just close it |
| 131 | + if (isPopupVisible) { |
| 132 | + closePopup(); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + closePopup(); |
| 137 | + $popup = $(profileTemplate); |
| 138 | + $("body").append($popup); |
| 139 | + isPopupVisible = true; |
| 140 | + positionPopup(); |
| 141 | + |
| 142 | + $popup.find("#phoenix-account-btn").on("click", function () { |
| 143 | + _handleAccountDetailsBtnClick(); |
| 144 | + closePopup(); |
| 145 | + }); |
| 146 | + |
| 147 | + $popup.find("#phoenix-support-btn").on("click", function () { |
| 148 | + _handleContactSupportBtnClick(); |
| 149 | + closePopup(); |
| 150 | + }); |
| 151 | + |
| 152 | + $popup.find("#phoenix-signout-btn").on("click", function () { |
| 153 | + _handleSignOutBtnClick(); |
| 154 | + closePopup(); |
| 155 | + }); |
| 156 | + |
| 157 | + // Set up global click handler to close popup when clicking outside |
| 158 | + // Delay attaching to avoid immediate closing |
| 159 | + setTimeout(function () { |
| 160 | + $(document).on("click.profilePopup", handleDocumentClick); |
| 161 | + }, 0); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Toggle the profile popup based on the user's login status |
| 166 | + * this function is called inside the src/extensionsIntegrated/Phoenix/main.js when user clicks on the profile icon |
| 167 | + */ |
| 168 | + function init() { |
| 169 | + // check if the popup is already visible or not. if visible close it |
| 170 | + if (isPopupVisible) { |
| 171 | + closePopup(); |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + if (isLoggedIn) { |
| 176 | + showProfilePopup(); |
| 177 | + } else { |
| 178 | + showLoginPopup(); |
| 179 | + } |
| 180 | + |
| 181 | + // handle window resize to reposition popup |
| 182 | + $(window).on("resize.profilePopup", function () { |
| 183 | + if (isPopupVisible) { |
| 184 | + positionPopup(); |
| 185 | + } |
| 186 | + }); |
| 187 | + } |
| 188 | + |
| 189 | + exports.init = init; |
| 190 | +}); |
0 commit comments