From 56305914443affe6bec8c021e2b0bfe6288faa0f Mon Sep 17 00:00:00 2001 From: Sam Corbett Date: Thu, 16 Jul 2015 14:38:00 +0100 Subject: [PATCH] Boolean effector parameters are rendered as checkboxes. --- .../webapp/assets/js/view/effector-invoke.js | 22 +++++--- .../main/webapp/assets/tpl/apps/param.html | 8 +++ .../javascript/specs/model/effector-spec.js | 54 +++++++++++-------- .../specs/view/effector-invoke-spec.js | 35 ++++++------ .../fixtures/effector-summary-list.json | 8 ++- 5 files changed, 83 insertions(+), 44 deletions(-) diff --git a/usage/jsgui/src/main/webapp/assets/js/view/effector-invoke.js b/usage/jsgui/src/main/webapp/assets/js/view/effector-invoke.js index 8d8f714b04..7c9e0bd8e3 100644 --- a/usage/jsgui/src/main/webapp/assets/js/view/effector-invoke.js +++ b/usage/jsgui/src/main/webapp/assets/js/view/effector-invoke.js @@ -71,6 +71,9 @@ define([ // select the body of the table we just rendered and append params var $tbody = this.$("tbody") _(params).each(function (param) { + // TODO: this should be another view whose implementation is specific to + // the type of the parameter (i.e. text, dates, checkboxes etc. can all + // be handled separately). $tbody.append(that.effectorParam({ name:param.name, type:param.type, @@ -112,19 +115,26 @@ define([ }, extractParamsFromTable:function () { - var parameters = {} - + var parameters = {}; + // iterate over the rows + // TODO: this should be generic alongside the rendering of parameters. this.$(".effector-param").each(function (index) { var key = $(this).find(".param-name").text(); - var value = $(this).find(".param-value").attr('id') == 'selector-container' ? - $(this).find(".param-value option:selected").attr("value") : - $(this).find(".param-value").val(); + var valElement = $(this).find(".param-value"); + var value; + if (valElement.attr('id') == 'selector-container') { + value = $(this).find(".param-value option:selected").attr("value") + } else if (valElement.is(":checkbox")) { + value = ("checked" == valElement.attr("checked")) ? "true" : "false"; + } else { + value = valElement.val(); + } //treat empty field as null value if (value !== '') { parameters[key] = value; } - }) + }); return parameters }, diff --git a/usage/jsgui/src/main/webapp/assets/tpl/apps/param.html b/usage/jsgui/src/main/webapp/assets/tpl/apps/param.html index 8bd59049c1..b292012eab 100644 --- a/usage/jsgui/src/main/webapp/assets/tpl/apps/param.html +++ b/usage/jsgui/src/main/webapp/assets/tpl/apps/param.html @@ -26,6 +26,14 @@ server-side mechanism for populating options in some situations. --> <% if (name == 'locations' || name == 'location') { %>
+ <% } else if (type == 'java.lang.Boolean') { %> + + <% if (defaultValue) { %> + + <% } else { %> + + <% } %> + <% } else { %> diff --git a/usage/jsgui/src/test/javascript/specs/model/effector-spec.js b/usage/jsgui/src/test/javascript/specs/model/effector-spec.js index d525ee4750..5ffdebd6b6 100644 --- a/usage/jsgui/src/test/javascript/specs/model/effector-spec.js +++ b/usage/jsgui/src/test/javascript/specs/model/effector-spec.js @@ -19,30 +19,42 @@ define([ "model/effector-summary", "model/effector-param" ], function (EffectorSummary, EffectorParam) { - $.ajaxSetup({ async:false }); - + + $.ajaxSetup({async: false}); + describe("effector-spec: EffectorSummary model", function () { - var effectorCollection = new EffectorSummary.Collection - effectorCollection.url = "fixtures/effector-summary-list.json" - effectorCollection.fetch() + var effectorCollection = new EffectorSummary.Collection; + effectorCollection.url = "fixtures/effector-summary-list.json"; + effectorCollection.fetch(); - it("must have 3 objects", function () { - expect(effectorCollection.length).toBe(3) - }) + it("must have start, stop and restart effectors", function () { + var actual = effectorCollection.pluck("name").sort(); + var expected = ["restart", "start", "stop"].sort(); + expect(actual).toEqual(expected); + }); - it("has a first object 'name'", function () { - var effector1 = effectorCollection.at(0) - expect(effector1.get("name")).toBe("start") - expect(effector1.get("returnType")).toBe("void") - expect(effector1.get("parameters").length).toBe(1) - }) + describe("the start effector", function () { + var startEffector = effectorCollection.at(0); + it("has void return type and two parameters", function () { + expect(startEffector.get("name")).toBe("start"); + expect(startEffector.get("returnType")).toBe("void"); + expect(startEffector.get("parameters").length).toBe(2); + }); + + it("has a parameter named 'locations'", function () { + var parameter = new EffectorParam.Model(startEffector.getParameterByName("locations")); + expect(parameter.get("name")).toBe("locations"); + expect(parameter.get("type")).toBe("java.util.Collection"); + expect(parameter.get("description")).toBe("A list of locations"); + }); - it(" effector1 has a first parameter named 'locations'", function () { - var effector1 = effectorCollection.at(0) - var param1 = new EffectorParam.Model(effector1.getParameterByName("locations")) - expect(param1.get("name")).toBe("locations") - expect(param1.get("type")).toBe("java.util.Collection") - expect(param1.get("description")).toBe("") + it("has a parameter named 'booleanValue'", function () { + var parameter = new EffectorParam.Model(startEffector.getParameterByName("booleanValue")); + expect(parameter.get("name")).toBe("booleanValue"); + expect(parameter.get("type")).toBe("java.lang.Boolean"); + expect(parameter.get("description")).toBe("True or false"); + expect(parameter.get("defaultValue")).toBe(true); + }); }) }) -}) +}); diff --git a/usage/jsgui/src/test/javascript/specs/view/effector-invoke-spec.js b/usage/jsgui/src/test/javascript/specs/view/effector-invoke-spec.js index dbd7c8ed65..a2685bcca9 100644 --- a/usage/jsgui/src/test/javascript/specs/view/effector-invoke-spec.js +++ b/usage/jsgui/src/test/javascript/specs/view/effector-invoke-spec.js @@ -32,10 +32,12 @@ define([ locationsFixture.url = 'fixtures/location-list.json' locationsFixture.fetch() + const effector = collection.at(0); + var modalView = new EffectorInvokeView({ tagName:"div", className:"modal", - model:collection.at(0), + model: effector, entity:entityFixture.at(0), locations: locationsFixture }) @@ -44,10 +46,6 @@ define([ // render and keep the reference to the view modalView.render() - // Select the third item in the option list rather than the "None" and - // horizontal bar placeholders. - modalView.$(".select-location option:eq(2)").attr("selected", "selected"); - it("must render a bootstrap modal", function () { expect(modalView.$(".modal-header").length).toBe(1) expect(modalView.$(".modal-body").length).toBe(1) @@ -61,19 +59,24 @@ define([ }) it("must have the list of parameters in body", function () { - expect(modalView.$(".modal-body table").length).toBe(1) - expect(modalView.$(".modal-body tr").length).toBe(2) // one tr from the head - expect(modalView.$(".modal-body .param-name").html()).toBe("locations") - }) - it("must have two buttons in the footer", function () { - expect(modalView.$(".modal-footer button").length).toBe(2) - expect(modalView.$(".modal-footer button.invoke-effector").length).toBe(1) - }) + expect(modalView.$(".modal-body table").length).toBe(1); + // +1 because one from table head + expect(modalView.$(".modal-body tr").length).toBe(effector.get("parameters").length + 1) + }); it("must properly extract parameters from table", function () { - var params = modalView.extractParamsFromTable() + // Select the third item in the option list rather than the "None" and + // horizontal bar placeholders. + window.m = modalView; + modalView.$(".select-location option:eq(2)").attr("selected", "selected"); + + var params = modalView.extractParamsFromTable(); + console.log(params); expect(params["locations"]).toBe("123") - expect(params).toEqual({"locations": "123"}) - }) + expect(params).toEqual({ + "locations": "123", + "booleanValue": "true" + }); + }); }) }) \ No newline at end of file diff --git a/usage/rest-api/src/test/resources/fixtures/effector-summary-list.json b/usage/rest-api/src/test/resources/fixtures/effector-summary-list.json index dd7aaaa48a..fe2828c87d 100644 --- a/usage/rest-api/src/test/resources/fixtures/effector-summary-list.json +++ b/usage/rest-api/src/test/resources/fixtures/effector-summary-list.json @@ -7,7 +7,13 @@ { "name":"locations", "type":"java.util.Collection", - "description":null + "description":"A list of locations" + }, + { + "name":"booleanValue", + "type":"java.lang.Boolean", + "description":"True or false", + "defaultValue": true } ], "links":{