1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
odoo.define('base_iban.iban_widget', function (require) {
"use strict";
var basicFields = require('web.basic_fields');
var core = require('web.core');
var fieldRegistry = require('web.field_registry');
var FieldChar = basicFields.FieldChar;
var _t = core._t;
/**
* IbanWidget is a widget to check if the iban number is valide.
* If the bank account is correct, it will show a green check pictogram
* next to number, if the number is not complient with IBAN format, a
* red cross will be show. This pictogram is computed every time the user
* changes the field (If user is typing, there is a debouce of 400ms).
*/
var IbanWidget = FieldChar.extend({
/**
* @constructor
*/
init: function () {
this._super.apply(this, arguments);
this.ibanIsValid;
this._isValid = true;
this._compute_debounce = _.debounce(this._compute, 400);
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Compute if iban is valid
* @private
*/
_compute: function () {
var self = this;
var content = this._getValue();
if (content.length === 0) {
this.ibanIsValid = true;
this.$el.last().filter('.o_iban').removeClass('fa-check text-success fa-times text-danger o_iban_fail');
this.$el.last().filter('.o_iban').attr('title', '');
} else if (content.length < 15) {
if (this.ibanIsValid !== false) {
this.ibanIsValid = false;
this._renderValid();
}
} else {
this._rpc({
model: 'res.partner.bank',
method: 'check_iban',
args: [[], content],
})
.then(function (result) {
if (result !== self.ibanIsValid) {
self.ibanIsValid = result;
self._renderValid();
}
});
}
},
/**
* @private
* @override
* @returns {Promise|undefined}
*/
_renderEdit: function () {
var res = this._super.apply(this, arguments);
this._compute();
return res;
},
/**
* Render the pictogram next to account number.
* @private
*/
_renderValid: function () {
var warningMessage = _t("Account isn't IBAN compliant.");
if (this.$el.filter('.o_iban').length === 0) {
var $span;
if (!this.ibanIsValid) {
$span = $('<span class="fa fa-times o_iban text-danger o_iban_fail"/>');
$span.attr('title', warningMessage);
} else {
$span = $('<span class="fa fa-check o_iban text-success"/>');
}
this.$el.addClass('o_iban_input_with_validator');
$span.insertAfter(this.$el);
this.$el = this.$el.add($span);
}
if (!this.ibanIsValid) {
this.$el.last().filter('.o_iban').removeClass('fa-check text-success').addClass('fa-times text-danger o_iban_fail');
this.$el.last().filter('.o_iban').attr('title', warningMessage);
} else {
this.$el.last().filter('.o_iban').removeClass('fa-times text-danger o_iban_fail').addClass('fa-check text-success');
this.$el.last().filter('.o_iban').attr('title', '');
}
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* @override
* @private
*/
_onChange: function () {
this._super.apply(this, arguments);
this._compute();
},
/**
* @override
* @private
*/
_onInput: function () {
this._super.apply(this, arguments);
this._compute_debounce();
},
});
fieldRegistry.add('iban', IbanWidget);
return IbanWidget;
});
|