2020-07-02 01:45:54 +02:00
|
|
|
$(() => {
|
2013-04-03 22:30:36 +02:00
|
|
|
// NB: this file is included on multiple pages. In each context,
|
|
|
|
// some of the jQuery selectors below will return empty lists.
|
2018-12-15 22:52:55 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const password_field = $("#id_password, #id_new_password1");
|
2018-12-15 22:52:55 +01:00
|
|
|
if (password_field.length > 0) {
|
2020-07-15 00:34:28 +02:00
|
|
|
$.validator.addMethod(
|
|
|
|
"password_strength",
|
|
|
|
(value) => common.password_quality(value, undefined, password_field),
|
|
|
|
() => common.password_warning(password_field.val(), password_field),
|
|
|
|
);
|
2018-12-15 22:52:55 +01:00
|
|
|
// Reset the state of the password strength bar if the page
|
|
|
|
// was just reloaded due to a validation failure on the backend.
|
2020-07-15 01:29:15 +02:00
|
|
|
common.password_quality(password_field.val(), $("#pw_strength .bar"), password_field);
|
2013-04-03 22:30:36 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
password_field.on("input", function () {
|
2018-12-15 22:52:55 +01:00
|
|
|
// Update the password strength bar even if we aren't validating
|
|
|
|
// the field yet.
|
2020-07-15 01:29:15 +02:00
|
|
|
common.password_quality($(this).val(), $("#pw_strength .bar"), $(this));
|
2018-12-15 22:52:55 +01:00
|
|
|
});
|
|
|
|
}
|
2013-04-03 22:30:36 +02:00
|
|
|
|
2013-04-03 20:33:25 +02:00
|
|
|
function highlight(class_to_add) {
|
|
|
|
// Set a class on the enclosing control group.
|
|
|
|
return function (element) {
|
2020-07-15 00:34:28 +02:00
|
|
|
$(element)
|
|
|
|
.closest(".control-group")
|
2020-07-15 01:29:15 +02:00
|
|
|
.removeClass("success error")
|
2013-04-03 20:33:25 +02:00
|
|
|
.addClass(class_to_add);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#registration, #password_reset").validate({
|
2013-04-03 22:30:36 +02:00
|
|
|
rules: {
|
2020-07-15 01:29:15 +02:00
|
|
|
password: "password_strength",
|
|
|
|
new_password1: "password_strength",
|
2013-04-03 22:30:36 +02:00
|
|
|
},
|
2012-10-03 23:43:25 +02:00
|
|
|
errorElement: "p",
|
|
|
|
errorPlacement: function (error, element) {
|
2013-04-03 20:33:25 +02:00
|
|
|
// NB: this is called at most once, when the error element
|
|
|
|
// is created.
|
2020-07-15 01:29:15 +02:00
|
|
|
element.next(".help-inline.alert.alert-error").remove();
|
|
|
|
if (element.next().is('label[for="' + element.attr("id") + '"]')) {
|
|
|
|
error.insertAfter(element.next()).addClass("help-inline alert alert-error");
|
2017-01-19 19:02:56 +01:00
|
|
|
} else {
|
2020-07-15 01:29:15 +02:00
|
|
|
error.insertAfter(element).addClass("help-inline alert alert-error");
|
2017-01-19 19:02:56 +01:00
|
|
|
}
|
2012-10-03 23:43:25 +02:00
|
|
|
},
|
2020-07-15 01:29:15 +02:00
|
|
|
highlight: highlight("error"),
|
|
|
|
unhighlight: highlight("success"),
|
2012-10-03 23:43:25 +02:00
|
|
|
});
|
|
|
|
|
2018-12-03 21:22:59 +01:00
|
|
|
if ($("#registration").length > 0) {
|
2018-12-04 18:48:53 +01:00
|
|
|
// Check if there is no input field with errors.
|
2020-07-15 01:29:15 +02:00
|
|
|
if ($(".help-inline:not(:empty)").length === 0) {
|
2018-12-04 18:48:53 +01:00
|
|
|
// Find the first input field present in the form that is
|
|
|
|
// not hidden and disabled and store it in a variable.
|
2019-11-02 00:06:25 +01:00
|
|
|
const firstInputElement = $("input:not(:hidden, :disabled)").first();
|
2018-12-04 18:48:53 +01:00
|
|
|
// Focus on the first input field in the form.
|
|
|
|
common.autofocus(firstInputElement);
|
2020-07-15 00:34:28 +02:00
|
|
|
} else {
|
|
|
|
// If input field with errors is present.
|
2018-12-04 18:50:13 +01:00
|
|
|
// Find the input field having errors and stores it in a variable.
|
2020-07-15 00:34:28 +02:00
|
|
|
const inputElementWithError = $(".help-inline:not(:empty)")
|
|
|
|
.first()
|
|
|
|
.parent()
|
|
|
|
.find("input");
|
2018-12-04 18:50:13 +01:00
|
|
|
// Focus on the input field having errors.
|
|
|
|
common.autofocus(inputElementWithError);
|
2018-12-03 21:22:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset error message displays
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#id_team_subdomain_error_client").css("display", "none");
|
|
|
|
if ($(".team_subdomain_error_server").text() === "") {
|
|
|
|
$(".team_subdomain_error_server").css("display", "none");
|
2018-12-03 21:22:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$("#timezone").val(moment.tz.guess());
|
|
|
|
}
|
|
|
|
|
2019-03-16 07:14:49 +01:00
|
|
|
// Code in this block will be executed when the /accounts/send_confirm
|
|
|
|
// endpoint is visited i.e. accounts_send_confirm.html is rendered.
|
|
|
|
if ($("[data-page-id='accounts-send-confirm']").length > 0) {
|
2020-07-20 21:26:58 +02:00
|
|
|
$("#resend_email_link").on("click", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".resend_confirm").submit();
|
2019-03-16 07:14:49 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-02 09:43:15 +01:00
|
|
|
// Code in this block will be executed when the user visits /register
|
|
|
|
// i.e. accounts_home.html is rendered.
|
|
|
|
if ($("[data-page-id='accounts-home']").length > 0) {
|
|
|
|
if (window.location.hash.substring(0, 1) === "#") {
|
|
|
|
document.email_form.action += window.location.hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 09:41:10 +01:00
|
|
|
// Code in this block will be executed when the user is at login page
|
|
|
|
// i.e. login.html is rendered.
|
|
|
|
if ($("[data-page-id='login-page']").length > 0) {
|
|
|
|
if (window.location.hash.substring(0, 1) === "#") {
|
|
|
|
/* We append the location.hash to the formaction so that URL can be
|
|
|
|
preserved after user is logged in. See this:
|
|
|
|
https://stackoverflow.com/questions/5283395/url-hash-is-persisting-between-redirects */
|
2020-07-15 01:29:15 +02:00
|
|
|
const email_formaction = $("#login_form").attr("action");
|
|
|
|
$("#login_form").attr("action", email_formaction + "/" + window.location.hash);
|
|
|
|
$(".social_login_form input[name='next']").attr("value", "/" + window.location.hash);
|
2019-03-02 09:41:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 18:51:36 +02:00
|
|
|
$("#send_confirm").validate({
|
2017-08-15 21:59:44 +02:00
|
|
|
errorElement: "div",
|
2016-12-02 14:06:06 +01:00
|
|
|
errorPlacement: function (error) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".email-frontend-error").empty();
|
2018-03-22 22:07:19 +01:00
|
|
|
$("#send_confirm .alert.email-backend-error").remove();
|
|
|
|
error.appendTo(".email-frontend-error").addClass("text-error");
|
2012-10-03 23:43:25 +02:00
|
|
|
},
|
2013-04-03 19:08:11 +02:00
|
|
|
success: function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#errors").empty();
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2012-09-25 22:58:59 +02:00
|
|
|
});
|
2013-07-06 03:38:37 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
$(".register-page #email, .login-page-container #id_username").on("focusout keydown", function (
|
|
|
|
e,
|
|
|
|
) {
|
2017-09-27 20:17:15 +02:00
|
|
|
// check if it is the "focusout" or if it is a keydown, then check if
|
|
|
|
// the keycode was the one for "enter" (13).
|
|
|
|
if (e.type === "focusout" || e.which === 13) {
|
|
|
|
$(this).val($.trim($(this).val()));
|
|
|
|
}
|
|
|
|
});
|
2017-10-06 03:58:09 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const show_subdomain_section = function (bool) {
|
|
|
|
const action = bool ? "hide" : "show";
|
2017-10-06 03:58:09 +02:00
|
|
|
$("#subdomain_section")[action]();
|
|
|
|
};
|
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
$("#realm_in_root_domain").on("change", function () {
|
2017-10-06 03:58:09 +02:00
|
|
|
show_subdomain_section($(this).is(":checked"));
|
|
|
|
});
|
2017-12-07 04:26:44 +01:00
|
|
|
|
|
|
|
$("#login_form").validate({
|
|
|
|
errorClass: "text-error",
|
|
|
|
wrapper: "div",
|
|
|
|
submitHandler: function (form) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#login_form").find(".loader").css("display", "inline-block");
|
2017-12-07 04:26:44 +01:00
|
|
|
$("#login_form").find("button .text").hide();
|
|
|
|
|
|
|
|
form.submit();
|
|
|
|
},
|
2018-02-02 02:26:16 +01:00
|
|
|
invalidHandler: function () {
|
|
|
|
// this removes all previous errors that were put on screen
|
|
|
|
// by the server.
|
|
|
|
$("#login_form .alert.alert-error").remove();
|
|
|
|
},
|
2019-01-09 08:20:57 +01:00
|
|
|
showErrors: function (error_map) {
|
|
|
|
if (error_map.password) {
|
|
|
|
$("#login_form .alert.alert-error").remove();
|
|
|
|
}
|
|
|
|
this.defaultShowErrors();
|
|
|
|
},
|
2017-12-07 04:26:44 +01:00
|
|
|
});
|
2018-01-25 19:08:40 +01:00
|
|
|
|
|
|
|
function check_subdomain_avilable(subdomain) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const url = "/json/realm/subdomain/" + subdomain;
|
2020-07-02 01:45:54 +02:00
|
|
|
$.get(url, (response) => {
|
2018-01-25 19:08:40 +01:00
|
|
|
if (response.msg !== "available") {
|
|
|
|
$("#id_team_subdomain_error_client").html(response.msg);
|
|
|
|
$("#id_team_subdomain_error_client").show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-17 22:18:46 +02:00
|
|
|
function update_full_name_section() {
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
$("#source_realm_select").length &&
|
|
|
|
$("#source_realm_select").find(":selected").val() !== "on"
|
|
|
|
) {
|
2018-06-17 22:18:46 +02:00
|
|
|
$("#full_name_input_section").hide();
|
|
|
|
$("#profile_info_section").show();
|
2020-07-15 01:29:15 +02:00
|
|
|
const avatar_url = $("#source_realm_select").find(":selected").attr("data-avatar");
|
|
|
|
const full_name = $("#source_realm_select").find(":selected").attr("data-full-name");
|
2018-06-17 22:18:46 +02:00
|
|
|
$("#profile_full_name").text(full_name);
|
|
|
|
$("#id_full_name").val(full_name);
|
|
|
|
$("#profile_avatar").attr("src", avatar_url);
|
|
|
|
} else {
|
|
|
|
$("#full_name_input_section").show();
|
|
|
|
$("#profile_info_section").hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
$("#source_realm_select").on("change", update_full_name_section);
|
2018-06-17 22:18:46 +02:00
|
|
|
update_full_name_section();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let timer;
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#id_team_subdomain").on("keydown", () => {
|
|
|
|
$(".team_subdomain_error_server").text("").css("display", "none");
|
|
|
|
$("#id_team_subdomain_error_client").css("display", "none");
|
2018-01-25 19:08:40 +01:00
|
|
|
clearTimeout(timer);
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#id_team_subdomain").on("keyup", () => {
|
2018-01-25 19:08:40 +01:00
|
|
|
clearTimeout(timer);
|
2020-07-15 01:29:15 +02:00
|
|
|
timer = setTimeout(check_subdomain_avilable, 250, $("#id_team_subdomain").val());
|
2018-01-25 19:08:40 +01:00
|
|
|
});
|
2019-08-03 12:01:07 +02:00
|
|
|
|
|
|
|
// GitHub auth
|
|
|
|
$("body").on("click", "#choose_email .choose-email-box", function () {
|
|
|
|
this.parentNode.submit();
|
|
|
|
});
|
2012-09-25 22:58:59 +02:00
|
|
|
});
|