From 3996a702feaf087dda5f5a62767b988e221f32b7 Mon Sep 17 00:00:00 2001 From: Rob Cutmore Date: Sat, 9 Jul 2016 09:06:30 -0400 Subject: [PATCH 1/3] Clean up formatting of targets modal events --- static/js/app/users.js | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/static/js/app/users.js b/static/js/app/users.js index 14dc7d5d..0059a410 100644 --- a/static/js/app/users.js +++ b/static/js/app/users.js @@ -168,30 +168,30 @@ function load() { $(document).ready(function() { load() - // Setup the event listeners - // Handle manual additions + // Setup the event listeners + // Handle manual additions $("#targetForm").submit(function() { - targets.DataTable() - .row.add([ - escapeHtml($("#firstName").val()), - escapeHtml($("#lastName").val()), - escapeHtml($("#email").val()), - escapeHtml($("#position").val()), - '' - ]) - .draw() - $("#targetForm>div>input").val('') - $("#firstName").focus() - return false - }) - // Handle Deletion + targets.DataTable() + .row.add([ + escapeHtml($("#firstName").val()), + escapeHtml($("#lastName").val()), + escapeHtml($("#email").val()), + escapeHtml($("#position").val()), + '' + ]) + .draw(); + $("#targetForm>div>input").val(''); + $("#firstName").focus(); + return false; + }); + // Handle Deletion $("#targetsTable").on("click", "span>i.fa-trash-o", function() { targets.DataTable() .row($(this).parents('tr')) .remove() .draw(); - }) + }); $("#modal").on("hide.bs.modal", function() { - dismiss() - }) -}) + dismiss(); + }); +}); From 21c55c579c4808cc0b005dbdb70a666e793eb44a Mon Sep 17 00:00:00 2001 From: Rob Cutmore Date: Sat, 9 Jul 2016 15:46:06 -0400 Subject: [PATCH 2/3] Add/update group target as needed When user submits a new target, target data table is checked for an existing entry based on target email. If an existing entry is found then it is updated, otherwise a new row is added to table. Target email addresses are now converted to lower case to help prevent duplicates as well. --- static/js/app/users.js | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/static/js/app/users.js b/static/js/app/users.js index 0059a410..84cb2a5d 100644 --- a/static/js/app/users.js +++ b/static/js/app/users.js @@ -171,15 +171,34 @@ $(document).ready(function() { // Setup the event listeners // Handle manual additions $("#targetForm").submit(function() { - targets.DataTable() - .row.add([ - escapeHtml($("#firstName").val()), - escapeHtml($("#lastName").val()), - escapeHtml($("#email").val()), - escapeHtml($("#position").val()), - '' - ]) - .draw(); + // Create new data row. + var emailInput = escapeHtml($("#email").val()).toLowerCase(); + var newRow = [ + escapeHtml($("#firstName").val()), + escapeHtml($("#lastName").val()), + emailInput, + escapeHtml($("#position").val()), + '' + ]; + + // Check table to see if email already exists. + var targetsTable = targets.DataTable(); + var existingRowIndex = targetsTable + .column(2, {order: "index"}) // Email column + .data() + .indexOf(emailInput); + + // Update or add new row as necessary. + if (existingRowIndex >= 0) { + targetsTable + .row(existingRowIndex, {order: "index"}) + .data(newRow); + } else { + targetsTable.row.add(newRow); + } + targetsTable.draw(); + + // Reset user input. $("#targetForm>div>input").val(''); $("#firstName").focus(); return false; From f88e68077ef0bc6302168a0864ed0feeae0341c5 Mon Sep 17 00:00:00 2001 From: Rob Cutmore Date: Sat, 9 Jul 2016 16:15:47 -0400 Subject: [PATCH 3/3] Add/update table when adding targets via CSV too --- static/js/app/users.js | 73 ++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/static/js/app/users.js b/static/js/app/users.js index 84cb2a5d..354b5e87 100644 --- a/static/js/app/users.js +++ b/static/js/app/users.js @@ -94,14 +94,11 @@ function edit(idx) { }, done: function(e, data) { $.each(data.result, function(i, record) { - targets.DataTable() - .row.add([ - escapeHtml(record.first_name), - escapeHtml(record.last_name), - escapeHtml(record.email), - escapeHtml(record.position), - '' - ]).draw() + addTarget( + record.first_name, + record.last_name, + record.email, + record.position); }); } }) @@ -117,6 +114,35 @@ function deleteGroup(idx) { } } +function addTarget(firstNameInput, lastNameInput, emailInput, positionInput) { + // Create new data row. + var email = escapeHtml(emailInput).toLowerCase(); + var newRow = [ + escapeHtml(firstNameInput), + escapeHtml(lastNameInput), + email, + escapeHtml(positionInput), + '' + ]; + + // Check table to see if email already exists. + var targetsTable = targets.DataTable(); + var existingRowIndex = targetsTable + .column(2, {order: "index"}) // Email column has index of 2 + .data() + .indexOf(email); + + // Update or add new row as necessary. + if (existingRowIndex >= 0) { + targetsTable + .row(existingRowIndex, {order: "index"}) + .data(newRow); + } else { + targetsTable.row.add(newRow); + } + targetsTable.draw(); +} + function load() { $("#groupTable").hide() $("#emptyMessage").hide() @@ -171,32 +197,11 @@ $(document).ready(function() { // Setup the event listeners // Handle manual additions $("#targetForm").submit(function() { - // Create new data row. - var emailInput = escapeHtml($("#email").val()).toLowerCase(); - var newRow = [ - escapeHtml($("#firstName").val()), - escapeHtml($("#lastName").val()), - emailInput, - escapeHtml($("#position").val()), - '' - ]; - - // Check table to see if email already exists. - var targetsTable = targets.DataTable(); - var existingRowIndex = targetsTable - .column(2, {order: "index"}) // Email column - .data() - .indexOf(emailInput); - - // Update or add new row as necessary. - if (existingRowIndex >= 0) { - targetsTable - .row(existingRowIndex, {order: "index"}) - .data(newRow); - } else { - targetsTable.row.add(newRow); - } - targetsTable.draw(); + addTarget( + $("#firstName").val(), + $("#lastName").val(), + $("#email").val(), + $("#position").val()); // Reset user input. $("#targetForm>div>input").val('');