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-rules",["jquery","i18n","jquery.tmpl","bootstrap","jquery.ui","knockout"
20 ,"knockout.simpleGrid","knockout.sortable","archiva/admin/repository/maven2/proxy-connectors"],
21 function(jquery,i18n,jqueryTmpl,bootstrap,jqueryUi,ko) {
23 ProxyConnectorRulesViewModel=function(proxyConnectorRules,proxyConnectors){
25 self.proxyConnectorRules=ko.observableArray(proxyConnectorRules?proxyConnectorRules:[]);
26 self.proxyConnectors=ko.observableArray(proxyConnectors);
27 self.proxyConnectors.id="select";
29 // FIXME get that from a REST service
31 this.ruleTypes=[new RuleType("BLACK_LIST","Black list","images/red-22-22.png"),new RuleType("WHITE_LIST","White list","images/green-22-22.png")];
33 this.findRuleType=function(proxyConnectorRule){
35 $.each(self.ruleTypes, function(index, value) {
36 if(value.type==proxyConnectorRule.proxyConnectorRuleType()){
43 this.findProxyConnector=function(sourceRepoId,targetRepoId){
44 for(var i=0;i<self.proxyConnectors().length;i++){
45 var proxyConnector=self.proxyConnectors()[i];
46 if(proxyConnector.sourceRepoId()==sourceRepoId && proxyConnector.targetRepoId()==targetRepoId){
47 return proxyConnector;
52 this.displayGrid=function(){
53 var mainContent = $("#main-content");
55 $.each(self.proxyConnectorRules(), function(index, value) {
56 value.ruleType=self.findRuleType(value);
59 this.gridViewModel = new ko.simpleGrid.viewModel({
60 data: self.proxyConnectorRules,
62 gridUpdateCallBack: function(){
63 //$("#main-content" ).find("#proxy-connectors-rules-view-tabsTable" ).find("[title]").tooltip();
67 ko.applyBindings(self,mainContent.find("#proxy-connector-rules-view").get(0));
69 removeSmallSpinnerImg(mainContent);
71 mainContent.find("#proxy-connectors-rules-view-tabs").on('show', function (e) {
72 $.log("on show:"+$(e.target).attr("href"));
73 if ($(e.target).attr("href")=="#proxy-connector-rules-edit") {
74 var proxyConnectorRuleViewModel = new ProxyConnectorRuleViewModel(new ProxyConnectorRule(),self,false);
75 ko.applyBindings(proxyConnectorRuleViewModel,mainContent.find("#proxy-connector-rules-edit" ).get(0));
76 activateProxyConnectorRulesEditTab();
80 addProxyConnectorRule=function(proxyConnectorRule){
81 $("#proxy-connector-rule-add-btn" ).button("loading");
82 $.log("addProxyConnectorRule");
83 self.saveProxyConnectorRule(proxyConnectorRule,"restServices/archivaServices/proxyConnectorRuleService/proxyConnectorRule",true,
85 $("#proxy-connector-rule-add-btn" ).button("reset");
89 this.saveProxyConnectorRule=function(proxyConnectorRule,url,add,completeFnCallback){
90 $.log("saveProxyConnectorRule:"+url);
91 var userMessages=$("#user-messages");
92 userMessages.html(mediumSpinnerImg());
96 contentType: 'application/json',
97 data: ko.toJSON(proxyConnectorRule),
99 success: function(data) {
100 $.log("save proxyConnectorRule pattern:"+proxyConnectorRule.pattern());
101 var message=$.i18n.prop(add?'proxy-connector-rule.added':'proxy-connector-rule.updated',proxyConnectorRule.pattern());
102 displaySuccessMessage(message);
103 proxyConnectorRule.modified(false);
105 // add rule type for image
106 proxyConnectorRule.ruleType=self.findRuleType(proxyConnectorRule);
107 self.proxyConnectorRules.push(proxyConnectorRule);
109 activateProxyConnectorRulesGridTab();
111 error: function(data) {
112 var res = $.parseJSON(data.responseText);
113 displayRestError(res);
115 complete:function(data){
116 removeMediumSpinnerImg(userMessages);
117 if(completeFnCallback){
118 completeFnCallback();
125 updateProxyConnectorRule=function(proxyConnectorRule){
126 $.log("updateProxyConnectorRule");
127 $("#main-content" ).find("#proxy-connectors-rules-edit-div").find("#proxy-connector-rule-update-btn").button("loading");
128 self.saveProxyConnectorRule(proxyConnectorRule,"restServices/archivaServices/proxyConnectorRuleService/updateProxyConnectorRule",
131 $("#proxy-connector-rule-update-btn" ).button("reset");
136 this.deleteProxyConnectorRule=function(proxyConnectorRule){
137 $("#main-content" ).find("proxy-connectors-rules-view-tabsTable").find(".btn").button("loading");
138 var userMessages=$("#user-messages");
139 userMessages.html(mediumSpinnerImg());
140 $.ajax("restServices/archivaServices/proxyConnectorRuleService/deleteProxyConnectorRule",
143 contentType: 'application/json',
144 data: ko.toJSON(proxyConnectorRule),
146 success:function(data){
147 var message=$.i18n.prop('proxy-connector-rule.deleted',proxyConnectorRule.pattern());
148 self.proxyConnectorRules.remove(proxyConnectorRule);
149 displaySuccessMessage(message);
151 error: function(data) {
152 var res = $.parseJSON(data.responseText);
153 displayRestError(res);
155 complete:function(data){
156 removeMediumSpinnerImg(userMessages);
157 $("#main-content" ).find("proxy-connectors-rules-view-tabsTable").find(".btn").button("reset");
163 removeProxyConnectorRule=function(proxyConnectorRule){
166 function(){self.deleteProxyConnectorRule(proxyConnectorRule);window.modalConfirmDialog.modal('hide')},
167 $.i18n.prop('ok'), $.i18n.prop('cancel'),
168 $.i18n.prop('proxy-connector-rule.delete.confirm',proxyConnectorRule.pattern()),"");
172 editProxyConnectorRule=function(proxyConnectorRule){
173 var proxyConnectorRuleViewModel=new ProxyConnectorRuleViewModel(proxyConnectorRule,self,true);
174 ko.applyBindings(proxyConnectorRuleViewModel,$("#main-content").find("#proxy-connector-rules-edit" ).get(0));
175 activateProxyConnectorRulesEditTab();
176 proxyConnectorRuleViewModel.activateRemoveChosen(self);
177 proxyConnectorRuleViewModel.activateRemoveAvailable(self);
186 ProxyConnectorRuleViewModel=function(proxyConnectorRule,proxyConnectorRulesViewModel,update){
188 this.proxyConnectorRule=proxyConnectorRule;
189 this.proxyConnectorRulesViewModel=proxyConnectorRulesViewModel;
190 this.availableProxyConnectors=ko.observableArray([]);
191 this.availableProxyConnectors.id="availableProxyConnectors";
194 $.each(this.proxyConnectorRulesViewModel.proxyConnectors(), function(idx, value) {
195 //$.log(idx + ': ' + value.sourceRepoId() +":"+value.targetRepoId());
197 // is it in proxyConnectorRule.proxyConnectors
198 $.each(self.proxyConnectorRule.proxyConnectors(),function(index,proxyConnector){
199 if(value.sourceRepoId()==proxyConnector.sourceRepoId() && value.targetRepoId()==proxyConnector.targetRepoId()){
204 self.availableProxyConnectors.push(value);
208 proxyConnectorMoved=function(arg){
209 $.log("repositoryMoved:"+arg.sourceIndex+" to " + arg.targetIndex);
210 self.proxyConnectorRule.modified(true);
211 self.activateRemoveChosen(self.proxyConnectorRulesViewModel);
212 self.activateRemoveAvailable(self.proxyConnectorRulesViewModel);
215 saveProxyConnectorRule=function(){
216 self.proxyConnectorRulesViewModel.saveProxyConnectorRule(self.proxyConnectorRule)
219 this.removeChosen=function(proxyConnectorRulesViewModel,sourceRepoId,targetRepoId){
220 $.log("removeChosen:"+sourceRepoId+":"+targetRepoId);
222 $.log("size before:"+self.proxyConnectorRule.proxyConnectors().length);
223 var proxyConnectorToRemove=null;
224 for(var i=0;i<self.proxyConnectorRule.proxyConnectors().length;i++){
225 if(self.proxyConnectorRule.proxyConnectors()[i].sourceRepoId()==sourceRepoId &&
226 self.proxyConnectorRule.proxyConnectors()[i].targetRepoId()==targetRepoId){
227 proxyConnectorToRemove=self.proxyConnectorRule.proxyConnectors()[i];
230 self.proxyConnectorRule.proxyConnectors.remove(proxyConnectorToRemove);
231 self.availableProxyConnectors.push(proxyConnectorToRemove);
232 $.log("size after:"+self.proxyConnectorRule.proxyConnectors().length);
233 var mainContent=$("#main-content");
234 mainContent.find("#proxy-connectors-rules-available-proxy-connectors" ).find("[data-source-repoId="+sourceRepoId+"][data-target-repoId="+targetRepoId+"]" ).on("click", function(){
235 self.removeAvailable(proxyConnectorRulesViewModel,$(this).attr("data-source-repoId"),$(this).attr("data-target-repoId"));
237 mainContent.find("#proxy-connectors-rules-edit-order-div" ).find("[data-source-repoId="+sourceRepoId+"][data-target-repoId="+targetRepoId+"]" ).off("click");
240 this.activateRemoveChosen=function(proxyConnectorRulesViewModel){
241 $("#main-content" ).find("#proxy-connectors-rules-edit-order-div" ).find(".icon-minus-sign" ).on("click", function(){
242 self.removeChosen(proxyConnectorRulesViewModel,$(this).attr("data-source-repoId"),$(this).attr("data-target-repoId"));
246 this.removeAvailable=function(proxyConnectorRulesViewModel,sourceRepoId,targetRepoId){
247 $.log("removeAvailable:"+sourceRepoId+":"+targetRepoId);
249 $.log("size before:"+self.availableProxyConnectors().length);
250 var proxyConnectorToAdd=null;
251 for(var i=0;i<self.availableProxyConnectors().length;i++){
252 if(self.availableProxyConnectors()[i].sourceRepoId()==sourceRepoId &&
253 self.availableProxyConnectors()[i].targetRepoId()==targetRepoId){
255 proxyConnectorToAdd=self.availableProxyConnectors()[i];
258 self.proxyConnectorRule.proxyConnectors.push(proxyConnectorToAdd);
259 self.availableProxyConnectors.remove(proxyConnectorToAdd);
260 $.log("size after:"+self.availableProxyConnectors().length);
261 var mainContent=$("#main-content");
262 mainContent.find("#proxy-connectors-rules-edit-order-div" ).find("[data-source-repoId="+sourceRepoId+"][data-target-repoId="+targetRepoId+"]" ).on("click", function(){
263 self.removeChosen(proxyConnectorRulesViewModel,$(this).attr("data-source-repoId"),$(this).attr("data-target-repoId"));
265 mainContent.find("#proxy-connectors-rules-available-proxy-connectors" ).find("[data-source-repoId="+sourceRepoId+"][data-target-repoId="+targetRepoId+"]" ).off("click");
268 this.activateRemoveAvailable=function(proxyConnectorRulesViewModel){
269 $("#main-content" ).find("#proxy-connectors-rules-available-proxy-connectors" ).find(".icon-plus-sign" ).on("click", function(){
270 self.removeAvailable(proxyConnectorRulesViewModel,$(this).attr("data-source-repoId"),$(this).attr("data-target-repoId"));
277 displayProxyConnectorsRules=function(){
278 $.log("displayProxyConnectorsRules");
280 var mainContent = $("#main-content");
281 mainContent.html($("#proxyConnectorsRulesMain").tmpl());
282 var userMessages=$("#user-messages");
283 userMessages.html(mediumSpinnerImg());
284 loadAllProxyConnectors(function(data){
285 var proxyConnectors = mapProxyConnectors(data);
287 $.ajax("restServices/archivaServices/proxyConnectorRuleService/proxyConnectorRules", {
290 success: function (data){
291 var proxyConnectorRules=mapProxyConnectorRules(data);
292 var proxyConnectorRulesViewModel = new ProxyConnectorRulesViewModel(proxyConnectorRules,proxyConnectors);
293 proxyConnectorRulesViewModel.displayGrid();
294 activateProxyConnectorRulesGridTab();
296 complete: function(data){
297 removeMediumSpinnerImg(userMessages);
305 ProxyConnectorRule=function(pattern,proxyConnectorRuleType,proxyConnectors){
306 //private String pattern;
309 this.modified=ko.observable(false);
311 //private String sourceRepoId;
312 this.pattern=ko.observable(pattern);
313 this.pattern.subscribe(function(newValue){
319 //private ProxyConnectorRuleType proxyConnectorRuleType;
320 this.proxyConnectorRuleType=ko.observable(proxyConnectorRuleType);
321 this.proxyConnectorRuleType.subscribe(function(newValue){
325 //private List<ProxyConnector> proxyConnectors;
326 this.proxyConnectors=ko.observableArray(proxyConnectors?proxyConnectors:[]);
327 this.proxyConnectors.subscribe(function(newValue){
334 mapProxyConnectorRule=function(data){
338 return new ProxyConnectorRule(data.pattern, data.proxyConnectorRuleType, mapProxyConnectors(data.proxyConnectors));
341 mapProxyConnectorRules=function(data){
342 var mappedProxyConnectorRules = $.map(data, function(item) {
343 return mapProxyConnectorRule(item);
345 return mappedProxyConnectorRules;
349 activateProxyConnectorRulesGridTab=function(){
350 var mainContent = $("#main-content");
351 mainContent.find("#proxy-connectors-rules-view-tabs-content div[class*='tab-pane']").removeClass("active");
352 mainContent.find("#proxy-connectors-rules-view-tabs li").removeClass("active");
354 mainContent.find("#proxy-connector-rules-view").addClass("active");
355 mainContent.find("#proxy-connectors-rules-view-tabs-li-grid").addClass("active");
356 mainContent.find("#proxy-connectors-rules-view-tabs-a-edit").html($.i18n.prop("add"));
360 activateProxyConnectorRulesEditTab=function(){
361 var mainContent = $("#main-content");
363 mainContent.find("#proxy-connectors-rules-view-tabs-content div[class*='tab-pane']").removeClass("active");
364 mainContent.find("#proxy-connectors-rules-view-tabs > li").removeClass("active");
366 mainContent.find("#proxy-connector-rules-edit").addClass("active");
367 mainContent.find("#proxy-connectors-rules-view-tabs-edit").addClass("active");
370 RuleType=function(type,label,image){