Skip to content

Commit

Permalink
refactor(module): moved configuration code from shared.module initial…
Browse files Browse the repository at this point in the history
…ization to app initialization
  • Loading branch information
beregovoy68 committed Oct 26, 2016
1 parent 6a8d9d2 commit d96136c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 78 deletions.
104 changes: 82 additions & 22 deletions src/js/app.js
Expand Up @@ -4,32 +4,92 @@
* @see controllers
* @see services
*/
var app = angular.module('app',
[
'restangular',
'waves.core',
'waves.core.services',

'ngclipboard',
'ngMessages',
'ngValidate',
'ngAnimate',
'ngMaterial',
'app.ui',
'app.shared',
'app.login',
'app.navigation',
'app.wallet',
'app.history',
'app.community'
]
);
app.run(['Restangular', 'constants.core', function (rest, coreConstants) {

// mock methods to implement late binding
var __mockShowError = function(message) {};
var __mockValidateAddress = function(address) {};

var app = angular.module('app', [
'restangular',
'waves.core',
'waves.core.services',

'ngclipboard',
'ngMessages',
'ngAnimate',
'ngMaterial',
'ngValidate',
'app.ui',
'app.shared',
'app.login',
'app.navigation',
'app.wallet',
'app.history',
'app.community'
]).config(AngularApplicationConfig).run(AngularApplicationRun);

function AngularApplicationConfig($validatorProvider) {
$validatorProvider.setDefaults({
errorClass: 'wInput-error',
onkeyup: false,
showErrors : function(errorMap, errorList) {
errorList.forEach(function(error) {
// can't use notificationService here cos services are not available in config phase
__mockShowError(error.message);
});

var i, elements;
for (i = 0, elements = this.validElements(); elements[i]; i++) {
angular.element(elements[i]).removeClass(this.settings.errorClass);
}

for (i = 0, elements = this.invalidElements(); elements[i]; i++) {
angular.element(elements[i]).addClass(this.settings.errorClass);
}
}
});
$validatorProvider.addMethod('address', function(value, element) {
return this.optional(element) || __mockValidateAddress(value);
}, 'Account number must be a sequence of 35 alphanumeric characters with no spaces, ' +
'optionally starting with \'1W\'');
$validatorProvider.addMethod('decimal', function(value, element, params) {
var maxdigits = angular.isNumber(params) ? params : Currency.WAV.precision;

var regex = new RegExp('^(?:-?\\d+)?(?:\\.\\d{1,' + maxdigits + '})?$');
return this.optional(element) || regex.test(value);
}, 'Amount is expected with a dot (.) as a decimal separator with no more than {0} fraction digits');
$validatorProvider.addMethod('password', function(value, element) {
if (this.optional(element))
return true;

var containsDigits = /[0-9]/.test(value);
var containsUppercase = /[A-Z]/.test(value);
var containsLowercase = /[a-z]/.test(value);

return containsDigits && containsUppercase && containsLowercase;
}, 'The password is too weak. A good password must contain at least one digit, ' +
'one uppercase and one lowercase letter');
}

AngularApplicationConfig.$inject = ['$validatorProvider'];

function AngularApplicationRun(rest, coreConstants, notificationService, addressService) {
// restangular configuration
rest.setDefaultHttpFields({
timeout: 10000 // milliseconds
});
//var url = coreConstants.NODE_ADDRESS;
var url = 'http://52.28.66.217:6869';
rest.setBaseUrl(url);
}]);

// override mock methods cos in config phase services are not available yet
__mockShowError = function (message) {
notificationService.error(message);
};
__mockValidateAddress = function (address) {
return addressService.validateDisplayAddress(address);
};
}

AngularApplicationRun.$inject = ['Restangular', 'constants.core', 'notificationService', 'addressService'];

57 changes: 1 addition & 56 deletions src/js/shared/shared.module.js
@@ -1,61 +1,6 @@
(function() {
'use strict';

// mock methods to implement late binding
var showError = function(message) {};
var validateAddress = function(address) {};

angular
.module('app.shared', [])
.config(['$validatorProvider', function ($validatorProvider) {
$validatorProvider.setDefaults({
errorClass: 'wInput-error',
onkeyup: false,
showErrors : function(errorMap, errorList) {
errorList.forEach(function(error) {
// can't use notificationService here cos services are not available in config phase
showError(error.message);
});

var i, elements;
for (i = 0, elements = this.validElements(); elements[i]; i++) {
angular.element(elements[i]).removeClass(this.settings.errorClass);
}

for (i = 0, elements = this.invalidElements(); elements[i]; i++) {
angular.element(elements[i]).addClass(this.settings.errorClass);
}
}
});
$validatorProvider.addMethod('address', function(value, element) {
return this.optional(element) || validateAddress(value);
}, 'Account number must be a sequence of 35 alphanumeric characters with no spaces, ' +
'optionally starting with \'1W\'');
$validatorProvider.addMethod('decimal', function(value, element, params) {
var maxdigits = angular.isNumber(params) ? params : Currency.WAV.precision;

var regex = new RegExp('^(?:-?\\d+)?(?:\\.\\d{1,' + maxdigits + '})?$');
return this.optional(element) || regex.test(value);
}, 'Amount is expected with a dot (.) as a decimal separator with no more than {0} fraction digits');
$validatorProvider.addMethod('password', function(value, element) {
if (this.optional(element))
return true;

var containsDigits = /[0-9]/.test(value);
var containsUppercase = /[A-Z]/.test(value);
var containsLowercase = /[a-z]/.test(value);

return containsDigits && containsUppercase && containsLowercase;
}, 'The password is too weak. A good password must contain at least one digit, ' +
'one uppercase and one lowercase letter');
}])
.run(['notificationService', 'addressService', function (notificationService, addressService) {
// override mock methods cos in config phase services are not available yet
showError = function (message) {
notificationService.error(message);
};
validateAddress = function (address) {
return addressService.validateDisplayAddress(address);
};
}]);
.module('app.shared', []);
})();

0 comments on commit d96136c

Please sign in to comment.