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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
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) {
23 RepositoryGroup=function(id,repositories,mergedIndexPath,mergedIndexTtl){
28 this.id=ko.observable(id);
29 this.id.subscribe(function(newValue){self.modified(true)});
31 //private String mergedIndexPath = "/.indexer";
32 this.mergedIndexPath=ko.observable(mergedIndexPath?mergedIndexPath:".indexer");
33 this.mergedIndexPath.subscribe(function(newValue){self.modified(true)});
35 // private int mergedIndexTtl = 30;
36 this.mergedIndexTtl=ko.observable(mergedIndexTtl?mergedIndexTtl:30);
37 this.mergedIndexTtl.subscribe(function(newValue){self.modified(true)});
39 // private List<String> repositories;
40 this.repositories=ko.observableArray(repositories);
41 this.repositories.subscribe(function(newValue){self.modified(true)});
43 // to store managedRepositories description not sended to server
44 this.managedRepositories=ko.observableArray([]);
45 this.managedRepositories.subscribe(function(newValue){self.modified(true)});
47 this.modified=ko.observable(false);
50 RepositoryGroupViewModel=function(repositoryGroup,update,repositoryGroupsViewModel){
52 this.repositoryGroup=repositoryGroup;
54 this.repositoryGroupsViewModel=repositoryGroupsViewModel;
55 this.availableRepositories=ko.observableArray([]);
56 this.applicationUrl=null;
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]);
64 repositoryMoved=function(arg){
65 $.log("repositoryMoved:"+arg.sourceIndex+" to " + arg.targetIndex);
67 for(var i=0;i<self.repositoryGroup.managedRepositories().length;i++){
68 repositories.push(self.repositoryGroup.managedRepositories()[i].id());
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);
79 this.saveRepositoryGroup=function(repositoryGroup){
81 self.repositoryGroupsViewModel.saveRepositoryGroup(repositoryGroup);
83 self.repositoryGroupsViewModel.addRepositoryGroup(repositoryGroup);
87 this.removeRepository=function(id){
88 $.log("removeRepository:"+id);
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]);
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()));
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);
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);
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]);
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()));
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);
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);
149 RepositoryGroupsViewModel=function(){
151 this.repositoryGroups=ko.observableArray([]);
152 this.managedRepositories=ko.observableArray([]);
153 this.applicationUrl="";
155 this.removeFromList=function(managedRepository){
156 $.log("removeFromList");
159 this.findManagedRepository=function(id){
160 return findManagedRepository(id,self.managedRepositories());
162 this.deleteRepositoryGroup=function(repositoryGroup){
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));
169 this.removeRepositoryGroup=function(repositoryGroup){
171 $.ajax("restServices/archivaServices/repositoryGroupService/deleteRepositoryGroup/"+encodeURIComponent(repositoryGroup.id()),
174 success: function(data) {
175 var message=$.i18n.prop('repository.group.deleted',repositoryGroup.id());
176 displaySuccessMessage(message);
177 self.repositoryGroups.remove(repositoryGroup);
179 error: function(data) {
180 var res = $.parseJSON(data.responseText);
181 displayRestError(res);
187 this.editRepositoryGroup=function(repositoryGroup){
189 var mainContent=$("#main-content");
191 url: "restServices/archivaServices/archivaAdministrationService/applicationUrl",
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();
208 this.editRepositoryGroupWithId=function(repositoryGroupId){
210 $.each(self.repositoryGroups(), function(index, value) {
211 if(value.id()==repositoryGroupId){
212 self.editRepositoryGroup(value);
217 this.saveRepositoryGroup=function(repositoryGroup){
219 $.log("saveRepositoryGroup, valid:"+valid);
223 var userMessages=$("#user-messages");
224 userMessages.html(mediumSpinnerImg());
225 var valid = $("#main-content").find("#repository-group-edit-form" ).valid();
228 $("#repository-group-save" ).button('loading');
229 $.ajax("restServices/archivaServices/repositoryGroupService/updateRepositoryGroup",
232 contentType: 'application/json',
233 data:ko.toJSON(repositoryGroup),
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);
241 error: function(data) {
242 var res = $.parseJSON(data.responseText);
243 displayRestError(res);
245 complete:function(data){
246 $("#repository-group-save" ).button('reset');
247 removeMediumSpinnerImg(userMessages);
254 this.addRepositoryGroup=function(repositoryGroup){
256 $.ajax("restServices/archivaServices/repositoryGroupService/addRepositoryGroup",
259 contentType: 'application/json',
260 data: ko.toJSON(repositoryGroup),
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);
269 error: function(data) {
270 var res = $.parseJSON(data.responseText);
271 displayRestError(res);
278 getManagedRepository=function(id){
279 $.log("getManagedRepository:"+id);
280 return findManagedRepository(self.managedRepositories());
285 displayRepositoryGroups=function(successFn){
287 var mainContent = $("#main-content");
288 mainContent.html(mediumSpinnerImg());
289 this.repositoryGroupsViewModel=new RepositoryGroupsViewModel();
292 loadManagedRepositories(function(data) {
295 url: "restServices/archivaServices/archivaAdministrationService/applicationUrl",
298 success: function(applicationUrl){
300 self.repositoryGroupsViewModel.managedRepositories(
301 mapManagedRepositories(data,applicationUrl?applicationUrl:window.location.toString().substringBeforeLast("/")));
302 self.repositoryGroupsViewModel.applicationUrl=applicationUrl;
303 $.ajax("restServices/archivaServices/repositoryGroupService/getRepositoriesGroups", {
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);
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");
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);
325 activateRepositoryGroupEditTab();
326 ko.applyBindings(repositoryGroupViewModel,mainContent.find("#repository-groups-edit" ).get(0));
327 repositoryGroupViewModel.renderSortableChosed(self.repositoryGroupsViewModel);
328 repositoryGroupViewModel.renderSortableAvailables(self.repositoryGroupsViewModel);
330 if ($(e.target).attr("href")=="#repository-groups-view") {
331 mainContent.find("#repository-groups-view-tabs-li-edit a").html($.i18n.prop("add"));
338 successFn(self.repositoryGroupsViewModel);
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;
356 for(var i=0;i<repositoryGroup.repositories().length;i++){
357 managedRepositories.push(self.repositoryGroupsViewModel.findManagedRepository(repositoryGroup.repositories()[i]));
359 $.log("end mapManagedRepositoriesToRepositoryGroup");
360 return managedRepositories;
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");
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"));
378 activateRepositoryGroupEditTab=function(){
379 $.log("activateRepositoryGroupEditTab");
380 var mainContent = $("#main-content");
382 mainContent.find("#repository-groups-view-tabs-content div[class*='tab-pane']").removeClass("active");
383 mainContent.find("#repository-groups-view-tabs li").removeClass("active");
385 mainContent.find("#repository-groups-edit").addClass("active");
386 mainContent.find("#repository-groups-view-tabs-li-edit").addClass("active");
391 repositoryGroupValidator=function(){
393 var theForm=$("#main-content").find("#repository-group-edit-form");
394 var validator = theForm.validate({
403 showErrors: function(validator, errorMap, errorList) {
404 customShowError("#main-content #repository-group-edit-form",validator,errorMap,errorMap);
410 mapRepositoryGroups=function(data){
414 var mappedRepositoryGroups = $.map(data, function(item) {
415 return mapRepositoryGroup(item);
417 return mappedRepositoryGroups;
420 mapRepositoryGroup=function(data){
421 return new RepositoryGroup(data.id, mapStringArray(data.repositories),data.mergedIndexPath,data.mergedIndexTtl);