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/features/10networkproxies/main",["jquery","i18n","jquery.tmpl","bootstrap","jquery.validate","knockout"
20 ,"knockout.simpleGrid"], function(jquery,i18n,jqueryTmpl,bootstrap,jqueryValidate,ko) {
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()}}
28 NetworkProxy=function(id,protocol,host,port,username,password,useNtlm){
31 this.id = ko.observable(id);
32 this.id.subscribe(function(newValue){self.modified(true)});
34 //private String protocol = "http";
35 this.protocol=ko.observable(protocol);
36 this.protocol.subscribe(function(newValue){self.modified(true)});
38 //private String host;
39 this.host=ko.observable(host);
40 this.host.subscribe(function(newValue){self.modified(true)});
42 //private int port = 8080;
43 this.port=ko.observable(port);
44 this.port.subscribe(function(newValue){self.modified(true)});
46 //private String username;
47 this.username=ko.observable(username?username:"");
48 this.username.subscribe(function(newValue){self.modified(true)});
50 //private String password;
51 this.password=ko.observable(password?password:"");
52 this.password.subscribe(function(newValue){self.modified(true)});
55 this.useNtlm=ko.observable(useNtlm?useNtlm:false);
56 this.useNtlm.subscribe(function(newValue){self.modified(true)});
58 this.modified=ko.observable(false);
61 NetworkProxyViewModel=function(networkProxy, update, networkProxiesViewModel,bulkMode){
63 this.networkProxy=networkProxy;
64 this.networkProxiesViewModel=networkProxiesViewModel;
66 this.bulkMode=false || bulkMode;
69 if (!$("#main-content" ).find("#network-proxy-edit-form").valid()){
76 $.ajax("restServices/archivaServices/networkProxyService/updateNetworkProxy",
79 contentType: 'application/json',
80 data: ko.toJSON(networkProxy),
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);
88 activateNetworkProxiesGridTab();
91 error: function(data) {
92 var res = $.parseJSON(data.responseText);
93 displayRestError(res);
99 $.ajax("restServices/archivaServices/networkProxyService/addNetworkProxy",
102 contentType: 'application/json',
103 data: ko.toJSON(networkProxy),
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();
111 error: function(data) {
112 var res = $.parseJSON(data.responseText);
113 displayRestError(res);
121 displayGrid=function(){
122 activateNetworkProxiesGridTab();
126 NetworkProxiesViewModel=function(){
127 this.networkProxies=ko.observableArray([]);
131 this.gridViewModel = null;
133 editNetworkProxy=function(networkProxy){
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');
146 this.bulkSave=function(){
147 return getModifiedNetworkProxies().length>0;
150 getModifiedNetworkProxies=function(){
151 var prx = $.grep(self.networkProxies(),
152 function (networkProxy,i) {
153 return networkProxy.modified();
159 updateModifiedNetworkProxies=function(){
160 var modifiedNetworkProxies = getModifiedNetworkProxies();
162 openDialogConfirm(function(){
163 for(var i=0;i<modifiedNetworkProxies.length;i++){
164 var viewModel = new NetworkProxyViewModel(modifiedNetworkProxies[i],true,self,false);
167 closeDialogConfirm();
170 $.i18n.prop('cancel'),
171 $.i18n.prop('networkproxy.bulk.save.confirm.title'),
172 $.i18n.prop('networkproxy.bulk.save.confirm',modifiedNetworkProxies.length));
177 updateNetworkProxy=function(networkProxy){
178 var viewModel = new NetworkProxyViewModel(networkProxy,true,self,false);
182 removeNetworkProxy=function(networkProxy){
185 $.ajax("restServices/archivaServices/networkProxyService/deleteNetworkProxy/"+encodeURIComponent(networkProxy.id()),
188 success: function(data) {
189 self.networkProxies.remove(networkProxy);
191 displaySuccessMessage($.i18n.prop('networkproxy.deleted',networkProxy.id()));
192 activateNetworkProxiesGridTab();
194 error: function(data) {
195 var res = $.parseJSON(data.responseText);
196 displayRestError(res);
198 complete: function(){
199 closeDialogConfirm();
202 )}, $.i18n.prop('ok'), $.i18n.prop('cancel'), $.i18n.prop('networkproxy.delete.confirm',networkProxy.id()),
203 $("#network-proxy-delete-warning-tmpl" ).tmpl(networkProxy));
208 displayNetworkProxies=function(){
210 var mainContent = $("#main-content");
211 mainContent.html(mediumSpinnerImg());
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');
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();
227 if ($(e.target).attr("href")=="#network-proxies-view") {
228 $("#main-content" ).find("#network-proxies-view-tabs-li-edit a").html($.i18n.prop("add"));
233 networkProxiesViewModel.networkProxies(mapNetworkProxies(data));
234 networkProxiesViewModel.gridViewModel = new ko.simpleGrid.viewModel({
235 data: networkProxiesViewModel.networkProxies,
238 headerText: $.i18n.prop('identifier'),
242 headerText: $.i18n.prop('protocol'),
246 headerText: $.i18n.prop('host'),
250 headerText: $.i18n.prop('port'),
254 headerText: $.i18n.prop('username'),
259 gridUpdateCallBack: function(networkProxy){
260 $("#main-content" ).find("#networkProxiesTable [title]").tooltip();
263 ko.applyBindings(networkProxiesViewModel,$("#main-content" ).find("#network-proxies-view").get(0));
268 loadNetworkProxies=function(successCallbackFn, errorCallbackFn){
269 $.ajax("restServices/archivaServices/networkProxyService/getNetworkProxies", {
272 success: successCallbackFn,
273 error: errorCallbackFn
277 activateNetworkProxyFormValidation=function(){
278 var editForm=$("#main-content" ).find("#network-proxy-edit-form");
279 var validator = editForm.validate({
283 url: "restServices/archivaUiServices/dataValidatorService/networkProxyIdNotExists",
287 showErrors: function(validator, errorMap, errorList) {
288 customShowError(editForm,validator,errorMap,errorMap);
291 validator.settings.messages["id"]=$.i18n.prop("id.required.or.alreadyexists");
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");
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"));
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");
310 mainContent.find("#network-proxies-view-tabs-li-edit").addClass("active");
311 mainContent.find("#network-proxies-edit").addClass("active");
314 mapNetworkProxy=function(data){
318 return new NetworkProxy(data.id,data.protocol,data.host,data.port,data.username,data.password,data.useNtlm);
321 mapNetworkProxies=function(data){
322 var mappedNetworkProxies = $.map(data, function(item) {
323 return mapNetworkProxy(item);
325 return mappedNetworkProxies;