1
1
' use strict'
2
2
3
- SubmitWorkFeaturesController = ($scope , SubmitWorkAPIService , API_URL ) ->
4
- vm = this
5
- vm .workId = $scope .workId
6
- vm .loading = true
7
- vm .showFeaturesModal = false
8
- vm .showUploadModal = false
9
- vm .showDefineFeaturesForm = false
10
- vm .activeFeature = null
3
+ SubmitWorkFeaturesController = ($scope , $rootScope , SubmitWorkService , SubmitWorkAPIService , API_URL ) ->
4
+ vm = this
5
+ vm .workId = $scope .workId
6
+ vm .loading = true
7
+ vm .showFeaturesModal = false
8
+ vm .showUploadModal = false
9
+ vm .showDefineFeaturesForm = false
10
+ vm .activeFeature = null
11
11
vm .featuresUploaderUploading = null
12
12
vm .featuresUploaderHasErrors = null
13
-
14
- vm .work =
15
- name : null
16
- requestType : null
17
- summary : null
18
- features : []
19
- featuresDetails : null
20
-
21
- # TODO: replace palceholder features & descriptions
22
- vm .defaultFeatures = [
23
- name : ' Login' ,
24
- description : ' Users can login / register for your app' ,
25
- notes : null ,
26
- custom : null
27
- ,
28
- name : ' Onboarding' ,
29
- description : ' Users can see data from social networks (FB, Twitter etc.) in your app' ,
30
- notes : null ,
31
- custom : null
32
- ,
33
- name : ' Registration' ,
34
- description : ' Users can create profiles with personal info' ,
35
- notes : null ,
36
- custom : null
37
- ,
38
- name : ' Location' ,
39
- description : ' A map with a user\' s GPS location that helps them get to places' ,
40
- notes : null ,
41
- custom : null
13
+ vm .features = []
14
+
15
+ # TODO: replace palceholder features & descriptions
16
+ config = {}
17
+
18
+ config .defaultFeatures = [
19
+ id : ' 123' ,
20
+ name : ' Login' ,
21
+ description : ' Users can login / register for your app' ,
22
+ notes : null ,
23
+ custom : null ,
24
+ selected : false
25
+ ,
26
+ id : ' 124' ,
27
+ name : ' Onboarding' ,
28
+ description : ' Users can see data from social networks (FB, Twitter etc.) in your app' ,
29
+ notes : null ,
30
+ custom : null ,
31
+ selected : false
32
+ ,
33
+ id : ' 125' ,
34
+ name : ' Registration' ,
35
+ description : ' Users can create profiles with personal info' ,
36
+ notes : null ,
37
+ custom : null ,
38
+ selected : false
39
+ ,
40
+ id : ' 126' ,
41
+ name : ' Location' ,
42
+ description : ' A map with a user\' s GPS location that helps them get to places' ,
43
+ notes : null ,
44
+ custom : null ,
45
+ selected : false
42
46
];
43
47
44
48
vm .showFeatures = ->
@@ -51,58 +55,59 @@ SubmitWorkFeaturesController = ($scope, SubmitWorkAPIService, API_URL) ->
51
55
vm .showDefineFeaturesForm = ! vm .showDefineFeaturesForm
52
56
53
57
vm .hideCustomFeatures = ->
54
- resetCustomFeature ()
55
58
vm .showDefineFeaturesForm = false
56
59
57
60
vm .activateFeature = (feature ) ->
58
61
vm .activeFeature = feature
59
62
60
63
vm .applyFeature = ->
61
- featureAdded = false
62
- features = vm .work .features
64
+ vm .features .forEach (feature) ->
65
+ if feature .name == vm .activeFeature .name
66
+ feature .selected = true
67
+
68
+ vm .activeFeature = null
69
+ onChange ()
63
70
64
- features .forEach (feature) ->
65
- featureAdded = true if feature .name == vm .activeFeature .name
71
+ vm .removeFeature = ->
72
+ vm .features .forEach (feature, index) ->
73
+ if feature .name == vm .activeFeature .name
74
+ vm .features .splice (index, 1 )
66
75
67
- unless featureAdded
68
- features .push vm .activeFeature
69
- vm .activeFeature = null
76
+ vm .activeFeature = null
77
+ onChange ()
70
78
71
79
vm .addCustomFeature = ->
72
80
customFeatureValid = vm .customFeature .name && vm .customFeature .description
73
81
74
82
if customFeatureValid
75
- vm .work . features . push vm . customFeature
76
- resetCustomFeature ()
83
+ vm .customFeature . selected = true
84
+ vm . features . push vm . customFeature
77
85
vm .hideCustomFeatures ()
86
+ onChange ()
78
87
79
- vm .save = (onSuccess ) ->
80
- if vm .workId
81
- params =
82
- id : vm .workId
88
+ vm .save = ->
89
+ uploaderValid = ! vm .featuresUploaderUploading && ! vm .featuresUploaderHasErrors
83
90
84
- resource = SubmitWorkAPIService .put params, vm .work
85
- resource .$promise .then (response) ->
86
- onSuccess? response
87
- resource .$promise .catch (response) ->
88
- # TODO: add error handling
91
+ updates = getUpdates ()
89
92
90
- vm .submitFeatures = ->
91
- workFeatures = vm .work .features
92
- formsValid = workFeatures .length
93
- uploaderValid = ! vm .featuresUploaderUploading && ! vm .featuresUploaderHasErrors
93
+ hasFeatures = updates .selectedFeatures .length || updates .customFeatures .length
94
94
95
- if formsValid && uploaderValid
96
- # TODO: Replace with proper back-end status
97
- vm .work .status = ' FeaturesAdded'
98
- vm .save (response) ->
99
- # TODO: navigate to "proceed to visuals" view
95
+ if uploaderValid && hasFeatures
96
+ SubmitWorkService .save (updates)
100
97
101
- resetCustomFeature = ->
102
- vm .customFeature =
103
- name : null
104
- description : null
105
- custom : true
98
+ getUpdates = ->
99
+ updates =
100
+ selectedFeatures : []
101
+ customFeatures : []
102
+ vm .features .forEach (feature) ->
103
+ if feature .id
104
+ if feature .selected
105
+ updates .selectedFeatures .push
106
+ id : feature .id
107
+ else
108
+ if feature .selected
109
+ updates .customFeatures .push feature
110
+ updates
106
111
107
112
configureUploader = ->
108
113
assetType = ' specs'
@@ -117,34 +122,50 @@ SubmitWorkFeaturesController = ($scope, SubmitWorkAPIService, API_URL) ->
117
122
workId : vm .workId
118
123
assetType : assetType
119
124
120
- activate = ->
121
- # initialize custom feature modal inputs
122
- resetCustomFeature ()
123
- configureUploader ()
125
+ onChange = ->
126
+ if SubmitWorkService .work .o .hasPending
127
+ return false
128
+
129
+ vm .loading = false
124
130
125
- if vm .workId
126
- params =
127
- id : vm .workId
131
+ vm .customFeature =
132
+ name : null
133
+ description : null
134
+ custom : true
128
135
129
- resource = SubmitWorkAPIService .get params
136
+ unless vm .features .length
137
+ config .defaultFeatures .forEach (feature) ->
138
+ vm .features .push feature
139
+ # add any custom features to vm
140
+ SubmitWorkService .work .features .forEach (feature) ->
141
+ if ! feature .id
142
+ feature .selected = true
143
+ vm .features .push feature
144
+ # set already selected features to selected on vm
145
+ SubmitWorkService .work .features .forEach (feature) ->
146
+ vm .features .forEach (vmFeature) ->
147
+ if feature .id == vmFeature .id
148
+ vmFeature .selected = true
149
+
150
+ updates = getUpdates ()
151
+ vm .selectedFeaturesCount = updates .selectedFeatures .length + updates .customFeatures .length
152
+
153
+ vm .work = SubmitWorkService .work
130
154
131
- resource .$promise .then (response) ->
132
- vm .work = response
133
- # TODO: remove once details are added to payload
134
- vm .work .featuresDetails = null
155
+ activate = ->
156
+ destroyWorkListener = $rootScope .$on " SubmitWorkService.work:changed" , ->
157
+ onChange ()
135
158
136
- resource . $promise . catch (response) ->
137
- # TODO: add error handling
159
+ $scope . $on ' $destroy ' , ->
160
+ destroyWorkListener ()
138
161
139
- resource .$promise .finally ->
140
- vm .loading = false
141
- else
142
- vm .loading = false
162
+ SubmitWorkService .fetch (vm .workId )
163
+ configureUploader ()
143
164
144
165
vm
145
166
146
167
activate ()
147
168
148
- SubmitWorkFeaturesController .$inject = [' $scope' , ' SubmitWorkAPIService' , ' API_URL' ]
169
+ SubmitWorkFeaturesController .$inject = [' $scope' , ' $rootScope ' , ' SubmitWorkService ' , ' SubmitWorkAPIService' , ' API_URL' ]
149
170
150
171
angular .module (' appirio-tech-ng-submit-work' ).controller ' SubmitWorkFeaturesController' , SubmitWorkFeaturesController
0 commit comments