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