aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.repository/src/main/webapp/WEB-INF/tags/servicetemplates/boundarydefinitions/browseForReqOrCap.tag
blob: 5195f9b6d81d4e168ff58fa7c8c6003bfa19c249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<%--
/*******************************************************************************
 * Copyright (c) 2014 University of Stuttgart.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *    Oliver Kopp - initial API and implementation and/or initial documentation
 *******************************************************************************/
--%>

<%@tag pageEncoding="UTF-8"%>

<%@attribute name="label" description="Requirement|Capability" required="true" %>
<%@attribute name="requirementsOrCapabilities" description="requirements|capabilities" required="true" %>
<%@attribute name="reqOrCap" description="requirement|capability" required="true" %>

<%@taglib prefix="b"  tagdir="/WEB-INF/tags/servicetemplates/boundarydefinitions"%>

<b:browseForX XShort="${reqOrCap}" XLong="${label}" />

<div class="modal fade" id="${reqOrCap}Diag">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title"><span id="addOrUpdate${reqOrCap}Span"></span> ${label}</h4>
			</div>
			<div class="modal-body">
				<form>
					<fieldset>
						<div class="form-group">
							<label for="${reqOrCap}Name">Name</label>
							<div>
								<input name="${reqOrCap}Name" id="${reqOrCap}Name" class="form-control" type="text">
							</div>
						</div>
						<div class="form-group">
							<label for="${reqOrCap}RefDiv">${label}</label>
							<div id="${reqOrCap}RefDiv" class="row">
								<div class="col-xs-10">
									<input id="X${reqOrCap}Ref" class="form-control" type="text"> <%-- The input id is prefixed with "X" as "requirementRef" alone does not work --%>
								</div>
								<div class="col-xs-2">
									<button type="button" class="btn btn-default btn-sm" onclick="browseFor${reqOrCap}($('#XReqRef'));">Browse</button>
								</div>
							</div>
						</div>
					</fieldset>
				</form>
			</div>

			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				<button id="add${reqOrCap}"    type="button" class="btn btn-primary" onclick="addorUpdate${reqOrCap}(false);">Add</button>
				<button id="delete${reqOrCap}" type="button" class="btn btn-danger" onclick="delete${reqOrCap}t();">Delete</button>
				<button id="update${reqOrCap}" type="button" class="btn btn-primary" onclick="addorUpdate${reqOrCap}(true);">Update</button>
			</div>
		</div>
	</div>
</div>


<script>
/**
 * Triggered by the browse button at requirements. In the dialog Add/Change Requirement
 */
function browseFor${reqOrCap}(field) {
	$("#${reqOrCap}ReferenceField").val($("#X${reqOrCap}Ref").val());
	$("#browseFor${reqOrCap}Diag").modal("show");
}

/**
 * Called by click on "Set" button at the browseForReqDiag
 */
function set${reqOrCap}Ref() {
	$("#X${reqOrCap}Ref").val($("#${reqOrCap}ReferenceField").val());
	$("#browseFor${reqOrCap}Diag").modal("hide");
}

/**
 * Called from the modal after the user clicks "Add" or "Delete"
 */
function addorUpdate${reqOrCap}(update) {
	var data = {
		name: $("#${reqOrCap}Name").val(),
		ref: $("#X${reqOrCap}Ref").val()
	}

	$.ajax({
		url: "boundarydefinitions/${requirementsOrCapabilities}/",
		data: data,
		type: "POST"
	}).fail(function(jqXHR, textStatus, errorThrown) {
		vShowAJAXError("Could not add ${label}", jqXHR, errorThrown);
	}).done(function(id) {
		// data is the new id
		var tableRow = [id, data.name, data.ref];
		${requirementsOrCapabilities}TableInfo.table.fnAddData(tableRow);

		if (update) {
			// update is implemented as delete + recreate
			// after successfull creation, we can delete
			deleteOnServerAndInTable(${requirementsOrCapabilities}TableInfo, '${label}', 'boundarydefinitions/${requirementsOrCapabilities}/', undefined, undefined, undefined, true);
			// TODO: we should hook into onSuccess/onError, but currently these are not exposed from deleteResource to deleteOnServerAndInTable
			vShowSuccess("Successfully updated ${label}.");
		} else {
			vShowSuccess("Successfully added ${label}.");
		}


		$('#${reqOrCap}Diag').modal('hide');
	});
};

function deleteRequirement() {
	$('#${reqOrCap}Diag').modal('hide');
	$("#delete${reqOrCap}").click();
}

/**
 * Called from the buttons in the table if the user clicks "Add" or "Edit"
 */
function open${reqOrCap}Editor(update) {
	if (update) {
		if (${requirementsOrCapabilities}TableInfo.selectedRow) {
			require(["winery-support"], function(ws) {
				if (ws.isEmptyTable(${requirementsOrCapabilities}TableInfo)) {
					vShowError("No ${requirementsOrCapabilities} available");
					return;
				}

				var data = ${requirementsOrCapabilities}TableInfo.table.fnGetData(${requirementsOrCapabilities}TableInfo.table.selectedRow);
				// we don't require the id as deleteOnServerAndInTable automatically deletes the selectedRow
				$("#${reqOrCap}Name").val(data[0][1]);
				$("#X${reqOrCap}Ref").val(data[0][2]);

				$("#add${reqOrCap}").hide();
				$("#update${reqOrCap}").show();
				$("#delete${reqOrCap}").show();

				$("#addOrUpdate${reqOrCap}Span").text("Change");
				$('#${reqOrCap}Diag').modal('show');
			});
		} else {
			vShowError("No ${label} selected");
		}
	} else {
		// create a new req/cap
		$("#add${reqOrCap}").show();
		$("#update${reqOrCap}").hide();
		$("#delete${reqOrCap}").hide();
		$("#addOrUpdate${reqOrCap}Span").text("Add");
		$('#${reqOrCap}Diag').modal('show');
	}
}

</script>