2018-08-08 16:35:49 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
2018-08-09 11:10:26 +12:00
|
|
|
(function(){
|
2018-08-08 16:35:49 +02:00
|
|
|
|
2018-08-09 11:10:26 +12:00
|
|
|
angular
|
2018-09-04 13:32:36 +09:00
|
|
|
.module('bitcoincom.services')
|
2018-08-29 15:38:23 +09:00
|
|
|
.factory('sendFlowStateService', sendFlowStateService);
|
2018-08-09 11:10:26 +12:00
|
|
|
|
2018-09-04 11:24:07 +09:00
|
|
|
function sendFlowStateService($log) {
|
2018-08-08 17:27:15 +02:00
|
|
|
|
2018-08-09 11:10:26 +12:00
|
|
|
var service = {
|
2018-09-04 11:24:07 +09:00
|
|
|
// Variables
|
2018-08-13 10:00:39 +12:00
|
|
|
state: {
|
|
|
|
|
amount: '',
|
2018-08-14 15:53:18 +02:00
|
|
|
displayAddress: null,
|
2018-08-13 10:00:39 +12:00
|
|
|
fromWalletId: '',
|
|
|
|
|
sendMax: false,
|
|
|
|
|
thirdParty: null,
|
|
|
|
|
toAddress: '',
|
2018-09-04 13:34:08 +09:00
|
|
|
toWalletId: '',
|
|
|
|
|
coin: '',
|
|
|
|
|
isRequestAmount: false,
|
|
|
|
|
isWalletTransfer: false
|
2018-08-13 10:00:39 +12:00
|
|
|
},
|
2018-08-09 11:10:26 +12:00
|
|
|
previousStates: [],
|
2018-08-08 17:27:15 +02:00
|
|
|
|
2018-08-09 11:10:26 +12:00
|
|
|
// Functions
|
2018-08-29 15:38:23 +09:00
|
|
|
init: init,
|
2018-08-09 11:10:26 +12:00
|
|
|
clear: clear,
|
2018-08-29 15:38:23 +09:00
|
|
|
getClone: getClone,
|
2018-08-09 11:10:26 +12:00
|
|
|
map: map,
|
2018-08-29 15:38:23 +09:00
|
|
|
pop: pop,
|
|
|
|
|
push: push,
|
2018-08-31 18:37:04 +09:00
|
|
|
isEmpty: isEmpty
|
2018-08-09 11:10:26 +12:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return service;
|
|
|
|
|
|
2018-09-04 11:24:07 +09:00
|
|
|
/**
|
|
|
|
|
* Init state & stack
|
|
|
|
|
* @param {Object} params
|
|
|
|
|
*/
|
2018-08-29 15:38:23 +09:00
|
|
|
function init(params) {
|
2018-09-04 11:24:07 +09:00
|
|
|
$log.debug("send-flow-state init()");
|
|
|
|
|
|
2018-08-29 15:38:23 +09:00
|
|
|
clear();
|
2018-08-31 18:37:04 +09:00
|
|
|
|
|
|
|
|
if (params) {
|
|
|
|
|
push(params);
|
|
|
|
|
}
|
2018-08-29 15:38:23 +09:00
|
|
|
}
|
|
|
|
|
|
2018-09-04 11:24:07 +09:00
|
|
|
/**
|
|
|
|
|
* Clear a state & stack
|
|
|
|
|
*/
|
2018-08-09 11:10:26 +12:00
|
|
|
function clear() {
|
2018-09-04 11:24:07 +09:00
|
|
|
$log.debug("send-flow-state clear()");
|
|
|
|
|
|
2018-08-09 13:06:50 +12:00
|
|
|
clearCurrent();
|
|
|
|
|
service.previousStates = [];
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 11:24:07 +09:00
|
|
|
/**
|
|
|
|
|
* Clear current state only
|
|
|
|
|
*/
|
2018-08-09 13:06:50 +12:00
|
|
|
function clearCurrent() {
|
2018-09-04 11:24:07 +09:00
|
|
|
$log.debug("send-flow-state clearCurrent()");
|
|
|
|
|
|
2018-08-13 10:00:39 +12:00
|
|
|
service.state = {
|
|
|
|
|
amount: '',
|
2018-08-14 15:53:18 +02:00
|
|
|
displayAddress: null,
|
2018-08-13 10:00:39 +12:00
|
|
|
fromWalletId: '',
|
|
|
|
|
sendMax: false,
|
|
|
|
|
thirdParty: null,
|
|
|
|
|
toAddress: '',
|
2018-09-04 13:34:08 +09:00
|
|
|
toWalletId: '',
|
|
|
|
|
coin: '',
|
|
|
|
|
isRequestAmount: false,
|
|
|
|
|
isWalletTransfer: false
|
2018-08-13 10:00:39 +12:00
|
|
|
}
|
2018-08-09 11:10:26 +12:00
|
|
|
}
|
2018-08-08 17:27:15 +02:00
|
|
|
|
2018-08-09 11:10:26 +12:00
|
|
|
/**
|
2018-09-04 11:24:07 +09:00
|
|
|
* Get a clone of the current state
|
2018-08-09 11:10:26 +12:00
|
|
|
*/
|
2018-08-29 15:38:23 +09:00
|
|
|
function getClone() {
|
2018-08-09 11:10:26 +12:00
|
|
|
var currentState = {};
|
2018-08-13 10:00:39 +12:00
|
|
|
Object.keys(service.state).forEach(function forCurrentParam(key) {
|
|
|
|
|
if (typeof service.state[key] !== 'function' && key !== 'previousStates') {
|
|
|
|
|
currentState[key] = service.state[key];
|
2018-08-09 11:10:26 +12:00
|
|
|
}
|
|
|
|
|
});
|
2018-08-09 13:01:48 +12:00
|
|
|
return currentState;
|
|
|
|
|
}
|
2018-08-09 11:10:26 +12:00
|
|
|
|
2018-09-04 11:24:07 +09:00
|
|
|
/**
|
|
|
|
|
* Fill in the current state from the params
|
|
|
|
|
* @param {Object} params
|
|
|
|
|
*/
|
2018-08-09 13:01:48 +12:00
|
|
|
function map(params) {
|
2018-08-09 11:10:26 +12:00
|
|
|
Object.keys(params).forEach(function forNewParam(key) {
|
2018-08-13 10:00:39 +12:00
|
|
|
service.state[key] = params[key];
|
2018-08-09 11:10:26 +12:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-04 11:24:07 +09:00
|
|
|
/**
|
|
|
|
|
* Pop state
|
|
|
|
|
*/
|
2018-08-29 15:38:23 +09:00
|
|
|
function pop() {
|
2018-09-04 11:24:07 +09:00
|
|
|
$log.debug('send-flow-state pop');
|
|
|
|
|
|
2018-08-09 11:10:26 +12:00
|
|
|
if (service.previousStates.length) {
|
2018-08-09 13:01:48 +12:00
|
|
|
var params = service.previousStates.pop();
|
2018-08-09 13:06:50 +12:00
|
|
|
clearCurrent();
|
2018-08-09 13:01:48 +12:00
|
|
|
map(params);
|
2018-08-09 11:10:26 +12:00
|
|
|
} else {
|
|
|
|
|
clear();
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-08-09 13:01:48 +12:00
|
|
|
|
2018-09-04 11:24:07 +09:00
|
|
|
/**
|
|
|
|
|
* Push state
|
|
|
|
|
* @param {Object} params
|
|
|
|
|
*/
|
2018-08-29 15:38:23 +09:00
|
|
|
function push(params) {
|
2018-09-04 11:24:07 +09:00
|
|
|
$log.debug('send-flow-state push');
|
|
|
|
|
|
2018-08-29 15:38:23 +09:00
|
|
|
var currentParams = getClone();
|
2018-08-09 13:01:48 +12:00
|
|
|
service.previousStates.push(currentParams);
|
2018-08-09 13:06:50 +12:00
|
|
|
clearCurrent();
|
2018-08-09 13:01:48 +12:00
|
|
|
map(params);
|
|
|
|
|
};
|
2018-08-31 18:37:04 +09:00
|
|
|
|
2018-09-04 11:24:07 +09:00
|
|
|
/**
|
|
|
|
|
* Is empty stack
|
|
|
|
|
*/
|
2018-08-31 18:37:04 +09:00
|
|
|
function isEmpty() {
|
|
|
|
|
return service.previousStates.length == 0;
|
|
|
|
|
};
|
2018-08-08 17:10:47 +02:00
|
|
|
};
|
|
|
|
|
|
2018-08-09 11:10:26 +12:00
|
|
|
})();
|