]> source.dussan.org Git - archiva.git/blob
c2d7a07119ef82d4be0c981697a5fe6cea40f4d1
[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/features/10networkproxies/main",["jquery","i18n","jquery.tmpl","bootstrap","jquery.validate","knockout"
20   ,"knockout.simpleGrid"], function(jquery,i18n,jqueryTmpl,bootstrap,jqueryValidate,ko) {
21
22   showMenu = function(administrationMenuItems) {
23       administrationMenuItems.push(
24             {  text : $.i18n.prop('menu.network-proxies')          , id: "menu-network-proxies-list-a"        , href: "#networkproxies"       , redback: "{permissions: ['archiva-manage-configuration']}", func: function(){displayNetworkProxies()}}
25     );
26   }
27    
28   NetworkProxy=function(id,protocol,host,port,username,password,useNtlm){
29     var self=this;
30     //private String id;
31     this.id = ko.observable(id);
32     this.id.subscribe(function(newValue){self.modified(true)});
33
34     //private String protocol = "http";
35     this.protocol=ko.observable(protocol);
36     this.protocol.subscribe(function(newValue){self.modified(true)});
37
38     //private String host;
39     this.host=ko.observable(host);
40     this.host.subscribe(function(newValue){self.modified(true)});
41
42     //private int port = 8080;
43     this.port=ko.observable(port);
44     this.port.subscribe(function(newValue){self.modified(true)});
45
46     //private String username;
47     this.username=ko.observable(username?username:"");
48     this.username.subscribe(function(newValue){self.modified(true)});
49
50     //private String password;
51     this.password=ko.observable(password?password:"");
52     this.password.subscribe(function(newValue){self.modified(true)});
53
54     //use NTLM proxy
55     this.useNtlm=ko.observable(useNtlm?useNtlm:false);
56     this.useNtlm.subscribe(function(newValue){self.modified(true)});
57
58     this.modified=ko.observable(false);
59   }
60
61   NetworkProxyViewModel=function(networkProxy, update, networkProxiesViewModel,bulkMode){
62     this.update=update;
63     this.networkProxy=networkProxy;
64     this.networkProxiesViewModel=networkProxiesViewModel;
65     var self=this;
66     this.bulkMode=false || bulkMode;
67
68     this.save=function(){
69       if (!$("#main-content" ).find("#network-proxy-edit-form").valid()){
70         return;
71       }
72       if (!this.bulkMode){
73         clearUserMessages();
74       }
75       if (update){
76         $.ajax("restServices/archivaServices/networkProxyService/updateNetworkProxy",
77           {
78             type: "POST",
79             contentType: 'application/json',
80             data: ko.toJSON(networkProxy),
81             dataType: 'json',
82             success: function(data) {
83               $.log("update proxy id:"+self.networkProxy.id());
84               var message=$.i18n.prop('networkproxy.updated',self.networkProxy.id());
85               displaySuccessMessage(message);
86               self.networkProxy.modified(false);
87               if (!this.bulkMode){
88                 activateNetworkProxiesGridTab();
89               }
90             },
91             error: function(data) {
92               var res = $.parseJSON(data.responseText);
93               displayRestError(res);
94             }
95           }
96         );
97       } else {
98
99         $.ajax("restServices/archivaServices/networkProxyService/addNetworkProxy",
100           {
101             type: "POST",
102             contentType: 'application/json',
103             data: ko.toJSON(networkProxy),
104             dataType: 'json',
105             success: function(data) {
106               self.networkProxy.modified(false);
107               self.networkProxiesViewModel.networkProxies.push(self.networkProxy);
108               displaySuccessMessage($.i18n.prop('networkproxy.added',self.networkProxy.id()));
109               activateNetworkProxiesGridTab();
110             },
111             error: function(data) {
112               var res = $.parseJSON(data.responseText);
113               displayRestError(res);
114             }
115           }
116         );
117
118       }
119     }
120
121     displayGrid=function(){
122       activateNetworkProxiesGridTab();
123     }
124   }
125
126   NetworkProxiesViewModel=function(){
127     this.networkProxies=ko.observableArray([]);
128
129     var self=this;
130
131     this.gridViewModel = null;
132
133     editNetworkProxy=function(networkProxy){
134       clearUserMessages();
135       $.log("editNetworkProxy");
136       var mainContent = $("#main-content");
137       mainContent.find("#network-proxies-view-tabs-li-edit a").html($.i18n.prop("edit"));
138       var viewModel = new NetworkProxyViewModel(networkProxy,true,self);
139       ko.applyBindings(viewModel,mainContent.find("#network-proxies-edit").get(0));
140       activateNetworkProxyFormValidation();
141       activateNetworkProxyEditTab();
142       mainContent.find("#network-proxy-btn-save").attr("disabled","true");
143       mainContent.find("#network-proxy-btn-save").button('toggle');
144     }
145
146     this.bulkSave=function(){
147       return getModifiedNetworkProxies().length>0;
148     }
149
150     getModifiedNetworkProxies=function(){
151       var prx = $.grep(self.networkProxies(),
152           function (networkProxy,i) {
153             return networkProxy.modified();
154           });
155       return prx;
156     }
157
158
159     updateModifiedNetworkProxies=function(){
160       var modifiedNetworkProxies = getModifiedNetworkProxies();
161
162       openDialogConfirm(function(){
163                           for(var i=0;i<modifiedNetworkProxies.length;i++){
164                             var viewModel = new NetworkProxyViewModel(modifiedNetworkProxies[i],true,self,false);
165                             viewModel.save();
166                           }
167                           closeDialogConfirm();
168                         },
169                         $.i18n.prop('ok'),
170                         $.i18n.prop('cancel'),
171                         $.i18n.prop('networkproxy.bulk.save.confirm.title'),
172                         $.i18n.prop('networkproxy.bulk.save.confirm',modifiedNetworkProxies.length));
173
174
175     }
176
177     updateNetworkProxy=function(networkProxy){
178       var viewModel = new NetworkProxyViewModel(networkProxy,true,self,false);
179       viewModel.save();
180     }
181
182     removeNetworkProxy=function(networkProxy){
183       openDialogConfirm(
184           function(){
185             $.ajax("restServices/archivaServices/networkProxyService/deleteNetworkProxy/"+encodeURIComponent(networkProxy.id()),
186               {
187                 type: "get",
188                 success: function(data) {
189                   self.networkProxies.remove(networkProxy);
190                   clearUserMessages();
191                   displaySuccessMessage($.i18n.prop('networkproxy.deleted',networkProxy.id()));
192                   activateNetworkProxiesGridTab();
193                 },
194                 error: function(data) {
195                   var res = $.parseJSON(data.responseText);
196                   displayRestError(res);
197                 },
198                 complete: function(){
199                   closeDialogConfirm();
200                 }
201               }
202             )}, $.i18n.prop('ok'), $.i18n.prop('cancel'), $.i18n.prop('networkproxy.delete.confirm',networkProxy.id()),
203             $("#network-proxy-delete-warning-tmpl" ).tmpl(networkProxy));
204     }
205   }
206
207
208   displayNetworkProxies=function(){
209     screenChange();
210     var mainContent = $("#main-content");
211     mainContent.html(mediumSpinnerImg());
212
213
214
215     loadNetworkProxies( function(data) {
216         var networkProxiesViewModel = new NetworkProxiesViewModel();
217         mainContent.html($("#networkProxiesMain").tmpl());
218         mainContent.find("#network-proxies-view-tabs a:first").tab('show');
219
220         mainContent.find("#network-proxies-view-tabs").on('show', function (e) {
221           if ($(e.target).attr("href")=="#network-proxies-edit") {
222             var viewModel = new NetworkProxyViewModel(new NetworkProxy(),false,networkProxiesViewModel);
223             ko.applyBindings(viewModel,$("#main-content" ).find("#network-proxies-edit").get(0));
224             activateNetworkProxyFormValidation();
225             clearUserMessages();
226           }
227           if ($(e.target).attr("href")=="#network-proxies-view") {
228             $("#main-content" ).find("#network-proxies-view-tabs-li-edit a").html($.i18n.prop("add"));
229             clearUserMessages();
230           }
231
232         });
233         networkProxiesViewModel.networkProxies(mapNetworkProxies(data));
234         networkProxiesViewModel.gridViewModel = new ko.simpleGrid.viewModel({
235           data: networkProxiesViewModel.networkProxies,
236           columns: [
237             {
238               headerText: $.i18n.prop('identifier'),
239               rowText: "id"
240             },
241             {
242               headerText: $.i18n.prop('protocol'),
243               rowText: "protocol"
244             },
245             {
246             headerText: $.i18n.prop('host'),
247             rowText: "host"
248             },
249             {
250             headerText: $.i18n.prop('port'),
251             rowText: "port"
252             },
253             {
254             headerText: $.i18n.prop('username'),
255             rowText: "username"
256             }
257           ],
258           pageSize: 5,
259           gridUpdateCallBack: function(networkProxy){
260             $("#main-content" ).find("#networkProxiesTable [title]").tooltip();
261           }
262         });
263         ko.applyBindings(networkProxiesViewModel,$("#main-content" ).find("#network-proxies-view").get(0));
264       }
265     )
266   }
267
268   loadNetworkProxies=function(successCallbackFn, errorCallbackFn){
269     $.ajax("restServices/archivaServices/networkProxyService/getNetworkProxies", {
270         type: "GET",
271         dataType: 'json',
272         success: successCallbackFn,
273         error: errorCallbackFn
274     });
275   }
276
277   activateNetworkProxyFormValidation=function(){
278     var editForm=$("#main-content" ).find("#network-proxy-edit-form");
279     var validator = editForm.validate({
280       rules: {id: {
281        required: true,
282        remote: {
283          url: "restServices/archivaUiServices/dataValidatorService/networkProxyIdNotExists",
284          type: "get"
285        }
286       }},
287       showErrors: function(validator, errorMap, errorList) {
288        customShowError(editForm,validator,errorMap,errorMap);
289       }
290     });
291     validator.settings.messages["id"]=$.i18n.prop("id.required.or.alreadyexists");
292   }
293
294   activateNetworkProxiesGridTab=function(){
295     var mainContent = $("#main-content");
296     mainContent.find("#network-proxies-view-tabs-li-edit").removeClass("active");
297     mainContent.find("#network-proxies-edit").removeClass("active");
298
299     mainContent.find("#network-proxies-view-tabs-li-grid").addClass("active");
300     mainContent.find("#network-proxies-view").addClass("active");
301     mainContent.find("#network-proxies-view-tabs-li-edit a").html($.i18n.prop("add"));
302
303   }
304
305   activateNetworkProxyEditTab=function(){
306     var mainContent = $("#main-content");
307     mainContent.find("#network-proxies-view-tabs-li-grid").removeClass("active");
308     mainContent.find("#network-proxies-view").removeClass("active");
309
310     mainContent.find("#network-proxies-view-tabs-li-edit").addClass("active");
311     mainContent.find("#network-proxies-edit").addClass("active");
312   }
313
314   mapNetworkProxy=function(data){
315     if (data==null){
316       return null;
317     }
318     return new NetworkProxy(data.id,data.protocol,data.host,data.port,data.username,data.password,data.useNtlm);
319   }
320
321   mapNetworkProxies=function(data){
322     var mappedNetworkProxies = $.map(data, function(item) {
323       return mapNetworkProxy(item);
324     });
325     return mappedNetworkProxies;
326   }
327
328 });