Wallet/src/js/models/profile.js

117 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
/**
* Profile
*
* credential: array of OBJECTS
*/
function Profile() {
this.version = '1.0.0';
};
Profile.create = function(opts) {
opts = opts || {};
var x = new Profile();
x.createdOn = Date.now();
2015-05-13 11:58:19 -03:00
x.credentials = opts.credentials || [];
2015-12-07 12:52:42 -03:00
x.disclaimerAccepted = false;
x.checked = {};
2015-03-06 12:00:10 -03:00
return x;
};
Profile.fromObj = function(obj) {
var x = new Profile();
2015-05-13 11:58:19 -03:00
2015-03-06 12:00:10 -03:00
x.createdOn = obj.createdOn;
x.credentials = obj.credentials;
2015-12-07 12:52:42 -03:00
x.disclaimerAccepted = obj.disclaimerAccepted;
x.checked = obj.checked || {};
2016-06-06 18:26:45 -03:00
x.checkedUA = obj.checkedUA || {};
2015-05-13 11:58:19 -03:00
2015-03-06 12:00:10 -03:00
if (x.credentials[0] && typeof x.credentials[0] != 'object')
throw ("credentials should be an object");
2015-05-13 11:58:19 -03:00
2015-03-06 12:00:10 -03:00
return x;
};
Profile.fromString = function(str) {
return Profile.fromObj(JSON.parse(str));
};
Profile.prototype.toObj = function() {
2016-06-06 18:26:45 -03:00
delete this.dirty;
2015-03-06 12:00:10 -03:00
return JSON.stringify(this);
};
2016-06-06 12:21:15 -03:00
Profile.prototype.hasWallet = function(walletId) {
for (var i in this.credentials) {
var c = this.credentials[i];
if (c.walletId == walletId) return true;
};
return false;
};
Profile.prototype.isChecked = function(ua, walletId) {
2016-06-06 18:26:45 -03:00
return !!(this.checkedUA == ua && this.checked[walletId]);
2016-06-06 12:21:15 -03:00
};
2016-06-14 12:28:21 -03:00
Profile.prototype.isDeviceChecked = function(ua) {
return this.checkedUA == ua;
};
2016-06-06 12:21:15 -03:00
Profile.prototype.setChecked = function(ua, walletId) {
if (this.checkedUA != ua) {
this.checkedUA = ua;
this.checked = {};
}
this.checked[walletId] = true;
2016-06-06 18:26:45 -03:00
this.dirty = true;
2016-06-06 12:21:15 -03:00
};
Profile.prototype.addWallet = function(credentials) {
2016-06-14 17:48:00 -03:00
if (!credentials.walletId)
throw 'credentials must have .walletId';
2016-06-06 12:21:15 -03:00
if (this.hasWallet(credentials.walletId))
return false;
this.credentials.push(credentials);
2016-06-06 18:26:45 -03:00
this.dirty = true;
return true;
};
Profile.prototype.updateWallet = function(credentials) {
2016-06-14 17:48:00 -03:00
if (!credentials.walletId)
throw 'credentials must have .walletId';
2016-06-06 18:26:45 -03:00
if (!this.hasWallet(credentials.walletId))
return false;
2016-06-14 17:48:00 -03:00
this.credentials = this.credentials.map(function(c) {
if(c.walletId != credentials.walletId ) {
return c;
} else {
return credentials
}
2016-06-06 18:26:45 -03:00
});
this.dirty = true;
2016-06-06 12:21:15 -03:00
return true;
};
Profile.prototype.deleteWallet = function(walletId) {
if (!this.hasWallet(walletId))
return false;
this.credentials = this.credentials.filter(function(c) {
return c.walletId != walletId;
});
2016-06-06 18:26:45 -03:00
this.dirty = true;
2016-06-06 12:21:15 -03:00
return true;
};