From 6e65f9aba6322bd3540a3ebb26cc59ec417353d8 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Mon, 28 Jan 2013 17:23:52 +0100 Subject: [PATCH] Added style bzr revid: nicolas.vanhoren@openerp.com-20130128162352-rmtvv7dsl82dn4rq --- .../static/ext/static/js/livesupport.js | 13 +- .../static/ext/static/js/oeclient.js | 150 ++++++++++++++++++ 2 files changed, 160 insertions(+), 3 deletions(-) diff --git a/addons/live_support/static/ext/static/js/livesupport.js b/addons/live_support/static/ext/static/js/livesupport.js index c606316c495..b70b6017667 100644 --- a/addons/live_support/static/ext/static/js/livesupport.js +++ b/addons/live_support/static/ext/static/js/livesupport.js @@ -7,7 +7,7 @@ define(["nova", "jquery", "underscore", "oeclient", "require"], function(nova, $ var connection; livesupport.main = function(server_url, db, login, password) { - $.ajax({ + var templates_def = $.ajax({ url: require.toUrl("./livesupport_templates.js"), jsonp: false, jsonpCallback: "oe_livesupport_templates_callback", @@ -15,7 +15,14 @@ define(["nova", "jquery", "underscore", "oeclient", "require"], function(nova, $ cache: true, }).then(function(content) { return templateEngine.loadFileContent(content); - }).then(function() { + }); + var css_def = $.Deferred(); + $('') + .appendTo($("head")).ready(function() { + css_def.resolve(); + }); + + $.when(templates_def, css_def).then(function() { console.log("starting client"); connection = new oeclient.Connection(new oeclient.JsonpRPCConnector(server_url), db, login, password); connection.getModel("res.users").search_read([["login", "=", ["demo"]]]).then(function(result) { @@ -214,7 +221,7 @@ define(["nova", "jquery", "underscore", "oeclient", "require"], function(nova, $ }); livesupport.Conversation = nova.Widget.$extend({ - tagClass: "oe_im_chatview", + className: "openerp_style oe_im_chatview", events: { "keydown input": "send_message", "click .oe_im_chatview_close": "destroy", diff --git a/addons/live_support/static/ext/static/js/oeclient.js b/addons/live_support/static/ext/static/js/oeclient.js index e38206a888f..2e423239526 100644 --- a/addons/live_support/static/ext/static/js/oeclient.js +++ b/addons/live_support/static/ext/static/js/oeclient.js @@ -173,6 +173,156 @@ define(["underscore", "jquery", "nova"], function(_, $, nova) { }, }); + /** + * Converts a string to a Date javascript object using OpenERP's + * datetime string format (exemple: '2011-12-01 15:12:35'). + * + * The time zone is assumed to be UTC (standard for OpenERP 6.1) + * and will be converted to the browser's time zone. + * + * @param {String} str A string representing a datetime. + * @returns {Date} + */ + oeclient.str_to_datetime = function(str) { + if(!str) { + return str; + } + var regex = /^(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)(?:\.\d+)?$/; + var res = regex.exec(str); + if ( !res ) { + throw new Error(_.str.sprintf(_t("'%s' is not a valid datetime"), str)); + } + var obj = Date.parseExact(res[1] + " UTC", 'yyyy-MM-dd HH:mm:ss zzz'); + if (! obj) { + throw new Error(_.str.sprintf(_t("'%s' is not a valid datetime"), str)); + } + return obj; + }; + + /** + * Converts a string to a Date javascript object using OpenERP's + * date string format (exemple: '2011-12-01'). + * + * As a date is not subject to time zones, we assume it should be + * represented as a Date javascript object at 00:00:00 in the + * time zone of the browser. + * + * @param {String} str A string representing a date. + * @returns {Date} + */ + oeclient.str_to_date = function(str) { + if(!str) { + return str; + } + var regex = /^\d\d\d\d-\d\d-\d\d$/; + var res = regex.exec(str); + if ( !res ) { + throw new Error(_.str.sprintf(_t("'%s' is not a valid date"), str)); + } + var obj = Date.parseExact(str, 'yyyy-MM-dd'); + if (! obj) { + throw new Error(_.str.sprintf(_t("'%s' is not a valid date"), str)); + } + return obj; + }; + + /** + * Converts a string to a Date javascript object using OpenERP's + * time string format (exemple: '15:12:35'). + * + * The OpenERP times are supposed to always be naive times. We assume it is + * represented using a javascript Date with a date 1 of January 1970 and a + * time corresponding to the meant time in the browser's time zone. + * + * @param {String} str A string representing a time. + * @returns {Date} + */ + oeclient.str_to_time = function(str) { + if(!str) { + return str; + } + var regex = /^(\d\d:\d\d:\d\d)(?:\.\d+)?$/; + var res = regex.exec(str); + if ( !res ) { + throw new Error(_.str.sprintf(_t("'%s' is not a valid time"), str)); + } + var obj = Date.parseExact("1970-01-01 " + res[1], 'yyyy-MM-dd HH:mm:ss'); + if (! obj) { + throw new Error(_.str.sprintf(_t("'%s' is not a valid time"), str)); + } + return obj; + }; + + /* + * Left-pad provided arg 1 with zeroes until reaching size provided by second + * argument. + * + * @param {Number|String} str value to pad + * @param {Number} size size to reach on the final padded value + * @returns {String} padded string + */ + var zpad = function(str, size) { + str = "" + str; + return new Array(size - str.length + 1).join('0') + str; + }; + + /** + * Converts a Date javascript object to a string using OpenERP's + * datetime string format (exemple: '2011-12-01 15:12:35'). + * + * The time zone of the Date object is assumed to be the one of the + * browser and it will be converted to UTC (standard for OpenERP 6.1). + * + * @param {Date} obj + * @returns {String} A string representing a datetime. + */ + oeclient.datetime_to_str = function(obj) { + if (!obj) { + return false; + } + return zpad(obj.getUTCFullYear(),4) + "-" + zpad(obj.getUTCMonth() + 1,2) + "-" + + zpad(obj.getUTCDate(),2) + " " + zpad(obj.getUTCHours(),2) + ":" + + zpad(obj.getUTCMinutes(),2) + ":" + zpad(obj.getUTCSeconds(),2); + }; + + /** + * Converts a Date javascript object to a string using OpenERP's + * date string format (exemple: '2011-12-01'). + * + * As a date is not subject to time zones, we assume it should be + * represented as a Date javascript object at 00:00:00 in the + * time zone of the browser. + * + * @param {Date} obj + * @returns {String} A string representing a date. + */ + oeclient.date_to_str = function(obj) { + if (!obj) { + return false; + } + return zpad(obj.getFullYear(),4) + "-" + zpad(obj.getMonth() + 1,2) + "-" + + zpad(obj.getDate(),2); + }; + + /** + * Converts a Date javascript object to a string using OpenERP's + * time string format (exemple: '15:12:35'). + * + * The OpenERP times are supposed to always be naive times. We assume it is + * represented using a javascript Date with a date 1 of January 1970 and a + * time corresponding to the meant time in the browser's time zone. + * + * @param {Date} obj + * @returns {String} A string representing a time. + */ + oeclient.time_to_str = function(obj) { + if (!obj) { + return false; + } + return zpad(obj.getHours(),2) + ":" + zpad(obj.getMinutes(),2) + ":" + + zpad(obj.getSeconds(),2); + }; + return oeclient; }); \ No newline at end of file