common.js: Migrate common.js module to use IIFE module style.

This module was exposing its functions as globals. This PR fixes
it use the IIFE module style that we use in our other modules.
This commit is contained in:
Harshit Bansal
2017-06-23 01:38:43 +05:30
committed by showell
parent f228700ef2
commit 6615f2f2e8
14 changed files with 35 additions and 28 deletions

View File

@@ -23,8 +23,6 @@
"DynamicText": false,
"bridge": false,
"page_params": false,
"status_classes": false,
"password_quality": false,
"attachments_ui": false,
"csrf_token": false,
"typeahead_helper": false,
@@ -153,7 +151,8 @@
"Clipboard": false,
"emoji_picker": false,
"hotspots": false,
"compose_ui": false
"compose_ui": false,
"common": false
},
"rules": {
"array-callback-return": "error",

View File

@@ -64,7 +64,9 @@ set_global('unread_ops', {
mark_message_as_read: noop,
});
set_global('status_classes', 'status_classes');
set_global('common', {
status_classes: 'status_classes',
});
function stub_selected_message(msg) {
set_global('current_msg_list', {

View File

@@ -1,10 +1,14 @@
var status_classes = 'alert-error alert-success alert-info';
var common = (function () {
function autofocus(selector) {
var exports = {};
exports.status_classes = 'alert-error alert-success alert-info';
exports.autofocus = function (selector) {
$(function () {
$(selector)[0].focus();
});
}
};
// Return a boolean indicating whether the password is acceptable.
// Also updates a Bootstrap progress bar control (a jQuery object)
@@ -14,7 +18,7 @@ function autofocus(selector) {
//
// This is in common.js because we want to use it from the signup page
// and also from the in-app password change interface.
function password_quality(password, bar, password_field) {
exports.password_quality = function (password, bar, password_field) {
// We load zxcvbn.js asynchronously, so the variable might not be set.
if (typeof zxcvbn === 'undefined') {
return undefined;
@@ -56,10 +60,12 @@ function password_quality(password, bar, password_field) {
}
return acceptable;
}
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports.status_classes = status_classes;
module.exports.autofocus = autofocus;
module.exports.password_quality = password_quality;
module.exports = common;
}

View File

@@ -154,7 +154,7 @@ function create_message_object() {
exports.create_message_object = create_message_object;
function compose_error(error_text, bad_input) {
$('#send-status').removeClass(status_classes)
$('#send-status').removeClass(common.status_classes)
.addClass('alert-error')
.stop(true).fadeTo(0, 1);
$('#error-msg').html(error_text);

View File

@@ -76,7 +76,7 @@ function show_box(msg_type, opts) {
$("#stream_toggle").removeClass("active");
$("#private_message_toggle").addClass("active");
}
$("#send-status").removeClass(status_classes).hide();
$("#send-status").removeClass(common.status_classes).hide();
$('#compose').css({visibility: "visible"});
$(".new_message_textarea").css("min-height", "3em");

View File

@@ -3,7 +3,7 @@ $(function () {
// some of the jQuery selectors below will return empty lists.
$.validator.addMethod('password_strength', function (value) {
return password_quality(value, undefined, $('#id_password, #id_new_password1'));
return common.password_quality(value, undefined, $('#id_password, #id_new_password1'));
}, "Password is too weak.");
function highlight(class_to_add) {
@@ -38,7 +38,7 @@ $(function () {
$('#id_password, #id_new_password1').on('change keyup', function () {
// Update the password strength bar even if we aren't validating
// the field yet.
password_quality($(this).val(), $('#pw_strength .bar'), $(this));
common.password_quality($(this).val(), $('#pw_strength .bar'), $(this));
});
$("#send_confirm").validate({

View File

@@ -106,7 +106,7 @@ exports.set_up = function () {
$('#new_password').on('change keyup', function () {
var field = $('#new_password');
password_quality(field.val(), $('#pw_strength .bar'), field);
common.password_quality(field.val(), $('#pw_strength .bar'), field);
});
$("form.your-account-settings").ajaxForm({
@@ -118,7 +118,7 @@ exports.set_up = function () {
var field = $('#new_password');
var new_pw = $('#new_password').val();
if (new_pw !== '') {
var password_ok = password_quality(new_pw, undefined, field);
var password_ok = common.password_quality(new_pw, undefined, field);
if (password_ok === undefined) {
// zxcvbn.js didn't load, for whatever reason.
settings_change_error(

View File

@@ -19,10 +19,10 @@ exports.message = function (response, status_box, cls, type) {
}
if (type === 'subscriptions-status') {
status_box.removeClass(status_classes).addClass(cls).children('#response')
status_box.removeClass(common.status_classes).addClass(cls).children('#response')
.text(response).stop(true).fadeTo(0, 1);
} else {
status_box.removeClass(status_classes).addClass(cls)
status_box.removeClass(common.status_classes).addClass(cls)
.text(response).stop(true).fadeTo(0, 1);
}

View File

@@ -6,7 +6,7 @@
{% block portico_content %}
<script type="text/javascript">
$(function () {
autofocus('#email');
common.autofocus('#email');
});
</script>
<div class="bg-image"></div>

View File

@@ -6,7 +6,7 @@
{% block portico_content %}
<script type="text/javascript">
$(function () {
autofocus('#email');
common.autofocus('#email');
});
</script>

View File

@@ -14,9 +14,9 @@
{% if password_auth_enabled %}
<script type="text/javascript">
{% if email %}
autofocus('#id_password');
common.autofocus('#id_password');
{% else %}
autofocus('#id_username');
common.autofocus('#id_username');
{% endif %}
</script>
{% endif %}

View File

@@ -215,9 +215,9 @@ Form is validated both client-side using jquery-validate (see signup.js) and ser
<script type="text/javascript">
if ($('#id_email:visible').length) {
autofocus('#id_email');
common.autofocus('#id_email');
} else if ($('#id_full_name:visible').length) {
autofocus('#id_full_name');
common.autofocus('#id_full_name');
}
$("[name=realm_org_type]").on("change", function () {

View File

@@ -41,7 +41,7 @@
</div>
<script type="text/javascript">
autofocus('#id_email');
common.autofocus('#id_email');
</script>
{% endblock %}

View File

@@ -69,7 +69,7 @@
</form>
<script type="text/javascript">
autofocus('#id_new_password1');
common.autofocus('#id_new_password1');
</script>
{% else %}
<p>{{ _('Sorry, the link you provided is invalid or has already been used.') }}</p>