[imp] separated apples and cucumbers

bzr revid: nicolas.vanhoren@openerp.com-20110819145543-q2q4admsahmawkbd
This commit is contained in:
niv-openerp 2011-08-19 16:55:43 +02:00
parent bf509d08c3
commit e6d99526f0
4 changed files with 140 additions and 134 deletions

View File

@ -23,6 +23,7 @@
"static/lib/underscore/underscore.string.js",
"static/src/js/boot.js",
"static/src/js/core.js",
"static/src/js/dates.js",
"static/src/js/formats.js",
"static/src/js/chrome.js",
"static/src/js/views.js",

View File

@ -50,6 +50,7 @@
openerp.base = function(instance) {
openerp.base.core(instance);
openerp.base.dates(instance);
openerp.base.formats(instance);
openerp.base.chrome(instance);
openerp.base.data(instance);

View File

@ -0,0 +1,138 @@
openerp.base.dates = function(openerp) {
/**
* Converts a string to a Date javascript object using OpenERP's
* datetime string format (exemple: '2011-12-01 15:12:35').
*
* The timezone is assumed to be UTC (standard for OpenERP 6.1)
* and will be converted to the browser's timezone.
*
* @param {String} str A string representing a datetime.
* @returns {Date}
*/
openerp.base.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/;
var res = regex.exec(str);
if ( res[0] != str ) {
throw "'" + str + "' is not a valid datetime";
}
var obj = Date.parse(str + " GMT");
if (! obj) {
throw "'" + str + "' is not a valid datetime";
}
return obj;
};
/**
* Converts a string to a Date javascript object using OpenERP's
* date string format (exemple: '2011-12-01').
*
* @param {String} str A string representing a date.
* @returns {Date}
*/
openerp.base.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[0] != str ) {
throw "'" + str + "' is not a valid date";
}
var obj = Date.parse(str);
if (! obj) {
throw "'" + str + "' is not a valid date";
}
return obj;
};
/**
* Converts a string to a Date javascript object using OpenERP's
* time string format (exemple: '15:12:35').
*
* @param {String} str A string representing a time.
* @returns {Date}
*/
openerp.base.str_to_time = function(str) {
if(!str) {
return str;
}
var regex = /\d\d:\d\d:\d\d/;
var res = regex.exec(str);
if ( res[0] != str ) {
throw "'" + str + "' is not a valid time";
}
var obj = Date.parse(str);
if (! obj) {
throw "'" + str + "' is not a valid time";
}
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 timezone 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.
*/
openerp.base.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').
*
* @param {Date} obj
* @returns {String} A string representing a date.
*/
openerp.base.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').
*
* @param {Date} obj
* @returns {String} A string representing a time.
*/
openerp.base.time_to_str = function(obj) {
if (!obj) {
return false;
}
return zpad(obj.getHours(),2) + ":" + zpad(obj.getMinutes(),2) + ":"
+ zpad(obj.getSeconds(),2);
};
};

View File

@ -1,140 +1,6 @@
openerp.base.formats = function(openerp) {
/**
* Converts a string to a Date javascript object using OpenERP's
* datetime string format (exemple: '2011-12-01 15:12:35').
*
* The timezone is assumed to be UTC (standard for OpenERP 6.1)
* and will be converted to the browser's timezone.
*
* @param {String} str A string representing a datetime.
* @returns {Date}
*/
openerp.base.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/;
var res = regex.exec(str);
if ( res[0] != str ) {
throw "'" + str + "' is not a valid datetime";
}
var obj = Date.parse(str + " GMT");
if (! obj) {
throw "'" + str + "' is not a valid datetime";
}
return obj;
};
/**
* Converts a string to a Date javascript object using OpenERP's
* date string format (exemple: '2011-12-01').
*
* @param {String} str A string representing a date.
* @returns {Date}
*/
openerp.base.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[0] != str ) {
throw "'" + str + "' is not a valid date";
}
var obj = Date.parse(str);
if (! obj) {
throw "'" + str + "' is not a valid date";
}
return obj;
};
/**
* Converts a string to a Date javascript object using OpenERP's
* time string format (exemple: '15:12:35').
*
* @param {String} str A string representing a time.
* @returns {Date}
*/
openerp.base.str_to_time = function(str) {
if(!str) {
return str;
}
var regex = /\d\d:\d\d:\d\d/;
var res = regex.exec(str);
if ( res[0] != str ) {
throw "'" + str + "' is not a valid time";
}
var obj = Date.parse(str);
if (! obj) {
throw "'" + str + "' is not a valid time";
}
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 timezone 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.
*/
openerp.base.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').
*
* @param {Date} obj
* @returns {String} A string representing a date.
*/
openerp.base.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').
*
* @param {Date} obj
* @returns {String} A string representing a time.
*/
openerp.base.time_to_str = function(obj) {
if (!obj) {
return false;
}
return zpad(obj.getHours(),2) + ":" + zpad(obj.getMinutes(),2) + ":"
+ zpad(obj.getSeconds(),2);
};
/**
* Formats a single atomic value based on a field descriptor
*