js/settings_account.js: Remove hard-coded field no, clean template context.

This commit is contained in:
Yashashvi Dave
2018-05-01 23:03:33 +05:30
committed by Tim Abbott
parent 427b404b9b
commit 512ab5dbaf
4 changed files with 18 additions and 17 deletions

View File

@@ -607,10 +607,13 @@ function render(template_name, args) {
}()); }());
(function custom_user_profile_field() { (function custom_user_profile_field() {
var args = {field_name: "GitHub user name", field_id: 2, field_value: "@GitHub", field_type: "text"}; var field = {name: "GitHub user name", id: 2, hint: "Or link to profile"};
var args = {field: field, field_value: "@GitHub", field_type: "text"};
var html = render('custom-user-profile-field', args); var html = render('custom-user-profile-field', args);
assert.equal($(html).find('input').attr('id'), 2); assert.equal($(html).find('input').attr('id'), 2);
assert.equal($(html).find('input').val(), "@GitHub"); assert.equal($(html).find('input').val(), "@GitHub");
assert.equal($(html).find('.field_hint').text(), "Or link to profile");
assert.equal($(html).find('label').text(), "GitHub user name");
}()); }());
(function deactivate_stream_modal() { (function deactivate_stream_modal() {

View File

@@ -57,18 +57,18 @@ exports.add_custom_profile_fields_to_settings = function () {
var all_custom_fields = page_params.custom_profile_fields; var all_custom_fields = page_params.custom_profile_fields;
all_custom_fields.forEach(function (field) { all_custom_fields.forEach(function (field) {
var field_type = settings_profile_fields.field_type_id_to_string(field.type);
var type; var type;
var value = people.my_custom_profile_data(field.id); var value = people.my_custom_profile_data(field.id);
var is_long_text = field.type === 2; var is_long_text = field_type === "Long Text";
// 1 & 2 type represent textual data. if (field_type === "Long Text" || field_type === "Short Text") {
if (field.type === 1 || field.type === 2) {
type = "text"; type = "text";
} else if (field.type === 3) { } else if (field_type === "Choice") {
type = "choice"; type = "choice";
} else if (field.type === 4) { } else if (field_type === "Date") {
type = "date"; type = "date";
} else if (field.type === 5) { } else if (field_type === "URL") {
type = "url"; type = "url";
} else { } else {
blueslip.error("Undefined field type."); blueslip.error("Undefined field type.");
@@ -78,12 +78,10 @@ exports.add_custom_profile_fields_to_settings = function () {
value = ""; value = "";
} }
var html = templates.render("custom-user-profile-field", {field_name: field.name, var html = templates.render("custom-user-profile-field", {field: field,
field_id: field.id,
field_type: type, field_type: type,
field_value: value, field_value: value,
is_long_text_field: is_long_text, is_long_text_field: is_long_text,
field_hint: field.hint,
}); });
$("#account-settings .custom-profile-fields-form").append(html); $("#account-settings .custom-profile-fields-form").append(html);
}); });

View File

@@ -8,12 +8,12 @@ var meta = {
var order = []; var order = [];
function field_type_id_to_string(type_id) { exports.field_type_id_to_string = function (type_id) {
var name = _.find(page_params.custom_profile_field_types, function (type) { var name = _.find(page_params.custom_profile_field_types, function (type) {
return type[0] === type_id; return type[0] === type_id;
})[1]; })[1];
return name; return name;
} };
function delete_profile_field(e) { function delete_profile_field(e) {
e.preventDefault(); e.preventDefault();
@@ -180,7 +180,7 @@ exports.populate_profile_fields = function (profile_fields_data) {
id: profile_field.id, id: profile_field.id,
name: profile_field.name, name: profile_field.name,
hint: profile_field.hint, hint: profile_field.hint,
type: field_type_id_to_string(profile_field.type), type: exports.field_type_id_to_string(profile_field.type),
choices: choices, choices: choices,
is_choice_field: is_choice_field, is_choice_field: is_choice_field,
}, },

View File

@@ -1,9 +1,9 @@
<div class="user-name-section custom_user_field"> <div class="user-name-section custom_user_field">
<label for="{{ field_name }}" class="title">{{ field_name }}</label> <label for="{{ field.name }}" class="title">{{ field.name }}</label>
{{#if is_long_text_field}} {{#if is_long_text_field}}
<textarea name="{{ field_name }}" id="{{ field_id }}" maxlength="500">{{ field_value }}</textarea> <textarea name="{{ field.name }}" id="{{ field.id }}" maxlength="500">{{ field_value }}</textarea>
{{else}} {{else}}
<input type="{{ field_type }}" name="{{ field_name }}" id="{{ field_id }}" value="{{ field_value }}" maxlength="50" /> <input type="{{ field_type }}" name="{{ field.name }}" id="{{ field.id }}" value="{{ field_value }}" maxlength="50" />
{{/if}} {{/if}}
<div class="field_hint">{{ field_hint }}</div> <div class="field_hint">{{ field.hint }}</div>
</div> </div>