5195f9b6d81d4e168ff58fa7c8c6003bfa19c249
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / webapp / WEB-INF / tags / servicetemplates / boundarydefinitions / browseForReqOrCap.tag
1 <%--
2 /*******************************************************************************
3  * Copyright (c) 2014 University of Stuttgart.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and the Apache License 2.0 which both accompany this distribution,
7  * and are available at http://www.eclipse.org/legal/epl-v10.html
8  * and http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Contributors:
11  *    Oliver Kopp - initial API and implementation and/or initial documentation
12  *******************************************************************************/
13 --%>
14
15 <%@tag pageEncoding="UTF-8"%>
16
17 <%@attribute name="label" description="Requirement|Capability" required="true" %>
18 <%@attribute name="requirementsOrCapabilities" description="requirements|capabilities" required="true" %>
19 <%@attribute name="reqOrCap" description="requirement|capability" required="true" %>
20
21 <%@taglib prefix="b"  tagdir="/WEB-INF/tags/servicetemplates/boundarydefinitions"%>
22
23 <b:browseForX XShort="${reqOrCap}" XLong="${label}" />
24
25 <div class="modal fade" id="${reqOrCap}Diag">
26         <div class="modal-dialog">
27                 <div class="modal-content">
28                         <div class="modal-header">
29                                 <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
30                                 <h4 class="modal-title"><span id="addOrUpdate${reqOrCap}Span"></span> ${label}</h4>
31                         </div>
32                         <div class="modal-body">
33                                 <form>
34                                         <fieldset>
35                                                 <div class="form-group">
36                                                         <label for="${reqOrCap}Name">Name</label>
37                                                         <div>
38                                                                 <input name="${reqOrCap}Name" id="${reqOrCap}Name" class="form-control" type="text">
39                                                         </div>
40                                                 </div>
41                                                 <div class="form-group">
42                                                         <label for="${reqOrCap}RefDiv">${label}</label>
43                                                         <div id="${reqOrCap}RefDiv" class="row">
44                                                                 <div class="col-xs-10">
45                                                                         <input id="X${reqOrCap}Ref" class="form-control" type="text"> <%-- The input id is prefixed with "X" as "requirementRef" alone does not work --%>
46                                                                 </div>
47                                                                 <div class="col-xs-2">
48                                                                         <button type="button" class="btn btn-default btn-sm" onclick="browseFor${reqOrCap}($('#XReqRef'));">Browse</button>
49                                                                 </div>
50                                                         </div>
51                                                 </div>
52                                         </fieldset>
53                                 </form>
54                         </div>
55
56                         <div class="modal-footer">
57                                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
58                                 <button id="add${reqOrCap}"    type="button" class="btn btn-primary" onclick="addorUpdate${reqOrCap}(false);">Add</button>
59                                 <button id="delete${reqOrCap}" type="button" class="btn btn-danger" onclick="delete${reqOrCap}t();">Delete</button>
60                                 <button id="update${reqOrCap}" type="button" class="btn btn-primary" onclick="addorUpdate${reqOrCap}(true);">Update</button>
61                         </div>
62                 </div>
63         </div>
64 </div>
65
66
67 <script>
68 /**
69  * Triggered by the browse button at requirements. In the dialog Add/Change Requirement
70  */
71 function browseFor${reqOrCap}(field) {
72         $("#${reqOrCap}ReferenceField").val($("#X${reqOrCap}Ref").val());
73         $("#browseFor${reqOrCap}Diag").modal("show");
74 }
75
76 /**
77  * Called by click on "Set" button at the browseForReqDiag
78  */
79 function set${reqOrCap}Ref() {
80         $("#X${reqOrCap}Ref").val($("#${reqOrCap}ReferenceField").val());
81         $("#browseFor${reqOrCap}Diag").modal("hide");
82 }
83
84 /**
85  * Called from the modal after the user clicks "Add" or "Delete"
86  */
87 function addorUpdate${reqOrCap}(update) {
88         var data = {
89                 name: $("#${reqOrCap}Name").val(),
90                 ref: $("#X${reqOrCap}Ref").val()
91         }
92
93         $.ajax({
94                 url: "boundarydefinitions/${requirementsOrCapabilities}/",
95                 data: data,
96                 type: "POST"
97         }).fail(function(jqXHR, textStatus, errorThrown) {
98                 vShowAJAXError("Could not add ${label}", jqXHR, errorThrown);
99         }).done(function(id) {
100                 // data is the new id
101                 var tableRow = [id, data.name, data.ref];
102                 ${requirementsOrCapabilities}TableInfo.table.fnAddData(tableRow);
103
104                 if (update) {
105                         // update is implemented as delete + recreate
106                         // after successfull creation, we can delete
107                         deleteOnServerAndInTable(${requirementsOrCapabilities}TableInfo, '${label}', 'boundarydefinitions/${requirementsOrCapabilities}/', undefined, undefined, undefined, true);
108                         // TODO: we should hook into onSuccess/onError, but currently these are not exposed from deleteResource to deleteOnServerAndInTable
109                         vShowSuccess("Successfully updated ${label}.");
110                 } else {
111                         vShowSuccess("Successfully added ${label}.");
112                 }
113
114
115                 $('#${reqOrCap}Diag').modal('hide');
116         });
117 };
118
119 function deleteRequirement() {
120         $('#${reqOrCap}Diag').modal('hide');
121         $("#delete${reqOrCap}").click();
122 }
123
124 /**
125  * Called from the buttons in the table if the user clicks "Add" or "Edit"
126  */
127 function open${reqOrCap}Editor(update) {
128         if (update) {
129                 if (${requirementsOrCapabilities}TableInfo.selectedRow) {
130                         require(["winery-support"], function(ws) {
131                                 if (ws.isEmptyTable(${requirementsOrCapabilities}TableInfo)) {
132                                         vShowError("No ${requirementsOrCapabilities} available");
133                                         return;
134                                 }
135
136                                 var data = ${requirementsOrCapabilities}TableInfo.table.fnGetData(${requirementsOrCapabilities}TableInfo.table.selectedRow);
137                                 // we don't require the id as deleteOnServerAndInTable automatically deletes the selectedRow
138                                 $("#${reqOrCap}Name").val(data[0][1]);
139                                 $("#X${reqOrCap}Ref").val(data[0][2]);
140
141                                 $("#add${reqOrCap}").hide();
142                                 $("#update${reqOrCap}").show();
143                                 $("#delete${reqOrCap}").show();
144
145                                 $("#addOrUpdate${reqOrCap}Span").text("Change");
146                                 $('#${reqOrCap}Diag').modal('show');
147                         });
148                 } else {
149                         vShowError("No ${label} selected");
150                 }
151         } else {
152                 // create a new req/cap
153                 $("#add${reqOrCap}").show();
154                 $("#update${reqOrCap}").hide();
155                 $("#delete${reqOrCap}").hide();
156                 $("#addOrUpdate${reqOrCap}Span").text("Add");
157                 $('#${reqOrCap}Diag').modal('show');
158         }
159 }
160
161 </script>