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

DEM-1190 Features Controller #27

Merged
merged 9 commits into from
Sep 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions example/js-files.jade
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

- scripts.push('submit-work.module')
- scripts.push('templates')
- scripts.push('services/optimist')

- services.push('submit-work')

- directives.push('type')
- directives.push('features')
Expand Down
199 changes: 110 additions & 89 deletions src/scripts/controllers/features.controller.coffee
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
'use strict'

SubmitWorkFeaturesController = ($scope, SubmitWorkAPIService, API_URL) ->
vm = this
vm.workId = $scope.workId
vm.loading = true
vm.showFeaturesModal = false
vm.showUploadModal = false
vm.showDefineFeaturesForm = false
vm.activeFeature = null
SubmitWorkFeaturesController = ($scope, $rootScope, SubmitWorkService, SubmitWorkAPIService, API_URL) ->
vm = this
vm.workId = $scope.workId
vm.loading = true
vm.showFeaturesModal = false
vm.showUploadModal = false
vm.showDefineFeaturesForm = false
vm.activeFeature = null
vm.featuresUploaderUploading = null
vm.featuresUploaderHasErrors = null

vm.work =
name : null
requestType: null
summary : null
features : []
featuresDetails: null

#TODO: replace palceholder features & descriptions
vm.defaultFeatures = [
name: 'Login',
description: 'Users can login / register for your app',
notes: null,
custom: null
,
name: 'Onboarding',
description: 'Users can see data from social networks (FB, Twitter etc.) in your app',
notes: null,
custom: null
,
name: 'Registration',
description: 'Users can create profiles with personal info',
notes: null,
custom: null
,
name: 'Location',
description: 'A map with a user\'s GPS location that helps them get to places',
notes: null,
custom: null
vm.features = []

# TODO: replace palceholder features & descriptions
config = {}

config.defaultFeatures = [
id: '123',
name: 'Login',
description: 'Users can login / register for your app',
notes: null,
custom: null,
selected: false
,
id: '124',
name: 'Onboarding',
description: 'Users can see data from social networks (FB, Twitter etc.) in your app',
notes: null,
custom: null,
selected: false
,
id: '125',
name: 'Registration',
description: 'Users can create profiles with personal info',
notes: null,
custom: null,
selected: false
,
id: '126',
name: 'Location',
description: 'A map with a user\'s GPS location that helps them get to places',
notes: null,
custom: null,
selected: false
];

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

vm.hideCustomFeatures= ->
resetCustomFeature()
vm.showDefineFeaturesForm = false

vm.activateFeature = (feature) ->
vm.activeFeature = feature

vm.applyFeature = ->
featureAdded = false
features = vm.work.features
vm.features.forEach (feature) ->
if feature.name == vm.activeFeature.name
feature.selected = true

vm.activeFeature = null
onChange()

features.forEach (feature) ->
featureAdded = true if feature.name == vm.activeFeature.name
vm.removeFeature = ->
vm.features.forEach (feature, index) ->
if feature.name == vm.activeFeature.name
vm.features.splice(index, 1)

unless featureAdded
features.push vm.activeFeature
vm.activeFeature = null
vm.activeFeature = null
onChange()

vm.addCustomFeature = ->
customFeatureValid = vm.customFeature.name && vm.customFeature.description

if customFeatureValid
vm.work.features.push vm.customFeature
resetCustomFeature()
vm.customFeature.selected = true
vm.features.push vm.customFeature
vm.hideCustomFeatures()
onChange()

vm.save = (onSuccess) ->
if vm.workId
params =
id: vm.workId
vm.save = ->
uploaderValid = !vm.featuresUploaderUploading && !vm.featuresUploaderHasErrors

resource = SubmitWorkAPIService.put params, vm.work
resource.$promise.then (response) ->
onSuccess? response
resource.$promise.catch (response) ->
# TODO: add error handling
updates = getUpdates()

vm.submitFeatures = ->
workFeatures = vm.work.features
formsValid = workFeatures.length
uploaderValid = !vm.featuresUploaderUploading && !vm.featuresUploaderHasErrors
hasFeatures = updates.selectedFeatures.length || updates.customFeatures.length

if formsValid && uploaderValid
# TODO: Replace with proper back-end status
vm.work.status = 'FeaturesAdded'
vm.save (response) ->
# TODO: navigate to "proceed to visuals" view
if uploaderValid && hasFeatures
SubmitWorkService.save(updates)

resetCustomFeature = ->
vm.customFeature =
name: null
description: null
custom: true
getUpdates = ->
updates =
selectedFeatures: []
customFeatures: []
vm.features.forEach (feature) ->
if feature.id
if feature.selected
updates.selectedFeatures.push
id: feature.id
else
if feature.selected
updates.customFeatures.push feature
updates

configureUploader = ->
assetType = 'specs'
Expand All @@ -117,34 +122,50 @@ SubmitWorkFeaturesController = ($scope, SubmitWorkAPIService, API_URL) ->
workId: vm.workId
assetType: assetType

activate = ->
# initialize custom feature modal inputs
resetCustomFeature()
configureUploader()
onChange = ->
if SubmitWorkService.work.o.hasPending
return false

vm.loading = false

if vm.workId
params =
id : vm.workId
vm.customFeature =
name: null
description: null
custom: true

resource = SubmitWorkAPIService.get params
unless vm.features.length
config.defaultFeatures.forEach (feature) ->
vm.features.push feature
# add any custom features to vm
SubmitWorkService.work.features.forEach (feature) ->
if !feature.id
feature.selected = true
vm.features.push feature
# set already selected features to selected on vm
SubmitWorkService.work.features.forEach (feature) ->
vm.features.forEach (vmFeature) ->
if feature.id == vmFeature.id
vmFeature.selected = true

updates = getUpdates()
vm.selectedFeaturesCount = updates.selectedFeatures.length + updates.customFeatures.length

vm.work = SubmitWorkService.work

resource.$promise.then (response) ->
vm.work = response
# TODO: remove once details are added to payload
vm.work.featuresDetails = null
activate = ->
destroyWorkListener = $rootScope.$on "SubmitWorkService.work:changed", ->
onChange()

resource.$promise.catch (response) ->
# TODO: add error handling
$scope.$on '$destroy', ->
destroyWorkListener()

resource.$promise.finally ->
vm.loading = false
else
vm.loading = false
SubmitWorkService.fetch(vm.workId)
configureUploader()

vm

activate()

SubmitWorkFeaturesController.$inject = ['$scope', 'SubmitWorkAPIService', 'API_URL']
SubmitWorkFeaturesController.$inject = ['$scope', '$rootScope', 'SubmitWorkService', 'SubmitWorkAPIService', 'API_URL']

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