2011-08-22 20:18:29 +02:00
/ * *
2013-04-13 21:00:13 +02:00
* EGroupware eTemplate2 - JS Date object
2011-08-22 20:18:29 +02:00
*
* @ license http : //opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @ package etemplate
* @ subpackage api
* @ link http : //www.egroupware.org
* @ author Nathan Gray
* @ copyright Nathan Gray 2011
* @ version $Id$
* /
"use strict" ;
/ * e g w : u s e s
jquery . jquery ;
2012-03-09 01:36:35 +01:00
jquery . jquery - ui ;
2011-08-29 23:15:53 +02:00
lib / date ;
2011-08-24 12:18:07 +02:00
et2 _core _inputWidget ;
et2 _core _valueWidget ;
2011-08-22 20:18:29 +02:00
* /
/ * *
* Class which implements the "date" XET - Tag
2013-04-13 21:00:13 +02:00
*
* @ augments et2 _inputWidget
2011-08-22 20:18:29 +02:00
* /
2013-04-13 21:00:13 +02:00
var et2 _date = et2 _inputWidget . extend (
{
2011-08-22 20:18:29 +02:00
attributes : {
"value" : {
"type" : "any"
} ,
"type" : {
"ignore" : false
2013-02-11 09:08:28 +01:00
} ,
"data_format" : {
"ignore" : true ,
"description" : "Format data is in. This is not used client-side because it's always a timestamp client side."
2011-08-22 20:18:29 +02:00
}
} ,
2013-02-11 09:08:28 +01:00
legacyOptions : [ "data_format" ] ,
2013-04-13 21:00:13 +02:00
/ * *
* Constructor
*
* @ memberOf et2 _date
* /
2011-08-24 12:05:52 +02:00
init : function ( ) {
2011-08-22 20:18:29 +02:00
this . _super . apply ( this , arguments ) ;
2012-03-09 01:36:35 +01:00
this . date = new Date ( ) ;
this . date . setHours ( 0 ) ;
this . date . setMinutes ( 0 ) ;
this . date . setSeconds ( 0 ) ;
2011-08-22 20:18:29 +02:00
this . input = null ;
this . createInputWidget ( ) ;
} ,
createInputWidget : function ( ) {
2011-08-29 23:15:53 +02:00
2012-03-09 01:36:35 +01:00
this . span = $j ( document . createElement ( "span" ) ) . addClass ( "et2_date" ) ;
2011-08-22 20:18:29 +02:00
2012-03-12 22:20:46 +01:00
this . input _date = $j ( document . createElement ( "input" ) ) ;
2013-08-20 11:42:00 +02:00
this . input _date . addClass ( "et2_date" ) . attr ( "type" , "text" ) . attr ( "size" , 5 )
2012-03-12 22:20:46 +01:00
. appendTo ( this . span ) ;
2012-03-09 01:36:35 +01:00
this . setDOMNode ( this . span [ 0 ] ) ;
// jQuery-UI date picker
2012-03-12 22:20:46 +01:00
if ( this . _type != 'date-timeonly' )
2011-08-31 21:58:38 +02:00
{
2012-03-12 22:20:46 +01:00
this . egw ( ) . calendar ( this . input _date , this . _type == "date-time" ) ;
2011-08-31 21:58:38 +02:00
}
2012-03-12 22:20:46 +01:00
else
2011-08-31 21:58:38 +02:00
{
2013-08-20 11:42:00 +02:00
this . input _date . addClass ( "et2_time" ) ;
2012-03-12 22:20:46 +01:00
this . egw ( ) . time ( this . input _date ) ;
2011-08-31 21:58:38 +02:00
}
2012-03-28 21:05:48 +02:00
// Update internal value when changed
2012-03-20 15:51:02 +01:00
var self = this ;
2012-04-16 23:32:42 +02:00
this . input _date . datepicker ( "option" , "onSelect" , function ( text , inst ) {
var d = new Date ( ) ;
var date _inst = null ;
if ( inst . inst && inst . inst . selectedYear )
{
date _inst = inst . inst ;
}
else if ( inst . selectedYear )
{
date _inst = inst ;
}
// Date could be in different places, if it's a datetime or just date
if ( date _inst )
{
d . setYear ( date _inst . selectedYear ) ;
d . setMonth ( date _inst . selectedMonth ) ;
d . setDate ( date _inst . selectedDay ) ;
}
if ( inst && inst . hour )
{
d . setHours ( inst . hour ) ;
d . setMinutes ( inst . minute ) ;
}
self . set _value ( d ) ;
2012-03-20 15:51:02 +01:00
} ) ;
2012-03-28 21:05:48 +02:00
// Framewok skips nulls, but null needs to be processed here
if ( this . options . value == null )
{
this . set _value ( null ) ;
}
2011-08-31 21:58:38 +02:00
} ,
2012-03-12 22:20:46 +01:00
2011-08-22 20:18:29 +02:00
set _type : function ( _type ) {
2012-03-22 16:56:16 +01:00
if ( _type != this . _type )
{
2012-03-29 01:05:42 +02:00
this . _type = _type ;
2012-03-22 16:56:16 +01:00
this . createInputWidget ( ) ;
}
2011-08-22 20:18:29 +02:00
} ,
set _value : function ( _value ) {
2012-03-22 16:56:16 +01:00
var old _value = this . getValue ( ) ;
2012-03-12 22:20:46 +01:00
if ( _value == null || _value == 0 )
2011-10-12 22:04:16 +02:00
{
this . value = _value ;
if ( this . input _date )
{
this . input _date . val ( "" ) ;
}
2012-03-20 18:45:51 +01:00
if ( old _value !== this . value )
{
2012-03-20 22:46:22 +01:00
this . change ( this . input _date ) ;
2012-03-20 18:45:51 +01:00
}
2011-10-12 22:04:16 +02:00
return ;
}
2011-08-31 21:58:38 +02:00
// Handle just time as a string in the form H:i
2011-08-22 20:18:29 +02:00
if ( typeof _value == 'string' && isNaN ( _value ) ) {
2012-03-29 01:05:42 +02:00
if ( _value . indexOf ( ":" ) > 0 && this . _type == "date-timeonly" ) {
2011-08-31 21:58:38 +02:00
this . value = _value ;
2012-03-12 22:20:46 +01:00
this . input _date . timepicker ( 'setTime' , _value ) ;
2012-03-20 18:45:51 +01:00
if ( old _value !== this . value )
{
2012-03-20 22:46:22 +01:00
this . change ( this . input _date ) ;
2012-03-20 18:45:51 +01:00
}
2012-03-12 22:20:46 +01:00
return ;
2011-08-29 23:15:53 +02:00
} else {
2012-03-08 01:20:04 +01:00
var text = new Date ( _value ) ;
// Handle timezone offset - times are already in user time
var localOffset = text . getTimezoneOffset ( ) * 60000 ;
this . date . setTime ( text . valueOf ( ) + localOffset ) ;
2012-03-09 01:36:35 +01:00
_value = Math . round ( this . date . valueOf ( ) / 1000 ) ;
2011-08-29 23:15:53 +02:00
}
2011-08-31 21:58:38 +02:00
} else if ( typeof _value == 'object' && _value . date ) {
this . date = _value . date ;
2012-03-09 01:36:35 +01:00
} else if ( typeof _value == 'object' && _value . valueOf ) {
this . date = _value ;
2013-07-09 00:24:36 +02:00
} else if ( typeof _value == 'number' || ! isNaN ( _value ) ) {
// Timestamp
// JS dates use milliseconds
this . date . setTime ( parseInt ( _value ) * 1000 ) ;
2011-08-22 20:18:29 +02:00
}
2012-03-20 15:51:02 +01:00
// Update input - popups do, but framework doesn't
if ( this . _type != 'date-timeonly' )
{
this . input _date . val ( jQuery . datepicker . formatDate ( this . input _date . datepicker ( "option" , "dateFormat" ) , this . date ) ) ;
}
if ( this . _type != 'date' )
{
var current = this . input _date . val ( ) ;
2012-03-29 01:05:42 +02:00
if ( this . _type != 'date-timeonly' )
2012-03-20 15:51:02 +01:00
{
current += " " ;
}
2013-08-20 20:37:09 +02:00
else
{
current = '' ;
}
2012-03-20 15:51:02 +01:00
this . input _date . val ( current + jQuery . datepicker . formatTime ( this . input _date . datepicker ( "option" , "timeFormat" ) , {
hour : this . date . getHours ( ) ,
minute : this . date . getMinutes ( ) ,
seconds : this . date . getSeconds ( ) ,
timezone : this . date . getTimezoneOffset ( )
} ) ) ;
}
2012-03-20 18:45:51 +01:00
if ( old _value != this . getValue ( ) )
{
this . change ( this . input _date ) ;
}
2011-08-31 02:08:59 +02:00
} ,
getValue : function ( ) {
2012-03-20 22:46:22 +01:00
if ( this . input _date . val ( ) == "" )
{
// User blanked the box
return null ;
}
2013-08-26 19:04:43 +02:00
// Convert to timestamp
return Math . round ( this . date . valueOf ( ) / 1000 ) ;
2011-08-22 20:18:29 +02:00
}
} ) ;
et2 _register _widget ( et2 _date , [ "date" , "date-time" , "date-timeonly" ] ) ;
2013-04-13 21:00:13 +02:00
/ * *
* @ augments et2 _date
* /
var et2 _date _duration = et2 _date . extend (
{
2011-08-30 22:50:55 +02:00
attributes : {
"data_format" : {
"name" : "Data format" ,
"default" : "m" ,
"type" : "string" ,
"description" : "Units to read/store the data. 'd' = days (float), 'h' = hours (float), 'm' = minutes (int)."
} ,
"display_format" : {
"name" : "Display format" ,
"default" : "dh" ,
"type" : "string" ,
"description" : "Permitted units for displaying the data. 'd' = days, 'h' = hours, 'm' = minutes. Use combinations to give a choice. Default is 'dh' = days or hours with selectbox."
} ,
"percent_allowed" : {
"name" : "Percent allowed" ,
"default" : false ,
"type" : "boolean" ,
"description" : "Allows to enter a percentage."
} ,
"hours_per_day" : {
"name" : "Hours per day" ,
"default" : 8 ,
"type" : "integer" ,
"description" : "Number of hours in a day, for converting between hours and (working) days."
} ,
"empty_not_0" : {
"name" : "0 or empty" ,
"default" : false ,
"type" : "boolean" ,
"description" : "Should the widget differ between 0 and empty, which get then returned as NULL"
} ,
"short_labels" : {
"name" : "Short labels" ,
"default" : false ,
"type" : "boolean" ,
"description" : "use d/h/m instead of day/hour/minute"
}
} ,
legacyOptions : [ "data_format" , "display_format" , "hours_per_day" , "empty_not_0" , "short_labels" ] ,
2011-09-08 19:11:49 +02:00
time _formats : { "d" : "d" , "h" : "h" , "m" : "m" } ,
2013-04-13 21:00:13 +02:00
/ * *
* Constructor
*
* @ memberOf et2 _date _duration
* /
2011-08-30 22:50:55 +02:00
init : function ( ) {
this . _super . apply ( this , arguments ) ;
this . input = null ;
// Legacy option put percent in with display format
if ( this . options . display _format . indexOf ( "%" ) != - 1 )
{
this . options . percent _allowed = true ;
this . options . display _format = this . options . display _format . replace ( "%" , "" ) ;
}
2011-09-08 19:11:49 +02:00
2012-03-30 16:30:26 +02:00
// Clean formats
this . options . display _format = this . options . display _format . replace ( /[^dhm]/ , '' ) ;
if ( ! this . options . display _format )
{
this . options . display _format = this . attributes . display _format [ "default" ] ;
}
2011-09-08 19:11:49 +02:00
// Get translations
this . time _formats = {
2012-03-02 11:44:56 +01:00
"d" : this . options . short _labels ? this . egw ( ) . lang ( "m" ) : this . egw ( ) . lang ( "Days" ) ,
"h" : this . options . short _labels ? this . egw ( ) . lang ( "h" ) : this . egw ( ) . lang ( "Hours" ) ,
"m" : this . options . short _labels ? this . egw ( ) . lang ( "m" ) : this . egw ( ) . lang ( "Minutes" )
2011-09-08 19:11:49 +02:00
} ,
2011-08-30 22:50:55 +02:00
this . createInputWidget ( ) ;
} ,
createInputWidget : function ( ) {
// Create nodes
this . node = $j ( document . createElement ( "span" ) ) ;
2011-08-31 21:58:38 +02:00
this . duration = $j ( document . createElement ( "input" ) ) . attr ( "size" , "2" ) ;
2011-08-30 22:50:55 +02:00
this . node . append ( this . duration ) ;
if ( this . options . display _format . length > 1 )
{
this . format = $j ( document . createElement ( "select" ) ) ;
this . node . append ( this . format ) ;
for ( var i = 0 ; i < this . options . display _format . length ; i ++ ) {
2011-09-08 19:11:49 +02:00
this . format . append ( "<option value='" + this . options . display _format [ i ] + "'>" + this . time _formats [ this . options . display _format [ i ] ] + "</option>" ) ;
2011-08-30 22:50:55 +02:00
}
2012-03-30 16:30:26 +02:00
}
else if ( this . time _formats [ this . options . display _format ] )
{
this . format = $j ( "<span>" + this . time _formats [ this . options . display _format ] + "</span>" ) . appendTo ( this . node ) ;
}
else
{
this . format = $j ( "<span>" + this . time _formats [ "m" ] + "</span>" ) . appendTo ( this . node ) ;
2011-08-30 22:50:55 +02:00
}
} ,
attachToDOM : function ( ) {
var node = this . getInputNode ( ) ;
2013-02-05 09:55:09 +01:00
if ( node )
{
$j ( node ) . bind ( "change.et2_inputWidget" , this , function ( e ) {
e . data . change ( this ) ;
} ) ;
}
2011-08-30 22:50:55 +02:00
et2 _DOMWidget . prototype . attachToDOM . apply ( this , arguments ) ;
} ,
getDOMNode : function ( ) {
return this . node [ 0 ] ;
} ,
getInputNode : function ( ) {
return this . duration [ 0 ] ;
} ,
/ * *
* Use id on node , same as DOMWidget
* /
set _id : function ( _value ) {
this . id = _value ;
2013-02-05 09:55:09 +01:00
var node = this . getDOMNode ( this ) ;
if ( node )
{
if ( _value != "" )
{
2013-07-17 15:08:48 +02:00
node . setAttribute ( "id" , this . getInstanceManager ( ) . uniqueId + '_' + this . id ) ;
2013-02-05 09:55:09 +01:00
}
else
{
node . removeAttribute ( "id" ) ;
}
}
2011-08-30 22:50:55 +02:00
} ,
set _value : function ( _value ) {
this . options . value = _value ;
2011-09-08 19:11:49 +02:00
var display = this . _convert _to _display ( _value ) ;
// Set display
if ( this . duration [ 0 ] . nodeName == "INPUT" )
{
this . duration . val ( display . value ) ;
}
else
{
this . duration . text ( display . value + " " ) ;
}
// Set unit as figured for display
if ( display . unit != this . options . display _format )
{
2012-03-30 16:30:26 +02:00
if ( this . format && this . format . children ( ) . length > 1 ) {
2011-09-08 19:11:49 +02:00
$j ( "option[value='" + display . unit + "']" , this . format ) . attr ( 'selected' , 'selected' ) ;
}
else
{
this . format . text ( this . time _formats [ display . unit ] ) ;
}
}
} ,
/ * *
* Converts the value in data format into value in display format .
*
* @ param _value int / float Data in data format
*
* @ return Object { value : Value in display format , unit : unit for display }
* /
_convert _to _display : function ( _value ) {
2011-08-30 22:50:55 +02:00
if ( _value )
2013-02-05 09:55:09 +01:00
{
2011-08-30 22:50:55 +02:00
// Put value into minutes for further processing
2013-02-05 09:55:09 +01:00
switch ( this . options . data _format )
2011-08-30 22:50:55 +02:00
{
case 'd' :
_value *= this . options . hours _per _day ;
// fall-through
case 'h' :
_value *= 60 ;
break ;
}
2013-02-05 09:55:09 +01:00
}
2011-08-30 22:50:55 +02:00
// Figure out best unit for display
var _unit = this . options . display _format == "d" ? "d" : "h" ;
if ( this . options . data _format . indexOf ( 'm' ) > - 1 && _value && _value < 60 )
2013-02-05 09:55:09 +01:00
{
_unit = 'm' ;
}
else if ( this . options . data _format . indexOf ( 'd' ) > - 1 && _value >= 60 * this . options . hours _per _day )
{
_unit = 'd' ;
}
2011-08-30 22:50:55 +02:00
_value = this . options . empty _not _0 && _value === '' || ! this . options . empty _not _0 && ! _value ? '' :
2013-02-05 09:55:09 +01:00
( _unit == 'm' ? parseInt ( _value ) : ( Math . round ( ( _value / 60.0 / ( _unit == 'd' ? this . options . hours _per _day : 1 ) ) * 100 ) / 100 ) ) ;
2011-09-22 23:28:26 +02:00
if ( _value === '' ) _unit = '' ;
2013-02-05 09:55:09 +01:00
// use decimal separator from user prefs
2011-08-30 22:50:55 +02:00
var sep = '.' ;
2012-03-02 11:44:56 +01:00
var format = this . egw ( ) . preference ( 'number_format' ) ;
2013-02-05 09:55:09 +01:00
if ( typeof _value == 'string' && format && ( sep = format [ 0 ] ) && sep != '.' )
{
_value = _value . replace ( '.' , sep ) ;
}
2011-08-30 22:50:55 +02:00
2011-09-08 19:11:49 +02:00
return { value : _value , unit : _unit } ;
2011-08-30 22:50:55 +02:00
} ,
/ * *
* Change displayed value into storage value and return
* /
getValue : function ( ) {
2011-08-31 02:08:59 +02:00
var value = this . duration . val ( ) ;
2011-08-30 22:50:55 +02:00
if ( value === '' )
{
2013-02-07 15:36:19 +01:00
return this . options . empty _not _0 ? null : 0 ;
2011-08-30 22:50:55 +02:00
}
// Put value into minutes for further processing
switch ( this . format ? this . format . val ( ) : this . options . display _format )
{
case 'd' :
value *= this . options . hours _per _day ;
// fall-through
case 'h' :
value *= 60 ;
break ;
}
switch ( this . options . data _format )
{
case 'd' :
value /= this . options . hours _per _day ;
// fall-through
case 'h' :
value /= 60.0 ;
break ;
}
return value ;
}
} ) ;
et2 _register _widget ( et2 _date _duration , [ "date-duration" ] ) ;
2013-04-13 21:00:13 +02:00
/ * *
* @ augments et2 _date _duration
* /
var et2 _date _duration _ro = et2 _date _duration . extend ( [ et2 _IDetachedDOM ] ,
{
/ * *
* @ memberOf et2 _date _duration _ro
* /
2011-09-08 19:11:49 +02:00
createInputWidget : function ( ) {
this . node = $j ( document . createElement ( "span" ) ) ;
var display = this . _convert _to _display ( this . options . value ) ;
this . duration = $j ( document . createElement ( "span" ) ) . appendTo ( this . node ) ;
this . format = $j ( document . createElement ( "span" ) ) . appendTo ( this . node ) ;
} ,
/ * *
* Code for implementing et2 _IDetachedDOM
* Fast - clonable read - only widget that only deals with DOM nodes , not the widget tree
* /
/ * *
* Build a list of attributes which can be set when working in the
* "detached" mode in the _attrs array which is provided
* by the calling code .
* /
getDetachedAttributes : function ( _attrs ) {
_attrs . push ( "value" ) ;
} ,
/ * *
* Returns an array of DOM nodes . The ( relativly ) same DOM - Nodes have to be
* passed to the "setDetachedAttributes" function in the same order .
* /
getDetachedNodes : function ( ) {
return [ this . duration [ 0 ] , this . format [ 0 ] ] ;
} ,
/ * *
* Sets the given associative attribute - > value array and applies the
* attributes to the given DOM - Node .
*
* @ param _nodes is an array of nodes which has to be in the same order as
* the nodes returned by "getDetachedNodes"
* @ param _values is an associative array which contains a subset of attributes
* returned by the "getDetachedAttributes" function and sets them to the
* given values .
* /
setDetachedAttributes : function ( _nodes , _values ) {
for ( var i = 0 ; i < _nodes . length ; i ++ ) {
// Clear the node
2011-09-13 19:37:29 +02:00
for ( var j = _nodes [ i ] . childNodes . length - 1 ; j >= 0 ; j -- )
2011-09-08 19:11:49 +02:00
{
2011-09-13 19:37:29 +02:00
_nodes [ i ] . removeChild ( _nodes [ i ] . childNodes [ j ] ) ;
2011-09-08 19:11:49 +02:00
}
}
2012-03-13 20:38:48 +01:00
if ( typeof _values . value !== 'undefined' )
2012-04-04 22:05:53 +02:00
{
_values . value = parseFloat ( _values . value ) ;
}
if ( _values . value )
2012-03-13 20:38:48 +01:00
{
var display = this . _convert _to _display ( _values . value ) ;
_nodes [ 0 ] . appendChild ( document . createTextNode ( display . value ) ) ;
_nodes [ 1 ] . appendChild ( document . createTextNode ( display . unit ) ) ;
}
2011-09-08 19:11:49 +02:00
}
} ) ;
et2 _register _widget ( et2 _date _duration _ro , [ "date-duration_ro" ] ) ;
2011-08-22 20:18:29 +02:00
/ * *
2011-08-31 22:32:24 +02:00
* et2 _date _ro is the readonly implementation of some date widget .
2013-04-13 21:00:13 +02:00
* @ augments et2 _valueWidget
2011-08-22 20:18:29 +02:00
* /
2013-04-13 21:00:13 +02:00
var et2 _date _ro = et2 _valueWidget . extend ( [ et2 _IDetachedDOM ] ,
{
2011-08-22 20:18:29 +02:00
/ * *
* Ignore all more advanced attributes .
* /
attributes : {
"value" : {
"type" : "integer"
} ,
"type" : {
"ignore" : false
2013-02-11 09:08:28 +01:00
} ,
"data_format" : {
"ignore" : true ,
"description" : "Format data is in. This is not used client-side because it's always a timestamp client side."
2011-08-22 20:18:29 +02:00
}
} ,
2013-02-11 09:08:28 +01:00
legacyOptions : [ "data_format" ] ,
2011-08-22 20:18:29 +02:00
/ * *
* Internal container for working easily with dates
* /
date : new Date ( ) ,
2013-04-13 21:00:13 +02:00
/ * *
* Constructor
*
* @ memberOf et2 _date _ro
* /
2011-08-22 20:18:29 +02:00
init : function ( ) {
this . _super . apply ( this , arguments ) ;
this . value = "" ;
2013-02-08 12:10:45 +01:00
this . span = $j ( document . createElement ( this . _type == "date-since" || this . _type == "date-time_today" ? "span" : "time" ) )
2011-08-29 23:15:53 +02:00
. addClass ( "et2_date_ro et2_label" ) ;
2011-08-22 20:18:29 +02:00
this . setDOMNode ( this . span [ 0 ] ) ;
} ,
set _value : function ( _value ) {
2011-09-12 17:21:42 +02:00
if ( typeof _value == 'undefined' ) _value = 0 ;
2011-08-22 20:18:29 +02:00
this . value = _value ;
2011-09-12 17:21:42 +02:00
2011-10-19 17:15:54 +02:00
if ( _value == 0 || _value == null )
2011-09-12 17:21:42 +02:00
{
2012-03-22 16:56:16 +01:00
this . span . attr ( "datetime" , "" ) . text ( "" ) ;
2011-09-12 17:21:42 +02:00
return ;
}
2012-03-08 01:20:04 +01:00
if ( typeof _value == 'string' && isNaN ( _value ) )
{
var text = new Date ( _value ) ;
// Handle timezone offset - times are already in user time, but parse does UTC
var localOffset = text . getTimezoneOffset ( ) * 60000 ;
// JS dates use milliseconds
this . date . setTime ( text . valueOf ( ) + localOffset ) ;
}
else
{
// JS dates use milliseconds
this . date . setTime ( parseInt ( _value ) * 1000 ) ;
}
2011-08-22 20:18:29 +02:00
var display = this . date . toString ( ) ;
2011-09-14 02:04:06 +02:00
switch ( this . _type ) {
2013-02-08 12:10:45 +01:00
case "date-time_today" :
// Today - just the time
if ( this . date . toDateString ( ) == new Date ( ) . toDateString ( ) )
{
display = date ( this . egw ( ) . preference ( 'timeformat' ) == '24' ? 'H:i' : 'g:i a' , this . date ) ;
}
// Before today - just the date
else
{
display = date ( this . egw ( ) . preference ( 'dateformat' ) , this . date ) ;
}
break ;
2011-08-22 20:18:29 +02:00
case "date" :
2012-03-02 11:44:56 +01:00
display = date ( this . egw ( ) . preference ( 'dateformat' ) , this . date ) ;
2011-08-22 20:18:29 +02:00
break ;
2011-08-29 23:15:53 +02:00
case "date-timeonly" :
2012-03-02 11:44:56 +01:00
display = date ( this . egw ( ) . preference ( 'timeformat' ) == '24' ? 'H:i' : 'g:i a' , this . date ) ;
2011-08-22 20:18:29 +02:00
break ;
case "date-time" :
2012-03-02 11:44:56 +01:00
display = date ( this . egw ( ) . preference ( 'dateformat' ) + " " +
( this . egw ( ) . preference ( 'timeformat' ) == '24' ? 'H:i' : 'g:i a' ) , this . date ) ;
2011-08-30 22:50:55 +02:00
break ;
case "date-since" :
var unit2label = {
'Y' : 'years' ,
'm' : 'month' ,
'd' : 'days' ,
'H' : 'hours' ,
'i' : 'minutes' ,
2011-09-02 18:23:26 +02:00
's' : 'seconds'
2011-08-30 22:50:55 +02:00
} ;
var unit2s = {
'Y' : 31536000 ,
'm' : 2628000 ,
'd' : 86400 ,
'H' : 3600 ,
'i' : 60 ,
2011-09-02 18:23:26 +02:00
's' : 1
2011-08-30 22:50:55 +02:00
} ;
var d = new Date ( ) ;
var diff = Math . round ( d . valueOf ( ) / 1000 ) - Math . round ( this . date . valueOf ( ) / 1000 ) ;
display = '' ;
for ( var unit in unit2s )
{
var unit _s = unit2s [ unit ] ;
if ( diff >= unit _s || unit == 's' )
{
2012-03-02 11:44:56 +01:00
display = Math . round ( diff / unit _s , 1 ) + ' ' + this . egw ( ) . lang ( unit2label [ unit ] ) ;
2011-08-30 22:50:55 +02:00
break ;
}
}
2013-04-13 21:00:13 +02:00
break ;
2011-08-22 20:18:29 +02:00
}
2011-08-29 23:15:53 +02:00
this . span . attr ( "datetime" , date ( "Y-m-d H:i:s" , this . date ) ) . text ( display ) ;
2011-09-12 17:21:42 +02:00
} ,
/ * *
* Creates a list of attributes which can be set when working in the
* "detached" mode . The result is stored in the _attrs array which is provided
* by the calling code .
* /
getDetachedAttributes : function ( _attrs ) {
2011-09-14 22:52:59 +02:00
_attrs . push ( "value" , "class" ) ;
2011-09-12 17:21:42 +02:00
} ,
/ * *
* Returns an array of DOM nodes . The ( relatively ) same DOM - Nodes have to be
* passed to the "setDetachedAttributes" function in the same order .
* /
getDetachedNodes : function ( ) {
return [ this . span [ 0 ] ] ;
} ,
/ * *
* Sets the given associative attribute - > value array and applies the
* attributes to the given DOM - Node .
*
* @ param _nodes is an array of nodes which have to be in the same order as
* the nodes returned by "getDetachedNodes"
* @ param _values is an associative array which contains a subset of attributes
* returned by the "getDetachedAttributes" function and sets them to the
* given values .
* /
setDetachedAttributes : function ( _nodes , _values ) {
this . span = jQuery ( _nodes [ 0 ] ) ;
this . set _value ( _values [ "value" ] ) ;
2011-09-14 22:52:59 +02:00
if ( _values [ "class" ] )
{
this . span . addClass ( _values [ "class" ] ) ;
}
2011-08-22 20:18:29 +02:00
}
} ) ;
2013-02-08 12:10:45 +01:00
et2 _register _widget ( et2 _date _ro , [ "date_ro" , "date-time_ro" , "date-since" , "date-time_today" ] ) ;
2011-08-30 22:50:55 +02:00
2013-04-13 21:00:13 +02:00
/ * *
* @ augments et2 _date _ro
* /
var et2 _date _timeonly _ro = et2 _date _ro . extend (
{
2011-08-31 22:32:24 +02:00
attributes : {
"value" : {
"type" : "string"
}
} ,
2013-04-13 21:00:13 +02:00
/ * *
* Construtor
*
* @ param _value
* @ memberOf et2 _date _timeonly _ro
* /
2011-08-31 22:32:24 +02:00
set _value : function ( _value ) {
2012-03-02 11:44:56 +01:00
if ( this . egw ( ) . preference ( "timeformat" ) == "12" && _value . indexOf ( ":" ) > 0 ) {
2011-08-31 22:32:24 +02:00
var parts = _value . split ( ":" ) ;
if ( parts [ 0 ] >= 12 ) {
this . span . text ( ( parts [ 0 ] == "12" ? "12" : parseInt ( parts [ 0 ] ) - 12 ) + ":" + parts [ 1 ] + " pm" ) ;
}
else
{
this . span . text ( _value + " am" ) ;
}
}
else
{
this . span . text ( _value ) ;
}
}
} ) ;
et2 _register _widget ( et2 _date _timeonly _ro , [ "date-timeonly_ro" ] ) ;