33const Mn = require ( 'backbone.marionette' ) ;
44const App = require ( '../../main' ) ;
55const ProxyHostModel = require ( '../../../models/proxy-host' ) ;
6+ const ProxyLocationModel = require ( '../../../models/proxy-host-location' ) ;
67const template = require ( './form.ejs' ) ;
78const certListItemTemplate = require ( '../certificates-list-item.ejs' ) ;
89const accessListItemTemplate = require ( './access-list-item.ejs' ) ;
10+ const CustomLocation = require ( './location' ) ;
911const Helpers = require ( '../../../lib/helpers' ) ;
1012
13+
1114require ( 'jquery-serializejson' ) ;
1215require ( 'selectize' ) ;
1316
1417module . exports = Mn . View . extend ( {
1518 template : template ,
1619 className : 'modal-dialog' ,
1720
21+ locationsCollection : new ProxyLocationModel . Collection ( ) ,
22+
1823 ui : {
1924 form : 'form' ,
2025 domain_names : 'input[name="domain_names"]' ,
2126 forward_host : 'input[name="forward_host"]' ,
2227 buttons : '.modal-footer button' ,
2328 cancel : 'button.cancel' ,
2429 save : 'button.save' ,
30+ add_location_btn : 'button.add_location' ,
31+ locations_container :'.locations_container' ,
2532 certificate_select : 'select[name="certificate_id"]' ,
2633 access_list_select : 'select[name="access_list_id"]' ,
2734 ssl_forced : 'input[name="ssl_forced"]' ,
@@ -32,6 +39,10 @@ module.exports = Mn.View.extend({
3239 letsencrypt : '.letsencrypt'
3340 } ,
3441
42+ regions : {
43+ locations_regions : '@ui.locations_container'
44+ } ,
45+
3546 events : {
3647 'change @ui.certificate_select' : function ( ) {
3748 let id = this . ui . certificate_select . val ( ) ;
@@ -82,6 +93,13 @@ module.exports = Mn.View.extend({
8293 }
8394 } ,
8495
96+ 'click @ui.add_location_btn' : function ( e ) {
97+ e . preventDefault ( ) ;
98+
99+ const model = new ProxyLocationModel . Model ( ) ;
100+ this . locationsCollection . add ( model ) ;
101+ } ,
102+
85103 'click @ui.save' : function ( e ) {
86104 e . preventDefault ( ) ;
87105
@@ -93,6 +111,16 @@ module.exports = Mn.View.extend({
93111 let view = this ;
94112 let data = this . ui . form . serializeJSON ( ) ;
95113
114+ // Add locations
115+ data . locations = [ ] ;
116+ this . locationsCollection . models . forEach ( ( location ) => {
117+ data . locations . push ( location . toJSON ( ) ) ;
118+ } ) ;
119+
120+ // Serialize collects path from custom locations
121+ // This field must be removed from root object
122+ delete data . path ;
123+
96124 // Manipulate
97125 data . forward_port = parseInt ( data . forward_port , 10 ) ;
98126 data . block_exploits = ! ! data . block_exploits ;
@@ -246,5 +274,20 @@ module.exports = Mn.View.extend({
246274 if ( typeof options . model === 'undefined' || ! options . model ) {
247275 this . model = new ProxyHostModel . Model ( ) ;
248276 }
277+
278+ this . locationsCollection = new ProxyLocationModel . Collection ( ) ;
279+
280+ // Custom locations
281+ this . showChildView ( 'locations_regions' , new CustomLocation . LocationCollectionView ( {
282+ collection : this . locationsCollection
283+ } ) ) ;
284+
285+ // Check wether there are any location defined
286+ if ( options . model && Array . isArray ( options . model . attributes . locations ) ) {
287+ options . model . attributes . locations . forEach ( ( location ) => {
288+ let m = new ProxyLocationModel . Model ( location ) ;
289+ this . locationsCollection . add ( m ) ;
290+ } ) ;
291+ }
249292 }
250293} ) ;
0 commit comments