3
3
const Mn = require ( 'backbone.marionette' ) ;
4
4
const App = require ( '../../main' ) ;
5
5
const ProxyHostModel = require ( '../../../models/proxy-host' ) ;
6
+ const ProxyLocationModel = require ( '../../../models/proxy-host-location' ) ;
6
7
const template = require ( './form.ejs' ) ;
7
8
const certListItemTemplate = require ( '../certificates-list-item.ejs' ) ;
8
9
const accessListItemTemplate = require ( './access-list-item.ejs' ) ;
10
+ const CustomLocation = require ( './location' ) ;
9
11
const Helpers = require ( '../../../lib/helpers' ) ;
10
12
13
+
11
14
require ( 'jquery-serializejson' ) ;
12
15
require ( 'selectize' ) ;
13
16
14
17
module . exports = Mn . View . extend ( {
15
18
template : template ,
16
19
className : 'modal-dialog' ,
17
20
21
+ locationsCollection : new ProxyLocationModel . Collection ( ) ,
22
+
18
23
ui : {
19
24
form : 'form' ,
20
25
domain_names : 'input[name="domain_names"]' ,
21
26
forward_host : 'input[name="forward_host"]' ,
22
27
buttons : '.modal-footer button' ,
23
28
cancel : 'button.cancel' ,
24
29
save : 'button.save' ,
30
+ add_location_btn : 'button.add_location' ,
31
+ locations_container :'.locations_container' ,
25
32
certificate_select : 'select[name="certificate_id"]' ,
26
33
access_list_select : 'select[name="access_list_id"]' ,
27
34
ssl_forced : 'input[name="ssl_forced"]' ,
@@ -32,6 +39,10 @@ module.exports = Mn.View.extend({
32
39
letsencrypt : '.letsencrypt'
33
40
} ,
34
41
42
+ regions : {
43
+ locations_regions : '@ui.locations_container'
44
+ } ,
45
+
35
46
events : {
36
47
'change @ui.certificate_select' : function ( ) {
37
48
let id = this . ui . certificate_select . val ( ) ;
@@ -82,6 +93,13 @@ module.exports = Mn.View.extend({
82
93
}
83
94
} ,
84
95
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
+
85
103
'click @ui.save' : function ( e ) {
86
104
e . preventDefault ( ) ;
87
105
@@ -93,6 +111,16 @@ module.exports = Mn.View.extend({
93
111
let view = this ;
94
112
let data = this . ui . form . serializeJSON ( ) ;
95
113
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
+
96
124
// Manipulate
97
125
data . forward_port = parseInt ( data . forward_port , 10 ) ;
98
126
data . block_exploits = ! ! data . block_exploits ;
@@ -246,5 +274,20 @@ module.exports = Mn.View.extend({
246
274
if ( typeof options . model === 'undefined' || ! options . model ) {
247
275
this . model = new ProxyHostModel . Model ( ) ;
248
276
}
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
+ }
249
292
}
250
293
} ) ;
0 commit comments