Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 9a3b5c4

Browse files
committed
Merge pull request #27 from appirio-tech/DEM-1190
DEM-1190 Features Controller
2 parents 4a63589 + a498929 commit 9a3b5c4

File tree

6 files changed

+396
-121
lines changed

6 files changed

+396
-121
lines changed

example/js-files.jade

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
- scripts.push('submit-work.module')
99
- scripts.push('templates')
10+
- scripts.push('services/optimist')
11+
12+
- services.push('submit-work')
1013

1114
- directives.push('type')
1215
- directives.push('features')
Lines changed: 110 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
'use strict'
22

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
1111
vm.featuresUploaderUploading = null
1212
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
4246
];
4347

4448
vm.showFeatures = ->
@@ -51,58 +55,59 @@ SubmitWorkFeaturesController = ($scope, SubmitWorkAPIService, API_URL) ->
5155
vm.showDefineFeaturesForm = !vm.showDefineFeaturesForm
5256

5357
vm.hideCustomFeatures= ->
54-
resetCustomFeature()
5558
vm.showDefineFeaturesForm = false
5659

5760
vm.activateFeature = (feature) ->
5861
vm.activeFeature = feature
5962

6063
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()
6370

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)
6675

67-
unless featureAdded
68-
features.push vm.activeFeature
69-
vm.activeFeature = null
76+
vm.activeFeature = null
77+
onChange()
7078

7179
vm.addCustomFeature = ->
7280
customFeatureValid = vm.customFeature.name && vm.customFeature.description
7381

7482
if customFeatureValid
75-
vm.work.features.push vm.customFeature
76-
resetCustomFeature()
83+
vm.customFeature.selected = true
84+
vm.features.push vm.customFeature
7785
vm.hideCustomFeatures()
86+
onChange()
7887

79-
vm.save = (onSuccess) ->
80-
if vm.workId
81-
params =
82-
id: vm.workId
88+
vm.save = ->
89+
uploaderValid = !vm.featuresUploaderUploading && !vm.featuresUploaderHasErrors
8390

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()
8992

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
9494

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)
10097

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
106111

107112
configureUploader = ->
108113
assetType = 'specs'
@@ -117,34 +122,50 @@ SubmitWorkFeaturesController = ($scope, SubmitWorkAPIService, API_URL) ->
117122
workId: vm.workId
118123
assetType: assetType
119124

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
124130

125-
if vm.workId
126-
params =
127-
id : vm.workId
131+
vm.customFeature =
132+
name: null
133+
description: null
134+
custom: true
128135

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
130154

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()
135158

136-
resource.$promise.catch (response) ->
137-
# TODO: add error handling
159+
$scope.$on '$destroy', ->
160+
destroyWorkListener()
138161

139-
resource.$promise.finally ->
140-
vm.loading = false
141-
else
142-
vm.loading = false
162+
SubmitWorkService.fetch(vm.workId)
163+
configureUploader()
143164

144165
vm
145166

146167
activate()
147168

148-
SubmitWorkFeaturesController.$inject = ['$scope', 'SubmitWorkAPIService', 'API_URL']
169+
SubmitWorkFeaturesController.$inject = ['$scope', '$rootScope', 'SubmitWorkService', 'SubmitWorkAPIService', 'API_URL']
149170

150171
angular.module('appirio-tech-ng-submit-work').controller 'SubmitWorkFeaturesController', SubmitWorkFeaturesController

0 commit comments

Comments
 (0)