Skip to content

Instantly share code, notes, and snippets.

@KutsenkoA
Created August 11, 2015 13:08
Show Gist options
  • Save KutsenkoA/7833c8009fe1c7435255 to your computer and use it in GitHub Desktop.
Save KutsenkoA/7833c8009fe1c7435255 to your computer and use it in GitHub Desktop.
Client profile controller
'use strict';
/**
* @ngdoc controller
* @name sdtp_project.controller:profileController
* @description
* Controller of client profile. Has methods for edit/save/update profile
* and methods to work with additional sections
*/
angular.module('sdtp_project')
.controller('profileController',
[
'$scope',
'$state',
'$anchorScroll',
'$timeout',
'clientsService',
'apiService',
'profileSections',
'popupService',
function(
$scope,
$state,
$anchorScroll,
$timeout,
clientsService,
apiService,
profileSections,
popupService) {
/* Get data from services section
*/
$scope.sections = profileSections;
$scope.forms = {
getCurrent: function() {
return this[profileSections.getCurrentFormName()];
}
};
$scope.assets = clientsService.assets;
$scope.sectors = clientsService.sectors;
$scope.industries = [];
$scope.maritalStatuses = clientsService.maritalStatuses;
$scope.healthIs = clientsService.healthIs;
$scope.stabilities = clientsService.stabilities;
/* Flags
*/
$scope.isAddAssetMenuVisible = false;
$scope.isCompanyNotKnowed = true;
/* Methods
*/
/**
* Return array of industries based on current sector. If sector not defined will return all industries
*
* @param sector Selected sector
* @returns {Array} Industries by sector
*/
$scope.industriesBySector = function(sector) {
var
industries = [];
$scope.sectors.forEach(function(currentSector) {
if (!sector || currentSector.name === sector) {
[].push.apply(industries, currentSector.industries);
}
});
return industries;
};
/**
* @ngdoc method
* @name nextSection
* @methodOf sdtp_project.controller:profileController
* @description
* Go to next sections
*/
$scope.nextSection = function() {
if (!profileSections.next()) {
$state.go('index.client.metrics');
}
};
/**
* @ngdoc method
* @name editProfile
* @methodOf sdtp_project.controller:profileController
* @description
* Turn certain profile section to edit mode
*
* @param {object} event - click event
* @param {string} section - Section name
*/
$scope.editProfile = function(event) {
var
targetInput;
if (!profileSections.getEditMode()) {
if (event) {
// It looks like workaround (crutch) but I didn't invent best solution
if (event.target.nodeName === 'INPUT') {
targetInput = event.target;
} else if (event.target.nodeName === 'SPAN' || event.target.nodeName === 'CUSTOM-SELECT') {
targetInput = false;
} else if (event.target.getElementsByTagName('INPUT').length) {
targetInput = event.target.getElementsByTagName('INPUT')[0];
} else if (event.target.parentNode.getElementsByTagName('INPUT').length) {
targetInput = event.target.parentNode.getElementsByTagName('INPUT')[0];
} else {
targetInput = $(event.target).closest('.form-fieldset').find('input')[0];
}
}
clientsService.setCurrentClientSaved(false);
if (targetInput) {
$timeout(function () {
targetInput.focus();
}, 300);
}
}
$scope.forms.getCurrent().$setPristine();
};
/**
* @ngdoc method
* @name nextSection
* @methodOf sdtp_project.controller:profileController
* @description
* Removes auto-name 'New Client'
*/
$scope.editClientName = function() {
if ($scope.client.id === 0
&& $scope.client.profile.name === 'New Profile') {
$scope.client.profile.name = '';
}
};
/**
* @ngdoc method
* @name cancelEditProfile
* @methodOf sdtp_project.controller:profileController
* @description
* Cancel editing profile and return fields to previous values.
*
* @param {string} section - Section name
*/
$scope.cancelEditProfile = function(section) {
profileSections.setEditMode(false);
clientsService.cloneSection(section,
$scope.client.id ? $scope.savedClient : clientsService.getDummyClient(),
$scope.client.profile);
clientsService.setCurrentClientSaved(true);
$scope.forms.getCurrent().$setPristine();
};
/**
* @ngdoc method
* @name addAssetOrLiability
* @methodOf sdtp_project.controller:profileController
* @description
* Add asset or liability to client profile
*
* @param {object} asset - new asset
*/
$scope.addAssetOrLiability = function(asset) {
var
newAsset = _.clone(asset);
newAsset.order = 100000 * (1 + $scope.client.profile.assets.length);
newAsset.orderClass = 'first-block';
$scope.client.profile.assets.push(newAsset);
clientsService.clientsList.updateAssets($scope.client.profile);
$scope.forms.getCurrent().$setPristine();
$scope.sections.setEditMode(true);
return true;
};
/**
* @ngdoc method
* @name removeAssetOrLiability
* @methodOf sdtp_project.controller:profileController
* @description
* Remove asset or liability from client profile
*
* @param {number} order - unique order of removed asset
*/
$scope.removeAssetOrLiability = function(order) {
$scope.client.profile.assets = $scope.client.profile.assets.filter(function(currentAsset) {
return currentAsset.order !== order;
});
clientsService.clientsList.updateAssets($scope.client.profile);
$scope.forms.getCurrent().$setPristine();
};
/**
* @ngdoc method
* @name addOtherIncome
* @methodOf sdtp_project.controller:profileController
* @description
* Add other income to client profile
*/
$scope.addOtherIncome = function() {
$scope.client.profile.OtherIncomes.push({});
$scope.sections.setEditMode(true);
};
/**
* @ngdoc method
* @name removeOtherIncome
* @methodOf sdtp_project.controller:profileController
* @description
* Remove other income from client profile
*
* @param {number} otherIncomeIndex - index of removed income
*/
$scope.removeOtherIncome = function(otherIncomeIndex) {
if ($scope.client.profile.OtherIncomes.length === 1) {
popupService.open('popupConfirmRemoveLastOtherIncome', {
okButtonHandler: function() {
$scope.client.profile.OtherIncomes = [];
$scope.sendProfile();
$scope.forms.getCurrent().$setPristine();
}
});
} else if ($scope.client.profile.OtherIncomes.length) {
$scope.client.profile.OtherIncomes = $scope.client.profile.OtherIncomes.filter(function(_, index) {
return index !== otherIncomeIndex;
});
}
};
/**
* @ngdoc method
* @name sendProfile
* @methodOf sdtp_project.controller:profileController
* @description
* Send client profile to server
*/
$scope.sendProfile = function() {
var
saveClientProfileSection = {};
if ($scope.client.id) {
/* Show spinner to pretend that we're doing a lot stuff
*/
$scope.client.profile.TWI.status = 'In Progress';
clientsService.cloneSection(profileSections.current(), $scope.client.profile, saveClientProfileSection);
clientsService
.updateClient($scope.client.id, saveClientProfileSection)
.success(function(result) {
$scope.savedClient = _.cloneDeep(result);
profileSections.setEditMode(false);
clientsService.setCurrentClientSaved(true);
})
.error(alert);
} else {
$scope.loadContainer = true;
clientsService.cloneAllSections($scope.client.profile, saveClientProfileSection);
clientsService
.createClient(saveClientProfileSection)
.success(function(result) {
$scope.savedClient = _.cloneDeep(result);
// fixme temporary hack
$timeout(function() {
$state.go('index.client.metrics', {clientId: result.id});
}, 2100);
})
.error(alert);
}
};
/**
* @ngdoc method
* @name isRadioButtonShowed
* @methodOf sdtp_project.controller:profileController
* @description
* Check if radio button should be displayed
*
* @param {boolean} editingState Edit/view state of current section
* @param {any} model Radio buttons group model
* @param {any} value Value of current radio
*/
$scope.isRadioButtonShowed = function(editingState, model, value) {
return editingState || (model && model === value);
};
/**
* @ngdoc method
* @name isValid
* @methodOf sdtp_project.controller:profileController
* @description
* Testy is current client portfolio valid
*/
$scope.isValid = function() {
var
currentForm = $scope.forms[$scope.sections.getCurrentFormName()];
return currentForm.$valid || currentForm.$setSubmitted();
};
/**
* @ngdoc method
* @name addAnAssetMenuSwitcher
* @methodOf sdtp_project.controller:profileController
* @description
* Show or hide new assets menu
*/
$scope.addAnAssetMenuSwitcher = function() {
$scope.isAddAssetMenuVisible = !$scope.isAddAssetMenuVisible;
}
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment