Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.topologymodeler / src / main / webapp / WEB-INF / tags / common / policies / policydiag.tag
1 <%--
2 /*******************************************************************************
3  * Copyright (c) 2012-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 import="org.eclipse.winery.model.tosca.TPolicyTemplate"%>
16 <%@tag description="Dialog to add or update a policy. Offers function showUpdateDiagForPolicy(policyElement) / showAddDiagForPolicy(nodeTemplateElement)" pageEncoding="UTF-8"%>
17 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
18
19 <%@taglib prefix="o"  tagdir="/WEB-INF/tags/common/orioneditor"%>
20 <%@taglib prefix="w"  tagdir="/WEB-INF/tags"%>
21 <%@taglib prefix="ct"  tagdir="/WEB-INF/tags/common"%>
22 <%@taglib prefix="wc" uri="http://www.eclipse.org/winery/functions"%>
23
24 <%@attribute name="allPolicyTypes" required="true" type="java.util.Collection" description="Collection&lt;QName&gt; of all available policy types" %>
25 <%@attribute name="repositoryURL" required="true" type="java.lang.String" description="The URL of winery's repository"%>
26
27 <div class="modal fade" id="PolicyDiag">
28         <div class="modal-dialog">
29                 <div class="modal-content">
30                         <div class="modal-header">
31                                 <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
32                                 <h4 class="modal-title">Policy</h4>
33                         </div>
34                         <div class="modal-body">
35                                 <ct:id_name_type idPrefix="policy" allTypes="${allPolicyTypes}" hideIdField="true" />
36
37                                 <div class="form-group">
38                                         <label for="policyTemplate" class="control-label">Policy Template:</label>
39
40                                         <input id="policyTemplate" class="form-control" name="policyTemplate"></input>
41                                 </div>
42
43                                 <o:orioneditorarea areaid="OrionpolicyXML" withoutsavebutton="true" />
44                         </div>
45                         <div class="modal-footer">
46                                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
47                                 <button type="button" id="deletePolicy" class="btn btn-danger" onclick="deletePolicy();">Delete</button>
48                                 <button type="button" id="updatePolicy" class="btn btn-primary" onclick="addOrUpdatePolicy(false);">Update</button>
49                                 <button type="button" id="addPolicy" class="btn btn-primary" onclick="addOrUpdatePolicy(true);">Add</button>
50                         </div>
51                 </div>
52         </div>
53 </div>
54
55 <c:set var="clazz" value="<%=org.eclipse.winery.model.tosca.TPolicy.class%>" />
56 <textarea id="emptyPolicy" class="hidden">${wc:XMLAsString(clazz, null)}</textarea>
57
58 <script>
59 //global variable set by showUpdateDiagForPolicy and read by addOrUpdatePolicy
60 var currentPolicyElement;
61
62 // possibly this is a duplicate information as we also have "currentlySelectedNodeTemplate" (or similar)
63 var currentNodeTemplateElement;
64
65 function updatePolicyTemplateSelect(valueToSelect) {
66         require(["winery-support-common"], function(w) {
67                 var type = $("#policyType").val();
68                 var fragment = w.getURLFragmentOutOfFullQName(type);
69                 var url = "${repositoryURL}/policytypes/" + fragment + "/instances/";
70                 $.ajax(url, {
71                         dataType: 'json'
72                 }).fail(function(jqXHR, textStatus, errorThrown) {
73                         vShowAJAXError("Could not get policy templates", jqXHR, errorThrown);
74                 }).done(function(data) {
75                         // add "(none)" to available items
76                         var none = {
77                                 id: "(none)",
78                                 text: "(none)"
79                         };
80                         data.unshift(none);
81
82                         if (typeof valueToSelect === "undefined") {
83                                 valueToSelect = "(none)";
84                         }
85
86                         $("#policyTemplate")
87                                 .select2({data: data})
88                                 .select2("val", valueToSelect);
89                 });
90         });
91 }
92
93 function showUpdateDiagForPolicy(policyElement) {
94         currentPolicyElement = policyElement;
95
96         $("#deletePolicy").show();
97         $("#updatePolicy").show();
98         $("#addPolicy").hide();
99
100         var name = policyElement.children("div.name").text();
101         var type = policyElement.children("span.type").text();
102
103         $("#policyName").val(name);
104         $("#policyType").val(type);
105
106         // onchange of type is not called, we have to update the template selection field for ourselves
107         // we also have to select the current user's choice
108         updatePolicyTemplateSelect(policyElement.children("span.template").text());
109
110         var diag = $("#PolicyDiag");
111         require(["winery-support-common"], function(w) {
112                 w.replaceDialogShownHookForOrionUpdate(diag, "OrionpolicyXML", currentPolicyElement.children("textarea").val());
113                 diag.modal("show");
114         });
115 }
116
117 function showAddDiagForPolicy(nodeTemplateElement) {
118         currentNodeTemplateElement = nodeTemplateElement;
119
120         $("#deletePolicy").hide();
121         $("#updatePolicy").hide();
122         $("#addPolicy").show();
123
124         $("#policyName").val("");
125
126         // fill policy template select field
127         updatePolicyTemplateSelect();
128
129         var diag = $("#PolicyDiag");
130         require(["winery-support-common"], function(w) {
131                 w.replaceDialogShownHookForOrionUpdate(diag, "OrionpolicyXML", $("#emptyPolicy").val());
132                 diag.modal("show");
133         });
134 }
135
136 function addOrUpdatePolicy(doAdd) {
137         if (highlightRequiredFields()) {
138                 vShowError("Please fill in all required fields");
139                 return;
140         }
141
142         require(["winery-support-common", "tmpl"], function(wsc, tmpl) {
143                 var res = wsc.synchronizeNameAndType("policy", false, [{
144                         attribute: "policyType",
145                         fieldSuffix: "Type",
146                 }, {
147                         attribute: "policyRef",
148                         fieldSuffix: "Template"
149                 }]);
150
151                 if (res) {
152                         var policyTemplate = $("#policyTemplate").select2("data");
153                         var renderData = {
154                                 name: $("#policyName").val(),
155
156                                 policyTypeText: $("#policyType :selected").text(),
157                                 policyTypeVal: $("#policyType").val(),
158
159                                 policyTemplateText: policyTemplate.text,
160                                 policyTemplateVal: policyTemplate.id,
161
162                                 xml: res.xml
163                         };
164                         var div = tmpl("tmpl-policy", renderData);
165                         if (doAdd) {
166                                 currentNodeTemplateElement.children("div.policiesContainer").children("div.content").children("div.addnewpolicy").before(div);
167                         } else {
168                                 currentPolicyElement.replaceWith(div);
169                         }
170                         $("#PolicyDiag").modal("hide");
171                 } else {
172                         vShowError("Could not synchronize XML fields");
173                 }
174         });
175 }
176
177
178 function deletePolicy() {
179         // We just have to remove the HTML element:
180         // The save operation converts the information in the HTML to XML
181         currentPolicyElement.remove();
182         $("#PolicyDiag").modal("hide");
183 }
184
185
186 $("#policyType")
187         .select2()
188         .on("change", updatePolicyTemplateSelect);
189 </script>
190
191 <%-- parameters: o.id, o.name, o.policyType, o.policyRef, o.xml. Has to be consistent with the HTML generated by policies.tag --%>
192 <script type="text/x-tmpl" id="tmpl-policy">
193         <div class="policy row"> <%-- "even"/"odd" is not set. Could be done by using $.prev() --%>
194                 <div class="col-xs-4 policy name">{%=o.name%}</div>
195
196                 <%-- we do not provide a link here. Link is only available at reload. Makes life easier here. makeArtifactTemplateURL at winery-common.js is a first hint of how to generate links --%>
197                 <div class="col-xs-4 policy type">{%=o.policyTypeText%}</div>
198                 <span class="type">{%=o.policyTypeVal%}</span>
199
200                 <div class="col-xs-4 policy template">{%=o.policyTemplateText%}</div>
201                 <span class="template">{%=o.policyTemplateVal%}</span>
202
203                 <textarea class="policy_xml">{%=o.xml%}</textarea>
204         </div>
205 </script>