summaryrefslogtreecommitdiff
path: root/addons/partner_autocomplete/static/src/js/iap_credit_checker_widget.js
diff options
context:
space:
mode:
authorAntony Lesuisse <al@openerp.com>2018-09-18 17:16:16 +0200
committerTWA <twa@odoo.com>2018-09-21 14:02:22 +0200
commita6e1eb9f0ad285fac7d0ca0b9f89f046d78ec9c7 (patch)
tree147f86739a95d6924ebda835c62716e66edcbdb5 /addons/partner_autocomplete/static/src/js/iap_credit_checker_widget.js
parentce16d0d2ab20ed223ab3dc2932ac7a6f27cccf5f (diff)
[ADD] partner_autocomplete: Added autocomplete on partners fields
For Name field or M2O, gives a list of companies Data comes from Odoo IAP Service
Diffstat (limited to 'addons/partner_autocomplete/static/src/js/iap_credit_checker_widget.js')
-rw-r--r--addons/partner_autocomplete/static/src/js/iap_credit_checker_widget.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/addons/partner_autocomplete/static/src/js/iap_credit_checker_widget.js b/addons/partner_autocomplete/static/src/js/iap_credit_checker_widget.js
new file mode 100644
index 00000000000..ab25dab0029
--- /dev/null
+++ b/addons/partner_autocomplete/static/src/js/iap_credit_checker_widget.js
@@ -0,0 +1,94 @@
+odoo.define('iap.credit.checker', function (require) {
+'use strict';
+
+var widgetRegistry = require('web.widget_registry');
+var Widget = require('web.Widget');
+
+var core = require('web.core');
+var rpc = require('web.rpc');
+
+var QWeb = core.qweb;
+
+var IAPCreditChecker = Widget.extend({
+ className: 'o_field_iap_credit_checker',
+
+ /**
+ * @constructor
+ * Prepares the basic rendering of edit mode by setting the root to be a
+ * div.dropdown.open.
+ * @see FieldChar.init
+ */
+ init: function (parent, data, options) {
+ this._super.apply(this, arguments);
+ this.service_name = options.attrs.service_name;
+ },
+
+ /**
+ * @override
+ */
+ start: function () {
+ this.$widget = $(QWeb.render('partner_autocomplete.iap_credit_checker'));
+ this.$loading = this.$widget.find('.loading');
+ this.$sufficient = this.$widget.find('.sufficient');
+ this.$insufficient = this.$widget.find('.insufficient');
+ this.$buyLink = this.$widget.find('.oe_link');
+
+ this.$widget.appendTo(this.$el);
+
+ this._getLink();
+ this._getCredits();
+ },
+
+ //--------------------------------------------------------------------------
+ // Private
+ //--------------------------------------------------------------------------
+ _getCredits: function () {
+ var self = this;
+ this._showLoading();
+
+ return rpc.query({
+ model: 'iap.account',
+ method: 'get_credits',
+ args: [this.service_name],
+ }, {
+ shadow: true,
+ }).then(function (credit) {
+ if (credit) self._showSufficient(credit);
+ else self._showInsufficient();
+ });
+ },
+
+ _getLink: function () {
+ var self = this;
+ return rpc.query({
+ model: 'iap.account',
+ method: 'get_credits_url',
+ args: [this.service_name],
+ }, {
+ shadow: true,
+ }).then(function (url) {
+ self.$buyLink.attr('href', url);
+ });
+ },
+
+ _showLoading: function () {
+ this.$loading.show();
+ this.$sufficient.hide();
+ this.$insufficient.hide();
+ },
+ _showSufficient: function (credits) {
+ this.$loading.hide();
+ this.$sufficient.show().find('.remaining_credits').text(credits);
+ this.$insufficient.hide();
+ },
+ _showInsufficient: function () {
+ this.$loading.hide();
+ this.$sufficient.hide();
+ this.$insufficient.show();
+ },
+});
+
+widgetRegistry.add('iap_credit_checker', IAPCreditChecker);
+
+return IAPCreditChecker;
+});