Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.topologymodeler / src / main / webapp / js / artifacttemplateselection.js
1 /*******************************************************************************
2  * Copyright (c) 2012-2013 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *    Oliver Kopp - initial API and implementation and/or initial documentation
11  *******************************************************************************/
12
13  // also loaded from the repository
14
15 // TODO: winery-common should be required -> encodeID; typing could be required (but that is no AMD module)
16 define([], function() {
17         $("#artifactTemplateName").typing({
18                 start: function(event, $elem) {
19                         flagArtifactTemplateNameAsUpdating();
20                 },
21                 stop: function(event, $elem) {
22                         checkArtifactTemplateName();
23                 }
24         });
25
26         $("#artifactTemplateNS").on("blur", checkArtifactTemplateName).on("change", checkArtifactTemplateName).on("focus", flagArtifactTemplateNameAsUpdating);
27
28         var repositoryURL;
29
30         return {
31                 setRepositoryURL: function(url) {
32                         repositoryURL = url;
33                 },
34                 checkArtifactTemplateName: checkArtifactTemplateName,
35                 flagArtifactTemplateNameAsUpdating: flagArtifactTemplateNameAsUpdating
36         };
37
38         function checkArtifactTemplateName() {
39                 var ns = $("#artifactTemplateNS").val();
40                 var name = $("#artifactTemplateName").val();
41                 var url = repositoryURL + "/artifacttemplates/" + encodeID(ns) + "/" + encodeID(name) + "/";
42                 if (name == "") {
43                         var valid = false;
44                         var invalidReason = "No name provided";
45                         setValidityStatus(valid, invalidReason);
46                 } else {
47                         $.ajax(url, {
48                                 type: 'HEAD',
49                                 dataType: 'html',
50                                 error: function(jqXHR, textStatus, errorThrown) {
51                                         if (jqXHR.status == 404) {
52                                                 // artifact template does not exist: everything is allright
53                                                 setValidityStatus(true, null);
54                                         } else {
55                                                 setValidityStatus(false, textStatus);
56                                         }
57                                 },
58                                 success: function(data, textStatus, jqXHR) {
59                                         setValidityStatus(false, "artifact template already exists");
60                                 }
61                         });
62                 }
63         }
64
65         function flagArtifactTemplateNameAsUpdating() {
66                 $("#artifactTemplateNameIsValid").removeClass("invalid").removeClass("valid").addClass("unknown");
67                 $("#artifactTemplateNameIsInvalidReason").text("");
68         }
69
70         function setValidityStatus(valid, invalidReason) {
71                 $("#artifactTemplateNameIsValid").removeClass("unknown");
72                 if (valid) {
73                         $("#artifactTemplateNameIsValid").addClass("valid");
74                         $("#artifactTemplateNameIsInvalidReason").text("Ok");
75                 } else {
76                         $("#artifactTemplateNameIsValid").addClass("invalid");
77                         $("#artifactTemplateNameIsInvalidReason").text(invalidReason);
78                 }
79         }
80
81
82 });