Wallet/src/js/controllers/pincode.js

84 lines
2 KiB
JavaScript
Raw Normal View History

2017-02-23 16:56:46 -05:00
'use strict';
2017-03-02 11:25:38 -03:00
angular.module('copayApp.controllers').controller('pincodeController', function($rootScope, $timeout, $scope, $log, $window, configService) {
2017-03-02 16:32:08 -03:00
$scope.currentPincode = $scope.newPincode = '';
2017-02-23 16:56:46 -05:00
angular.element($window).on('keydown', function(e) {
2017-03-02 11:25:38 -03:00
if (e.which === 8) {
2017-02-23 16:56:46 -05:00
e.preventDefault();
$scope.delete();
}
2017-03-01 17:29:39 -03:00
if (e && e.key.match(/^[0-9]$/))
2017-02-23 16:56:46 -05:00
$scope.add(e.key);
2017-03-01 17:29:39 -03:00
else if (e && e.keyCode == 27)
2017-03-02 16:32:08 -03:00
$scope.close();
2017-03-01 17:29:39 -03:00
else if (e && e.keyCode == 13)
$scope.save();
2017-02-23 16:56:46 -05:00
});
$scope.add = function(value) {
2017-03-01 17:29:39 -03:00
updatePassCode(value);
2017-02-23 16:56:46 -05:00
};
$scope.delete = function() {
2017-03-02 16:32:08 -03:00
if ($scope.currentPincode.length > 0) {
$scope.currentPincode = $scope.currentPincode.substring(0, $scope.currentPincode.length - 1);
2017-02-23 16:56:46 -05:00
updatePassCode();
}
};
function isComplete() {
2017-03-02 16:32:08 -03:00
if ($scope.currentPincode.length < 4) return false;
2017-02-23 16:56:46 -05:00
else return true;
};
function updatePassCode(value) {
2017-03-02 16:32:08 -03:00
if (value && $scope.currentPincode.length < 4)
$scope.currentPincode = $scope.currentPincode + value;
2017-02-23 16:56:46 -05:00
$timeout(function() {
$scope.$apply();
});
};
2017-02-24 14:46:54 -05:00
2017-03-01 17:29:39 -03:00
$scope.save = function() {
2017-03-02 16:32:08 -03:00
if (!isComplete()) return;
var config = configService.getSync();
var match = config.pincode.value == $scope.currentPincode ? true : false;
if (!$scope.locking && !match) return;
checkCodes();
};
2017-03-01 17:29:39 -03:00
2017-03-02 16:32:08 -03:00
function checkCodes() {
if (!$scope.newPincode) {
$scope.newPincode = $scope.currentPincode;
$scope.currentPincode = '';
2017-03-01 17:29:39 -03:00
} else {
2017-03-02 16:32:08 -03:00
if ($scope.newPincode == $scope.currentPincode)
saveSettings($scope.locking, $scope.newPincode);
2017-03-01 17:29:39 -03:00
}
2017-03-02 16:32:08 -03:00
$timeout(function() {
$scope.$apply();
});
2017-02-24 14:46:54 -05:00
};
2017-02-28 10:32:10 -03:00
2017-03-02 16:32:08 -03:00
function saveSettings(enabled, value) {
var opts = {
pincode: {
enabled: enabled,
value: value
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
$scope.close();
});
2017-02-28 10:32:10 -03:00
};
2017-03-02 11:25:38 -03:00
2017-03-02 16:32:08 -03:00
$scope.close = function() {
$rootScope.$emit('updatePincodeOption');
$scope.pincodeModal.hide();
2017-03-02 11:25:38 -03:00
};
2017-02-23 16:56:46 -05:00
});