Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.topologymodeler / src / main / webapp / WEB-INF / tags / common / orioneditor / orioneditorarea.tag
1 <%--
2 /*******************************************************************************
3  * Copyright (c) 2013-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 <%@tag description="Wrapper for an orion editing area" pageEncoding="UTF-8"%>
15
16 <%@attribute name="areaid" required="true" description="The id of the editing area."%>
17 <%@attribute name="withoutsavebutton" required="false"%>
18 <%@attribute name="initialtext" required="false" description="The value to put in the editor. Can be also passed as body of this tag"%>
19 <%@attribute name="url" required="false"%>
20 <%@attribute name="hidden" required="false" description="if not empty, the form is hidden"%>
21 <%@attribute name="method" required="false" description="the method to use. Defaults to PUT"%>
22
23 <%-- QUICK HACK to change the method from POST to PUT after saving an empty documentation the first time --%>
24 <%@attribute name="reloadAfterSuccess" required="false" description="Trigger a page reload after success (if true)"%>
25
26 <%@taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
27 <%@taglib prefix="wc" uri="http://www.eclipse.org/winery/functions" %>
28
29 <div>
30         <div id="${areaid}" class="orionxmleditordiv" <c:if test="{$not empty hidden}">style="display: none;"</c:if>><pre>${wc:escapeHtml4(initialtext)}<jsp:doBody/></pre></div>
31         <c:if test="${empty withoutsavebutton}">
32                 <button class="btn btn-primary" type="button" onclick="window.winery.orionareas['${areaid}'].save(this);" data-loading-text="Saving...">Save</button>
33         </c:if>
34 </div>
35
36 <script>
37 if (window.winery === undefined) {
38         window.winery = {};
39 }
40 if (window.winery.orionareas === undefined) {
41         window.winery.orionareas = {};
42 }
43 require(["orioneditor"], function(edit) {
44         var config = {
45                 id: "${areaid}", // used for URL update
46                 reloadAfterSuccess : "${reloadAfterSuccess}",
47                 editor: edit({
48                         contentType: "application/xml",
49                         parent: "${areaid}"
50                         // todo: we can set the initial text by the parameter "contents"
51                 }),
52                 ajaxOptions : {
53                         contentType: "text/xml"
54                 },
55                 fixEditorHeight: function() {
56                         // fix the editor
57                         // orion puts "height:0px" -> we remove that
58                         $("#${areaid}").removeAttr("style");
59                         // due to the CSS style, the height is 300px
60                         // "just" adapt the editor to that size
61                         this.editor.resize();
62                 },
63                 save: function(button) {
64                         var btn = $(button); // also works if button is undefined
65                         btn.button("loading");
66
67                         var options = this.ajaxOptions;
68                         options.data = this.editor.getText();
69
70                         // ensure that "config" variable is initialized within the ajax call
71                         var config = this;
72                         // the following code does not use "this" anymore as the "this" in the function references to the jqXHR instead of config
73                         $.ajax(options).done(function( data, textStatus, jqXHR ) {
74                                 if (data !== undefined) {
75                                         // data contains the new id
76                                         url = url.replace(config.id, data);
77                                         conifg.id = data;
78                                         config.ajaxOptions.url = url;
79                                 }
80                                 if (config.reloadAfterSuccess) {
81                                         location.reload();
82                                 } else {
83                                         vShowSuccess("sucessfully saved");
84                                         btn.button("reset");
85                                 }
86                         }).fail(function(jqXHR, textStatus, errorThrown) {
87                                 vShowAJAXError("Could not add update XML", jqXHR, errorThrown);
88                                 btn.button("reset");
89                         });
90                 }
91         };
92
93         // now, editor is defined
94         // we cannot "fix" the appearance as the editor height determination does not work on hidden fields
95
96         // url is an optional parameter to the .tag
97         if ("${url}" != "") {
98                 config.ajaxOptions.url = "${url}";
99         }
100         // method is an optional parameter to the .tag
101         if ("${method}" == "") {
102                 config.ajaxOptions.type = "PUT";
103         } else {
104                 config.ajaxOptions.type = "${method}";
105         }
106
107         // store the config in global variable
108         window.winery.orionareas["${areaid}"] = config;
109 });
110 </script>