summaryrefslogtreecommitdiff
path: root/addons/sms/static/src/js/sms_widget.js
diff options
context:
space:
mode:
authorPierre Rousseau <pro@odoo.com>2018-01-30 08:18:44 +0100
committerRémi Rahir <rar@odoo.com>2018-02-06 12:57:50 +0100
commit3c2d407dd6d7c3635e3e6b74496a0effc9fc227b (patch)
tree39f5f257e818c1c7c1a6d13b7c110e8cba142118 /addons/sms/static/src/js/sms_widget.js
parent78958625840800a44843e74b6f08d1aad016b607 (diff)
[IMP] sms: show characters count
Diffstat (limited to 'addons/sms/static/src/js/sms_widget.js')
-rw-r--r--addons/sms/static/src/js/sms_widget.js120
1 files changed, 120 insertions, 0 deletions
diff --git a/addons/sms/static/src/js/sms_widget.js b/addons/sms/static/src/js/sms_widget.js
new file mode 100644
index 00000000000..a01ff3753cf
--- /dev/null
+++ b/addons/sms/static/src/js/sms_widget.js
@@ -0,0 +1,120 @@
+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 framework = require('web.framework');
+
+var FieldText = basicFields.FieldText;
+var QWeb = core.qweb;
+
+/**
+ * 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({
+ /**
+ * @constructor
+ */
+ init: function () {
+ this._super.apply(this, arguments);
+ this.nbrChar = 0;
+ this.nbrSMS = 0;
+ this.encoding = "GSM7";
+ this.tagName = 'div';
+ },
+
+ //--------------------------------------------------------------------------
+ // Private
+ //--------------------------------------------------------------------------
+
+ /**
+ * 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);
+ this._renderSMS();
+ },
+ /**
+ * 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);
+ },
+ /**
+ * @private
+ * @override
+ */
+ _renderEdit: function () {
+ this.$el.empty();
+ this._prepareInput($('<textarea/>')).appendTo(this.$el);
+ this.$el.append($(QWeb.render("sms.sms_count", {})));
+ this._compute();
+ },
+ /**
+ * 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 number of characters, sms and the encoding.
+ * @private
+ */
+ _renderSMS: function () {
+ this.$('.o_sms_count').text(this.nbrChar + ' / ' + this.nbrSMS + ' SMS (' + this.encoding + ') ');
+ },
+
+ //--------------------------------------------------------------------------
+ // Handlers
+ //--------------------------------------------------------------------------
+
+ /**
+ * @override
+ * @private
+ */
+ _onChange: function () {
+ this._super.apply(this, arguments);
+ this._compute();
+ },
+ /**
+ * @override
+ * @private
+ */
+ _onInput: function () {
+ this._super.apply(this, arguments);
+ this._compute();
+ },
+});
+
+fieldRegistry.add('sms_widget', SmsWidget);
+
+return SmsWidget;
+});