]> source.dussan.org Git - archiva.git/blob
be7956de002199704d267e7fc142acae1bb93f68
[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/10maven2/repository-groups",["jquery","i18n","jquery.tmpl","bootstrap","jquery.validate","jquery.ui","knockout"
20   ,"knockout.simpleGrid","knockout.sortable","archiva/admin/repository/10maven2/repositories"],
21 function(jquery,i18n,jqueryTmpl,bootstrap,jqueryValidate,jqueryUi,ko) {
22
23   RepositoryGroup=function(id,repositories,mergedIndexPath){
24
25     var self=this;
26
27     //private String id;
28     this.id=ko.observable(id);
29     this.id.subscribe(function(newValue){self.modified(true)});
30
31     //private String mergedIndexPath = "/.indexer";
32     this.mergedIndexPath=ko.observable(mergedIndexPath?mergedIndexPath:".indexer");
33     this.mergedIndexPath.subscribe(function(newValue){self.modified(true)});
34
35     // private List<String> repositories;
36     this.repositories=ko.observableArray(repositories);
37     this.repositories.subscribe(function(newValue){self.modified(true)});
38
39     // to store managedRepositories description not sended to server
40     this.managedRepositories=ko.observableArray([]);
41     this.managedRepositories.subscribe(function(newValue){self.modified(true)});
42
43     this.modified=ko.observable(false);
44   }
45
46   RepositoryGroupViewModel=function(repositoryGroup,update,repositoryGroupsViewModel){
47     var self = this;
48     this.repositoryGroup=repositoryGroup;
49     this.update=update;
50     this.repositoryGroupsViewModel=repositoryGroupsViewModel;
51     this.availableRepositories=ko.observableArray([]);
52     this.applicationUrl=null;
53
54     for (var i=0;i<repositoryGroupsViewModel.managedRepositories().length;i++){
55       if ( $.inArray(repositoryGroupsViewModel.managedRepositories()[i].id(),this.repositoryGroup.repositories())<0){
56         this.availableRepositories.push(repositoryGroupsViewModel.managedRepositories()[i]);
57       }
58     }
59
60     repositoryMoved=function(arg){
61       $.log("repositoryMoved:"+arg.sourceIndex+" to " + arg.targetIndex);
62       var repositories=[];
63       for(var i=0;i<self.repositoryGroup.managedRepositories().length;i++){
64         repositories.push(self.repositoryGroup.managedRepositories()[i].id());
65       }
66       self.repositoryGroup.repositories(repositories);
67       self.repositoryGroup.modified(true);
68       var mainContent=$("#main-content");
69       mainContent.find("#repository-groups-edit-available-repositories").find(".icon-plus-sign" ).off("click");
70       mainContent.find("#repository-groups-edit-order-div").find(".icon-minus-sign" ).off("click");
71       self.renderSortableAvailables(self.repositoryGroupsViewModel);
72       self.renderSortableChoosed(self.repositoryGroupsViewModel);
73     }
74
75     this.saveRepositoryGroup=function(repositoryGroup){
76       if (self.update){
77         self.repositoryGroupsViewModel.saveRepositoryGroup(repositoryGroup);
78       } else {
79         self.repositoryGroupsViewModel.addRepositoryGroup(repositoryGroup);
80       }
81     }
82
83     this.removeRepository=function(id){
84       $.log("removeRepository:"+id);
85     }
86
87     this.removeAvailable=function(idVal){
88       for (var i=0;i<self.repositoryGroupsViewModel.managedRepositories().length;i++){
89         if(self.repositoryGroupsViewModel.managedRepositories()[i].id()==idVal){
90           self.availableRepositories.remove(repositoryGroupsViewModel.managedRepositories()[i]);
91         }
92       }
93
94       for(var i= 0;i<self.repositoryGroupsViewModel.managedRepositories().length;i++){
95         if(self.repositoryGroupsViewModel.managedRepositories()[i].id()==idVal){
96           $.log("find repo to add");
97           self.repositoryGroup.repositories.push(idVal);
98           self.repositoryGroup.managedRepositories.push(findManagedRepository(idVal,self.repositoryGroupsViewModel.managedRepositories()));
99         }
100       }
101       $("#main-content").find("#repository-groups-edit-order-div").find("#minus-"+idVal ).on("click",function(){
102         var idVal = $(this).attr("id");
103         idVal=idVal.substringAfterFirst("minus-");
104         self.removeChoosed(idVal);
105       });
106     }
107
108     this.renderSortableAvailables=function(repositoryGroupsViewModel){
109       $("#main-content").find("#repository-groups-edit-available-repositories").find(".icon-plus-sign" ).on("click",function(){
110         var idVal = $(this).attr("id");
111         idVal=idVal.substringAfterFirst("plus-");
112         self.removeAvailable(idVal);
113       });
114     }
115
116     this.removeChoosed=function(idVal){
117       for (var i=0;i<self.repositoryGroupsViewModel.managedRepositories().length;i++){
118         if(self.repositoryGroupsViewModel.managedRepositories()[i].id()==idVal){
119           self.availableRepositories.push(repositoryGroupsViewModel.managedRepositories()[i]);
120         }
121       }
122
123       for(var i= 0;i<self.repositoryGroup.repositories().length;i++){
124         if(self.repositoryGroup.repositories()[i]==idVal){
125           self.repositoryGroup.repositories.remove(self.repositoryGroup.repositories()[i]);
126           self.repositoryGroup.managedRepositories.remove(findManagedRepository(idVal,self.repositoryGroupsViewModel.managedRepositories()));
127         }
128       }
129       $("#main-content").find("#repository-groups-edit-available-repositories").find("#plus-"+idVal ).on("click",function(){
130         var idVal = $(this).attr("id");
131         idVal=idVal.substringAfterFirst("plus-");
132         self.removeAvailable(idVal);
133       });
134     }
135
136     this.renderSortableChoosed=function(repositoryGroupsViewModel){
137       $("#main-content").find("#repository-groups-edit-order-div").find(".icon-minus-sign" ).on("click",function(){
138         var idVal = $(this).attr("id");
139         idVal=idVal.substringAfterFirst("minus-");
140         self.removeChoosed(idVal);
141       });
142     }
143   }
144
145   RepositoryGroupsViewModel=function(){
146     var self=this;
147     this.repositoryGroups=ko.observableArray([]);
148     this.managedRepositories=ko.observableArray([]);
149     this.applicationUrl="";
150
151     this.removeFromList=function(managedRepository){
152       $.log("removeFromList");
153     }
154
155     this.findManagedRepository=function(id){
156       return findManagedRepository(id,self.managedRepositories());
157     }
158     this.deleteRepositoryGroup=function(repositoryGroup){
159       openDialogConfirm(
160           function(){self.removeRepositoryGroup(repositoryGroup);window.modalConfirmDialog.modal('hide')},
161           $.i18n.prop('ok'), $.i18n.prop('cancel'),
162           $.i18n.prop('repository.group.delete.confirm',repositoryGroup.id()),
163           $("#repository-group-delete-warning-tmpl").tmpl(self.repositoryGroup));
164     }
165     this.removeRepositoryGroup=function(repositoryGroup){
166       clearUserMessages();
167       $.ajax("restServices/archivaServices/repositoryGroupService/deleteRepositoryGroup/"+encodeURIComponent(repositoryGroup.id()),
168         {
169           type: "GET",
170           success: function(data) {
171             var message=$.i18n.prop('repository.group.deleted',repositoryGroup.id());
172             displaySuccessMessage(message);
173             self.repositoryGroups.remove(repositoryGroup);
174           },
175           error: function(data) {
176             var res = $.parseJSON(data.responseText);
177             displayRestError(res);
178           }
179         }
180       );
181     }
182
183     this.editRepositoryGroup=function(repositoryGroup){
184
185       var mainContent=$("#main-content");
186       $.ajax({
187           url: "restServices/archivaServices/archivaAdministrationService/applicationUrl",
188           type: "GET",
189           dataType: 'text',
190           success: function(applicationUrl){
191             $.log("editRepositoryGroup:"+repositoryGroup.id()+",repositories:"+repositoryGroup.repositories().length+",managed:"+repositoryGroup.managedRepositories().length);
192             var repositoryGroupViewModel=new RepositoryGroupViewModel(repositoryGroup,true,self);
193             repositoryGroupViewModel.applicationUrl=applicationUrl;
194             activateRepositoryGroupEditTab();
195             ko.applyBindings(repositoryGroupViewModel,mainContent.find("#repository-groups-edit" ).get(0));
196             repositoryGroupViewModel.renderSortableChoosed(self);
197             repositoryGroupViewModel.renderSortableAvailables(self);
198             mainContent.find("#repository-groups-view-tabs-li-edit" ).find("a").html($.i18n.prop("edit"));
199             repositoryGroupValidator();
200           }
201         });
202     }
203
204     this.editRepositoryGroupWithId=function(repositoryGroupId){
205
206       $.each(self.repositoryGroups(), function(index, value) {
207         if(value.id()==repositoryGroupId){
208           self.editRepositoryGroup(value);
209         }
210       });
211     }
212
213     this.saveRepositoryGroup=function(repositoryGroup){
214       if(valid==false){
215         $.log("saveRepositoryGroup, valid:"+valid);
216         return;
217       }
218       clearUserMessages();
219       var userMessages=$("#user-messages");
220       userMessages.html(mediumSpinnerImg());
221       var valid = $("#main-content").find("#repository-group-edit-form" ).valid();
222
223
224       $("#repository-group-save" ).button('loading');
225       $.ajax("restServices/archivaServices/repositoryGroupService/updateRepositoryGroup",
226         {
227           type: "POST",
228           contentType: 'application/json',
229           data:ko.toJSON(repositoryGroup),
230           dataType: 'json',
231           success: function(data) {
232             $.log("update repositoryGroup id:"+repositoryGroup.id());
233             var message=$.i18n.prop('repository.group.updated',repositoryGroup.id());
234             displaySuccessMessage(message);
235             repositoryGroup.modified(false);
236           },
237           error: function(data) {
238             var res = $.parseJSON(data.responseText);
239             displayRestError(res);
240           },
241           complete:function(data){
242             $("#repository-group-save" ).button('reset');
243             removeMediumSpinnerImg(userMessages);
244           }
245         }
246       );
247
248     };
249
250     this.addRepositoryGroup=function(repositoryGroup){
251       clearUserMessages();
252       $.ajax("restServices/archivaServices/repositoryGroupService/addRepositoryGroup",
253         {
254           type: "POST",
255           contentType: 'application/json',
256           data: ko.toJSON(repositoryGroup),
257           dataType: 'json',
258           success: function(data) {
259             $.log("update repositoryGroup id:"+repositoryGroup.id());
260             var message=$.i18n.prop('repository.group.added',repositoryGroup.id());
261             displaySuccessMessage(message);
262             repositoryGroup.modified(false);
263             self.repositoryGroups.push(repositoryGroup);
264           },
265           error: function(data) {
266             var res = $.parseJSON(data.responseText);
267             displayRestError(res);
268           }
269         }
270       );
271
272     }
273
274     getManagedRepository=function(id){
275       $.log("getManagedRepository:"+id);
276       return findManagedRepository(self.managedRepositories());
277     }
278
279   }
280
281   displayRepositoryGroups=function(successFn){
282     screenChange();
283     var mainContent = $("#main-content");
284     mainContent.html(mediumSpinnerImg());
285     this.repositoryGroupsViewModel=new RepositoryGroupsViewModel();
286     var self=this;
287
288     loadManagedRepositories(function(data) {
289
290       $.ajax({
291           url: "restServices/archivaServices/archivaAdministrationService/applicationUrl",
292           type: "GET",
293           dataType: 'text',
294           success: function(applicationUrl){
295
296             self.repositoryGroupsViewModel.managedRepositories(
297                 mapManagedRepositories(data,applicationUrl?applicationUrl:window.location.toString().substringBeforeLast("/")));
298             self.repositoryGroupsViewModel.applicationUrl=applicationUrl;
299             $.ajax("restServices/archivaServices/repositoryGroupService/getRepositoriesGroups", {
300                 type: "GET",
301                 dataType: 'json',
302                 success: function(data) {
303                   var mappedRepositoryGroups=mapRepositoryGroups(data);
304                   for(var i=0;i<mappedRepositoryGroups.length;i++){
305                     mappedRepositoryGroups[i]
306                         .managedRepositories(self.mapManagedRepositoriesToRepositoryGroup(mappedRepositoryGroups[i]));
307                     mappedRepositoryGroups[i].modified(false);
308                     $.log("mappedRepositoryGroups.repositories().length:"+mappedRepositoryGroups[i].repositories().length);
309                   }
310                   mainContent.html($("#repositoryGroupsMain").tmpl());
311                   self.repositoryGroupsViewModel.repositoryGroups(mappedRepositoryGroups);
312                   $.log("displayRepositoryGroups#applyBindings before");
313                   ko.applyBindings(self.repositoryGroupsViewModel,mainContent.find("#repository-groups-view" ).get(0));
314                   $.log("displayRepositoryGroups#applyBindings after");
315
316                   mainContent.find("#repository-groups-view-tabs").on('show', function (e) {
317                     if ($(e.target).attr("href")=="#repository-groups-edit") {
318                       var repositoryGroup = new RepositoryGroup();
319                       var repositoryGroupViewModel=new RepositoryGroupViewModel(repositoryGroup,false,self.repositoryGroupsViewModel);
320
321                       activateRepositoryGroupEditTab();
322                       ko.applyBindings(repositoryGroupViewModel,mainContent.find("#repository-groups-edit" ).get(0));
323                       repositoryGroupViewModel.renderSortableChoosed(self.repositoryGroupsViewModel);
324                       repositoryGroupViewModel.renderSortableAvailables(self.repositoryGroupsViewModel);
325                     }
326                     if ($(e.target).attr("href")=="#repository-groups-view") {
327                       mainContent.find("#repository-groups-view-tabs-li-edit a").html($.i18n.prop("add"));
328                       clearUserMessages();
329                     }
330
331                   });
332
333                   if(successFn){
334                     successFn(self.repositoryGroupsViewModel);
335                   }
336
337                 }
338               }
339             );
340
341           }
342         });
343     });
344
345     this.mapManagedRepositoriesToRepositoryGroup=function(repositoryGroup){
346       $.log("mapManagedRepositoriesToRepositoryGroup");
347       var managedRepositories=new Array();
348       if (!repositoryGroup.repositories()) {
349         repositoryGroup.repositories(new Array());
350         return managedRepositories;
351       }
352       for(var i=0;i<repositoryGroup.repositories().length;i++){
353         managedRepositories.push(self.repositoryGroupsViewModel.findManagedRepository(repositoryGroup.repositories()[i]));
354       }
355       $.log("end mapManagedRepositoriesToRepositoryGroup");
356       return managedRepositories;
357     }
358
359   }
360
361
362
363   activateRepositoryGroupsGridTab=function(){
364     var mainContent = $("#main-content");
365     mainContent.find("#repository-groups-view-tabs-content div[class*='tab-pane']").removeClass("active");
366     mainContent.find("#repository-groups-view-tabs li").removeClass("active");
367
368     mainContent.find("#repository-groups-view").addClass("active");
369     mainContent.find("#repository-groups-view-tabs-li-grid").addClass("active");
370     mainContent.find("#repository-groups-view-tabs-li-edit a").html($.i18n.prop("add"));
371
372   }
373
374   activateRepositoryGroupEditTab=function(){
375     $.log("activateRepositoryGroupEditTab");
376     var mainContent = $("#main-content");
377
378     mainContent.find("#repository-groups-view-tabs-content div[class*='tab-pane']").removeClass("active");
379     mainContent.find("#repository-groups-view-tabs li").removeClass("active");
380
381     mainContent.find("#repository-groups-edit").addClass("active");
382     mainContent.find("#repository-groups-view-tabs-li-edit").addClass("active");
383
384
385   }
386
387   repositoryGroupValidator=function(){
388     $.log("validator");
389     var theForm=$("#main-content").find("#repository-group-edit-form");
390     var validator = theForm.validate({
391         rules: {
392             id: {
393               required: true
394               },
395             mergedIndexPath:{
396               required:true
397             }
398         },
399         showErrors: function(validator, errorMap, errorList) {
400            customShowError("#main-content #repository-group-edit-form",validator,errorMap,errorMap);
401         }
402     });
403     return validator;
404   }
405
406   mapRepositoryGroups=function(data){
407     if (data == null){
408       return new Array();
409     }
410     var mappedRepositoryGroups = $.map(data, function(item) {
411       return mapRepositoryGroup(item);
412     });
413     return mappedRepositoryGroups;
414   }
415
416   mapRepositoryGroup=function(data){
417     return new RepositoryGroup(data.id, mapStringArray(data.repositories),data.mergedIndexPath);
418   }
419
420 });