Commit 68e488f6 authored by Tejesh P's avatar Tejesh P
Browse files

ENH: formhandler table to not allow add and edit happen at same time, fixes #123 by @sindhura.ch

also refactor existing tests to use formhandler_csv.json over formhandler.json
parent 3c9c35b8
Pipeline #71276 passed with stage
in 2 minutes and 4 seconds
......@@ -255,8 +255,10 @@ function addHandler($this, template_data, options, template) {
$('.add button', $this)
.on('click', function () {
var add_btn = $('.add button', $this)
var edit_btn = $('.edit button', $this)
if (add_btn.html().toLowerCase() == 'save') {
add_btn.html('Add')
edit_btn.prop('disabled', false)
var columns_data = $('.new-row input[data-key]')
$('.loader', $this).removeClass('d-none')
......@@ -265,6 +267,13 @@ function addHandler($this, template_data, options, template) {
$.each(columns_data, function (key, column) {
data[column.getAttribute('data-key')] = column.value
})
// if no changes made to new empty celled row, rerender the table.
if (!_.some(data)) {
$('.loader', $this).addClass('d-none')
template_data.isAdd = false
render_template('table', template_data, options, $this, template)
return
}
$.ajax(options.src, {
method: 'POST',
......@@ -279,6 +288,7 @@ function addHandler($this, template_data, options, template) {
.fail(failHandler.bind(this, $this, template))
} else if (add_btn.html().toLowerCase() == 'add') {
add_btn.html('Save')
edit_btn.prop('disabled', true)
template_data.isAdd = true
render_template('table', template_data, options, $this, template)
add_edit_events($this, add_btn)
......@@ -290,8 +300,10 @@ function editHandler($this, template_data, options, template) {
$('.edit button', $this)
.on('click', function () {
var edit_btn = $('.edit button', $this)
var add_btn = $('.add button', $this)
if (edit_btn.html().toLowerCase() == 'save') {
edit_btn.html('Edit') // TODO: remove hardcoding of name Edit
add_btn.prop('disabled', false)
var edited_rows = $('.edited-row')
if (edited_rows.length > 0)
$('.loader', $this).removeClass('d-none')
......@@ -327,6 +339,7 @@ function editHandler($this, template_data, options, template) {
render_template('table', template_data, options, $this, template)
} else if (edit_btn.html().toLowerCase() == 'edit') {
edit_btn.html('Save') // TODO: remove hardcoding of name Save
add_btn.prop('disabled', true)
template_data.isEdit = true
render_template('table', template_data, options, $this, template)
add_edit_events($this, edit_btn)
......
Continent,Cross,ID,Name,Shapes,Stripes,Symbols,Text,Union-Flag,c1,c2,c3,c4,c5,c6,c7,c8,date_col
E,,AND,Andorra,,Horizontal,,,,35,1,26,0,32,0,0,4,16-01-2013
Europe,,AND,Andorra,,Horizontal,,,,35,1,26,0,32,0,0,4,16-01-2013
Asia,,ARE,United Arab Emirates,,Horizontal,,,,24.0,0.0,0.0,25.0,0.0,0.0,25.0,24.0,17-02-2013
Asia,,AFG,Afghanistan,,Vertical,,Country,,28.0,1.0,0.0,33.0,0.0,0.0,33.0,3.0,06-02-2013
North America,,ATG,Antigua Barbuda,,,,,,50.0,0.0,5.0,0.0,0.0,10.0,25.0,7.0,11-02-2013
......
[
{
"ID": "AND",
"c1": 35,
"c2": 1,
"c3": 26,
"c4": 0,
"c5": 32,
"c6": 0,
"c7": 0,
"c8": 4,
"Name": "Andorra",
"Continent": "Europe",
"Symbols": null,
"Shapes": null,
"Stripes": "Vertical",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "ARE",
"c1": 24,
"c2": 0,
"c3": 0,
"c4": 25,
"c5": 0,
"c6": 0,
"c7": 25,
"c8": 24,
"Name": "United Arab Emirates",
"Continent": "Asia",
"Symbols": null,
"Shapes": null,
"Stripes": "Horizontal",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "AFG",
"c1": 28,
"c2": 1,
"c3": 0,
"c4": 33,
"c5": 0,
"c6": 0,
"c7": 33,
"c8": 3,
"Name": "Afghanistan",
"Continent": "Asia",
"Symbols": null,
"Shapes": null,
"Stripes": "Vertical",
"Cross": null,
"Union-Flag": null,
"Text": "Country"
},
{
"ID": "ATG",
"c1": 50,
"c2": 0,
"c3": 5,
"c4": 0,
"c5": 0,
"c6": 10,
"c7": 25,
"c8": 7,
"Name": "Antigua Barbuda",
"Continent": "North America",
"Symbols": null,
"Shapes": null,
"Stripes": null,
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "ALB",
"c1": 87,
"c2": 0,
"c3": 0,
"c4": 0,
"c5": 0,
"c6": 0,
"c7": 12,
"c8": 0,
"Name": "Albania",
"Continent": "Europe",
"Symbols": "Bird",
"Shapes": null,
"Stripes": null,
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "ARM",
"c1": 33,
"c2": 33,
"c3": 0,
"c4": 0,
"c5": 33,
"c6": 0,
"c7": 0,
"c8": 0,
"Name": "Armenia",
"Continent": "Asia",
"Symbols": null,
"Shapes": null,
"Stripes": "Horizontal",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "AGO",
"c1": 48,
"c2": 0,
"c3": 4,
"c4": 0,
"c5": 0,
"c6": 0,
"c7": 46,
"c8": 0,
"Name": "Angola",
"Continent": "Africa",
"Symbols": "Weapon, Agriculture",
"Shapes": null,
"Stripes": "Horizontal",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "ARG",
"c1": 0,
"c2": 2,
"c3": 0,
"c4": 0,
"c5": 0,
"c6": 66,
"c7": 0,
"c8": 31,
"Name": "Argentina",
"Continent": "South America",
"Symbols": "Sun",
"Shapes": null,
"Stripes": "Horizontal",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "AUT",
"c1": 66,
"c2": 0,
"c3": 0,
"c4": 0,
"c5": 0,
"c6": 0,
"c7": 0,
"c8": 33,
"Name": "Austria",
"Continent": "Europe",
"Symbols": null,
"Shapes": null,
"Stripes": "Horizontal",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "AUS",
"c1": 9,
"c2": 0,
"c3": 0,
"c4": 0,
"c5": 79,
"c6": 0,
"c7": 0,
"c8": 11,
"Name": "Australia",
"Continent": "Oceania",
"Symbols": null,
"Shapes": "Stars",
"Stripes": null,
"Cross": null,
"Union-Flag": "Yes",
"Text": null
},
{
"ID": "AZE",
"c1": 31,
"c2": 0,
"c3": 0,
"c4": 33,
"c5": 0,
"c6": 33,
"c7": 0,
"c8": 1,
"Name": "Azerbaijan",
"Continent": "Asia",
"Symbols": null,
"Shapes": "Stars, Crescent",
"Stripes": "Horizontal",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BIH",
"c1": 0,
"c2": 0,
"c3": 25,
"c4": 0,
"c5": 70,
"c6": 0,
"c7": 0,
"c8": 4,
"Name": "Bosnia Herzegovina",
"Continent": "Europe",
"Symbols": null,
"Shapes": null,
"Stripes": null,
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BRB",
"c1": 0,
"c2": 0,
"c3": 29,
"c4": 0,
"c5": 66,
"c6": 0,
"c7": 3,
"c8": 0,
"Name": "Barbados",
"Continent": "North America",
"Symbols": "Emblem",
"Shapes": null,
"Stripes": "Vertical",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BGD",
"c1": 20,
"c2": 0,
"c3": 0,
"c4": 79,
"c5": 0,
"c6": 0,
"c7": 0,
"c8": 0,
"Name": "Bangladesh",
"Continent": "Asia",
"Symbols": "Sun",
"Shapes": "Circle",
"Stripes": null,
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BEL",
"c1": 33,
"c2": 0,
"c3": 33,
"c4": 0,
"c5": 0,
"c6": 0,
"c7": 33,
"c8": 0,
"Name": "Belgium",
"Continent": "Europe",
"Symbols": null,
"Shapes": null,
"Stripes": "Vertical",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BFA",
"c1": 49,
"c2": 0,
"c3": 2,
"c4": 48,
"c5": 0,
"c6": 0,
"c7": 0,
"c8": 0,
"Name": "Burkina Faso",
"Continent": "Africa",
"Symbols": null,
"Shapes": null,
"Stripes": "Horizontal",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BGR",
"c1": 33,
"c2": 0,
"c3": 0,
"c4": 33,
"c5": 0,
"c6": 0,
"c7": 0,
"c8": 33,
"Name": "Bulgaria",
"Continent": "Europe",
"Symbols": null,
"Shapes": null,
"Stripes": "Horizontal",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BHR",
"c1": 67,
"c2": 0,
"c3": 0,
"c4": 0,
"c5": 0,
"c6": 0,
"c7": 0,
"c8": 32,
"Name": "Bahrain",
"Continent": "Asia",
"Symbols": null,
"Shapes": null,
"Stripes": null,
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BDI",
"c1": 30,
"c2": 0,
"c3": 0,
"c4": 34,
"c5": 0,
"c6": 0,
"c7": 0,
"c8": 34,
"Name": "Burundi",
"Continent": "Africa",
"Symbols": null,
"Shapes": "Stars",
"Stripes": "Diagonal",
"Cross": "Yes",
"Union-Flag": null,
"Text": null
},
{
"ID": "BEN",
"c1": 30,
"c2": 0,
"c3": 29,
"c4": 40,
"c5": 0,
"c6": 0,
"c7": 0,
"c8": 0,
"Name": "Benin",
"Continent": "Africa",
"Symbols": null,
"Shapes": null,
"Stripes": "Horizontal",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BRN",
"c1": 6,
"c2": 0,
"c3": 54,
"c4": 0,
"c5": 0,
"c6": 0,
"c7": 17,
"c8": 21,
"Name": "Brunei Darussalam",
"Continent": "Asia",
"Symbols": null,
"Shapes": null,
"Stripes": null,
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BOL",
"c1": 33,
"c2": 0,
"c3": 33,
"c4": 33,
"c5": 0,
"c6": 0,
"c7": 0,
"c8": 0,
"Name": "Bolivia",
"Continent": "South America",
"Symbols": null,
"Shapes": null,
"Stripes": "Horizontal",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BRA",
"c1": 0,
"c2": 0,
"c3": 17,
"c4": 68,
"c5": 12,
"c6": 0,
"c7": 0,
"c8": 1,
"Name": "Brazil",
"Continent": "South America",
"Symbols": null,
"Shapes": "Stars, Circle",
"Stripes": null,
"Cross": null,
"Union-Flag": null,
"Text": "Mottos"
},
{
"ID": "BHS",
"c1": 0,
"c2": 0,
"c3": 21,
"c4": 0,
"c5": 0,
"c6": 57,
"c7": 21,
"c8": 0,
"Name": "Bahamas",
"Continent": "North America",
"Symbols": null,
"Shapes": "Triangle",
"Stripes": "Horizontal",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BTN",
"c1": 41,
"c2": 1,
"c3": 43,
"c4": 0,
"c5": 0,
"c6": 0,
"c7": 0,
"c8": 14,
"Name": "Bhutan",
"Continent": "Asia",
"Symbols": "Animal",
"Shapes": null,
"Stripes": null,
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BWA",
"c1": 0,
"c2": 0,
"c3": 0,
"c4": 0,
"c5": 0,
"c6": 74,
"c7": 16,
"c8": 8,
"Name": "Botswana",
"Continent": "Africa",
"Symbols": null,
"Shapes": null,
"Stripes": "Horizontal",
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BLR",
"c1": 64,
"c2": 0,
"c3": 0,
"c4": 29,
"c5": 0,
"c6": 0,
"c7": 0,
"c8": 6,
"Name": "Belarus",
"Continent": "Europe",
"Symbols": null,
"Shapes": null,
"Stripes": null,
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "BLZ",
"c1": 17,
"c2": 0,
"c3": 0,
"c4": 5,
"c5": 54,
"c6": 0,
"c7": 1,
"c8": 21,
"Name": "Belize",
"Continent": "North America",
"Symbols": null,
"Shapes": null,
"Stripes": null,
"Cross": null,
"Union-Flag": null,
"Text": null
},
{
"ID": "CAN",
"c1": 63,
"c2": 0,
"c3": 0,
"c4": 0,
"c5": 0,
"c6": 0,
"c7": 0,
"c8": 36,
"Name": "Canada",
"Continent": "North America",
"Symbols": "Plant",
"Shapes": null,
"Stripes": "Vertical",
"Cross": null,
"Union-Flag": null,
"Text": null