fix for array additions in DS settings

This commit is contained in:
Stephen Birarda 2017-06-15 14:59:12 -07:00
parent ea6a38148c
commit 0565cfa2b7

View file

@ -39,7 +39,8 @@ var Settings = {
ACCESS_TOKEN_SELECTOR: '[name="metaverse.access_token"]', ACCESS_TOKEN_SELECTOR: '[name="metaverse.access_token"]',
PLACES_TABLE_ID: 'places-table', PLACES_TABLE_ID: 'places-table',
FORM_ID: 'settings-form', FORM_ID: 'settings-form',
INVALID_ROW_CLASS: 'invalid-input' INVALID_ROW_CLASS: 'invalid-input',
DATA_ROW_INDEX: 'data-row-index'
}; };
var viewHelpers = { var viewHelpers = {
@ -1159,7 +1160,8 @@ function makeTable(setting, keypath, setting_value) {
html += "<tr class='" + Settings.DATA_ROW_CLASS + "' " + html += "<tr class='" + Settings.DATA_ROW_CLASS + "' " +
(isCategorized ? ("data-category='" + categoryValue + "'") : "") + " " + (isCategorized ? ("data-category='" + categoryValue + "'") : "") + " " +
(isArray ? "" : "name='" + keypath + "." + rowIndexOrName + "'") + ">"; (isArray ? "" : "name='" + keypath + "." + rowIndexOrName + "'") +
(isArray ? Settings.DATA_ROW_INDEX + "='" + (row_num - 1) + "'" : "" ) + ">";
if (setting.numbered === true) { if (setting.numbered === true) {
html += "<td class='numbered'>" + row_num + "</td>" html += "<td class='numbered'>" + row_num + "</td>"
@ -1398,6 +1400,15 @@ function addTableRow(row) {
var setting_name = table.attr("name"); var setting_name = table.attr("name");
row.addClass(Settings.DATA_ROW_CLASS + " " + Settings.NEW_ROW_CLASS); row.addClass(Settings.DATA_ROW_CLASS + " " + Settings.NEW_ROW_CLASS);
// if this is an array, add the row index (which is the index of the last row + 1)
// as a data attribute to the row
var row_index = 0;
if (isArray) {
var previous_row_index = parseInt(row.siblings('.' + Settings.DATA_ROW_CLASS + ':last').attr(Settings.DATA_ROW_INDEX), 10);
row_index = previous_row_index + 1;
row.attr(Settings.DATA_ROW_INDEX, row_index);
}
var focusChanged = false; var focusChanged = false;
_.each(row.children(), function(element) { _.each(row.children(), function(element) {
@ -1430,7 +1441,6 @@ function addTableRow(row) {
var isDropdown = input.hasClass("table-dropdown"); var isDropdown = input.hasClass("table-dropdown");
if (isArray) { if (isArray) {
var row_index = row.siblings('.' + Settings.DATA_ROW_CLASS).length
var key = $(element).attr('name'); var key = $(element).attr('name');
// are there multiple columns or just one? // are there multiple columns or just one?
@ -1438,17 +1448,14 @@ function addTableRow(row) {
var num_columns = row.children('.' + Settings.DATA_COL_CLASS).length var num_columns = row.children('.' + Settings.DATA_COL_CLASS).length
var newName = setting_name + "[" + row_index + "]" + (num_columns > 1 ? "." + key : ""); var newName = setting_name + "[" + row_index + "]" + (num_columns > 1 ? "." + key : "");
if (isCheckbox) { input.attr("name", newName);
input.attr("name", newName)
} else {
if (isDropdown) { if (isDropdown) {
// default values for hidden inputs inside child selects gets cleared so we need to remind it // default values for hidden inputs inside child selects gets cleared so we need to remind it
var selectElement = $(element).children("select"); var selectElement = $(element).children("select");
selectElement.attr("data-hidden-input", newName); selectElement.attr("data-hidden-input", newName);
$(element).children("input").val(selectElement.val()); $(element).children("input").val(selectElement.val());
} }
input.attr("name", newName);
}
} else { } else {
// because the name of the setting in question requires the key // because the name of the setting in question requires the key
// setup a hook to change the HTML name of the element whenever the key changes // setup a hook to change the HTML name of the element whenever the key changes