2011-09-08 01:32:24 +02:00
/ * *
* eGroupWare eTemplate2 - JS Link object
*
* @ 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 2011 Nathan Gray
* @ version $Id$
* /
"use strict" ;
/ * e g w : u s e s
jquery . jquery ;
jquery . jquery - ui ;
et2 _core _inputWidget ;
et2 _core _valueWidget ;
* /
/ * *
* UI widgets for Egroupware linking system
* /
var et2 _link _to = et2 _inputWidget . extend ( {
attributes : {
"application" : {
"name" : "Application" ,
"type" : "string" ,
"default" : egw _getAppName ( ) ,
"description" : "Limit to the listed application or applications (comma seperated)"
} ,
"blur" : {
"name" : "Placeholder" ,
"type" : "string" ,
"default" : "" ,
"description" : "This text get displayed if an input-field is empty and does not have the input-focus (blur). It can be used to show a default value or a kind of help-text."
} ,
} ,
search _timeout : 200 , //ms after change to send query
minimum _characters : 2 , // Don't send query unless there's at least this many chars
init : function ( ) {
this . _super . apply ( this , arguments ) ;
this . div = null ;
this . search = null ;
this . app _select = null ;
2011-09-09 02:05:46 +02:00
this . link _button = null ;
this . status _span = null ;
this . comment = null ;
this . file _upload = null ;
2011-09-08 01:32:24 +02:00
this . cache = { } ;
this . createInputWidget ( ) ;
} ,
destroy : function ( ) {
this . _super . apply ( this , arguments ) ;
this . div = null ;
this . search . autocomplete ( "destroy" ) ;
this . search = null ;
this . app _select = null ;
2011-09-09 02:05:46 +02:00
this . link _button = null ;
this . status _span = null ;
this . comment = null ;
this . file _upload . destroy ( ) ;
this . file _upload = null ;
} ,
2011-09-08 01:32:24 +02:00
2011-09-09 02:05:46 +02:00
/ * *
* Override to provide proper node for file widget to go in
* /
getDOMNode : function ( _sender ) {
if ( _sender == this ) {
return this . div [ 0 ] ;
} else {
return this . file _div [ 0 ] ;
}
2011-09-08 01:32:24 +02:00
} ,
createInputWidget : function ( ) {
var self = this ;
2011-09-09 02:05:46 +02:00
this . div = $j ( document . createElement ( "div" ) ) . addClass ( "et2_link_to" ) ;
2011-09-08 01:32:24 +02:00
// Application selection
this . app _select = $j ( document . createElement ( "select" ) ) . appendTo ( this . div )
. change ( function ( e ) {
self . cache = { } ; // Clear cache when app changes
2011-09-09 02:05:46 +02:00
self . options . value . app = self . app _select . val ( ) ;
} )
. css ( "width" , "39%" ) ;
2011-09-08 01:32:24 +02:00
for ( var key in this . options . select _options ) {
var option = $j ( document . createElement ( "option" ) )
. attr ( "value" , key )
. text ( this . options . select _options [ key ] ) ;
option . appendTo ( this . app _select ) ;
}
self . options . value . app = this . app _select . val ( ) ;
// Search input
2011-09-09 02:05:46 +02:00
this . search = $j ( document . createElement ( "input" ) ) . attr ( "type" , "search" )
. css ( "width" , "50%" )
. appendTo ( this . div ) ;
2011-09-08 01:32:24 +02:00
// Link button
this . link _button = $j ( document . createElement ( "button" ) )
. text ( egw . lang ( "link" ) )
. appendTo ( this . div ) . hide ( )
. click ( this , this . createLink ) ;
2011-09-09 02:05:46 +02:00
this . set _blur ( this . options . blur ? this . options . blur : egw . lang ( "search" ) , this . search ) ;
// Span for indicating status
this . status _span = $j ( document . createElement ( "span" ) )
. appendTo ( this . div ) . addClass ( "status" ) . hide ( ) ;
// Link comment field
this . comment = $j ( document . createElement ( "input" ) )
. css ( "display" , "block" ) . css ( "width" , "89%" )
. appendTo ( this . div ) . hide ( ) ;
this . set _blur ( egw . lang ( "Comment..." ) , this . comment ) ;
2011-09-08 01:32:24 +02:00
2011-09-09 02:05:46 +02:00
// Autocomplete
2011-09-08 01:32:24 +02:00
this . search . autocomplete ( {
source : function ( request , response ) { return self . query ( request , response ) ; } ,
select : function ( event , item ) { event . data = self ; self . select ( event , item ) ; return false ; } ,
focus : function ( event , item ) {
event . stopPropagation ( ) ;
self . search . val ( item . item . label ) ;
return false ;
} ,
minLength : self . minimum _characters ,
disabled : self . options . disabled
} ) ;
2011-09-09 02:05:46 +02:00
// Need a div for file upload widget
this . file _div = $j ( document . createElement ( "div" ) ) . appendTo ( this . div ) ;
2011-09-08 01:32:24 +02:00
this . setDOMNode ( this . div [ 0 ] ) ;
} ,
2011-09-09 02:05:46 +02:00
doLoadingFinished : function ( ) {
this . _super . apply ( this , arguments ) ;
var self = this ;
// File upload
var file _attrs = {
multiple : true ,
id : this . id + '_file' ,
onFinish : function ( event , file _count ) {
self . filesUploaded ( event ) ;
}
} ;
this . file _upload = et2 _createWidget ( "file" , file _attrs , this ) ;
return true ;
} ,
2011-09-08 01:32:24 +02:00
transformAttributes : function ( _attrs ) {
this . _super . apply ( this , arguments ) ;
2011-09-08 14:24:53 +02:00
_attrs [ "select_options" ] = egw . link _app _list ( 'query' ) ;
2011-09-08 01:32:24 +02:00
// Check whether the options entry was found, if not read it from the
// content array.
if ( _attrs [ "select_options" ] == null )
{
_attrs [ "select_options" ] = this . getArrayMgr ( 'content' )
2011-09-08 20:36:09 +02:00
. getEntry ( "options-" + this . id )
2011-09-08 01:32:24 +02:00
}
// Default to an empty object
if ( _attrs [ "select_options" ] == null )
{
_attrs [ "select_options" ] = { } ;
}
} ,
getValue : function ( ) {
return this . options . value ;
} ,
2011-09-09 02:05:46 +02:00
set _blur : function ( _value , input ) {
if ( typeof input == 'undefined' ) input = this . search ;
2011-09-08 01:32:24 +02:00
if ( _value ) {
2011-09-09 02:05:46 +02:00
input . attr ( "placeholder" , _value ) ; // HTML5
if ( ! input [ 0 ] . placeholder ) {
2011-09-08 01:32:24 +02:00
// Not HTML5
2011-09-09 02:05:46 +02:00
if ( input . val ( ) == "" ) input . val ( this . options . blur ) ;
input . focus ( input , function ( e ) {
var placeholder = _value ;
if ( e . data . val ( ) == placeholder ) e . data . val ( "" ) ;
} ) . blur ( input , function ( e ) {
var placeholder = _value ;
if ( e . data . val ( ) == "" ) e . data . val ( placeholder ) ;
2011-09-08 01:32:24 +02:00
} ) ;
2011-09-09 02:05:46 +02:00
if ( input . val ( ) == "" ) input . val ( _value ) ;
2011-09-08 01:32:24 +02:00
}
} else {
this . search . removeAttr ( "placeholder" ) ;
}
} ,
/ * *
* Ask server for entries matching selected app / type and filtered by search string
* /
query : function ( request , response ) {
if ( request . term in this . cache ) {
return response ( this . cache [ request . term ] ) ;
}
this . search . addClass ( "loading" ) ;
this . link _button . hide ( ) ;
var request = new egw _json _request ( "etemplate_widget_link::ajax_link_search::etemplate" ,
[ this . app _select . val ( ) , '' , request . term ] ,
this
) ;
this . response = response ;
request . sendRequest ( true , this . _results , this ) ;
} ,
/ * *
* User selected a value
* /
select : function ( event , selected ) {
event . data . options . value . id = selected . item . value ;
event . data . search . val ( selected . item . label ) ;
event . data . link _button . show ( ) ;
2011-09-09 02:05:46 +02:00
event . data . comment . show ( ) ;
2011-09-08 01:32:24 +02:00
} ,
/ * *
* Server found some results
* /
_results : function ( data ) {
this . search . removeClass ( "loading" ) ;
var result = [ ] ;
for ( var id in data ) {
result . push ( { "value" : id , "label" : data [ id ] } ) ;
}
this . cache [ this . search . val ( ) ] = result ;
this . response ( result ) ;
} ,
2011-09-09 02:05:46 +02:00
/ * *
* User uploaded some files , and they ' re done
* /
filesUploaded : function ( event ) {
var self = this ;
this . link _button . show ( ) ;
this . comment . hide ( ) ;
// Add some comment fields
this . file _upload . progress . children ( ) . each ( function ( ) {
var comment = jQuery ( document . createElement ( "input" ) )
. appendTo ( this ) . hide ( ) ;
self . set _blur ( egw . lang ( "Comment..." ) , comment ) ;
var comment _icon = jQuery ( document . createElement ( "span" ) )
. appendTo ( this )
. addClass ( "ui-icon ui-icon-comment" )
. click ( function ( ) { comment _icon . hide ( ) ; comment . toggle ( ) ; } )
} ) ;
} ,
2011-09-08 01:32:24 +02:00
/ * *
* Create a link using the current internal values
* /
createLink : function ( event ) {
2011-09-09 02:05:46 +02:00
// Hide extra controls
event . data . link _button . attr ( "disabled" , true ) ;
var values = event . data . options . value ;
var self = event . data ;
var links = [ ] ;
// Links to other entries
if ( values . id ) {
links . push ( {
app : values . app ,
id : values . id ,
remark : self . comment . val ( )
} ) ;
self . comment . val ( "" ) ;
self . search . val ( "" ) ;
}
// Files
for ( var file in self . file _upload . options . value ) {
links . push ( {
app : 'file' ,
id : file ,
name : self . file _upload . options . value [ file ] . name ,
type : self . file _upload . options . value [ file ] . type ,
remark : jQuery ( "li[file='" + self . file _upload . options . value [ file ] . name + "'] > input" , self . file _upload . progress )
. filter ( function ( ) { return jQuery ( this ) . attr ( "placeholder" ) != jQuery ( this ) . val ( ) ; } ) . val ( )
} ) ;
}
var request = new egw _json _request ( "etemplate_widget_link::ajax_link::etemplate" ,
[ values . to _app , values . to _id , links ] ,
this
) ;
request . sendRequest ( true , self . _link _result , self ) ;
} ,
/ * *
* Sent some links , server has a result
* /
_link _result : function ( success ) {
if ( success ) {
this . comment . hide ( ) ;
this . link _button . hide ( ) . attr ( "disabled" , false ) ;
this . status _span . fadeIn ( ) . delay ( 1000 ) . fadeOut ( ) ;
delete this . options . value . app ;
delete this . options . value . id ;
for ( var file in this . file _upload . options . value ) {
delete this . file _upload . options . value [ file ] ;
}
this . file _upload . progress . empty ( ) ;
}
2011-09-08 01:32:24 +02:00
}
} ) ;
et2 _register _widget ( et2 _link _to , [ "link-to" ] ) ;