]> source.dussan.org Git - archiva.git/blob
4f55ec1534e3a6b885d74c0bd8e1c6d63775128c
[archiva.git] /
1 /*
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
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  * under the License.
18  */
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) {
22
23   ProxyConnectorRulesViewModel=function(proxyConnectorRules,proxyConnectors){
24     var self=this;
25     self.proxyConnectorRules=ko.observableArray(proxyConnectorRules?proxyConnectorRules:[]);
26     self.proxyConnectors=ko.observableArray(proxyConnectors);
27     self.proxyConnectors.id="select";
28
29     // FIXME get that from a REST service
30     // FIXME i18n
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")];
32
33     this.findRuleType=function(proxyConnectorRule){
34       var ruleType;
35       $.each(self.ruleTypes, function(index, value) {
36         if(value.type==proxyConnectorRule.proxyConnectorRuleType()){
37           ruleType=value;
38         }
39       });
40       return ruleType;
41     }
42
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;
48         }
49       }
50     }
51
52     this.displayGrid=function(){
53       var mainContent = $("#main-content");
54
55       $.each(self.proxyConnectorRules(), function(index, value) {
56         value.ruleType=self.findRuleType(value);
57       });
58
59       this.gridViewModel = new ko.simpleGrid.viewModel({
60         data: self.proxyConnectorRules,
61         pageSize: 5,
62         gridUpdateCallBack: function(){
63           //$("#main-content" ).find("#proxy-connectors-rules-view-tabsTable" ).find("[title]").tooltip();
64         }
65       });
66
67       ko.applyBindings(self,mainContent.find("#proxy-connector-rules-view").get(0));
68
69       removeSmallSpinnerImg(mainContent);
70
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();
77         }
78       });
79     }
80     addProxyConnectorRule=function(proxyConnectorRule){
81       $("#proxy-connector-rule-add-btn" ).button("loading");
82       $.log("addProxyConnectorRule");
83       self.saveProxyConnectorRule(proxyConnectorRule,"restServices/archivaServices/proxyConnectorRuleService/proxyConnectorRule",true,
84       function(){
85         $("#proxy-connector-rule-add-btn" ).button("reset");
86       });
87     }
88
89     this.saveProxyConnectorRule=function(proxyConnectorRule,url,add,completeFnCallback){
90       $.log("saveProxyConnectorRule:"+url);
91       var userMessages=$("#user-messages");
92       userMessages.html(mediumSpinnerImg());
93       $.ajax(url,
94         {
95           type: "POST",
96           contentType: 'application/json',
97           data: ko.toJSON(proxyConnectorRule),
98           dataType: 'json',
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);
104             if(add){
105               // add rule type for image
106               proxyConnectorRule.ruleType=self.findRuleType(proxyConnectorRule);
107               self.proxyConnectorRules.push(proxyConnectorRule);
108             }
109             activateProxyConnectorRulesGridTab();
110           },
111           error: function(data) {
112             var res = $.parseJSON(data.responseText);
113             displayRestError(res);
114           },
115           complete:function(data){
116             removeMediumSpinnerImg(userMessages);
117             if(completeFnCallback){
118               completeFnCallback();
119             }
120           }
121         }
122       );
123     }
124
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",
129                                   false,
130                                   function(){
131                                     $("#proxy-connector-rule-update-btn" ).button("reset");
132                                   }
133       );
134     }
135
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",
141        {
142          type:"POST",
143          contentType: 'application/json',
144          data: ko.toJSON(proxyConnectorRule),
145          dataType: 'json',
146          success:function(data){
147            var message=$.i18n.prop('proxy-connector-rule.deleted',proxyConnectorRule.pattern());
148            self.proxyConnectorRules.remove(proxyConnectorRule);
149            displaySuccessMessage(message);
150          },
151          error: function(data) {
152            var res = $.parseJSON(data.responseText);
153            displayRestError(res);
154          },
155          complete:function(data){
156            removeMediumSpinnerImg(userMessages);
157            $("#main-content" ).find("proxy-connectors-rules-view-tabsTable").find(".btn").button("reset");
158          }
159        }
160       );
161     }
162
163     removeProxyConnectorRule=function(proxyConnectorRule){
164
165       openDialogConfirm(
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()),"");
169
170     }
171
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);
178     }
179
180     remove=function(){
181       $.log("remove");
182     }
183
184   }
185
186   ProxyConnectorRuleViewModel=function(proxyConnectorRule,proxyConnectorRulesViewModel,update){
187     var self=this;
188     this.proxyConnectorRule=proxyConnectorRule;
189     this.proxyConnectorRulesViewModel=proxyConnectorRulesViewModel;
190     this.availableProxyConnectors=ko.observableArray([]);
191     this.availableProxyConnectors.id="availableProxyConnectors";
192     this.update=update;
193
194     $.each(this.proxyConnectorRulesViewModel.proxyConnectors(), function(idx, value) {
195       //$.log(idx + ': ' + value.sourceRepoId() +":"+value.targetRepoId());
196       var available=true;
197       // is it in proxyConnectorRule.proxyConnectors
198       $.each(self.proxyConnectorRule.proxyConnectors(),function(index,proxyConnector){
199         if(value.sourceRepoId()==proxyConnector.sourceRepoId() && value.targetRepoId()==proxyConnector.targetRepoId()){
200           available=false;
201         }
202       });
203       if(available==true){
204         self.availableProxyConnectors.push(value);
205       }
206     });
207
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);
213     }
214
215     saveProxyConnectorRule=function(){
216       self.proxyConnectorRulesViewModel.saveProxyConnectorRule(self.proxyConnectorRule)
217     }
218
219     this.removeChosen=function(proxyConnectorRulesViewModel,sourceRepoId,targetRepoId){
220       $.log("removeChosen:"+sourceRepoId+":"+targetRepoId);
221
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];
228         }
229       }
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"));
236       });
237       mainContent.find("#proxy-connectors-rules-edit-order-div" ).find("[data-source-repoId="+sourceRepoId+"][data-target-repoId="+targetRepoId+"]" ).off("click");
238     }
239
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"));
243       });
244     }
245
246     this.removeAvailable=function(proxyConnectorRulesViewModel,sourceRepoId,targetRepoId){
247       $.log("removeAvailable:"+sourceRepoId+":"+targetRepoId);
248
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){
254           $.log("found");
255           proxyConnectorToAdd=self.availableProxyConnectors()[i];
256         }
257       }
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"));
264       });
265       mainContent.find("#proxy-connectors-rules-available-proxy-connectors" ).find("[data-source-repoId="+sourceRepoId+"][data-target-repoId="+targetRepoId+"]" ).off("click");
266     }
267
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"));
271       });
272     }
273
274   }
275
276
277   displayProxyConnectorsRules=function(){
278     $.log("displayProxyConnectorsRules");
279     screenChange();
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);
286
287         $.ajax("restServices/archivaServices/proxyConnectorRuleService/proxyConnectorRules", {
288           type: "GET",
289           dataType: 'json',
290           success: function (data){
291             var proxyConnectorRules=mapProxyConnectorRules(data);
292             var proxyConnectorRulesViewModel = new ProxyConnectorRulesViewModel(proxyConnectorRules,proxyConnectors);
293             proxyConnectorRulesViewModel.displayGrid();
294             activateProxyConnectorRulesGridTab();
295           },
296           complete: function(data){
297             removeMediumSpinnerImg(userMessages);
298           }
299
300         });
301
302     });
303   }
304
305   ProxyConnectorRule=function(pattern,proxyConnectorRuleType,proxyConnectors){
306     //private String pattern;
307     var self=this;
308
309     this.modified=ko.observable(false);
310
311     //private String sourceRepoId;
312     this.pattern=ko.observable(pattern);
313     this.pattern.subscribe(function(newValue){
314       self.modified(true);
315     });
316
317     this.ruleType=null;
318
319     //private ProxyConnectorRuleType proxyConnectorRuleType;
320     this.proxyConnectorRuleType=ko.observable(proxyConnectorRuleType);
321     this.proxyConnectorRuleType.subscribe(function(newValue){
322       self.modified(true);
323     });
324
325     //private List<ProxyConnector> proxyConnectors;
326     this.proxyConnectors=ko.observableArray(proxyConnectors?proxyConnectors:[]);
327     this.proxyConnectors.subscribe(function(newValue){
328       self.modified(true);
329     });
330
331     this.ruleType=null;
332   }
333
334   mapProxyConnectorRule=function(data){
335     if (data==null){
336       return null;
337     }
338     return new ProxyConnectorRule(data.pattern, data.proxyConnectorRuleType, mapProxyConnectors(data.proxyConnectors));
339   }
340
341   mapProxyConnectorRules=function(data){
342     var mappedProxyConnectorRules = $.map(data, function(item) {
343       return mapProxyConnectorRule(item);
344     });
345     return mappedProxyConnectorRules;
346   }
347
348
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");
353
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"));
357
358   }
359
360   activateProxyConnectorRulesEditTab=function(){
361     var mainContent = $("#main-content");
362
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");
365
366     mainContent.find("#proxy-connector-rules-edit").addClass("active");
367     mainContent.find("#proxy-connectors-rules-view-tabs-edit").addClass("active");
368   }
369
370   RuleType=function(type,label,image){
371     this.type=type;
372     this.label=label;
373     this.image=image;
374   }
375
376 });