1
+ angular . module ( "umbraco" ) . controller ( "Perplex.FormsSecurity.EditController" ,
2
+ function ( $scope , $controller , perplexFormResource , navigationService , treeService , formResource , $routeParams , notificationsService , $http , $compile ) {
3
+ $controller ( 'UmbracoForms.Editors.Security.EditController' , { $scope : $scope } ) ;
4
+
5
+ var defaultSave = $scope . save ;
6
+
7
+ $scope . loading = true ;
8
+ $scope . folders = [ ] ;
9
+ $scope . userId = $routeParams . id ;
10
+ $scope . startNodes = [ ] ;
11
+
12
+ // Fetch Umbraco's default edit view.
13
+ $http . get ( '/App_Plugins/UmbracoForms/backoffice/FormSecurity/edit.html' ) . success ( function ( template ) {
14
+ // Remove Umbraco's controller
15
+ template = template . replace ( / n g - c o n t r o l l e r \s * = \s * ( [ " ' ] ) U m b r a c o F o r m s .E d i t o r s .S e c u r i t y .E d i t C o n t r o l l e r \1/ , '' ) ;
16
+
17
+ // Add Form Start Nodes directive before the first umb-control-group
18
+ template = template . replace ( / ( .* ?) ( < u m b - c o n t r o l - g r o u p .* ?) / , function ( full , one , two ) {
19
+ return one + '<perplex-forms-start-nodes></perplex-forms-start-nodes>' + two
20
+ } ) ;
21
+
22
+ $compile ( $ ( "#perplex-forms-security-edit" ) . html ( template ) . contents ( ) ) ( $scope ) ;
23
+ } ) ;
24
+
25
+ $scope . save = function ( ) {
26
+ defaultSave ( ) ;
27
+ saveStartNodes ( ) ;
28
+ }
29
+
30
+ function saveStartNodes ( ) {
31
+ perplexFormResource . setFormStartNodes ( $scope . userId , $scope . startNodes ) . then ( function ( response ) {
32
+ notificationsService . showNotification ( { type : 0 , header : "Start Nodes in Forms saved" } ) ;
33
+ } ) ;
34
+ }
35
+
36
+ // Load the start nodes for the selected user
37
+ perplexFormResource . getFormStartNodes ( $scope . userId ) . then ( function ( response ) {
38
+ $scope . startNodes = _ . pluck ( response . data , 'id' ) ;
39
+
40
+ // Load folders
41
+ perplexFormResource . getRootFolder ( ) . then ( function ( response ) {
42
+ if ( response . data != null ) {
43
+ var rootFolder = response . data ;
44
+
45
+ $scope . folders . push ( rootFolder ) ;
46
+ initFolder ( rootFolder ) ;
47
+ }
48
+
49
+ updateDisabledFolders ( ) ;
50
+
51
+ $scope . loading = false ;
52
+ } , function ( error ) {
53
+ // TODO: Handle error
54
+ $scope . loading = false ;
55
+ } ) ;
56
+ } ) ;
57
+
58
+ // Initialize a folder and its subfolders
59
+ function initFolder ( folder ) {
60
+ // If this folder is set as a startnode,
61
+ // expand to this folder
62
+ var isStartFolder = _ . contains ( $scope . startNodes , folder . id ) ;
63
+ if ( isStartFolder ) {
64
+ folder . selected = true ;
65
+
66
+ // Expand all parent folders and the folder itself
67
+ _ . each ( folder . path , function ( folderId ) {
68
+ var ff = findFolder ( folderId ) ;
69
+ if ( ff != null ) {
70
+ ff . expanded = true ;
71
+ }
72
+ } ) ;
73
+ }
74
+
75
+ // Init subfolders
76
+ _ . each ( folder . folders , function ( subFolder ) {
77
+ initFolder ( subFolder ) ;
78
+ } )
79
+ }
80
+
81
+ // Recusively find a folder inside a list of folders
82
+ function findFolder ( id , parent ) {
83
+ // No parent given, search through $scope.folders
84
+ if ( parent == null ) {
85
+ var folder = _ . find ( $scope . folders , { id : id } ) ;
86
+ if ( folder != null ) {
87
+ return folder ;
88
+ }
89
+
90
+ // Search sub folders
91
+ for ( var i = 0 ; i < $scope . folders . length ; i ++ ) {
92
+ folder = findFolder ( id , $scope . folders [ i ] ) ;
93
+ if ( folder != null ) {
94
+ return folder ;
95
+ }
96
+ }
97
+ } else {
98
+ if ( parent . id == id ) {
99
+ return parent ;
100
+ }
101
+
102
+ // Search sub folders
103
+ for ( var i = 0 ; i < parent . folders . length ; i ++ ) {
104
+ folder = findFolder ( id , parent . folders [ i ] ) ;
105
+ if ( folder != null ) {
106
+ return folder ;
107
+ }
108
+ }
109
+ }
110
+ }
111
+
112
+ $scope . toggleFolder = function ( folder ) {
113
+ // Set expanded state
114
+ folder . expanded = ! folder . expanded ;
115
+ }
116
+
117
+ // Returns whether or not to show, does not actually show/hide
118
+ $scope . showFolder = function ( folder ) {
119
+ // Always show root
120
+ if ( folder . id === '-1' ) {
121
+ return true ;
122
+ }
123
+
124
+ var parent = findFolder ( folder . parentId ) ;
125
+ if ( parent == null ) {
126
+ return false ;
127
+ }
128
+
129
+ return parent . expanded && $scope . showFolder ( parent ) ;
130
+ }
131
+
132
+ $scope . selectFolder = function ( folder ) {
133
+ folder . selected = ! folder . selected ;
134
+
135
+ if ( folder . selected ) {
136
+ $scope . startNodes . push ( folder . id ) ;
137
+ } else {
138
+ var index = $scope . startNodes . indexOf ( folder . id ) ;
139
+ $scope . startNodes . splice ( index , 1 ) ;
140
+ }
141
+
142
+ updateDisabledFolders ( ) ;
143
+ }
144
+
145
+ // Sets the disabled state of all folders based on
146
+ // the currently selected startnodes
147
+ function updateDisabledFolders ( ) {
148
+ _ . each ( $scope . folders , updateDisabledFolder ) ;
149
+ }
150
+
151
+ function updateDisabledFolder ( folder ) {
152
+ // No startnodes? Then everything is enabled
153
+ if ( $scope . startNodes . length === 0 ) {
154
+ folder . disabled = false ;
155
+ } else {
156
+ // Startnodes are not in folder's path, folder is disabled
157
+ if ( ! _ . any ( $scope . startNodes , function ( sn ) {
158
+ return folder . path . indexOf ( sn ) > - 1 ;
159
+ } ) ) {
160
+ folder . disabled = true ;
161
+ } else {
162
+ // Otherwise => enabled
163
+ folder . disabled = false ;
164
+ }
165
+ }
166
+
167
+ // And traverse children
168
+ _ . each ( folder . folders , updateDisabledFolder ) ;
169
+ }
170
+
171
+ $scope . clear = function ( ) {
172
+ _ . each ( $scope . startNodes , function ( folderId ) {
173
+ var folder = findFolder ( folderId ) ;
174
+ if ( folder != null ) {
175
+ folder . selected = false ;
176
+ }
177
+ } ) ;
178
+
179
+ $scope . startNodes = [ ] ;
180
+
181
+ updateDisabledFolders ( ) ;
182
+ }
183
+ } ) ;
0 commit comments