summaryrefslogtreecommitdiff
path: root/addons/sms/static/src/js/fields_sms_widget.js
diff options
context:
space:
mode:
authorThibault Delavallée <tde@odoo.com>2019-06-27 16:44:04 +0000
committerThibault Delavallée <tde@odoo.com>2019-07-12 14:54:11 +0000
commit14ccc770bba873f591a0a556d2c28e36ccc78d1e (patch)
treeb3518860b5eb3346e0d8b81066a1cbc0e8d12af5 /addons/sms/static/src/js/fields_sms_widget.js
parent9ad7a88a08ceccc3665acd8972ac47614205bf0e (diff)
[IMP] sms: improve SMS widget
Related to task 1922163 Linked to PR #33510 Co-Authored-By: Thibault Delavallee <tde@odoo.com> Co-Authored-By: Pierre Rousseau <pro@odoo.com>
Diffstat (limited to 'addons/sms/static/src/js/fields_sms_widget.js')
-rw-r--r--addons/sms/static/src/js/fields_sms_widget.js161
1 files changed, 161 insertions, 0 deletions
diff --git a/addons/sms/static/src/js/fields_sms_widget.js b/addons/sms/static/src/js/fields_sms_widget.js
new file mode 100644
index 00000000000..e9dbf8e7cc7
--- /dev/null
+++ b/addons/sms/static/src/js/fields_sms_widget.js
@@ -0,0 +1,161 @@
+odoo.define('sms.sms_widget', function (require) {
+"use strict";
+
+var basicFields = require('web.basic_fields');
+var core = require('web.core');
+var fieldRegistry = require('web.field_registry');
+
+var FieldText = basicFields.FieldText;
+
+var _t = core._t;
+/**
+ * SmsWidget is a widget to display a textarea (the body) and a text representing
+ * the number of SMS and the number of characters. This text is computed every
+ * time the user changes the body.
+ */
+var SmsWidget = FieldText.extend({
+ className: 'o_field_text',
+ /**
+ * @constructor
+ */
+ init: function () {
+ this._super.apply(this, arguments);
+ this.nbrChar = 0;
+ this.nbrSMS = 0;
+ this.encoding = 'GSM7';
+ },
+
+ //--------------------------------------------------------------------------
+ // Private: override widget
+ //--------------------------------------------------------------------------
+
+ /**
+ * @private
+ * @override
+ */
+ _renderEdit: function () {
+ var def = this._super.apply(this, arguments);
+
+ this._compute();
+ var $sms_container = $('<div class="o_sms_container"/>');
+ $sms_container.append(this._renderSMSInfo());
+ $sms_container.append(this._renderIAPButton());
+ this.$el = this.$el.add($sms_container);
+
+ return def;
+ },
+
+ //--------------------------------------------------------------------------
+ // Private: SMS
+ //--------------------------------------------------------------------------
+
+ /**
+ * Compute the number of characters and sms
+ * @private
+ */
+ _compute: function () {
+ var content = this._getValue();
+ this.encoding = this._extractEncoding(content);
+ this.nbrChar = content.length;
+ this.nbrChar += (content.match(/\n/g) || []).length;
+ this.nbrSMS = this._countSMS(this.nbrChar, this.encoding);
+ },
+
+ /**
+ * Count the number of SMS of the content
+ * @private
+ * @returns {integer} Number of SMS
+ */
+ _countSMS: function () {
+ if (this.nbrChar === 0) {
+ return 0;
+ }
+ if (this.encoding === 'UNICODE') {
+ if (this.nbrChar <= 70) {
+ return 1;
+ }
+ return Math.ceil(this.nbrChar / 67);
+ }
+ if (this.nbrChar <= 160) {
+ return 1;
+ }
+ return Math.ceil(this.nbrChar / 153);
+ },
+
+ /**
+ * Extract the encoding depending on the characters in the content
+ * @private
+ * @param {String} content Content of the SMS
+ * @returns {String} Encoding of the content (GSM7 or UNICODE)
+ */
+ _extractEncoding: function (content) {
+ if (String(content).match(RegExp("^[@£$¥èéùìòÇ\\nØø\\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\\\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà]*$"))) {
+ return 'GSM7';
+ }
+ return 'UNICODE';
+ },
+
+ /**
+ * Render the IAP button to redirect to IAP pricing
+ * @private
+ */
+ _renderIAPButton: function () {
+ return $('<a>', {
+ 'href': 'https://iap-services.odoo.com/iap/sms/pricing',
+ 'target': '_blank',
+ 'title': _t('SMS Pricing'),
+ 'aria-label': _t('SMS Pricing'),
+ 'class': 'fa fa-lg fa-info-circle',
+ });
+ },
+
+ /**
+ * Render the number of characters, sms and the encoding.
+ * @private
+ */
+ _renderSMSInfo: function () {
+ var string = _.str.sprintf(_t('%s chars, fits in %s SMS (%s) '), this.nbrChar, this.nbrSMS, this.encoding);
+ var $span = $('<span>', {
+ 'class': 'text-muted o_sms_count',
+ });
+ $span.text(string);
+ return $span;
+ },
+
+ /**
+ * Update widget SMS information with re-computed info about length, ...
+ * @private
+ */
+ _updateSMSInfo: function () {
+ this._compute();
+ var string = _.str.sprintf(_t('%s chars, fits in %s SMS (%s) '), this.nbrChar, this.nbrSMS, this.encoding);
+ this.$('.o_sms_count').text(string);
+ },
+
+ //--------------------------------------------------------------------------
+ // Handlers
+ //--------------------------------------------------------------------------
+
+ /**
+ * @override
+ * @private
+ */
+ _onChange: function () {
+ this._super.apply(this, arguments);
+ this._updateSMSInfo();
+ },
+
+ /**
+ * @override
+ * @private
+ */
+ _onInput: function () {
+ this._super.apply(this, arguments);
+ this._updateSMSInfo();
+ },
+});
+
+fieldRegistry.add('sms_widget', SmsWidget);
+
+return SmsWidget;
+});