Wallet/src/js/services/send-flow-state.service.js

142 lines
2.8 KiB
JavaScript
Raw Normal View History

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