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){
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 List<String> repositories;
36 this.repositories=ko.observableArray(repositories);
37 this.repositories.subscribe(function(newValue){self.modified(true)});
39 // to store managedRepositories description not sended to server
40 this.managedRepositories=ko.observableArray([]);
41 this.managedRepositories.subscribe(function(newValue){self.modified(true)});
43 this.modified=ko.observable(false);
46 RepositoryGroupViewModel=function(repositoryGroup,update,repositoryGroupsViewModel){
48 this.repositoryGroup=repositoryGroup;
50 this.repositoryGroupsViewModel=repositoryGroupsViewModel;
51 this.availableRepositories=ko.observableArray([]);
52 this.applicationUrl=null;
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]);
60 repositoryMoved=function(arg){
61 $.log("repositoryMoved:"+arg.sourceIndex+" to " + arg.targetIndex);
63 for(var i=0;i<self.repositoryGroup.managedRepositories().length;i++){
64 repositories.push(self.repositoryGroup.managedRepositories()[i].id());
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);
75 this.saveRepositoryGroup=function(repositoryGroup){
77 self.repositoryGroupsViewModel.saveRepositoryGroup(repositoryGroup);
79 self.repositoryGroupsViewModel.addRepositoryGroup(repositoryGroup);
83 this.removeRepository=function(id){
84 $.log("removeRepository:"+id);
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]);
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()));
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);
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);
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]);
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()));
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);
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);
145 RepositoryGroupsViewModel=function(){
147 this.repositoryGroups=ko.observableArray([]);
148 this.managedRepositories=ko.observableArray([]);
149 this.applicationUrl="";
151 this.removeFromList=function(managedRepository){
152 $.log("removeFromList");
155 this.findManagedRepository=function(id){
156 return findManagedRepository(id,self.managedRepositories());
158 this.deleteRepositoryGroup=function(repositoryGroup){
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));
165 this.removeRepositoryGroup=function(repositoryGroup){
167 $.ajax("restServices/archivaServices/repositoryGroupService/deleteRepositoryGroup/"+encodeURIComponent(repositoryGroup.id()),
170 success: function(data) {
171 var message=$.i18n.prop('repository.group.deleted',repositoryGroup.id());
172 displaySuccessMessage(message);
173 self.repositoryGroups.remove(repositoryGroup);
175 error: function(data) {
176 var res = $.parseJSON(data.responseText);
177 displayRestError(res);
183 this.editRepositoryGroup=function(repositoryGroup){
185 var mainContent=$("#main-content");
187 url: "restServices/archivaServices/archivaAdministrationService/applicationUrl",
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();
204 this.editRepositoryGroupWithId=function(repositoryGroupId){
206 $.each(self.repositoryGroups(), function(index, value) {
207 if(value.id()==repositoryGroupId){
208 self.editRepositoryGroup(value);
213 this.saveRepositoryGroup=function(repositoryGroup){
215 $.log("saveRepositoryGroup, valid:"+valid);
219 var userMessages=$("#user-messages");
220 userMessages.html(mediumSpinnerImg());
221 var valid = $("#main-content").find("#repository-group-edit-form" ).valid();
224 $("#repository-group-save" ).button('loading');
225 $.ajax("restServices/archivaServices/repositoryGroupService/updateRepositoryGroup",
228 contentType: 'application/json',
229 data:ko.toJSON(repositoryGroup),
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);
237 error: function(data) {
238 var res = $.parseJSON(data.responseText);
239 displayRestError(res);
241 complete:function(data){
242 $("#repository-group-save" ).button('reset');
243 removeMediumSpinnerImg(userMessages);
250 this.addRepositoryGroup=function(repositoryGroup){
252 $.ajax("restServices/archivaServices/repositoryGroupService/addRepositoryGroup",
255 contentType: 'application/json',
256 data: ko.toJSON(repositoryGroup),
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);
265 error: function(data) {
266 var res = $.parseJSON(data.responseText);
267 displayRestError(res);
274 getManagedRepository=function(id){
275 $.log("getManagedRepository:"+id);
276 return findManagedRepository(self.managedRepositories());
281 displayRepositoryGroups=function(successFn){
283 var mainContent = $("#main-content");
284 mainContent.html(mediumSpinnerImg());
285 this.repositoryGroupsViewModel=new RepositoryGroupsViewModel();
288 loadManagedRepositories(function(data) {
291 url: "restServices/archivaServices/archivaAdministrationService/applicationUrl",
294 success: function(applicationUrl){
296 self.repositoryGroupsViewModel.managedRepositories(
297 mapManagedRepositories(data,applicationUrl?applicationUrl:window.location.toString().substringBeforeLast("/")));
298 self.repositoryGroupsViewModel.applicationUrl=applicationUrl;
299 $.ajax("restServices/archivaServices/repositoryGroupService/getRepositoriesGroups", {
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);
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");
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);
321 activateRepositoryGroupEditTab();
322 ko.applyBindings(repositoryGroupViewModel,mainContent.find("#repository-groups-edit" ).get(0));
323 repositoryGroupViewModel.renderSortableChoosed(self.repositoryGroupsViewModel);
324 repositoryGroupViewModel.renderSortableAvailables(self.repositoryGroupsViewModel);
326 if ($(e.target).attr("href")=="#repository-groups-view") {
327 mainContent.find("#repository-groups-view-tabs-li-edit a").html($.i18n.prop("add"));
334 successFn(self.repositoryGroupsViewModel);
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;
352 for(var i=0;i<repositoryGroup.repositories().length;i++){
353 managedRepositories.push(self.repositoryGroupsViewModel.findManagedRepository(repositoryGroup.repositories()[i]));
355 $.log("end mapManagedRepositoriesToRepositoryGroup");
356 return managedRepositories;
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");
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"));
374 activateRepositoryGroupEditTab=function(){
375 $.log("activateRepositoryGroupEditTab");
376 var mainContent = $("#main-content");
378 mainContent.find("#repository-groups-view-tabs-content div[class*='tab-pane']").removeClass("active");
379 mainContent.find("#repository-groups-view-tabs li").removeClass("active");
381 mainContent.find("#repository-groups-edit").addClass("active");
382 mainContent.find("#repository-groups-view-tabs-li-edit").addClass("active");
387 repositoryGroupValidator=function(){
389 var theForm=$("#main-content").find("#repository-group-edit-form");
390 var validator = theForm.validate({
399 showErrors: function(validator, errorMap, errorList) {
400 customShowError("#main-content #repository-group-edit-form",validator,errorMap,errorMap);
406 mapRepositoryGroups=function(data){
410 var mappedRepositoryGroups = $.map(data, function(item) {
411 return mapRepositoryGroup(item);
413 return mappedRepositoryGroups;
416 mapRepositoryGroup=function(data){
417 return new RepositoryGroup(data.id, mapStringArray(data.repositories),data.mergedIndexPath);