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/10maven2/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();
194 self.proxyConnector.whiteListPatterns(tab);
195 self.proxyConnector.modified(true);
199 removeWhitelistPattern=function(pattern){
200 self.proxyConnector.whiteListPatterns.remove(pattern);
201 self.proxyConnector.modified(true);
204 this.save=function(){
205 //FIXME data controls !!!
207 var userMessages=$("#user-messages");
208 userMessages.html(mediumSpinnerImg());
209 $("#proxy-connector-btn-save" ).button("loading");
210 // update is delete then add
212 $.ajax("restServices/archivaServices/proxyConnectorService/updateProxyConnector",
215 data: ko.toJSON(self.proxyConnector),
216 contentType: 'application/json',
218 success: function(data) {
219 displaySuccessMessage($.i18n.prop('proxyconnector.updated'));
220 activateProxyConnectorsGridTab();
221 self.proxyConnector.modified(false);
223 error: function(data) {
224 var res = $.parseJSON(data.responseText);
225 displayRestError(res);
227 complete: function(){
228 removeMediumSpinnerImg(userMessages);
229 $("#proxy-connector-btn-save" ).button("reset");
235 $.ajax("restServices/archivaServices/proxyConnectorService/addProxyConnector",
238 data: ko.toJSON(self.proxyConnector),
239 contentType: 'application/json',
241 success: function(data) {
242 displaySuccessMessage($.i18n.prop('proxyconnector.added'));
243 activateProxyConnectorsGridTab();
244 self.proxyConnector.modified(false);
245 self.proxyConnectorsViewModel.proxyConnectors.push(self.proxyConnector);
247 error: function(data) {
248 var res = $.parseJSON(data.responseText);
249 displayRestError(res);
251 complete: function(){
252 removeMediumSpinnerImg(userMessages);
253 $("#proxy-connector-btn-save" ).button("reset");
260 this.deleteProperty=function(key){
261 for(var i=0;i<self.proxyConnector.propertiesEntries().length;i++){
262 var entry=self.proxyConnector.propertiesEntries()[i];
263 if (entry.key()==key()){
264 self.proxyConnector.propertiesEntries.remove(entry);
265 self.proxyConnector.modified(true);
271 this.addProperty=function(){
272 var mainContent=$("#main-content");
273 var key=mainContent.find("#property-key").val();
274 var value=mainContent.find("#property-value").val();
275 var oldTab = self.proxyConnector.propertiesEntries();
276 oldTab.push(new Entry(key,value));
277 self.proxyConnector.propertiesEntries(oldTab);
278 mainContent.find("#property-key").val("");
279 mainContent.find("#property-value").val("");
280 self.proxyConnector.modified(true);
283 displayGrid=function(){
284 activateProxyConnectorsGridTab();
288 ProxyConnectorsViewModel=function(){
290 this.proxyConnectors=ko.observableArray([]);
291 this.proxyConnectors.subscribe(function(newValue){
292 $.log("ProxyConnectorsViewModel#proxyConnectors modified");
293 self.proxyConnectors().sort(function(a,b){
294 if ( a.sourceRepoId()== b.sourceRepoId()) return a.order() - b.order();
295 return (a.sourceRepoId() > b.sourceRepoId())? -1:1;
298 this.policyInformations=ko.observableArray([]);
299 this.managedRepositories=ko.observableArray([]);
300 this.remoteRepositories=ko.observableArray([]);
301 this.networkProxies=ko.observableArray([]);
303 this.bulkSave=function(){
304 return getModifiedProxyConnectors().length>0;
307 getModifiedProxyConnectors=function(){
308 var prx = $.grep(self.proxyConnectors(),
309 function (proxyConnector,i) {
310 return proxyConnector.modified();
315 this.updateModifiedProxyConnectors=function(){
316 var modifiedProxyConnectors = getModifiedProxyConnectors();
318 openDialogConfirm(function(){
319 for(var i=0;i<modifiedProxyConnectors.length;i++){
320 var viewModel = new ProxyConnectorViewModel(modifiedProxyConnectors[i],true,self,false);
323 closeDialogConfirm();
326 $.i18n.prop('cancel'),
327 $.i18n.prop('proxy-connectors.bulk.save.confirm.title'),
328 $.i18n.prop('proxy.connector.bulk.save.confirm',modifiedProxyConnectors.length));
331 updateProxyConnector=function(proxyConnector){
332 var viewModel = new ProxyConnectorViewModel(proxyConnector,true,self,false);
336 editProxyConnector=function(proxyConnector){
337 var proxyConnectorViewModel=new ProxyConnectorViewModel(proxyConnector,true,self);
338 var mainContent = $("#main-content");
339 mainContent.find("#proxy-connectors-edit").html($("#proxy-connector-edit-form-tmpl").tmpl());
340 ko.applyBindings(proxyConnectorViewModel,mainContent.find("#proxy-connectors-edit").get(0));
341 activateProxyConnectorsEditTab();
342 mainContent.find("#proxy-connectors-view-tabs-li-edit a").html($.i18n.prop("edit"));
345 deleteProxyConnector=function(proxyConnector){
350 removeProxyConnector(proxyConnector,function(){
351 displaySuccessMessage($.i18n.prop('proxyconnector.removed'));
352 self.proxyConnectors.remove(proxyConnector);
353 closeDialogConfirm();
354 })}, $.i18n.prop('ok'), $.i18n.prop('cancel'), $.i18n.prop('proxyconnector.delete.confirm'),
355 $("#proxy-connector-delete-warning-tmpl").tmpl(proxyConnector));
361 getManagedRepository=function(id){
362 return findManagedRepository(id,self.managedRepositories());
365 getRemoteRepository=function(id){
366 var remoteRepository=$.grep(self.remoteRepositories(),
368 return repo.id()==id;
371 return ($.isArray(remoteRepository) && remoteRepository.length>0) ? remoteRepository[0]:new RemoteRepository();
374 getProxyConnector=function(sourceRepoId,targetRepoId){
375 var proxyConnectors=$.grep(self.proxyConnectors(),
376 function(proxyConnector,idx){
377 return proxyConnector.sourceRepoId()==sourceRepoId
378 && proxyConnector.targetRepoId()==targetRepoId;
381 var res = ($.isArray(proxyConnectors) && proxyConnectors.length>0) ? proxyConnectors[0]:new ProxyConnector();
384 showSettings=function(){
385 $.log("showSettings");
386 $("#body_content" ).find(".popover" ).hide();
387 //$("#main-content").find("[id^='proxy-connectors-grid-remoterepo-settings-edit-']" ).popover("hide");
389 buildSettings=function(proxyConnector){
390 var tmplHtml = $("#proxy-connectors-remote-settings-popover-tmpl")
392 proxyConnectorsViewModel: self,
393 proxyConnector:ko.toJS(proxyConnector)
396 var targetImg = $(("#proxy-connectors-grid-remoterepo-settings-edit-")
397 +proxyConnector.sourceRepoId().replace(/\./g,"\\\.")+"-"+proxyConnector.targetRepoId().replace(/\./g,"\\\."));
401 this.displaySettings=function(sourceRepoId,targetRepoId,targetContentStartId, targetImgStartId){
402 var proxyConnector=getProxyConnector(sourceRepoId,targetRepoId);
403 showSettings(proxyConnector,targetContentStartId,targetImgStartId);
406 this.findPolicyInformationName=function(id){
407 for(var i=0;i<self.policyInformations().length;i++){
408 if (id==self.policyInformations()[i].id()){
409 return self.policyInformations()[i].name();
415 orderChangeAware=function(proxyConnector){
416 return findProxyConnectorsWithSourceId(proxyConnector).length>1;
419 findProxyConnectorsWithSourceId=function(proxyConnector){
420 return $.grep(self.proxyConnectors(),function(curProxyConnector,idx){
421 return curProxyConnector.sourceRepoId()==proxyConnector.sourceRepoId();
426 displayOrderEdit=function(proxyConnector){
427 var proxyConnectors=findProxyConnectorsWithSourceId(proxyConnector);
428 $.log("displayOrderEdit:"+proxyConnector.sourceRepoId()+",number:"+proxyConnectors.length);
430 var managedRepository = getManagedRepository(proxyConnector.sourceRepoId());
431 var proxyConnectorEditOrderViewModel=new ProxyConnectorEditOrderViewModel(proxyConnectors,self,managedRepository);
432 ko.applyBindings(proxyConnectorEditOrderViewModel,$("#main-content").find("#proxy-connector-edit-order").get(0));
433 activateProxyConnectorsEditOrderTab();
436 this.displayGrid=function(){
437 this.gridViewModel = new ko.simpleGrid.viewModel({
438 data: self.proxyConnectors,
440 gridUpdateCallBack: function(){
441 $("#main-content" ).find("#proxyConnectorsTable" ).find("[title]").tooltip();
444 var mainContent = $("#main-content");
446 ko.applyBindings(this,mainContent.find("#proxy-connectors-view").get(0));
447 var prxGrids=mainContent.find("[id^='proxy-connectors-grid-remoterepo-settings-edit-']");
449 removeSmallSpinnerImg();
450 mainContent.find("#proxy-connectors-view-tabs #proxy-connectors-view-tabs-a-network-proxies-grid").tab('show');
452 mainContent.find("#proxy-connectors-view-tabs").on('show', function (e) {
453 $.log("on show:"+$(e.target).attr("href"));
454 if ($(e.target).attr("href")=="#proxy-connectors-edit") {
455 $.log("#proxy-connectors-edit");
456 var proxyConnector=new ProxyConnector();
457 var defaultPolicies=new Array();
458 // populate with defaut policies options
459 for (i=0;i<self.policyInformations().length;i++){
460 defaultPolicies.push(new Entry(self.policyInformations()[i].id(),self.policyInformations()[i].defaultOption));
462 proxyConnector.policiesEntries(defaultPolicies);
463 var proxyConnectorViewModel=new ProxyConnectorViewModel(proxyConnector,false,self);
464 mainContent.find("#proxy-connectors-edit").html($("#proxy-connector-edit-form-tmpl").tmpl());
465 ko.applyBindings(proxyConnectorViewModel,mainContent.find("#proxy-connectors-edit").get(0));
466 mainContent.find("#sourceRepoId" ).select2();
467 mainContent.find("#targetRepoId" ).select2();
469 if ($(e.target).attr("href")=="#proxy-connectors-view") {
470 $("#proxy-connectors-view-tabs-a-network-proxies-grid").html($.i18n.prop("proxy-connectors.grid.tab.title"));
471 mainContent.find("#proxy-connectors-view-tabs-li-edit a").html($.i18n.prop("add"));
473 if ($(e.target).attr("href")=="#proxy-connectors-edit-order") {
474 activateProxyConnectorsEditOrderTab();
482 ProxyConnectorEditOrderViewModel=function(proxyConnectors,proxyConnectorsViewModel,managedRepository){
484 this.proxyConnectors=ko.observableArray(proxyConnectors);
485 this.proxyConnectorsViewModel=proxyConnectorsViewModel;
486 this.managedRepository=managedRepository;
487 proxyConnectorMoved=function(arg){
488 $.log("proxyConnectorMoved:"+arg.sourceIndex+" to " + arg.targetIndex);
489 // if only 1 move just update two whereas update all with the new order
490 if (arg.targetIndex-arg.sourceIndex==1){
491 self.proxyConnectors()[arg.targetIndex].order(arg.targetIndex+1);
492 self.proxyConnectors()[arg.sourceIndex].order(arg.sourceIndex+1);
494 for (i=0;i<self.proxyConnectors().length;i++){
495 self.proxyConnectors()[i].order(i+1);
500 this.findRemoteRepository=function(id){
501 $.log("findRemoteRepository:"+id());
502 for(var i=0;i<self.proxyConnectorsViewModel.remoteRepositories().length;i++){
503 if (self.proxyConnectorsViewModel.remoteRepositories()[i].id()==id()){
504 return self.proxyConnectorsViewModel.remoteRepositories()[i];
510 this.updateModifiedProxyConnectors=function(){
511 self.proxyConnectorsViewModel.updateModifiedProxyConnectors();
514 displaySettings=function(sourceRepoId,targetRepoId){
515 $.log("ProxyConnectorEditOrderViewModel#showSettings:"+sourceRepoId+"-"+targetRepoId);
516 self.proxyConnectorsViewModel.displaySettings(sourceRepoId,targetRepoId,
517 "#proxy-connectors-order-remoterepo-settings-content-",
518 "#proxy-connectors-order-remoterepo-settings-edit-");
523 displayProxyConnectors=function(){
525 var mainContent = $("#main-content");
526 mainContent.html($("#proxyConnectorsMain").tmpl());
527 mainContent.append(smallSpinnerImg());
529 this.proxyConnectorsViewModel = new ProxyConnectorsViewModel();
532 loadManagedRepositories(function(data) {
533 self.proxyConnectorsViewModel.managedRepositories(mapManagedRepositories(data));
535 loadRemoteRepositories(function(data) {
536 self.proxyConnectorsViewModel.remoteRepositories(mapRemoteRepositories(data));
538 loadNetworkProxies(function(data) {
539 self.proxyConnectorsViewModel.networkProxies(mapNetworkProxies(data));
541 loadAllPolicies( function(data) {
542 self.proxyConnectorsViewModel.policyInformations(mapPolicyInformations(data));
544 loadAllProxyConnectors( function(data) {
545 self.proxyConnectorsViewModel.proxyConnectors(mapProxyConnectors(data));
546 self.proxyConnectorsViewModel.displayGrid();
559 loadAllPolicies=function(successCallBackFn,errorCallBackFn){
560 $.ajax("restServices/archivaServices/proxyConnectorService/allPolicies", {
563 success: successCallBackFn,
564 error: errorCallBackFn
569 loadAllProxyConnectors=function(successCallBackFn,errorCallBackFn){
570 $.ajax("restServices/archivaServices/proxyConnectorService/getProxyConnectors", {
573 success: successCallBackFn,
574 error: errorCallBackFn
578 activateProxyConnectorsGridTab=function(){
579 var mainContent = $("#main-content");
580 mainContent.find("#proxy-connectors-view-tabs-content div[class*='tab-pane']").removeClass("active");
581 mainContent.find("#proxy-connectors-view-tabs li").removeClass("active");
583 mainContent.find("#proxy-connectors-view").addClass("active");
584 mainContent.find("#proxy-connectors-view-tabs-li-grid").addClass("active");
585 mainContent.find("#proxy-connectors-view-tabs-li-edit a").html($.i18n.prop("add"));
589 activateProxyConnectorsEditTab=function(){
590 var mainContent = $("#main-content");
592 mainContent.find("#proxy-connectors-view-tabs-content div[class*='tab-pane']").removeClass("active");
593 mainContent.find("#proxy-connectors-view-tabs li").removeClass("active");
595 mainContent.find("#proxy-connectors-edit").addClass("active");
596 mainContent.find("#proxy-connectors-view-tabs-li-edit").addClass("active");
599 activateProxyConnectorsEditOrderTab=function(){
600 var mainContent = $("#main-content");
602 mainContent.find("#proxy-connectors-view-tabs-content div[class*='tab-pane']").removeClass("active");
603 mainContent.find("#proxy-connectors-view-tabs li").removeClass("active");
605 mainContent.find("#proxy-connector-edit-order").addClass("active");
606 mainContent.find("#proxy-connectors-view-tabs-li-edit-order").addClass("active");
609 mapProxyConnector=function(data){
613 var policiesEntries = data.policiesEntries == null ? []: $.each(data.policiesEntries,function(item){
614 return new Entry(item.key, item.value);
616 if (!$.isArray(policiesEntries)){
619 var propertiesEntries = data.propertiesEntries == null ? []: $.each(data.propertiesEntries,function(item){
620 return new Entry(item.key, item.value);
622 if (!$.isArray(propertiesEntries)){
623 propertiesEntries=[];
625 return new ProxyConnector(data.sourceRepoId,data.targetRepoId,data.proxyId,mapStringArray(data.blackListPatterns),
626 mapStringArray(data.whiteListPatterns),policiesEntries,propertiesEntries,
627 data.disabled,data.order);
630 mapProxyConnectors=function(data){
631 var mappedProxyConnectors = $.map(data, function(item) {
632 return mapProxyConnector(item);
634 return mappedProxyConnectors;
637 mapPolicyInformation=function(data){
641 var policyInformation = new PolicyInformation(mapStringArray(data.options),data.defaultOption,data.id,data.name);
642 $.log("policyInformation.options:"+policyInformation.options());
643 return policyInformation;
646 mapPolicyInformations=function(data){
647 return $.map(data, function(item) {
648 return mapPolicyInformation(item);
652 removeProxyConnector=function(proxyConnector,fnSuccessCallback){
654 var url="restServices/archivaServices/proxyConnectorService/removeProxyConnector?";
655 url += "sourceRepoId="+encodeURIComponent(proxyConnector.sourceRepoId());
656 url += "&targetRepoId="+encodeURIComponent(proxyConnector.targetRepoId());
660 contentType: 'application/json',
661 success: function(data) {
664 error: function(data) {
665 var res = $.parseJSON(data.responseText);
666 displayRestError(res);