Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / webapp / WEB-INF / tags / addComponentInstance.tag
1 <%--
2 /*******************************************************************************
3  * Copyright (c) 2012-2013 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 <%@tag description="used by genericcomponentpage.jsp and by implementations.jsp to create a component instance" pageEncoding="UTF-8"%>
15 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
16 <%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
17 <%@taglib prefix="w" uri="http://www.eclipse.org/winery/repository/functions"%>
18 <%--
19 function createResource(nameOfResource, fields, url, onSuccess) cannot be used as this method is more diverse
20 --%>
21
22 <%@attribute name="label" required="true" description="The lable to display"%>
23
24 <%@attribute name="URL" description=""%>
25 <%@attribute name="onSuccess" description=""%>
26 <%@attribute name="type" description="added to dataToSend when doing a POST"%>
27 <%@attribute name="typeSelectorData" type="java.util.Collection" description="All available types when creating a template. We do not support types with names (additional to the id) as the current TOSCA specification does not foresee the usage of both name and id at types"%>
28 <%@attribute name="openinnewwindow" description="if true, the editor for the created component instance is openend in a new window"%>
29
30 <div class="modal fade" id="addComponentInstanceDiag">
31 <div class="modal-dialog">
32 <div class="modal-content">
33         <div class="modal-header">
34                 <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
35                 <h4 class="modal-title">Add ${label}</h4>
36         </div>
37         <div class="modal-body" style="overflow-y: inherit;">
38                 <form id="addComponentInstanceForm" enctype="multipart/form-data">
39                         <%-- we send namespace + name to server. There, the ID is generated out of the name --%>
40                         <fieldset>
41                                 <div class="form-group">
42                                         <label for="nameOfNewCI" class="control-label">Name</label>
43                                         <input class="form-control" name="name" id="nameOfNewCI" type="text" required="required" />
44                                 </div>
45
46                                 <t:namespaceChooser idOfInput="namespace" allNamespaces="${w:allNamespaces()}"></t:namespaceChooser>
47
48                         <%-- (optional) the type, for instance at an artifact template or node type implementation --%>
49                         <c:choose>
50                                 <%-- Either directly given ... --%>
51                                 <c:when test="${not empty type}">
52                                         <%-- then, we just submit it together with the other data --%>
53                                         <input id="ciType" type="hidden" class="form-control" name="type" value="${type}"/>
54                                 </c:when>
55                                 <%-- ... or a list is given given. --%>
56                                 <%-- This is somewhat ugly as the UI displays no type dialog if no types are existing, but a template is to be created.
57                                         We consider that as special case and do not add code to work around that issue.
58                                         A good solution is to present an error dialog to the user if he hits that case:
59                                         A hint should be presented to state that the user has to add a type first. --%>
60                                 <c:when test="${empty type and not empty typeSelectorData}">
61                                         <div class="form-group">
62                                                 <label for="ciType" class="control-label">Type</label>
63                                                 <%-- similar code to artifacts.jsp.openLink${name}ArtifactDiag().ajax.success --%>
64                                                 <select id="ciType" name="type" class="form-control">
65                                                         <c:forEach var="typeId" items="${typeSelectorData}">
66                                                                 <option value="${typeId.QName}">${typeId.xmlId.decoded}</option>
67                                                         </c:forEach>
68                                                 </select>
69                                         </div>
70                                 </c:when>
71                         </c:choose>
72
73                         </fieldset>
74
75                 </form>
76         </div>
77         <div class="modal-footer">
78                 <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
79                 <button type="button" class="btn btn-primary" onclick="addComponentInstance();">Add</button>
80         </div>
81 </div>
82 </div>
83 </div>
84
85 <script>
86 <c:if test="${empty type and not empty typeSelectorData}">
87 $("#ciType").select2();
88 </c:if>
89
90 $("#addComponentInstanceDiag").on("shown.bs.modal", function() {
91         $("#nameOfNewCI").focus();
92 });
93
94 function addComponentInstance() {
95         if (highlightRequiredFields()) {
96                 vShowError("Please fill in all required fields");
97                 return;
98         }
99
100         var namespace = $("#namespace").val();
101         require(["URIjs/URI"], function(URI) {
102                 if (!URI(namespace).is("absolute")) {
103                         vShowError("Please enter a valid namespace");
104                         return;
105                 }
106
107                 var dataToSend = $('#addComponentInstanceForm').serialize();
108                 $.ajax({
109                         type: "POST",
110                         async: false,
111                         "data": dataToSend,
112                         <c:if test="${not empty URL}">"url": "${URL}",</c:if>
113                         dataType: "text",
114                         error: function(jqXHR, textStatus, errorThrown) {
115                                 vShowAJAXError("Could not add ${label}", jqXHR, errorThrown);
116                         },
117                         success: function(resData, textStatus, jqXHR) {
118                                 <c:if test="${not empty onSuccess}">
119                                 ${onSuccess}
120                                 </c:if>
121
122                                 //if we want to add the new entry directly in the list, we have to start with following:
123                                 //var name = $('#nameOfNewCI').val();
124                                 //var namespace = $('#namespaceOfNewCI').val();
125
126                                 //otherwise: directly open edito
127                                 $('#addComponentInstanceDiag').modal('hide');
128                                 // open editor for newly created component (assumption: window.location ends with "/")
129                                 var loc = jqXHR.getResponseHeader('Location');
130                                 <c:choose>
131                                         <c:when test="${openinnewwindow}">
132                                                 window.open(loc, "_blank");
133                                         </c:when>
134                                         <c:otherwise>
135                                                 window.location = loc;
136                                         </c:otherwise>
137                                 </c:choose>
138                         }
139                 });
140         });
141 }
142
143 function openNewCIdiag() {
144         $('#addComponentInstanceDiag').modal('show');
145 }
146
147 </script>