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/proxy-connectors",["jquery","i18n","jquery.tmpl","bootstrap","jquery.validate","knockout"
20 ,"knockout.simpleGrid","knockout.sortable","select2"],
21 function(jquery,i18n,jqueryTmpl,bootstrap,jqueryValidate,ko) {
23 ProxyConnector=function(sourceRepoId,targetRepoId,proxyId,blackListPatterns,whiteListPatterns,policiesEntries,propertiesEntries,
27 this.modified=ko.observable(false);
29 //private String sourceRepoId;
30 this.sourceRepoId=ko.observable(sourceRepoId);
31 this.sourceRepoId.subscribe(function(newValue){
35 //private String targetRepoId;
36 this.targetRepoId=ko.observable(targetRepoId);
37 this.targetRepoId.subscribe(function(newValue){
41 this.previousProxyId=proxyId;
43 //private String proxyId;
44 this.proxyId=ko.observable(proxyId);
45 this.proxyId.subscribe(function(newValue){
46 if(newValue!=self.previousProxyId){
47 $.log("proxyId modified:"+newValue+",previous:"+self.previousProxyId);
48 self.previousProxyId=newValue;
53 //private List<String> blackListPatterns;
54 this.blackListPatterns=ko.observableArray(blackListPatterns==null?[]:blackListPatterns);
55 this.blackListPatterns.subscribe(function(newValue){
59 //private List<String> whiteListPatterns;
60 this.whiteListPatterns=ko.observableArray(whiteListPatterns==null?[]:whiteListPatterns);
61 this.whiteListPatterns.subscribe(function(newValue){
65 //private List<PropertyEntry> policiesEntries;
66 this.policiesEntries=ko.observableArray(policiesEntries==null?new Array():policiesEntries);
67 this.policiesEntries.subscribe(function(newValue){
71 //private List<PropertyEntry> properties;
72 this.propertiesEntries=ko.observableArray(propertiesEntries==null?new Array():propertiesEntries);
73 this.propertiesEntries.subscribe(function(newValue){
77 //private boolean disabled = false;
78 this.disabled=ko.observable(disabled);
79 this.disabled.subscribe(function(newValue){
83 //private int order = 0;
84 this.order=ko.observable(order?order:0);
85 this.order.subscribe(function(newValue){
91 this.updatePolicyEntry=function(key,value){
92 $.log("updatePolicyEntry:"+key+":"+value);
94 for(var i=0;i<self.policiesEntries().length;i++){
95 if (self.policiesEntries()[i].key==key){
96 self.policiesEntries()[i].value=value;
102 self.policiesEntries().push(new Entry(key,value));
108 PolicyInformation=function(options,defaultOption,id,name){
111 this.modified=ko.observable(false);
113 //private List<String> options;
114 this.options=ko.observableArray(options);
115 this.options.subscribe(function(newValue){self.modified(true)});
117 //private String defaultOption;
118 this.defaultOption=ko.observable(defaultOption);
119 this.defaultOption.subscribe(function(newValue){self.modified(true)});
122 this.id=ko.observable(id);
123 this.id.subscribe(function(newValue){self.modified(true)});
125 //private String name;
126 this.name=ko.observable(name);
127 this.name.subscribe(function(newValue){self.modified(true)});
131 ProxyConnectorViewModel=function(proxyConnector,update,proxyConnectorsViewModel){
133 this.proxyConnector=proxyConnector;
134 this.proxyConnectorsViewModel=proxyConnectorsViewModel;
136 this.modified=ko.observable(false);
142 getSelectedPolicyOption=function(id){
143 var policiesEntries=self.proxyConnector.policiesEntries();
144 if (policiesEntries!=null){
145 for (i=0;i<policiesEntries.length;i++){
146 var curKey = $.isFunction(policiesEntries[i].key)? policiesEntries[i].key():policiesEntries[i].key;
148 return $.isFunction(policiesEntries[i].value)? policiesEntries[i].value():policiesEntries[i].value;
155 changePolicyOption=function(id){
156 var selectedOption=$("#main-content").find("#policy-"+id ).find("option:selected");
157 if (selectedOption.length>0){
158 var value = selectedOption.val();
159 $.log("changePolicyOption:"+id+":"+value);
160 self.proxyConnector.updatePolicyEntry(id,value);
166 getPolicyOptions=function(id){
167 var policyInformations=self.proxyConnectorsViewModel.policyInformations();
168 for(var i=0;i<policyInformations.length;i++){
169 if (policyInformations[i].id()==id){
170 return policyInformations[i].options();
177 addBlacklistPattern=function(){
178 var pattern = $("#main-content").find("#blacklist-value").val();
179 var tab = self.proxyConnector.blackListPatterns();
181 self.proxyConnector.blackListPatterns(tab);
182 self.proxyConnector.modified(true);
185 removeBlacklistPattern=function(pattern){
186 self.proxyConnector.blackListPatterns.remove(pattern);
187 self.proxyConnector.modified(true);
190 addWhitelistPattern=function(){
191 var pattern = $("#main-content" ).find("#whitelist-value").val();
192 var tab = self.proxyConnector.whiteListPatterns();
195 self.proxyConnector.whiteListPatterns(tab);
196 self.proxyConnector.modified(true);
200 removeWhitelistPattern=function(pattern){
201 self.proxyConnector.whiteListPatterns.remove(pattern);
202 self.proxyConnector.modified(true);
205 this.save=function(){
206 //FIXME data controls !!!
208 var userMessages=$("#user-messages");
209 userMessages.html(mediumSpinnerImg());
210 $("#proxy-connector-btn-save" ).button("loading");
211 // update is delete then add
213 $.ajax("restServices/archivaServices/proxyConnectorService/updateProxyConnector",
216 data: ko.toJSON(self.proxyConnector),
217 contentType: 'application/json',
219 success: function(data) {
220 displaySuccessMessage($.i18n.prop('proxyconnector.updated'));
221 activateProxyConnectorsGridTab();
222 self.proxyConnector.modified(false);
224 error: function(data) {
225 var res = $.parseJSON(data.responseText);
226 displayRestError(res);
228 complete: function(){
229 removeMediumSpinnerImg(userMessages);
230 $("#proxy-connector-btn-save" ).button("reset");
236 $.ajax("restServices/archivaServices/proxyConnectorService/addProxyConnector",
239 data: ko.toJSON(self.proxyConnector),
240 contentType: 'application/json',
242 success: function(data) {
243 displaySuccessMessage($.i18n.prop('proxyconnector.added'));
244 activateProxyConnectorsGridTab();
245 self.proxyConnector.modified(false);
246 self.proxyConnectorsViewModel.proxyConnectors.push(self.proxyConnector);
248 error: function(data) {
249 var res = $.parseJSON(data.responseText);
250 displayRestError(res);
252 complete: function(){
253 removeMediumSpinnerImg(userMessages);
254 $("#proxy-connector-btn-save" ).button("reset");
261 this.deleteProperty=function(key){
262 for(var i=0;i<self.proxyConnector.propertiesEntries().length;i++){
263 var entry=self.proxyConnector.propertiesEntries()[i];
264 if (entry.key()==key()){
265 self.proxyConnector.propertiesEntries.remove(entry);
266 self.proxyConnector.modified(true);
272 this.addProperty=function(){
273 var mainContent=$("#main-content");
274 var key=mainContent.find("#property-key").val();
275 var value=mainContent.find("#property-value").val();
276 var oldTab = self.proxyConnector.propertiesEntries();
277 oldTab.push(new Entry(key,value));
278 self.proxyConnector.propertiesEntries(oldTab);
279 mainContent.find("#property-key").val("");
280 mainContent.find("#property-value").val("");
281 self.proxyConnector.modified(true);
284 displayGrid=function(){
285 activateProxyConnectorsGridTab();
289 ProxyConnectorsViewModel=function(){
291 this.proxyConnectors=ko.observableArray([]);
292 this.proxyConnectors.subscribe(function(newValue){
293 $.log("ProxyConnectorsViewModel#proxyConnectors modified");
294 self.proxyConnectors().sort(function(a,b){
295 if ( a.sourceRepoId()== b.sourceRepoId()) return a.order() - b.order();
296 return (a.sourceRepoId() > b.sourceRepoId())? -1:1;
299 this.policyInformations=ko.observableArray([]);
300 this.managedRepositories=ko.observableArray([]);
301 this.remoteRepositories=ko.observableArray([]);
302 this.networkProxies=ko.observableArray([]);
304 this.bulkSave=function(){
305 return getModifiedProxyConnectors().length>0;
308 getModifiedProxyConnectors=function(){
309 var prx = $.grep(self.proxyConnectors(),
310 function (proxyConnector,i) {
311 return proxyConnector.modified();
316 this.updateModifiedProxyConnectors=function(){
317 var modifiedProxyConnectors = getModifiedProxyConnectors();
319 openDialogConfirm(function(){
320 for(var i=0;i<modifiedProxyConnectors.length;i++){
321 var viewModel = new ProxyConnectorViewModel(modifiedProxyConnectors[i],true,self,false);
324 closeDialogConfirm();
327 $.i18n.prop('cancel'),
328 $.i18n.prop('proxy-connectors.bulk.save.confirm.title'),
329 $.i18n.prop('proxy.connector.bulk.save.confirm',modifiedProxyConnectors.length));
332 updateProxyConnector=function(proxyConnector){
333 var viewModel = new ProxyConnectorViewModel(proxyConnector,true,self,false);
337 editProxyConnector=function(proxyConnector){
338 var proxyConnectorViewModel=new ProxyConnectorViewModel(proxyConnector,true,self);
339 var mainContent = $("#main-content");
340 mainContent.find("#proxy-connectors-edit").html($("#proxy-connector-edit-form-tmpl").tmpl());
341 ko.applyBindings(proxyConnectorViewModel,mainContent.find("#proxy-connectors-edit").get(0));
342 activateProxyConnectorsEditTab();
343 mainContent.find("#proxy-connectors-view-tabs-li-edit a").html($.i18n.prop("edit"));
346 deleteProxyConnector=function(proxyConnector){
351 removeProxyConnector(proxyConnector,function(){
352 displaySuccessMessage($.i18n.prop('proxyconnector.removed'));
353 self.proxyConnectors.remove(proxyConnector);
354 closeDialogConfirm();
355 })}, $.i18n.prop('ok'), $.i18n.prop('cancel'), $.i18n.prop('proxyconnector.delete.confirm'),
356 $("#proxy-connector-delete-warning-tmpl").tmpl(proxyConnector));
362 getManagedRepository=function(id){
363 return findManagedRepository(id,self.managedRepositories());
366 getRemoteRepository=function(id){
367 var remoteRepository=$.grep(self.remoteRepositories(),
369 return repo.id()==id;
372 return ($.isArray(remoteRepository) && remoteRepository.length>0) ? remoteRepository[0]:new RemoteRepository();
375 getProxyConnector=function(sourceRepoId,targetRepoId){
376 var proxyConnectors=$.grep(self.proxyConnectors(),
377 function(proxyConnector,idx){
378 return proxyConnector.sourceRepoId()==sourceRepoId
379 && proxyConnector.targetRepoId()==targetRepoId;
382 var res = ($.isArray(proxyConnectors) && proxyConnectors.length>0) ? proxyConnectors[0]:new ProxyConnector();
385 showSettings=function(){
386 $.log("showSettings");
387 $("#body_content" ).find(".popover" ).hide();
388 //$("#main-content").find("[id^='proxy-connectors-grid-remoterepo-settings-edit-']" ).popover("hide");
390 buildSettings=function(proxyConnector){
391 var tmplHtml = $("#proxy-connectors-remote-settings-popover-tmpl")
393 proxyConnectorsViewModel: self,
394 proxyConnector:ko.toJS(proxyConnector)
397 var targetImg = $(("#proxy-connectors-grid-remoterepo-settings-edit-")
398 +proxyConnector.sourceRepoId().replace(/\./g,"\\\.")+"-"+proxyConnector.targetRepoId().replace(/\./g,"\\\."));
402 this.displaySettings=function(sourceRepoId,targetRepoId,targetContentStartId, targetImgStartId){
403 var proxyConnector=getProxyConnector(sourceRepoId,targetRepoId);
404 showSettings(proxyConnector,targetContentStartId,targetImgStartId);
407 this.findPolicyInformationName=function(id){
408 for(var i=0;i<self.policyInformations().length;i++){
409 if (id==self.policyInformations()[i].id()){
410 return self.policyInformations()[i].name();
416 orderChangeAware=function(proxyConnector){
417 return findProxyConnectorsWithSourceId(proxyConnector).length>1;
420 findProxyConnectorsWithSourceId=function(proxyConnector){
421 return $.grep(self.proxyConnectors(),function(curProxyConnector,idx){
422 return curProxyConnector.sourceRepoId()==proxyConnector.sourceRepoId();
427 displayOrderEdit=function(proxyConnector){
428 var proxyConnectors=findProxyConnectorsWithSourceId(proxyConnector);
429 $.log("displayOrderEdit:"+proxyConnector.sourceRepoId()+",number:"+proxyConnectors.length);
431 var managedRepository = getManagedRepository(proxyConnector.sourceRepoId());
432 var proxyConnectorEditOrderViewModel=new ProxyConnectorEditOrderViewModel(proxyConnectors,self,managedRepository);
433 ko.applyBindings(proxyConnectorEditOrderViewModel,$("#main-content").find("#proxy-connector-edit-order").get(0));
434 activateProxyConnectorsEditOrderTab();
437 this.displayGrid=function(){
438 this.gridViewModel = new ko.simpleGrid.viewModel({
439 data: self.proxyConnectors,
441 gridUpdateCallBack: function(){
442 $("#main-content" ).find("#proxyConnectorsTable" ).find("[title]").tooltip();
445 var mainContent = $("#main-content");
447 ko.applyBindings(this,mainContent.find("#proxy-connectors-view").get(0));
448 var prxGrids=mainContent.find("[id^='proxy-connectors-grid-remoterepo-settings-edit-']");
450 removeSmallSpinnerImg();
451 mainContent.find("#proxy-connectors-view-tabs #proxy-connectors-view-tabs-a-network-proxies-grid").tab('show');
453 mainContent.find("#proxy-connectors-view-tabs").on('show', function (e) {
454 $.log("on show:"+$(e.target).attr("href"));
455 if ($(e.target).attr("href")=="#proxy-connectors-edit") {
456 $.log("#proxy-connectors-edit");
457 var proxyConnector=new ProxyConnector();
458 var defaultPolicies=new Array();
459 // populate with defaut policies options
460 for (i=0;i<self.policyInformations().length;i++){
461 defaultPolicies.push(new Entry(self.policyInformations()[i].id(),self.policyInformations()[i].defaultOption));
463 proxyConnector.policiesEntries(defaultPolicies);
464 var proxyConnectorViewModel=new ProxyConnectorViewModel(proxyConnector,false,self);
465 mainContent.find("#proxy-connectors-edit").html($("#proxy-connector-edit-form-tmpl").tmpl());
466 ko.applyBindings(proxyConnectorViewModel,mainContent.find("#proxy-connectors-edit").get(0));
467 mainContent.find("#sourceRepoId" ).select2();
468 mainContent.find("#targetRepoId" ).select2();
470 if ($(e.target).attr("href")=="#proxy-connectors-view") {
471 $("#proxy-connectors-view-tabs-a-network-proxies-grid").html($.i18n.prop("proxy-connectors.grid.tab.title"));
472 mainContent.find("#proxy-connectors-view-tabs-li-edit a").html($.i18n.prop("add"));
474 if ($(e.target).attr("href")=="#proxy-connectors-edit-order") {
475 activateProxyConnectorsEditOrderTab();
483 ProxyConnectorEditOrderViewModel=function(proxyConnectors,proxyConnectorsViewModel,managedRepository){
485 this.proxyConnectors=ko.observableArray(proxyConnectors);
486 this.proxyConnectorsViewModel=proxyConnectorsViewModel;
487 this.managedRepository=managedRepository;
488 proxyConnectorMoved=function(arg){
489 $.log("proxyConnectorMoved:"+arg.sourceIndex+" to " + arg.targetIndex);
490 // if only 1 move just update two whereas update all with the new order
491 if (arg.targetIndex-arg.sourceIndex==1){
492 self.proxyConnectors()[arg.targetIndex].order(arg.targetIndex+1);
493 self.proxyConnectors()[arg.sourceIndex].order(arg.sourceIndex+1);
495 for (i=0;i<self.proxyConnectors().length;i++){
496 self.proxyConnectors()[i].order(i+1);
501 this.findRemoteRepository=function(id){
502 $.log("findRemoteRepository:"+id());
503 for(var i=0;i<self.proxyConnectorsViewModel.remoteRepositories().length;i++){
504 if (self.proxyConnectorsViewModel.remoteRepositories()[i].id()==id()){
505 return self.proxyConnectorsViewModel.remoteRepositories()[i];
511 this.updateModifiedProxyConnectors=function(){
512 self.proxyConnectorsViewModel.updateModifiedProxyConnectors();
515 displaySettings=function(sourceRepoId,targetRepoId){
516 $.log("ProxyConnectorEditOrderViewModel#showSettings:"+sourceRepoId+"-"+targetRepoId);
517 self.proxyConnectorsViewModel.displaySettings(sourceRepoId,targetRepoId,
518 "#proxy-connectors-order-remoterepo-settings-content-",
519 "#proxy-connectors-order-remoterepo-settings-edit-");
524 displayProxyConnectors=function(){
526 var mainContent = $("#main-content");
527 mainContent.html($("#proxyConnectorsMain").tmpl());
528 mainContent.append(smallSpinnerImg());
530 this.proxyConnectorsViewModel = new ProxyConnectorsViewModel();
533 loadManagedRepositories(function(data) {
534 self.proxyConnectorsViewModel.managedRepositories(mapManagedRepositories(data));
536 loadRemoteRepositories(function(data) {
537 self.proxyConnectorsViewModel.remoteRepositories(mapRemoteRepositories(data));
539 loadNetworkProxies(function(data) {
540 self.proxyConnectorsViewModel.networkProxies(mapNetworkProxies(data));
542 loadAllPolicies( function(data) {
543 self.proxyConnectorsViewModel.policyInformations(mapPolicyInformations(data));
545 loadAllProxyConnectors( function(data) {
546 self.proxyConnectorsViewModel.proxyConnectors(mapProxyConnectors(data));
547 self.proxyConnectorsViewModel.displayGrid();
560 loadAllPolicies=function(successCallBackFn,errorCallBackFn){
561 $.ajax("restServices/archivaServices/proxyConnectorService/allPolicies", {
564 success: successCallBackFn,
565 error: errorCallBackFn
570 loadAllProxyConnectors=function(successCallBackFn,errorCallBackFn){
571 $.ajax("restServices/archivaServices/proxyConnectorService/getProxyConnectors", {
574 success: successCallBackFn,
575 error: errorCallBackFn
579 activateProxyConnectorsGridTab=function(){
580 var mainContent = $("#main-content");
581 mainContent.find("#proxy-connectors-view-tabs-content div[class*='tab-pane']").removeClass("active");
582 mainContent.find("#proxy-connectors-view-tabs li").removeClass("active");
584 mainContent.find("#proxy-connectors-view").addClass("active");
585 mainContent.find("#proxy-connectors-view-tabs-li-grid").addClass("active");
586 mainContent.find("#proxy-connectors-view-tabs-li-edit a").html($.i18n.prop("add"));
590 activateProxyConnectorsEditTab=function(){
591 var mainContent = $("#main-content");
593 mainContent.find("#proxy-connectors-view-tabs-content div[class*='tab-pane']").removeClass("active");
594 mainContent.find("#proxy-connectors-view-tabs li").removeClass("active");
596 mainContent.find("#proxy-connectors-edit").addClass("active");
597 mainContent.find("#proxy-connectors-view-tabs-li-edit").addClass("active");
600 activateProxyConnectorsEditOrderTab=function(){
601 var mainContent = $("#main-content");
603 mainContent.find("#proxy-connectors-view-tabs-content div[class*='tab-pane']").removeClass("active");
604 mainContent.find("#proxy-connectors-view-tabs li").removeClass("active");
606 mainContent.find("#proxy-connector-edit-order").addClass("active");
607 mainContent.find("#proxy-connectors-view-tabs-li-edit-order").addClass("active");
610 mapProxyConnector=function(data){
614 var policiesEntries = data.policiesEntries == null ? []: $.each(data.policiesEntries,function(item){
615 return new Entry(item.key, item.value);
617 if (!$.isArray(policiesEntries)){
620 var propertiesEntries = data.propertiesEntries == null ? []: $.each(data.propertiesEntries,function(item){
621 return new Entry(item.key, item.value);
623 if (!$.isArray(propertiesEntries)){
624 propertiesEntries=[];
626 return new ProxyConnector(data.sourceRepoId,data.targetRepoId,data.proxyId,mapStringArray(data.blackListPatterns),
627 mapStringArray(data.whiteListPatterns),policiesEntries,propertiesEntries,
628 data.disabled,data.order);
631 mapProxyConnectors=function(data){
632 var mappedProxyConnectors = $.map(data, function(item) {
633 return mapProxyConnector(item);
635 return mappedProxyConnectors;
638 mapPolicyInformation=function(data){
642 var policyInformation = new PolicyInformation(mapStringArray(data.options),data.defaultOption,data.id,data.name);
643 $.log("policyInformation.options:"+policyInformation.options());
644 return policyInformation;
647 mapPolicyInformations=function(data){
648 return $.map(data, function(item) {
649 return mapPolicyInformation(item);
653 removeProxyConnector=function(proxyConnector,fnSuccessCallback){
655 var url="restServices/archivaServices/proxyConnectorService/removeProxyConnector?";
656 url += "sourceRepoId="+encodeURIComponent(proxyConnector.sourceRepoId());
657 url += "&targetRepoId="+encodeURIComponent(proxyConnector.targetRepoId());
661 contentType: 'application/json',
662 success: function(data) {
665 error: function(data) {
666 var res = $.parseJSON(data.responseText);
667 displayRestError(res);