]> source.dussan.org Git - archiva.git/blob
7de78f71a46ad77f298698279fc39d7e8c31d601
[archiva.git] /
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 define("archiva/admin/repository/legacy/main", ["jquery", 'i18n','knockout'],
20         function(jquery,i18n,ko) {
21
22             showMenu = function(administrationMenuItems) {
23                 administrationMenuItems.push(
24                         {
25                             text: $.i18n.prop('menu.legacy-artifact-support'),
26                             id: "menu-legacy-support-list-a",
27                             href: "#legacy",
28                             redback: "{permissions: ['archiva-manage-configuration']}",
29                             func: function() {
30                                 displayLegacyArtifactPathSupport()
31                             }
32                         });
33             }
34
35
36             //-------------------------
37             // legacy path part
38             //-------------------------
39
40             LegacyArtifactPath = function(path, groupId, artifactId, version, classifier, type, update) {
41                 //private String path;
42                 this.path = ko.observable(path);
43
44                 /**
45                  * The artifact reference, as " [groupId] :
46                  * [artifactId] : [version] : [classifier] : [type] ".
47                  */
48                 //private String artifact;
49                 //this.artifact=ko.observable(artifact);
50                 this.update = update;
51                 //private String groupId;
52                 this.groupId = ko.observable(groupId);
53
54                 //private String artifactId;
55                 this.artifactId = ko.observable(artifactId);
56
57                 //private String version;
58                 this.version = ko.observable(version);
59
60                 //private String classifier;
61                 this.classifier = ko.observable(classifier);
62
63                 //private String type;
64                 this.type = ko.observable(type);
65
66                 this.modified = ko.observable();
67
68                 this.artifact = ko.computed(function() {
69                     var artifactValue = "";
70                     if (this.groupId()) {
71                         artifactValue += this.groupId();
72                     }
73                     if (this.artifactId()) {
74                         artifactValue += ":" + this.artifactId();
75                     }
76                     if (this.version()) {
77                         artifactValue += ":" + this.version();
78                     }
79                     if (this.classifier()) {
80                         artifactValue += ":" + this.classifier();
81                     }
82                     if (this.type()) {
83                         artifactValue += ":" + this.type();
84                     }
85                     return artifactValue;
86                 }, this);
87             };
88
89             mapLegacyArtifactPaths = function(data) {
90                 if (data) {
91                     return $.isArray(data) ? $.map(data, function(item) {
92                         return mapLegacyArtifactPath(item);
93                     }) : [mapLegacyArtifactPath(data)];
94                 }
95                 return [];
96             };
97
98             mapLegacyArtifactPath = function(data) {
99                 return data ? new LegacyArtifactPath(data.path, data.groupId, data.artifactId, data.version, data.classifier, data.type) : null;
100             };
101
102             activateLegacyArtifactPathFormValidation = function() {
103                 var theForm = $("#main-content").find("#legacy-artifact-paths-edit-form");
104                 var validator = theForm.validate({
105                     showErrors: function(validator, errorMap, errorList) {
106                         customShowError("#main-content #legacy-artifact-paths-edit-form", validator, errorMap, errorMap);
107                     }
108                 });
109             };
110
111             LegacyArtifactPathViewModel = function(legacyArtifactPath, update, legacyArtifactPathsViewModel) {
112                 var self = this;
113                 this.update = update;
114                 this.legacyArtifactPath = legacyArtifactPath;
115                 this.legacyArtifactPathsViewModel = legacyArtifactPathsViewModel;
116
117                 this.display = function() {
118                     var mainContent = $("#main-content");
119                     ko.applyBindings(self, mainContent.find("#legacy-artifact-paths-edit").get(0));
120                     mainContent.find("#legacy-artifact-paths-view-tabs-li-edit a").html($.i18n.prop("edit"));
121                     activateLegacyArtifactPathFormValidation();
122                     activateLegacyArtifactPathsEditTab();
123                 };
124
125                 displayGrid = function() {
126                     activateLegacyArtifactPathsGridTab();
127                 };
128
129                 calculatePath = function() {
130                     var path = "";
131                     if (self.legacyArtifactPath.groupId()) {
132                         path += self.legacyArtifactPath.groupId() + "/jars/";
133                     }
134                     if (self.legacyArtifactPath.artifactId()) {
135                         path += self.legacyArtifactPath.artifactId();
136                     }
137                     if (self.legacyArtifactPath.version()) {
138                         path += "-" + self.legacyArtifactPath.version();
139                     }
140                     if (self.legacyArtifactPath.classifier()) {
141                         path += "-" + self.legacyArtifactPath.classifier();
142                     }
143                     if (self.legacyArtifactPath.type()) {
144                         path += "." + self.legacyArtifactPath.type();
145                     }
146                     self.legacyArtifactPath.path(path);
147                 };
148
149                 this.save = function() {
150                     var theForm = $("#main-content").find("#legacy-artifact-paths-edit-form");
151                     if (!theForm.valid()) {
152                         return;
153                     }
154                     // do that on server side
155                     /*if (theForm.find("#artifact" ).val()
156                      !=theForm.find("#path" ).val()){
157                      var errorList=[{
158                      message: $.i18n.prop("path must match artifact"),
159                      element: theForm.find("#path" ).get(0)
160                      }];
161                      customShowError("#main-content #legacy-artifact-paths-edit-form", null, null, errorList);
162                      return;
163                      }*/
164                     // TODO call id exists if add ?
165                     clearUserMessages();
166                     $.log("save ok");
167                     if (self.update) {
168                         $.log("update");
169                     } else {
170                         $.ajax("restServices/archivaServices/archivaAdministrationService/addLegacyArtifactPath",
171                                 {
172                                     type: "POST",
173                                     contentType: 'application/json',
174                                     data: ko.toJSON(self.legacyArtifactPath),
175                                     dataType: 'json',
176                                     success: function(data) {
177                                         self.legacyArtifactPath.modified(false);
178                                         self.legacyArtifactPathsViewModel.legacyArtifactPaths.push(self.legacyArtifactPath);
179                                         displaySuccessMessage($.i18n.prop('legacy-artifact-path.added', self.legacyArtifactPath.path()));
180                                         activateLegacyArtifactPathsGridTab();
181                                     },
182                                     error: function(data) {
183                                         var res = $.parseJSON(data.responseText);
184                                         displayRestError(res);
185                                     }
186                                 }
187                         );
188                     }
189                 }
190             };
191
192             LegacyArtifactPathsViewModel = function() {
193                 var self = this;
194                 this.legacyArtifactPaths = ko.observableArray([]);
195
196                 this.gridViewModel = new ko.simpleGrid.viewModel({
197                     data: self.legacyArtifactPaths,
198                     columns: [
199                         {
200                             headerText: $.i18n.prop('legacy-artifact-paths.path'),
201                             rowText: "path"
202                         },
203                         {
204                             headerText: $.i18n.prop('legacy-artifact-paths.artifact'),
205                             rowText: "artifact"
206                         }
207                     ],
208                     pageSize: 5,
209                     gridUpdateCallBack: function(networkProxy) {
210                         $("#main-content").find("#legacy-artifact-paths-table").find("[title]").tooltip();
211                     }
212                 });
213
214
215                 editLegacyArtifactPath = function(legacyArtifactPath) {
216                     var legacyArtifactPathViewModel = new LegacyArtifactPathViewModel(legacyArtifactPath, true);
217                     legacyArtifactPathViewModel.display();
218                 };
219
220                 removeLegacyArtifactPath = function(legacyArtifactPath) {
221
222                     openDialogConfirm(
223                             function() {
224
225                                 $.ajax("restServices/archivaServices/archivaAdministrationService/deleteLegacyArtifactPath?path=" + encodeURIComponent(legacyArtifactPath.path()),
226                                         {
227                                             type: "GET",
228                                             dataType: 'json',
229                                             success: function(data) {
230                                                 self.legacyArtifactPaths.remove(legacyArtifactPath);
231                                                 displaySuccessMessage($.i18n.prop('legacy-artifact-path.removed', legacyArtifactPath.path()));
232                                                 activateLegacyArtifactPathsGridTab();
233                                             },
234                                             error: function(data) {
235                                                 var res = $.parseJSON(data.responseText);
236                                                 displayRestError(res);
237                                             },
238                                             complete: function() {
239                                                 closeDialogConfirm();
240                                             }
241                                         }
242                                 );
243                             }, $.i18n.prop('ok'), $.i18n.prop('cancel'), $.i18n.prop('legacy-artifact-path.delete.confirm', legacyArtifactPath.path()),
244                             $("#legacy-artifact-path-delete-warning-tmpl").tmpl(legacyArtifactPath));
245
246                 };
247
248                 updateLegacyArtifactPath = function(legacyArtifactPath) {
249
250                 }
251
252             };
253
254             displayLegacyArtifactPathSupport = function() {
255                 screenChange();
256                 var mainContent = $("#main-content");
257                 mainContent.html(mediumSpinnerImg());
258
259                 $.ajax("restServices/archivaServices/archivaAdministrationService/getLegacyArtifactPaths", {
260                     type: "GET",
261                     dataType: 'json',
262                     success: function(data) {
263                         mainContent.html($("#legacy-artifact-path-main").tmpl());
264                         var legacyArtifactPathsViewModel = new LegacyArtifactPathsViewModel();
265                         var legacyPaths = mapLegacyArtifactPaths(data);
266                         $.log("legacyPaths:" + legacyPaths.length);
267                         legacyArtifactPathsViewModel.legacyArtifactPaths(legacyPaths);
268                         ko.applyBindings(legacyArtifactPathsViewModel, mainContent.find("#legacy-artifact-paths-view").get(0));
269
270                         mainContent.find("#legacy-artifact-paths-view-tabs").on('show', function(e) {
271                             if ($(e.target).attr("href") == "#legacy-artifact-paths-edit") {
272                                 var viewModel = new LegacyArtifactPathViewModel(new LegacyArtifactPath(), false, legacyArtifactPathsViewModel);
273                                 viewModel.display();
274                                 activateLegacyArtifactPathFormValidation();
275                                 clearUserMessages();
276                             }
277                             if ($(e.target).attr("href") == "#legacy-artifact-paths-view") {
278                                 mainContent.find("#legacy-artifact-paths-view-tabs-li-edit a").html($.i18n.prop("add"));
279                                 clearUserMessages();
280                             }
281
282                         });
283
284
285                         activateLegacyArtifactPathsGridTab();
286                     }
287                 });
288
289
290             };
291
292
293             activateLegacyArtifactPathsGridTab = function() {
294                 var mainContent = $("#main-content");
295                 mainContent.find("#legacy-artifact-paths-view-tabs-li-edit").removeClass("active");
296                 mainContent.find("#legacy-artifact-paths-edit").removeClass("active");
297
298                 mainContent.find("#legacy-artifact-paths-view-tabs-li-grid").addClass("active");
299                 mainContent.find("#legacy-artifact-paths-view").addClass("active");
300                 mainContent.find("#legacy-artifact-paths-view-tabs-li-edit a").html($.i18n.prop("add"));
301
302             };
303
304             activateLegacyArtifactPathsEditTab = function() {
305                 var mainContent = $("#main-content");
306                 mainContent.find("#legacy-artifact-paths-view-tabs-li-grid").removeClass("active");
307                 mainContent.find("#legacy-artifact-paths-view").removeClass("active");
308
309                 mainContent.find("#legacy-artifact-paths-view-tabs-li-edit").addClass("active");
310                 mainContent.find("#legacy-artifact-paths-edit").addClass("active");
311             };
312
313
314
315
316         }
317 );