keeweb/app/scripts/views/fields/field-view-otp.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-04-01 21:19:27 +02:00
'use strict';
var FieldViewText = require('./field-view-text');
var FieldViewOtp = FieldViewText.extend({
2016-04-01 23:09:16 +02:00
otpTimeout: null,
otpValue: null,
otpGenerator: null,
2016-04-01 21:19:27 +02:00
renderValue: function(value) {
2016-04-01 23:09:16 +02:00
this.resetOtpTimer();
if (!value) {
return '';
}
if (value !== this.otpGenerator) {
this.otpGenerator = value;
this.requestOtpUpdate();
}
return this.otpValue;
2016-04-01 21:19:27 +02:00
},
getEditValue: function(value) {
2016-04-01 23:09:16 +02:00
return value && value.url;
},
remove: function() {
this.resetOtpTimer();
this.value = null;
this.otpGenerator = null;
FieldViewText.prototype.remove.apply(this, arguments);
},
resetOtpTimer: function() {
if (this.otpTimeout) {
clearTimeout(this.otpTimeout);
}
},
requestOtpUpdate: function() {
if (this.value) {
this.value.next(this.otpUpdated.bind(this));
}
},
otpUpdated: function(pass, timeLeft) {
if (!this.value) {
return;
}
this.otpValue = pass || '';
this.render();
2016-04-01 23:16:23 +02:00
if (this.otpValue && timeLeft) {
2016-04-01 23:09:16 +02:00
this.otpTimeout = setTimeout(this.requestOtpUpdate.bind(this), timeLeft);
}
2016-04-01 21:19:27 +02:00
}
});
module.exports = FieldViewOtp;