2012-02-06 10:32:57 +04:00
function ucwords ( str ) {
return ( str + '' ) . replace ( /^([a-z])|\s+([a-z])/g , function ( $1 ) {
return $1 . toUpperCase ( ) ;
} ) ;
}
String . prototype . strip _tags = function ( ) {
tags = this ;
2012-05-28 16:38:31 +04:00
stripped = tags . replace ( /<(.|\n)*?>/g , '' ) ;
2012-02-06 10:32:57 +04:00
return stripped ;
2012-02-22 01:31:35 +04:00
} ;
2012-02-06 10:32:57 +04:00
Contacts = {
UI : {
2012-07-15 12:29:03 +04:00
notify : function ( params ) {
$ ( '#notification' ) . text ( params . message ) ;
2012-04-27 12:34:45 +04:00
$ ( '#notification' ) . fadeIn ( ) ;
2012-07-15 12:29:03 +04:00
setTimeout ( function ( ) { $ ( '#notification' ) . fadeOut ( ) ; } , 10000 ) ;
2012-04-27 12:34:45 +04:00
} ,
2012-02-06 10:32:57 +04:00
notImplemented : function ( ) {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( t ( 'contacts' , 'Sorry, this functionality has not been implemented yet' ) , t ( 'contacts' , 'Not implemented' ) ) ;
2012-02-06 10:32:57 +04:00
} ,
searchOSM : function ( obj ) {
var adr = Contacts . UI . propertyContainerFor ( obj ) . find ( '.adr' ) . val ( ) ;
if ( adr == undefined ) {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( t ( 'contacts' , 'Couldn\'t get a valid address.' ) , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
return ;
}
// FIXME: I suck at regexp. /Tanghus
var adrarr = adr . split ( ';' ) ;
var adrstr = '' ;
if ( adrarr [ 2 ] . trim ( ) != '' ) {
adrstr = adrstr + adrarr [ 2 ] . trim ( ) + ',' ;
}
if ( adrarr [ 3 ] . trim ( ) != '' ) {
adrstr = adrstr + adrarr [ 3 ] . trim ( ) + ',' ;
}
if ( adrarr [ 4 ] . trim ( ) != '' ) {
adrstr = adrstr + adrarr [ 4 ] . trim ( ) + ',' ;
}
if ( adrarr [ 5 ] . trim ( ) != '' ) {
adrstr = adrstr + adrarr [ 5 ] . trim ( ) + ',' ;
}
if ( adrarr [ 6 ] . trim ( ) != '' ) {
adrstr = adrstr + adrarr [ 6 ] . trim ( ) ;
}
adrstr = encodeURIComponent ( adrstr ) ;
2012-06-27 13:46:42 +04:00
var uri = 'http://open.mapquestapi.com/nominatim/v1/search.php?q=' + adrstr + '&limit=10&addressdetails=1&polygon=1&zoom=' ;
2012-02-06 10:32:57 +04:00
var newWindow = window . open ( uri , '_blank' ) ;
newWindow . focus ( ) ;
} ,
mailTo : function ( obj ) {
var adr = Contacts . UI . propertyContainerFor ( $ ( obj ) ) . find ( 'input[type="email"]' ) . val ( ) . trim ( ) ;
if ( adr == '' ) {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( t ( 'contacts' , 'Please enter an email address.' ) , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
return ;
}
window . location . href = 'mailto:' + adr ;
} ,
propertyContainerFor : function ( obj ) {
return $ ( obj ) . parents ( '.propertycontainer' ) . first ( ) ;
} ,
checksumFor : function ( obj ) {
return $ ( obj ) . parents ( '.propertycontainer' ) . first ( ) . data ( 'checksum' ) ;
} ,
propertyTypeFor : function ( obj ) {
return $ ( obj ) . parents ( '.propertycontainer' ) . first ( ) . data ( 'element' ) ;
} ,
loading : function ( obj , state ) {
if ( state ) {
$ ( obj ) . addClass ( 'loading' ) ;
} else {
$ ( obj ) . removeClass ( 'loading' ) ;
}
} ,
showCardDAVUrl : function ( username , bookname ) {
2012-06-22 01:29:05 +04:00
$ ( '#carddav_url' ) . val ( totalurl + '/' + username + '/' + decodeURIComponent ( bookname ) ) ;
2012-02-06 10:32:57 +04:00
$ ( '#carddav_url' ) . show ( ) ;
$ ( '#carddav_url_close' ) . show ( ) ;
} ,
loadListHandlers : function ( ) {
2012-05-02 14:10:44 +04:00
$ ( '.propertylist li a.delete' ) . unbind ( 'click' ) ;
$ ( '.propertylist li a.delete' ) . unbind ( 'keydown' ) ;
var deleteItem = function ( obj ) {
obj . tipsy ( 'hide' ) ;
Contacts . UI . Card . deleteProperty ( obj , 'list' ) ;
}
$ ( '.propertylist li a.delete, .addresscard .delete' ) . click ( function ( ) { deleteItem ( $ ( this ) ) } ) ;
$ ( '.propertylist li a.delete, .addresscard .delete' ) . keydown ( function ( ) { deleteItem ( $ ( this ) ) } ) ;
$ ( '.propertylist li a.mail' ) . click ( function ( ) { Contacts . UI . mailTo ( this ) } ) ;
$ ( '.propertylist li a.mail' ) . keydown ( function ( ) { Contacts . UI . mailTo ( this ) } ) ;
$ ( '.addresscard .globe' ) . click ( function ( ) { $ ( this ) . tipsy ( 'hide' ) ; Contacts . UI . searchOSM ( this ) ; } ) ;
$ ( '.addresscard .globe' ) . keydown ( function ( ) { $ ( this ) . tipsy ( 'hide' ) ; Contacts . UI . searchOSM ( this ) ; } ) ;
$ ( '.addresscard .edit' ) . click ( function ( ) { $ ( this ) . tipsy ( 'hide' ) ; Contacts . UI . Card . editAddress ( this , false ) ; } ) ;
$ ( '.addresscard .edit' ) . keydown ( function ( ) { $ ( this ) . tipsy ( 'hide' ) ; Contacts . UI . Card . editAddress ( this , false ) ; } ) ;
2012-04-22 22:44:11 +04:00
$ ( '.addresscard,.propertylist li,.propertycontainer' ) . hover (
function ( ) {
$ ( this ) . find ( '.globe,.mail,.delete,.edit' ) . animate ( { opacity : 1.0 } , 200 , function ( ) { } ) ;
} ,
function ( ) {
$ ( this ) . find ( '.globe,.mail,.delete,.edit' ) . animate ( { opacity : 0.1 } , 200 , function ( ) { } ) ;
}
2012-02-06 10:32:57 +04:00
) ;
} ,
loadHandlers : function ( ) {
2012-05-02 14:10:44 +04:00
var deleteItem = function ( obj ) {
obj . tipsy ( 'hide' ) ;
Contacts . UI . Card . deleteProperty ( obj , 'single' ) ;
}
2012-06-06 17:28:39 +04:00
var goToUrl = function ( obj ) {
var url = Contacts . UI . propertyContainerFor ( obj ) . find ( '#url' ) . val ( ) ;
if ( url != '' ) {
var newWindow = window . open ( url , '_blank' ) ;
newWindow . focus ( ) ;
}
}
2012-05-02 14:10:44 +04:00
$ ( '#identityprops a.delete' ) . click ( function ( ) { deleteItem ( $ ( this ) ) } ) ;
$ ( '#identityprops a.delete' ) . keydown ( function ( ) { deleteItem ( $ ( this ) ) } ) ;
$ ( '#categories_value a.edit' ) . click ( function ( ) { $ ( this ) . tipsy ( 'hide' ) ; OCCategories . edit ( ) ; } ) ;
$ ( '#categories_value a.edit' ) . keydown ( function ( ) { $ ( this ) . tipsy ( 'hide' ) ; OCCategories . edit ( ) ; } ) ;
2012-06-06 17:28:39 +04:00
$ ( '#url_value a.globe' ) . click ( function ( ) { $ ( this ) . tipsy ( 'hide' ) ; goToUrl ( $ ( this ) ) ; } ) ;
$ ( '#url_value a.globe' ) . keydown ( function ( ) { $ ( this ) . tipsy ( 'hide' ) ; goToUrl ( $ ( this ) ) ; } ) ;
2012-02-10 19:40:40 +04:00
$ ( '#fn_select' ) . combobox ( {
'id' : 'fn' ,
'name' : 'value' ,
2012-05-10 22:43:40 +04:00
'classes' : [ 'contacts_property' , 'nonempty' , 'huge' , 'tip' , 'float' ] ,
2012-05-12 21:52:16 +04:00
'attributes' : { 'placeholder' : t ( 'contacts' , 'Enter name' ) } ,
'title' : t ( 'contacts' , 'Format custom, Short name, Full name, Reverse or Reverse with comma' ) } ) ;
2012-02-06 10:32:57 +04:00
$ ( '#bday' ) . datepicker ( {
dateFormat : 'dd-mm-yy'
} ) ;
// Style phone types
2012-03-06 01:02:44 +04:00
$ ( '#phonelist' ) . find ( 'select.contacts_property' ) . multiselect ( {
2012-02-06 10:32:57 +04:00
noneSelectedText : t ( 'contacts' , 'Select type' ) ,
header : false ,
selectedList : 4 ,
classes : 'typelist'
} ) ;
2012-05-02 14:10:44 +04:00
$ ( '#edit_name' ) . click ( function ( ) { Contacts . UI . Card . editName ( ) } ) ;
$ ( '#edit_name' ) . keydown ( function ( ) { Contacts . UI . Card . editName ( ) } ) ;
2012-02-06 10:32:57 +04:00
2012-06-28 03:45:59 +04:00
$ ( '#phototools li a' ) . click ( function ( ) {
$ ( this ) . tipsy ( 'hide' ) ;
} ) ;
$ ( '#contacts_details_photo_wrapper' ) . hover (
function ( ) {
$ ( '#phototools' ) . slideDown ( 200 ) ;
} ,
function ( ) {
$ ( '#phototools' ) . slideUp ( 200 ) ;
}
) ;
$ ( '#phototools' ) . hover (
function ( ) {
$ ( this ) . removeClass ( 'transparent' ) ;
} ,
function ( ) {
$ ( this ) . addClass ( 'transparent' ) ;
}
) ;
$ ( '#phototools .upload' ) . click ( function ( ) {
$ ( '#file_upload_start' ) . trigger ( 'click' ) ;
} ) ;
$ ( '#phototools .cloud' ) . click ( function ( ) {
OC . dialogs . filepicker ( t ( 'contacts' , 'Select photo' ) , Contacts . UI . Card . cloudPhotoSelected , false , 'image' , true ) ;
} ) ;
2012-02-06 10:32:57 +04:00
/* Initialize the photo edit dialog */
2012-06-04 14:52:04 +04:00
$ ( '#edit_photo_dialog' ) . dialog ( {
2012-06-06 01:58:46 +04:00
autoOpen : false , modal : true , height : 'auto' , width : 'auto'
2012-06-04 14:52:04 +04:00
} ) ;
2012-02-06 10:32:57 +04:00
$ ( '#edit_photo_dialog' ) . dialog ( 'option' , 'buttons' , [
{
text : "Ok" ,
click : function ( ) {
Contacts . UI . Card . savePhoto ( this ) ;
$ ( this ) . dialog ( 'close' ) ;
}
} ,
{
text : "Cancel" ,
click : function ( ) { $ ( this ) . dialog ( 'close' ) ; }
}
] ) ;
2012-05-14 13:05:50 +04:00
2012-05-10 18:53:25 +04:00
// Name has changed. Update it and reorder.
$ ( '#fn' ) . change ( function ( ) {
2012-05-28 16:38:31 +04:00
var name = $ ( '#fn' ) . val ( ) . strip _tags ( ) ;
2012-06-20 23:00:07 +04:00
var item = $ ( '.contacts li[data-id="' + Contacts . UI . Card . id + '"]' ) ;
2012-05-14 19:40:16 +04:00
$ ( item ) . find ( 'a' ) . html ( name ) ;
2012-06-04 14:56:48 +04:00
Contacts . UI . Card . fn = name ;
2012-05-10 18:53:25 +04:00
var added = false ;
2012-06-18 01:14:02 +04:00
$ ( '.contacts li[data-bookid="' + Contacts . UI . Card . bookid + '"]' ) . each ( function ( ) {
2012-05-10 18:53:25 +04:00
if ( $ ( this ) . text ( ) . toLowerCase ( ) > name . toLowerCase ( ) ) {
$ ( this ) . before ( item ) . fadeIn ( 'fast' ) ;
added = true ;
return false ;
}
} ) ;
if ( ! added ) {
2012-06-20 23:00:07 +04:00
$ ( '#contacts ul[data-id="' + Contacts . UI . Card . bookid + '"]' ) . append ( item ) ;
2012-05-10 18:53:25 +04:00
}
2012-05-14 13:05:50 +04:00
Contacts . UI . Contacts . scrollTo ( Contacts . UI . Card . id ) ;
2012-05-10 18:53:25 +04:00
} ) ;
2012-05-10 22:43:40 +04:00
$ ( '#contacts_deletecard' ) . click ( function ( ) { Contacts . UI . Card . doDelete ( ) ; return false ; } ) ;
$ ( '#contacts_deletecard' ) . keydown ( function ( event ) {
2012-07-15 06:15:57 +04:00
if ( event . which == 13 || event . which == 32 ) {
2012-05-10 22:43:40 +04:00
Contacts . UI . Card . doDelete ( ) ;
}
return false ;
} ) ;
$ ( '#contacts_downloadcard' ) . click ( function ( ) { Contacts . UI . Card . doExport ( ) ; return false ; } ) ;
$ ( '#contacts_downloadcard' ) . keydown ( function ( event ) {
2012-07-15 06:15:57 +04:00
if ( event . which == 13 || event . which == 32 ) {
2012-05-10 22:43:40 +04:00
Contacts . UI . Card . doExport ( ) ;
}
return false ;
} ) ;
2012-05-14 13:05:50 +04:00
// Profile picture upload handling
// New profile picture selected
$ ( '#file_upload_start' ) . change ( function ( ) {
Contacts . UI . Card . uploadPhoto ( this . files ) ;
} ) ;
$ ( '#contacts_details_photo_wrapper' ) . bind ( 'dragover' , function ( event ) {
$ ( event . target ) . addClass ( 'droppable' ) ;
event . stopPropagation ( ) ;
event . preventDefault ( ) ;
} ) ;
$ ( '#contacts_details_photo_wrapper' ) . bind ( 'dragleave' , function ( event ) {
$ ( event . target ) . removeClass ( 'droppable' ) ;
//event.stopPropagation();
//event.preventDefault();
} ) ;
$ ( '#contacts_details_photo_wrapper' ) . bind ( 'drop' , function ( event ) {
event . stopPropagation ( ) ;
event . preventDefault ( ) ;
$ ( event . target ) . removeClass ( 'droppable' ) ;
$ . fileUpload ( event . originalEvent . dataTransfer . files ) ;
} ) ;
2012-03-12 17:12:27 +04:00
$ ( '#categories' ) . multiple _autocomplete ( { source : categories } ) ;
2012-04-03 05:28:12 +04:00
$ ( '#contacts_deletecard' ) . tipsy ( { gravity : 'ne' } ) ;
$ ( '#contacts_downloadcard' ) . tipsy ( { gravity : 'ne' } ) ;
2012-05-03 17:21:29 +04:00
$ ( '#contacts_propertymenu_button' ) . tipsy ( ) ;
2012-07-14 16:25:25 +04:00
$ ( '#contacts_newcontact, #contacts_import, #chooseaddressbook' ) . tipsy ( { gravity : 'sw' } ) ;
2012-06-27 04:10:50 +04:00
$ ( 'body' ) . click ( function ( e ) {
if ( ! $ ( e . target ) . is ( '#contacts_propertymenu_button' ) ) {
$ ( '#contacts_propertymenu_dropdown' ) . hide ( ) ;
}
} ) ;
function propertyMenu ( ) {
var menu = $ ( '#contacts_propertymenu_dropdown' ) ;
if ( menu . is ( ':hidden' ) ) {
menu . show ( ) ;
menu . find ( 'li' ) . first ( ) . focus ( ) ;
} else {
menu . hide ( ) ;
}
}
$ ( '#contacts_propertymenu_button' ) . click ( propertyMenu ) ;
$ ( '#contacts_propertymenu_button' ) . keydown ( propertyMenu ) ;
function propertyMenuItem ( ) {
var type = $ ( this ) . data ( 'type' ) ;
Contacts . UI . Card . addProperty ( type ) ;
$ ( '#contacts_propertymenu_dropdown' ) . hide ( ) ;
}
$ ( '#contacts_propertymenu_dropdown a' ) . click ( propertyMenuItem ) ;
$ ( '#contacts_propertymenu_dropdown a' ) . keydown ( propertyMenuItem ) ;
2012-02-06 10:32:57 +04:00
} ,
Card : {
id : '' ,
fn : '' ,
fullname : '' ,
shortname : '' ,
famname : '' ,
givname : '' ,
addname : '' ,
honpre : '' ,
honsuf : '' ,
data : undefined ,
2012-07-15 06:15:57 +04:00
update : function ( params ) { // params {cid:int, aid:int}
if ( ! params ) { params = { } ; }
2012-07-15 23:58:45 +04:00
$ ( '#contacts li,#contacts h3' ) . removeClass ( 'active' ) ;
2012-07-15 06:15:57 +04:00
console . log ( 'Card, cid: ' + params . cid + ' aid: ' + params . aid ) ;
var newid , bookid , firstitem ;
if ( ! parseInt ( params . cid ) && ! parseInt ( params . aid ) ) {
2012-06-28 00:43:57 +04:00
firstitem = $ ( '#contacts ul' ) . first ( ) . find ( 'li:first-child' ) ;
2012-06-20 23:00:07 +04:00
if ( firstitem . length > 0 ) {
2012-07-15 06:15:57 +04:00
newid = parseInt ( firstitem . data ( 'id' ) ) ;
bookid = parseInt ( firstitem . data ( 'bookid' ) ) ;
2012-06-20 23:00:07 +04:00
}
2012-07-15 06:15:57 +04:00
} else if ( ! parseInt ( params . cid ) && parseInt ( params . aid ) ) {
bookid = parseInt ( params . aid ) ;
newid = parseInt ( $ ( '#contacts' ) . find ( 'li[data-bookid="' + bookid + '"]' ) . first ( ) . data ( 'id' ) ) ;
} else if ( parseInt ( params . cid ) && ! parseInt ( params . aid ) ) {
newid = parseInt ( params . cid ) ;
bookid = parseInt ( $ ( '#contacts li[data-id="' + newid + '"]' ) . data ( 'bookid' ) ) ;
2012-03-29 17:24:32 +04:00
} else {
2012-07-15 06:15:57 +04:00
newid = parseInt ( params . cid ) ;
bookid = parseInt ( params . aid ) ;
2012-03-29 17:24:32 +04:00
}
2012-07-15 06:15:57 +04:00
if ( ! bookid || ! newid ) {
bookid = parseInt ( $ ( '#contacts h3' ) . first ( ) . data ( 'id' ) ) ;
newid = parseInt ( $ ( '#contacts' ) . find ( 'li[data-bookid="' + bookid + '"]' ) . first ( ) . data ( 'id' ) ) ;
2012-06-26 20:14:59 +04:00
}
2012-07-15 06:15:57 +04:00
console . log ( 'newid: ' + newid + ' bookid: ' + bookid ) ;
2012-06-18 04:21:29 +04:00
var localLoadContact = function ( newid , bookid ) {
2012-06-17 01:37:24 +04:00
if ( $ ( '.contacts li' ) . length > 0 ) {
2012-07-15 23:58:45 +04:00
$ ( '#contacts li[data-id="' + newid + '"],#contacts h3[data-id="' + bookid + '"]' ) . addClass ( 'active' ) ;
2012-05-14 12:12:36 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'contactdetails.php' ) , { 'id' : newid } , function ( jsondata ) {
if ( jsondata . status == 'success' ) {
2012-07-09 21:32:37 +04:00
$ ( '#contacts ul[data-id="' + bookid + '"]' ) . slideDown ( 300 ) ;
2012-06-18 01:14:02 +04:00
Contacts . UI . Card . loadContact ( jsondata . data , bookid ) ;
2012-05-14 12:12:36 +04:00
} else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
}
} ) ;
}
}
2012-05-07 00:24:36 +04:00
// Make sure proper DOM is loaded.
2012-07-09 21:32:37 +04:00
if ( ! $ ( '#card' ) . length && newid ) {
console . log ( 'Loading card DOM' ) ;
2012-07-06 04:46:43 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'loadcard.php' ) , { requesttoken : requesttoken } , function ( jsondata ) {
2012-02-14 16:57:11 +04:00
if ( jsondata . status == 'success' ) {
2012-05-14 12:12:36 +04:00
$ ( '#rightcontent' ) . html ( jsondata . data . page ) . ready ( function ( ) {
Contacts . UI . loadHandlers ( ) ;
2012-06-18 01:14:02 +04:00
localLoadContact ( newid , bookid ) ;
2012-05-14 12:12:36 +04:00
} ) ;
2012-05-03 13:11:26 +04:00
} else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-02-14 16:57:11 +04:00
}
} ) ;
}
2012-06-20 23:00:07 +04:00
else if ( ! newid ) {
2012-07-09 21:32:37 +04:00
console . log ( 'Loading intro' ) ;
2012-02-14 16:57:11 +04:00
// load intro page
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'loadintro.php' ) , { } , function ( jsondata ) {
if ( jsondata . status == 'success' ) {
id = '' ;
$ ( '#rightcontent' ) . data ( 'id' , '' ) ;
$ ( '#rightcontent' ) . html ( jsondata . data . page ) ;
} else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-02-14 16:57:11 +04:00
}
} ) ;
}
2012-05-14 19:48:58 +04:00
else {
2012-06-18 04:21:29 +04:00
localLoadContact ( newid , bookid ) ;
2012-05-14 19:48:58 +04:00
}
2012-02-14 16:57:11 +04:00
} ,
2012-04-10 23:53:36 +04:00
doExport : function ( ) {
2012-02-06 10:32:57 +04:00
document . location . href = OC . linkTo ( 'contacts' , 'export.php' ) + '?contactid=' + this . id ;
} ,
2012-05-10 18:53:25 +04:00
editNew : function ( ) { // add a new contact
this . id = '' ; this . fn = '' ; this . fullname = '' ; this . givname = '' ; this . famname = '' ; this . addname = '' ; this . honpre = '' ; this . honsuf = '' ;
//Contacts.UI.Card.add(t('contacts', 'Contact')+';'+t('contacts', 'New')+';;;', t('contacts', 'New Contact'), '', true);
2012-06-24 18:07:18 +04:00
Contacts . UI . Card . add ( ';;;;;' , '' , '' , true ) ;
2012-05-10 18:53:25 +04:00
return false ;
} ,
2012-06-28 00:43:57 +04:00
createEntry : function ( data ) {
return $ ( '<li data-id="' + data . id + '" data-bookid="' + data . addressbookid + '" role="button"><a href="' + OC . linkTo ( 'contacts' , 'index.php' ) + '&id=' + data . id + '" style="background: url(' + OC . filePath ( 'contacts' , '' , 'thumbnail.php' ) + '?id=' + data . id + ') no-repeat scroll 0% 0% transparent;">' + data . displayname + '</a></li>' ) ;
} ,
2012-04-03 05:28:12 +04:00
add : function ( n , fn , aid , isnew ) { // add a new contact
2012-07-09 21:32:37 +04:00
console . log ( 'Adding ' + fn ) ;
2012-06-24 18:03:10 +04:00
aid = aid ? aid : $ ( '#contacts h3.active' ) . first ( ) . data ( 'id' ) ;
2012-05-14 12:12:36 +04:00
var localAddcontact = function ( n , fn , aid , isnew ) {
$ . post ( OC . filePath ( 'contacts' , 'ajax' , 'addcontact.php' ) , { n : n , fn : fn , aid : aid , isnew : isnew } ,
function ( jsondata ) {
if ( jsondata . status == 'success' ) {
$ ( '#rightcontent' ) . data ( 'id' , jsondata . data . id ) ;
var id = jsondata . data . id ;
2012-06-20 23:00:07 +04:00
var aid = jsondata . data . aid ;
2012-05-14 12:12:36 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'contactdetails.php' ) , { 'id' : id } , function ( jsondata ) {
if ( jsondata . status == 'success' ) {
2012-06-18 04:21:29 +04:00
Contacts . UI . Card . loadContact ( jsondata . data , aid ) ;
2012-06-20 23:00:07 +04:00
$ ( '#contacts .active' ) . removeClass ( 'active' ) ;
2012-05-14 19:40:16 +04:00
var item = $ ( '<li data-id="' + jsondata . data . id + '" class="active"><a href="index.php?id=' + jsondata . data . id + '" style="background: url(' + OC . filePath ( 'contacts' , '' , 'thumbnail.php' ) + '?id=' + jsondata . data . id + ') no-repeat scroll 0% 0% transparent;">' + Contacts . UI . Card . fn + '</a></li>' ) ;
2012-05-14 12:12:36 +04:00
var added = false ;
2012-06-20 23:00:07 +04:00
$ ( '#contacts ul[data-id="' + aid + '"] li' ) . each ( function ( ) {
2012-05-14 12:12:36 +04:00
if ( $ ( this ) . text ( ) . toLowerCase ( ) > Contacts . UI . Card . fn . toLowerCase ( ) ) {
$ ( this ) . before ( item ) . fadeIn ( 'fast' ) ;
added = true ;
return false ;
}
} ) ;
if ( ! added ) {
2012-06-20 23:00:07 +04:00
$ ( '#contacts ul[data-id="' + aid + '"]' ) . append ( item ) ;
2012-05-14 12:12:36 +04:00
}
if ( isnew ) { // add some default properties
Contacts . UI . Card . addProperty ( 'EMAIL' ) ;
Contacts . UI . Card . addProperty ( 'TEL' ) ;
$ ( '#fn' ) . focus ( ) ;
}
}
else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
}
} ) ;
$ ( '#contact_identity' ) . show ( ) ;
$ ( '#actionbar' ) . show ( ) ;
}
else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
}
} ) ;
}
2012-07-09 21:32:37 +04:00
if ( ! $ ( '#card' ) . length ) {
console . log ( 'Loading card DOM' ) ;
2012-06-26 20:04:22 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'loadcard.php' ) , { 'requesttoken' : requesttoken } , function ( jsondata ) {
2012-04-09 18:29:35 +04:00
if ( jsondata . status == 'success' ) {
2012-05-14 12:12:36 +04:00
$ ( '#rightcontent' ) . html ( jsondata . data . page ) . ready ( function ( ) {
Contacts . UI . loadHandlers ( ) ;
localAddcontact ( n , fn , aid , isnew ) ;
} ) ;
2012-04-09 18:29:35 +04:00
} else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
}
} ) ;
2012-05-14 12:12:36 +04:00
} else {
localAddcontact ( n , fn , aid , isnew ) ;
}
2012-02-14 13:34:52 +04:00
} ,
2012-04-10 23:53:36 +04:00
doDelete : function ( ) {
2012-02-06 10:32:57 +04:00
$ ( '#contacts_deletecard' ) . tipsy ( 'hide' ) ;
2012-03-29 17:24:32 +04:00
OC . dialogs . confirm ( t ( 'contacts' , 'Are you sure you want to delete this contact?' ) , t ( 'contacts' , 'Warning' ) , function ( answer ) {
if ( answer == true ) {
2012-06-09 17:00:18 +04:00
$ . post ( OC . filePath ( 'contacts' , 'ajax' , 'deletecard.php' ) , { 'id' : Contacts . UI . Card . id } , function ( jsondata ) {
2012-03-29 17:24:32 +04:00
if ( jsondata . status == 'success' ) {
2012-06-18 01:14:02 +04:00
var newid = '' , bookid ;
2012-06-20 23:00:07 +04:00
var curlistitem = $ ( '#contacts li[data-id="' + jsondata . data . id + '"]' ) ;
2012-06-24 03:48:37 +04:00
var newlistitem = curlistitem . prev ( 'li' ) ;
2012-07-14 16:25:25 +04:00
if ( ! newlistitem ) {
2012-06-24 03:48:37 +04:00
newlistitem = curlistitem . next ( 'li' ) ;
2012-02-14 13:34:52 +04:00
}
2012-03-29 17:24:32 +04:00
curlistitem . remove ( ) ;
2012-07-14 16:25:25 +04:00
if ( $ ( newlistitem ) . is ( 'li' ) ) {
2012-03-29 17:24:32 +04:00
newid = newlistitem . data ( 'id' ) ;
2012-07-14 16:25:25 +04:00
bookid = newlistitem . data ( 'bookid' ) ;
2012-02-14 13:34:52 +04:00
}
2012-03-29 17:24:32 +04:00
$ ( '#rightcontent' ) . data ( 'id' , newid ) ;
this . id = this . fn = this . fullname = this . shortname = this . famname = this . givname = this . addname = this . honpre = this . honsuf = '' ;
this . data = undefined ;
2012-05-07 00:24:36 +04:00
2012-06-17 01:37:24 +04:00
if ( $ ( '.contacts li' ) . length > 0 ) { // Load first in list.
2012-07-15 06:15:57 +04:00
Contacts . UI . Card . update ( { cid : newid , aid : bookid } ) ;
2012-03-29 17:24:32 +04:00
} else {
// load intro page
2012-04-24 23:33:34 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'loadintro.php' ) , { } , function ( jsondata ) {
2012-03-29 17:24:32 +04:00
if ( jsondata . status == 'success' ) {
id = '' ;
$ ( '#rightcontent' ) . data ( 'id' , '' ) ;
$ ( '#rightcontent' ) . html ( jsondata . data . page ) ;
}
else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
}
} ) ;
}
}
else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
}
} ) ;
2012-02-06 10:32:57 +04:00
}
} ) ;
return false ;
} ,
2012-06-18 01:14:02 +04:00
loadContact : function ( jsondata , bookid ) {
2012-02-06 10:32:57 +04:00
this . data = jsondata ;
this . id = this . data . id ;
2012-06-18 01:14:02 +04:00
this . bookid = bookid ;
2012-02-14 13:34:52 +04:00
$ ( '#rightcontent' ) . data ( 'id' , this . id ) ;
2012-02-06 10:32:57 +04:00
this . populateNameFields ( ) ;
this . loadPhoto ( ) ;
this . loadMails ( ) ;
this . loadPhones ( ) ;
this . loadAddresses ( ) ;
this . loadSingleProperties ( ) ;
2012-05-02 14:10:44 +04:00
Contacts . UI . loadListHandlers ( ) ;
2012-03-07 19:39:56 +04:00
if ( this . data . NOTE ) {
$ ( '#note' ) . data ( 'checksum' , this . data . NOTE [ 0 ] [ 'checksum' ] ) ;
2012-05-04 17:47:19 +04:00
var note = $ ( '#note' ) . find ( 'textarea' ) ;
var txt = this . data . NOTE [ 0 ] [ 'value' ] ;
var nheight = txt . split ( '\n' ) . length > 4 ? txt . split ( '\n' ) . length + 2 : 5 ;
note . css ( 'min-height' , nheight + 'em' ) ;
note . attr ( 'rows' , nheight ) ;
note . val ( txt ) ;
2012-03-07 19:39:56 +04:00
$ ( '#note' ) . show ( ) ;
2012-05-04 17:47:19 +04:00
note . expandingTextarea ( ) ;
2012-05-04 18:36:45 +04:00
$ ( '#contacts_propertymenu_dropdown a[data-type="NOTE"]' ) . parent ( ) . hide ( ) ;
2012-03-07 19:39:56 +04:00
} else {
$ ( '#note' ) . data ( 'checksum' , '' ) ;
$ ( '#note' ) . find ( 'textarea' ) . val ( '' ) ;
2012-04-21 23:40:20 +04:00
$ ( '#note' ) . hide ( ) ;
2012-05-04 18:36:45 +04:00
$ ( '#contacts_propertymenu_dropdown a[data-type="NOTE"]' ) . parent ( ) . show ( ) ;
2012-03-07 19:39:56 +04:00
}
2012-02-06 10:32:57 +04:00
} ,
loadSingleProperties : function ( ) {
2012-06-06 17:28:39 +04:00
var props = [ 'BDAY' , 'NICKNAME' , 'ORG' , 'URL' , 'CATEGORIES' ] ;
2012-02-06 10:32:57 +04:00
// Clear all elements
2012-03-06 01:02:44 +04:00
$ ( '#ident .propertycontainer' ) . each ( function ( ) {
2012-02-06 10:32:57 +04:00
if ( props . indexOf ( $ ( this ) . data ( 'element' ) ) > - 1 ) {
$ ( this ) . data ( 'checksum' , '' ) ;
$ ( this ) . find ( 'input' ) . val ( '' ) ;
$ ( this ) . hide ( ) ;
$ ( this ) . prev ( ) . hide ( ) ;
}
} ) ;
for ( var prop in props ) {
2012-06-06 17:28:39 +04:00
var propname = props [ prop ] ;
if ( this . data [ propname ] != undefined ) {
$ ( '#contacts_propertymenu_dropdown a[data-type="' + propname + '"]' ) . parent ( ) . hide ( ) ;
var property = this . data [ propname ] [ 0 ] ;
2012-02-06 10:32:57 +04:00
var value = property [ 'value' ] , checksum = property [ 'checksum' ] ;
2012-06-06 17:28:39 +04:00
if ( propname == 'BDAY' ) {
var val = $ . datepicker . parseDate ( 'yy-mm-dd' , value . substring ( 0 , 10 ) ) ;
value = $ . datepicker . formatDate ( 'dd-mm-yy' , val ) ;
2012-02-06 10:32:57 +04:00
}
2012-06-06 17:28:39 +04:00
$ ( '#contact_identity' ) . find ( '#' + propname . toLowerCase ( ) ) . val ( value ) ;
$ ( '#contact_identity' ) . find ( '#' + propname . toLowerCase ( ) + '_value' ) . data ( 'checksum' , checksum ) ;
$ ( '#contact_identity' ) . find ( '#' + propname . toLowerCase ( ) + '_label' ) . show ( ) ;
$ ( '#contact_identity' ) . find ( '#' + propname . toLowerCase ( ) + '_value' ) . show ( ) ;
2012-02-06 11:04:06 +04:00
} else {
2012-06-06 17:28:39 +04:00
$ ( '#contacts_propertymenu_dropdown a[data-type="' + propname + '"]' ) . parent ( ) . show ( ) ;
2012-02-06 10:32:57 +04:00
}
}
} ,
populateNameFields : function ( ) {
2012-05-10 22:43:40 +04:00
var props = [ 'FN' , 'N' ] ;
// Clear all elements
$ ( '#ident .propertycontainer' ) . each ( function ( ) {
if ( props . indexOf ( $ ( this ) . data ( 'element' ) ) > - 1 ) {
$ ( this ) . data ( 'checksum' , '' ) ;
$ ( this ) . find ( 'input' ) . val ( '' ) ;
}
} ) ;
2012-02-06 17:18:40 +04:00
this . fn = '' ; this . fullname = '' ; this . givname = '' ; this . famname = '' ; this . addname = '' ; this . honpre = '' ; this . honsuf = '' ;
2012-02-06 10:32:57 +04:00
var narray = undefined ;
2012-05-10 22:43:40 +04:00
if ( this . data . FN ) {
this . fn = this . data . FN [ 0 ] [ 'value' ] ;
}
else {
this . fn = '' ;
}
2012-02-06 10:32:57 +04:00
if ( this . data . N == undefined ) {
narray = [ this . fn , '' , '' , '' , '' ] ; // Checking for non-existing 'N' property :-P
} else {
narray = this . data . N [ 0 ] [ 'value' ] ;
}
2012-03-09 10:36:05 +04:00
this . famname = narray [ 0 ] || '' ;
this . givname = narray [ 1 ] || '' ;
this . addname = narray [ 2 ] || '' ;
this . honpre = narray [ 3 ] || '' ;
this . honsuf = narray [ 4 ] || '' ;
2012-02-06 10:32:57 +04:00
if ( this . honpre . length > 0 ) {
this . fullname += this . honpre + ' ' ;
}
if ( this . givname . length > 0 ) {
this . fullname += ' ' + this . givname ;
}
if ( this . addname . length > 0 ) {
this . fullname += ' ' + this . addname ;
}
if ( this . famname . length > 0 ) {
this . fullname += ' ' + this . famname ;
}
if ( this . honsuf . length > 0 ) {
this . fullname += ', ' + this . honsuf ;
}
2012-05-17 18:46:07 +04:00
$ ( '#n' ) . val ( narray . join ( ';' ) ) ;
2012-02-10 19:40:40 +04:00
$ ( '#fn_select option' ) . remove ( ) ;
2012-05-17 18:46:07 +04:00
var names = [ this . fn , this . fullname , this . givname + ' ' + this . famname , this . famname + ' ' + this . givname , this . famname + ', ' + this . givname ] ;
2012-03-06 01:03:33 +04:00
if ( this . data . ORG ) {
names [ names . length ] = this . data . ORG [ 0 ] . value ;
}
2012-02-10 19:40:40 +04:00
$ . each ( names , function ( key , value ) {
$ ( '#fn_select' )
. append ( $ ( '<option></option>' )
. text ( value ) ) ;
} ) ;
2012-05-11 03:13:19 +04:00
$ ( '#fn_select' ) . combobox ( 'value' , this . fn ) ;
2012-02-06 10:32:57 +04:00
$ ( '#contact_identity' ) . find ( '*[data-element="N"]' ) . data ( 'checksum' , this . data . N [ 0 ] [ 'checksum' ] ) ;
2012-05-10 22:43:40 +04:00
if ( this . data . FN ) {
$ ( '#contact_identity' ) . find ( '*[data-element="FN"]' ) . data ( 'checksum' , this . data . FN [ 0 ] [ 'checksum' ] ) ;
}
2012-02-14 16:57:11 +04:00
$ ( '#contact_identity' ) . show ( ) ;
2012-02-06 10:32:57 +04:00
} ,
2012-03-07 19:39:56 +04:00
hasCategory : function ( category ) {
2012-02-17 12:35:18 +04:00
if ( this . data . CATEGORIES ) {
2012-03-12 19:44:16 +04:00
var categories = this . data . CATEGORIES [ 0 ] [ 'value' ] . split ( /,\s*/ ) ;
for ( var c in categories ) {
2012-03-07 19:39:56 +04:00
var cat = this . data . CATEGORIES [ 0 ] [ 'value' ] [ c ] ;
if ( typeof cat === 'string' && ( cat . toUpperCase ( ) === category . toUpperCase ( ) ) ) {
return true ;
}
}
2012-02-17 12:35:18 +04:00
}
2012-03-07 19:39:56 +04:00
return false ;
} ,
2012-03-13 00:55:54 +04:00
categoriesChanged : function ( newcategories ) { // Categories added/deleted.
2012-04-13 01:08:48 +04:00
categories = $ . map ( newcategories , function ( v ) { return v ; } ) ;
$ ( '#categories' ) . multiple _autocomplete ( 'option' , 'source' , categories ) ;
2012-03-12 19:44:16 +04:00
var categorylist = $ ( '#categories_value' ) . find ( 'input' ) ;
2012-03-12 19:47:29 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'categories/categoriesfor.php' ) , { 'id' : Contacts . UI . Card . id } , function ( jsondata ) {
2012-03-07 19:39:56 +04:00
if ( jsondata . status == 'success' ) {
$ ( '#categories_value' ) . data ( 'checksum' , jsondata . data . checksum ) ;
2012-03-12 19:44:16 +04:00
categorylist . val ( jsondata . data . value ) ;
2012-03-07 19:39:56 +04:00
} else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-03-07 19:39:56 +04:00
}
} ) ;
} ,
2012-02-09 22:04:07 +04:00
savePropertyInternal : function ( name , fields , oldchecksum , checksum ) {
// TODO: Add functionality for new fields.
2012-05-02 14:10:44 +04:00
//console.log('savePropertyInternal: ' + name + ', fields: ' + fields + 'checksum: ' + checksum);
//console.log('savePropertyInternal: ' + this.data[name]);
2012-03-07 19:39:56 +04:00
var multivalue = [ 'CATEGORIES' ] ;
2012-02-09 22:04:07 +04:00
var params = { } ;
2012-03-07 19:39:56 +04:00
var value = multivalue . indexOf ( name ) != - 1 ? new Array ( ) : undefined ;
2012-02-09 22:04:07 +04:00
jQuery . each ( fields , function ( i , field ) {
//.substring(11,'parameters[TYPE][]'.indexOf(']'))
if ( field . name . substring ( 0 , 5 ) === 'value' ) {
2012-03-07 19:39:56 +04:00
if ( multivalue . indexOf ( name ) != - 1 ) {
value . push ( field . value ) ;
} else {
value = field . value ;
}
2012-02-09 22:04:07 +04:00
} else if ( field . name . substring ( 0 , 10 ) === 'parameters' ) {
var p = field . name . substring ( 11 , 'parameters[TYPE][]' . indexOf ( ']' ) ) ;
if ( ! ( p in params ) ) {
params [ p ] = [ ] ;
}
params [ p ] . push ( field . value ) ;
}
} ) ;
for ( var i in this . data [ name ] ) {
if ( this . data [ name ] [ i ] [ 'checksum' ] == oldchecksum ) {
this . data [ name ] [ i ] [ 'checksum' ] = checksum ;
this . data [ name ] [ i ] [ 'value' ] = value ;
this . data [ name ] [ i ] [ 'parameters' ] = params ;
}
}
} ,
2012-02-06 10:32:57 +04:00
saveProperty : function ( obj ) {
if ( ! $ ( obj ) . hasClass ( 'contacts_property' ) ) {
return false ;
}
if ( $ ( obj ) . hasClass ( 'nonempty' ) && $ ( obj ) . val ( ) . trim ( ) == '' ) {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( t ( 'contacts' , 'This property has to be non-empty.' ) , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
return false ;
}
container = $ ( obj ) . parents ( '.propertycontainer' ) . first ( ) ; // get the parent holding the metadata.
2012-07-08 18:10:12 +04:00
Contacts . UI . loading ( obj , true ) ;
2012-02-06 10:32:57 +04:00
var checksum = container . data ( 'checksum' ) ;
var name = container . data ( 'element' ) ;
2012-03-06 01:02:44 +04:00
var fields = container . find ( 'input.contacts_property,select.contacts_property' ) . serializeArray ( ) ;
2012-05-11 03:13:19 +04:00
switch ( name ) {
case 'FN' :
var nempty = true ;
for ( var i in Contacts . UI . Card . data . N [ 0 ] [ 'value' ] ) {
if ( Contacts . UI . Card . data . N [ 0 ] [ 'value' ] [ i ] != '' ) {
nempty = false ;
break ;
}
}
if ( nempty ) {
$ ( '#n' ) . val ( fields [ 0 ] . value + ';;;;' ) ;
Contacts . UI . Card . data . N [ 0 ] [ 'value' ] = Array ( fields [ 0 ] . value , '' , '' , '' , '' ) ;
setTimeout ( function ( ) { Contacts . UI . Card . saveProperty ( $ ( '#n' ) ) } , 500 ) ;
}
break ;
}
2012-03-13 05:55:17 +04:00
var q = container . find ( 'input.contacts_property,select.contacts_property,textarea.contacts_property' ) . serialize ( ) ;
2012-02-06 10:32:57 +04:00
if ( q == '' || q == undefined ) {
2012-05-02 14:10:44 +04:00
OC . dialogs . alert ( t ( 'contacts' , 'Couldn\'t serialize elements.' ) , t ( 'contacts' , 'Error' ) ) ;
2012-07-08 18:10:12 +04:00
Contacts . UI . loading ( obj , false ) ;
2012-02-06 10:32:57 +04:00
return false ;
}
q = q + '&id=' + this . id + '&name=' + name ;
if ( checksum != undefined && checksum != '' ) { // save
q = q + '&checksum=' + checksum ;
2012-06-13 19:35:42 +04:00
console . log ( 'Saving: ' + q ) ;
2012-03-07 19:39:56 +04:00
$ ( obj ) . attr ( 'disabled' , 'disabled' ) ;
2012-04-24 23:33:34 +04:00
$ . post ( OC . filePath ( 'contacts' , 'ajax' , 'saveproperty.php' ) , q , function ( jsondata ) {
2012-02-06 10:32:57 +04:00
if ( jsondata . status == 'success' ) {
container . data ( 'checksum' , jsondata . data . checksum ) ;
2012-02-09 22:04:07 +04:00
Contacts . UI . Card . savePropertyInternal ( name , fields , checksum , jsondata . data . checksum ) ;
2012-07-08 18:10:12 +04:00
Contacts . UI . loading ( obj , false ) ;
2012-03-07 19:39:56 +04:00
$ ( obj ) . removeAttr ( 'disabled' ) ;
2012-02-06 10:32:57 +04:00
return true ;
}
else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-07-08 18:15:52 +04:00
Contacts . UI . loading ( obj , false ) ;
2012-03-07 19:39:56 +04:00
$ ( obj ) . removeAttr ( 'disabled' ) ;
2012-02-06 10:32:57 +04:00
return false ;
}
} , 'json' ) ;
} else { // add
2012-06-13 19:35:42 +04:00
console . log ( 'Adding: ' + q ) ;
2012-03-07 19:39:56 +04:00
$ ( obj ) . attr ( 'disabled' , 'disabled' ) ;
2012-04-24 23:33:34 +04:00
$ . post ( OC . filePath ( 'contacts' , 'ajax' , 'addproperty.php' ) , q , function ( jsondata ) {
2012-02-06 10:32:57 +04:00
if ( jsondata . status == 'success' ) {
container . data ( 'checksum' , jsondata . data . checksum ) ;
2012-02-09 22:04:07 +04:00
// TODO: savePropertyInternal doesn't know about new fields
//Contacts.UI.Card.savePropertyInternal(name, fields, checksum, jsondata.data.checksum);
2012-07-08 18:15:52 +04:00
Contacts . UI . loading ( obj , false ) ;
2012-03-07 19:39:56 +04:00
$ ( obj ) . removeAttr ( 'disabled' ) ;
2012-02-06 10:32:57 +04:00
return true ;
}
else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-07-08 18:15:52 +04:00
Contacts . UI . loading ( obj , false ) ;
2012-03-07 19:39:56 +04:00
$ ( obj ) . removeAttr ( 'disabled' ) ;
2012-02-06 10:32:57 +04:00
return false ;
}
} , 'json' ) ;
}
} ,
2012-04-03 05:28:12 +04:00
addProperty : function ( type ) {
2012-02-06 10:32:57 +04:00
switch ( type ) {
case 'PHOTO' :
2012-03-07 19:39:56 +04:00
this . loadPhoto ( true ) ;
2012-02-06 10:32:57 +04:00
$ ( '#file_upload_form' ) . show ( ) ;
2012-05-04 18:36:45 +04:00
$ ( '#contacts_propertymenu_dropdown a[data-type="' + type + '"]' ) . parent ( ) . hide ( ) ;
2012-03-27 14:26:59 +04:00
$ ( '#file_upload_start' ) . trigger ( 'click' ) ;
2012-02-06 10:32:57 +04:00
break ;
2012-03-07 19:39:56 +04:00
case 'NOTE' :
$ ( '#note' ) . show ( ) ;
2012-05-04 18:36:45 +04:00
$ ( '#contacts_propertymenu_dropdown a[data-type="' + type + '"]' ) . parent ( ) . hide ( ) ;
2012-05-04 17:47:19 +04:00
$ ( '#note' ) . find ( 'textarea' ) . expandingTextarea ( ) ;
2012-03-27 14:26:59 +04:00
$ ( '#note' ) . find ( 'textarea' ) . focus ( ) ;
2012-03-07 19:39:56 +04:00
break ;
2012-02-06 10:32:57 +04:00
case 'EMAIL' :
if ( $ ( '#emaillist>li' ) . length == 1 ) {
$ ( '#emails' ) . show ( ) ;
}
Contacts . UI . Card . addMail ( ) ;
break ;
case 'TEL' :
if ( $ ( '#phonelist>li' ) . length == 1 ) {
$ ( '#phones' ) . show ( ) ;
}
Contacts . UI . Card . addPhone ( ) ;
break ;
case 'ADR' :
if ( $ ( '#addressdisplay>dl' ) . length == 1 ) {
$ ( '#addresses' ) . show ( ) ;
}
Contacts . UI . Card . editAddress ( 'new' , true ) ;
break ;
case 'NICKNAME' :
2012-06-06 17:28:39 +04:00
case 'URL' :
2012-02-06 10:32:57 +04:00
case 'ORG' :
case 'BDAY' :
2012-03-08 00:50:55 +04:00
case 'CATEGORIES' :
2012-02-06 10:32:57 +04:00
$ ( 'dl dt[data-element="' + type + '"],dd[data-element="' + type + '"]' ) . show ( ) ;
2012-03-27 14:26:59 +04:00
$ ( 'dd[data-element="' + type + '"]' ) . find ( 'input' ) . focus ( ) ;
2012-05-04 18:36:45 +04:00
$ ( '#contacts_propertymenu_dropdown a[data-type="' + type + '"]' ) . parent ( ) . hide ( ) ;
2012-02-06 10:32:57 +04:00
break ;
}
} ,
deleteProperty : function ( obj , type ) {
2012-07-14 16:25:25 +04:00
console . log ( 'deleteProperty' ) ;
2012-02-06 10:32:57 +04:00
Contacts . UI . loading ( obj , true ) ;
var checksum = Contacts . UI . checksumFor ( obj ) ;
2012-04-22 22:44:11 +04:00
if ( checksum ) {
2012-06-09 17:00:18 +04:00
$ . post ( OC . filePath ( 'contacts' , 'ajax' , 'deleteproperty.php' ) , { 'id' : this . id , 'checksum' : checksum } , function ( jsondata ) {
2012-02-06 10:32:57 +04:00
if ( jsondata . status == 'success' ) {
if ( type == 'list' ) {
Contacts . UI . propertyContainerFor ( obj ) . remove ( ) ;
} else if ( type == 'single' ) {
var proptype = Contacts . UI . propertyTypeFor ( obj ) ;
2012-04-30 12:30:10 +04:00
Contacts . UI . Card . data [ proptype ] = null ;
2012-03-13 17:56:03 +04:00
var othertypes = [ 'NOTE' , 'PHOTO' ] ;
if ( othertypes . indexOf ( proptype ) != - 1 ) {
2012-03-13 05:55:17 +04:00
Contacts . UI . propertyContainerFor ( obj ) . data ( 'checksum' , '' ) ;
2012-03-27 12:12:30 +04:00
if ( proptype == 'PHOTO' ) {
Contacts . UI . Contacts . refreshThumbnail ( Contacts . UI . Card . id ) ;
2012-04-30 12:30:10 +04:00
Contacts . UI . Card . loadPhoto ( true ) ;
2012-03-27 14:26:59 +04:00
} else if ( proptype == 'NOTE' ) {
$ ( '#note' ) . find ( 'textarea' ) . val ( '' ) ;
2012-04-22 22:44:11 +04:00
Contacts . UI . propertyContainerFor ( obj ) . hide ( ) ;
2012-03-27 12:12:30 +04:00
}
2012-03-13 05:55:17 +04:00
} else {
$ ( 'dl dt[data-element="' + proptype + '"],dd[data-element="' + proptype + '"]' ) . hide ( ) ;
$ ( 'dl dd[data-element="' + proptype + '"]' ) . data ( 'checksum' , '' ) ;
2012-03-27 14:26:59 +04:00
$ ( 'dl dd[data-element="' + proptype + '"]' ) . find ( 'input' ) . val ( '' ) ;
2012-03-13 05:55:17 +04:00
}
2012-05-04 18:36:45 +04:00
$ ( '#contacts_propertymenu_dropdown a[data-type="' + proptype + '"]' ) . parent ( ) . show ( ) ;
2012-02-06 10:32:57 +04:00
Contacts . UI . loading ( obj , false ) ;
} else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( t ( 'contacts' , '\'deleteProperty\' called without type argument. Please report at bugs.owncloud.org' ) , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
Contacts . UI . loading ( obj , false ) ;
}
}
else {
Contacts . UI . loading ( obj , false ) ;
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
}
} ) ;
} else { // Property hasn't been saved so there's nothing to delete.
if ( type == 'list' ) {
Contacts . UI . propertyContainerFor ( obj ) . remove ( ) ;
} else if ( type == 'single' ) {
var proptype = Contacts . UI . propertyTypeFor ( obj ) ;
$ ( 'dl dt[data-element="' + proptype + '"],dd[data-element="' + proptype + '"]' ) . hide ( ) ;
2012-05-04 18:36:45 +04:00
$ ( '#contacts_propertymenu_dropdown a[data-type="' + proptype + '"]' ) . parent ( ) . show ( ) ;
2012-02-06 10:32:57 +04:00
Contacts . UI . loading ( obj , false ) ;
} else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( t ( 'contacts' , '\'deleteProperty\' called without type argument. Please report at bugs.owncloud.org' ) , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
}
}
} ,
editName : function ( ) {
2012-05-28 17:59:58 +04:00
var params = { id : this . id } ;
2012-02-06 10:32:57 +04:00
/* Initialize the name edit dialog */
2012-02-06 10:32:57 +04:00
if ( $ ( '#edit_name_dialog' ) . dialog ( 'isOpen' ) == true ) {
$ ( '#edit_name_dialog' ) . dialog ( 'moveToTop' ) ;
2012-05-02 14:10:44 +04:00
} else {
2012-05-28 17:59:58 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'editname.php' ) , { id : this . id } , function ( jsondata ) {
if ( jsondata . status == 'success' ) {
$ ( 'body' ) . append ( '<div id="name_dialog"></div>' ) ;
$ ( '#name_dialog' ) . html ( jsondata . data . page ) . find ( '#edit_name_dialog' ) . dialog ( {
2012-05-11 20:21:12 +04:00
modal : true ,
2012-05-28 17:59:58 +04:00
closeOnEscape : true ,
title : t ( 'contacts' , 'Edit name' ) ,
2012-02-06 10:32:57 +04:00
height : 'auto' , width : 'auto' ,
buttons : {
'Ok' : function ( ) {
Contacts . UI . Card . saveName ( this ) ;
2012-05-28 17:59:58 +04:00
$ ( this ) . dialog ( 'close' ) ;
2012-02-06 10:32:57 +04:00
} ,
2012-05-28 17:59:58 +04:00
'Cancel' : function ( ) { $ ( this ) . dialog ( 'close' ) ; }
2012-02-06 10:32:57 +04:00
} ,
2012-05-02 14:10:44 +04:00
close : function ( event , ui ) {
2012-02-06 10:32:57 +04:00
$ ( this ) . dialog ( 'destroy' ) . remove ( ) ;
2012-05-28 17:59:58 +04:00
$ ( '#name_dialog' ) . remove ( ) ;
2012-05-02 14:10:44 +04:00
} ,
open : function ( event , ui ) {
2012-02-06 10:32:57 +04:00
// load 'N' property - maybe :-P
2012-05-02 14:10:44 +04:00
}
2012-03-07 19:39:56 +04:00
} ) ;
} else {
2012-03-12 17:12:27 +04:00
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-03-07 19:39:56 +04:00
}
2012-02-06 10:32:57 +04:00
} ) ;
}
} ,
saveName : function ( dlg ) {
2012-05-02 14:10:44 +04:00
//console.log('saveName, id: ' + this.id);
2012-02-06 17:18:40 +04:00
var n = new Array ( $ ( dlg ) . find ( '#fam' ) . val ( ) . strip _tags ( ) , $ ( dlg ) . find ( '#giv' ) . val ( ) . strip _tags ( ) , $ ( dlg ) . find ( '#add' ) . val ( ) . strip _tags ( ) , $ ( dlg ) . find ( '#pre' ) . val ( ) . strip _tags ( ) , $ ( dlg ) . find ( '#suf' ) . val ( ) . strip _tags ( ) ) ;
2012-02-06 10:32:57 +04:00
this . famname = n [ 0 ] ;
this . givname = n [ 1 ] ;
this . addname = n [ 2 ] ;
this . honpre = n [ 3 ] ;
this . honsuf = n [ 4 ] ;
2012-02-10 10:21:56 +04:00
this . fullname = '' ;
2012-02-08 11:33:51 +04:00
2012-02-06 10:32:57 +04:00
$ ( '#n' ) . val ( n . join ( ';' ) ) ;
if ( n [ 3 ] . length > 0 ) {
this . fullname = n [ 3 ] + ' ' ;
}
this . fullname += n [ 1 ] + ' ' + n [ 2 ] + ' ' + n [ 0 ] ;
if ( n [ 4 ] . length > 0 ) {
this . fullname += ', ' + n [ 4 ] ;
}
2012-02-10 19:40:40 +04:00
$ ( '#fn_select option' ) . remove ( ) ;
//$('#fn_select').combobox('value', this.fn);
2012-03-07 19:39:56 +04:00
var tmp = [ this . fullname , this . givname + ' ' + this . famname , this . famname + ' ' + this . givname , this . famname + ', ' + this . givname ] ;
var names = new Array ( ) ;
for ( var name in tmp ) {
if ( names . indexOf ( tmp [ name ] ) == - 1 ) {
names . push ( tmp [ name ] ) ;
}
}
2012-02-10 19:40:40 +04:00
$ . each ( names , function ( key , value ) {
$ ( '#fn_select' )
. append ( $ ( '<option></option>' )
. text ( value ) ) ;
} ) ;
2012-02-06 10:32:57 +04:00
if ( this . id == '' ) {
var aid = $ ( dlg ) . find ( '#aid' ) . val ( ) ;
2012-02-06 17:18:40 +04:00
Contacts . UI . Card . add ( n . join ( ';' ) , $ ( '#short' ) . text ( ) , aid ) ;
2012-02-06 10:32:57 +04:00
} else {
Contacts . UI . Card . saveProperty ( $ ( '#n' ) ) ;
}
} ,
loadAddresses : function ( ) {
$ ( '#addresses' ) . hide ( ) ;
2012-03-06 01:02:44 +04:00
$ ( '#addressdisplay dl.propertycontainer' ) . remove ( ) ;
2012-02-06 10:32:57 +04:00
for ( var adr in this . data . ADR ) {
$ ( '#addressdisplay dl' ) . first ( ) . clone ( ) . insertAfter ( $ ( '#addressdisplay dl' ) . last ( ) ) . show ( ) ;
$ ( '#addressdisplay dl' ) . last ( ) . removeClass ( 'template' ) . addClass ( 'propertycontainer' ) ;
$ ( '#addressdisplay dl' ) . last ( ) . data ( 'checksum' , this . data . ADR [ adr ] [ 'checksum' ] ) ;
var adrarray = this . data . ADR [ adr ] [ 'value' ] ;
var adrtxt = '' ;
2012-06-12 00:13:45 +04:00
if ( adrarray [ 0 ] && adrarray [ 0 ] . length > 0 ) {
2012-02-06 10:32:57 +04:00
adrtxt = adrtxt + '<li>' + adrarray [ 0 ] . strip _tags ( ) + '</li>' ;
}
2012-06-12 00:13:45 +04:00
if ( adrarray [ 1 ] && adrarray [ 1 ] . length > 0 ) {
2012-02-06 10:32:57 +04:00
adrtxt = adrtxt + '<li>' + adrarray [ 1 ] . strip _tags ( ) + '</li>' ;
}
2012-06-12 00:13:45 +04:00
if ( adrarray [ 2 ] && adrarray [ 2 ] . length > 0 ) {
2012-02-06 10:32:57 +04:00
adrtxt = adrtxt + '<li>' + adrarray [ 2 ] . strip _tags ( ) + '</li>' ;
}
2012-06-12 00:13:45 +04:00
if ( ( adrarray [ 3 ] && adrarray [ 5 ] ) && adrarray [ 3 ] . length > 0 || adrarray [ 5 ] . length > 0 ) {
2012-02-06 10:32:57 +04:00
adrtxt = adrtxt + '<li>' + adrarray [ 5 ] . strip _tags ( ) + ' ' + adrarray [ 3 ] . strip _tags ( ) + '</li>' ;
}
2012-06-12 00:13:45 +04:00
if ( adrarray [ 4 ] && adrarray [ 4 ] . length > 0 ) {
2012-02-06 10:32:57 +04:00
adrtxt = adrtxt + '<li>' + adrarray [ 4 ] . strip _tags ( ) + '</li>' ;
}
2012-06-12 00:13:45 +04:00
if ( adrarray [ 6 ] && adrarray [ 6 ] . length > 0 ) {
2012-02-06 10:32:57 +04:00
adrtxt = adrtxt + '<li>' + adrarray [ 6 ] . strip _tags ( ) + '</li>' ;
}
$ ( '#addressdisplay dl' ) . last ( ) . find ( '.addresslist' ) . html ( adrtxt ) ;
var types = new Array ( ) ;
var ttypes = new Array ( ) ;
for ( var param in this . data . ADR [ adr ] [ 'parameters' ] ) {
if ( param . toUpperCase ( ) == 'TYPE' ) {
types . push ( t ( 'contacts' , ucwords ( this . data . ADR [ adr ] [ 'parameters' ] [ param ] . toLowerCase ( ) ) ) ) ;
ttypes . push ( this . data . ADR [ adr ] [ 'parameters' ] [ param ] ) ;
}
}
$ ( '#addressdisplay dl' ) . last ( ) . find ( '.adr_type_label' ) . text ( types . join ( '/' ) ) ;
$ ( '#addressdisplay dl' ) . last ( ) . find ( '.adr_type' ) . val ( ttypes . join ( ',' ) ) ;
$ ( '#addressdisplay dl' ) . last ( ) . find ( '.adr' ) . val ( adrarray . join ( ';' ) ) ;
$ ( '#addressdisplay dl' ) . last ( ) . data ( 'checksum' , this . data . ADR [ adr ] [ 'checksum' ] ) ;
}
if ( $ ( '#addressdisplay dl' ) . length > 1 ) {
$ ( '#addresses' ) . show ( ) ;
2012-02-06 11:15:23 +04:00
$ ( '#contact_communication' ) . show ( ) ;
2012-02-06 10:32:57 +04:00
}
return false ;
} ,
editAddress : function ( obj , isnew ) {
var container = undefined ;
2012-05-28 17:59:58 +04:00
var params = { id : this . id } ;
2012-02-06 10:32:57 +04:00
if ( obj === 'new' ) {
isnew = true ;
2012-05-11 20:16:52 +04:00
$ ( '#addressdisplay dl' ) . first ( ) . clone ( true ) . insertAfter ( $ ( '#addressdisplay dl' ) . last ( ) ) . show ( ) ;
2012-02-06 10:32:57 +04:00
container = $ ( '#addressdisplay dl' ) . last ( ) ;
container . removeClass ( 'template' ) . addClass ( 'propertycontainer' ) ;
} else {
2012-05-28 17:59:58 +04:00
params [ 'checksum' ] = Contacts . UI . checksumFor ( obj ) ;
2012-02-06 10:32:57 +04:00
}
/* Initialize the address edit dialog */
if ( $ ( '#edit_address_dialog' ) . dialog ( 'isOpen' ) == true ) {
$ ( '#edit_address_dialog' ) . dialog ( 'moveToTop' ) ;
} else {
2012-05-28 17:59:58 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'editaddress.php' ) , params , function ( jsondata ) {
if ( jsondata . status == 'success' ) {
$ ( 'body' ) . append ( '<div id="address_dialog"></div>' ) ;
$ ( '#address_dialog' ) . html ( jsondata . data . page ) . find ( '#edit_address_dialog' ) . dialog ( {
2012-02-06 10:32:57 +04:00
height : 'auto' , width : 'auto' ,
buttons : {
'Ok' : function ( ) {
if ( isnew ) {
Contacts . UI . Card . saveAddress ( this , $ ( '#addressdisplay dl:last-child' ) . find ( 'input' ) . first ( ) , isnew ) ;
} else {
Contacts . UI . Card . saveAddress ( this , obj , isnew ) ;
}
2012-05-28 17:59:58 +04:00
$ ( this ) . dialog ( 'close' ) ;
2012-02-06 10:32:57 +04:00
} ,
'Cancel' : function ( ) {
2012-05-28 17:59:58 +04:00
$ ( this ) . dialog ( 'close' ) ;
2012-02-06 10:32:57 +04:00
if ( isnew ) {
container . remove ( ) ;
}
}
} ,
close : function ( event , ui ) {
$ ( this ) . dialog ( 'destroy' ) . remove ( ) ;
2012-05-28 17:59:58 +04:00
$ ( '#address_dialog' ) . remove ( ) ;
2012-02-06 10:32:57 +04:00
if ( isnew ) {
container . remove ( ) ;
}
2012-04-03 09:29:47 +04:00
} ,
2012-02-06 10:32:57 +04:00
open : function ( event , ui ) {
2012-04-03 09:29:47 +04:00
$ ( "#adr_city" ) . autocomplete ( {
source : function ( request , response ) {
$ . ajax ( {
url : "http://ws.geonames.org/searchJSON" ,
dataType : "jsonp" ,
data : {
featureClass : "P" ,
style : "full" ,
maxRows : 12 ,
2012-04-03 10:13:10 +04:00
lang : lang ,
2012-04-03 09:29:47 +04:00
name _startsWith : request . term
} ,
success : function ( data ) {
response ( $ . map ( data . geonames , function ( item ) {
return {
label : item . name + ( item . adminName1 ? ", " + item . adminName1 : "" ) + ", " + item . countryName ,
value : item . name ,
country : item . countryName
}
} ) ) ;
}
} ) ;
} ,
minLength : 2 ,
select : function ( event , ui ) {
2012-04-03 10:13:10 +04:00
if ( ui . item && $ ( '#adr_country' ) . val ( ) . trim ( ) . length == 0 ) {
2012-04-03 09:29:47 +04:00
$ ( '#adr_country' ) . val ( ui . item . country ) ;
}
} ,
open : function ( ) {
$ ( this ) . removeClass ( "ui-corner-all" ) . addClass ( "ui-corner-top" ) ;
} ,
close : function ( ) {
$ ( this ) . removeClass ( "ui-corner-top" ) . addClass ( "ui-corner-all" ) ;
}
} ) ;
$ ( "#adr_country" ) . autocomplete ( {
source : function ( request , response ) {
$ . ajax ( {
url : "http://ws.geonames.org/searchJSON" ,
dataType : "jsonp" ,
data : {
/*featureClass: "A",*/
featureCode : "PCLI" ,
/*countryBias: "true",*/
/*style: "full",*/
2012-04-03 10:13:10 +04:00
lang : lang ,
2012-04-03 09:29:47 +04:00
maxRows : 12 ,
name _startsWith : request . term
} ,
success : function ( data ) {
response ( $ . map ( data . geonames , function ( item ) {
return {
label : item . name ,
value : item . name
}
} ) ) ;
}
} ) ;
} ,
minLength : 2 ,
select : function ( event , ui ) {
/ * i f ( u i . i t e m ) {
$ ( '#adr_country' ) . val ( ui . item . country ) ;
}
log ( ui . item ?
"Selected: " + ui . item . label :
"Nothing selected, input was " + this . value ) ; * /
} ,
open : function ( ) {
$ ( this ) . removeClass ( "ui-corner-all" ) . addClass ( "ui-corner-top" ) ;
} ,
close : function ( ) {
$ ( this ) . removeClass ( "ui-corner-top" ) . addClass ( "ui-corner-all" ) ;
}
} ) ;
}
2012-03-07 19:39:56 +04:00
} ) ;
} else {
alert ( jsondata . data . message ) ;
}
2012-02-06 10:32:57 +04:00
} ) ;
}
} ,
saveAddress : function ( dlg , obj , isnew ) {
if ( isnew ) {
container = $ ( '#addressdisplay dl' ) . last ( ) ;
obj = $ ( '#addressdisplay dl:last-child' ) . find ( 'input' ) . first ( ) ;
} else {
checksum = Contacts . UI . checksumFor ( obj ) ;
container = Contacts . UI . propertyContainerFor ( obj ) ;
}
2012-02-18 14:42:58 +04:00
var adr = new Array ( $ ( dlg ) . find ( '#adr_pobox' ) . val ( ) . strip _tags ( ) , $ ( dlg ) . find ( '#adr_extended' ) . val ( ) . strip _tags ( ) , $ ( dlg ) . find ( '#adr_street' ) . val ( ) . strip _tags ( ) , $ ( dlg ) . find ( '#adr_city' ) . val ( ) . strip _tags ( ) , $ ( dlg ) . find ( '#adr_region' ) . val ( ) . strip _tags ( ) , $ ( dlg ) . find ( '#adr_zipcode' ) . val ( ) . strip _tags ( ) , $ ( dlg ) . find ( '#adr_country' ) . val ( ) . strip _tags ( ) ) ;
2012-02-06 10:32:57 +04:00
$ ( container ) . find ( '.adr' ) . val ( adr . join ( ';' ) ) ;
$ ( container ) . find ( '.adr_type' ) . val ( $ ( dlg ) . find ( '#adr_type' ) . val ( ) ) ;
$ ( container ) . find ( '.adr_type_label' ) . html ( t ( 'contacts' , ucwords ( $ ( dlg ) . find ( '#adr_type' ) . val ( ) . toLowerCase ( ) ) ) ) ;
Contacts . UI . Card . saveProperty ( $ ( container ) . find ( 'input' ) . first ( ) ) ;
var adrtxt = '' ;
if ( adr [ 0 ] . length > 0 ) {
adrtxt = adrtxt + '<li>' + adr [ 0 ] + '</li>' ;
}
if ( adr [ 1 ] . length > 0 ) {
adrtxt = adrtxt + '<li>' + adr [ 1 ] + '</li>' ;
}
if ( adr [ 2 ] . length > 0 ) {
adrtxt = adrtxt + '<li>' + adr [ 2 ] + '</li>' ;
}
if ( adr [ 3 ] . length > 0 || adr [ 5 ] . length > 0 ) {
adrtxt = adrtxt + '<li>' + adr [ 5 ] + ' ' + adr [ 3 ] + '</li>' ;
}
if ( adr [ 4 ] . length > 0 ) {
adrtxt = adrtxt + '<li>' + adr [ 4 ] + '</li>' ;
}
if ( adr [ 6 ] . length > 0 ) {
adrtxt = adrtxt + '<li>' + adr [ 6 ] + '</li>' ;
}
$ ( container ) . find ( '.addresslist' ) . html ( adrtxt ) ;
} ,
uploadPhoto : function ( filelist ) {
if ( ! filelist ) {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( t ( 'contacts' , 'No files selected for upload.' ) , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
return ;
}
var file = filelist [ 0 ] ;
var target = $ ( '#file_upload_target' ) ;
var form = $ ( '#file_upload_form' ) ;
var totalSize = 0 ;
if ( file . size > $ ( '#max_upload' ) . val ( ) ) {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( t ( 'contacts' , 'The file you are trying to upload exceed the maximum size for file uploads on this server.' ) , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
return ;
} else {
target . load ( function ( ) {
var response = jQuery . parseJSON ( target . contents ( ) . text ( ) ) ;
if ( response != undefined && response . status == 'success' ) {
Contacts . UI . Card . editPhoto ( response . data . id , response . data . tmp ) ;
//alert('File: ' + file.tmp + ' ' + file.name + ' ' + file.mime);
} else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( response . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
}
} ) ;
form . submit ( ) ;
}
} ,
2012-04-22 22:44:11 +04:00
loadPhotoHandlers : function ( ) {
2012-05-02 14:10:44 +04:00
$ ( '#phototools li a' ) . tipsy ( 'hide' ) ;
2012-04-23 00:58:48 +04:00
$ ( '#phototools li a' ) . tipsy ( ) ;
2012-04-22 22:44:11 +04:00
if ( this . data . PHOTO ) {
$ ( '#phototools .delete' ) . click ( function ( ) {
$ ( this ) . tipsy ( 'hide' ) ;
Contacts . UI . Card . deleteProperty ( $ ( '#contacts_details_photo' ) , 'single' ) ;
$ ( this ) . hide ( ) ;
} ) ;
$ ( '#phototools .edit' ) . click ( function ( ) {
$ ( this ) . tipsy ( 'hide' ) ;
Contacts . UI . Card . editCurrentPhoto ( ) ;
} ) ;
2012-06-28 03:45:59 +04:00
$ ( '#phototools .delete' ) . show ( ) ;
$ ( '#phototools .edit' ) . show ( ) ;
2012-04-22 22:44:11 +04:00
} else {
$ ( '#phototools .delete' ) . hide ( ) ;
$ ( '#phototools .edit' ) . hide ( ) ;
}
} ,
cloudPhotoSelected : function ( path ) {
2012-04-24 23:33:34 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'oc_photo.php' ) , { 'path' : path , 'id' : Contacts . UI . Card . id } , function ( jsondata ) {
2012-04-22 22:44:11 +04:00
if ( jsondata . status == 'success' ) {
//alert(jsondata.data.page);
Contacts . UI . Card . editPhoto ( jsondata . data . id , jsondata . data . tmp )
$ ( '#edit_photo_dialog_img' ) . html ( jsondata . data . page ) ;
}
else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
}
} ) ;
} ,
2012-04-30 12:30:10 +04:00
loadPhoto : function ( refresh ) {
2012-06-28 03:45:59 +04:00
var self = this ;
var refreshstr = ( refresh ? '&refresh=1' + Math . random ( ) : '' )
2012-05-02 14:10:44 +04:00
$ ( '#phototools li a' ) . tipsy ( 'hide' ) ;
2012-05-28 17:59:58 +04:00
var wrapper = $ ( '#contacts_details_photo_wrapper' ) ;
2012-06-28 03:45:59 +04:00
wrapper . addClass ( 'loading' ) . addClass ( 'wait' ) ;
var img = new Image ( ) ;
$ ( img ) . load ( function ( ) {
$ ( 'img.contacts_details_photo' ) . remove ( )
$ ( this ) . addClass ( 'contacts_details_photo' ) . hide ( ) ;
wrapper . removeClass ( 'loading' ) . removeClass ( 'wait' ) ;
$ ( this ) . insertAfter ( $ ( '#phototools' ) ) . fadeIn ( ) ;
} ) . error ( function ( ) {
// notify the user that the image could not be loaded
$ ( t ( 'contacts' , 'something went wrong.' ) ) . insertAfter ( $ ( '#phototools' ) ) ;
} ) . attr ( 'src' , OC . linkTo ( 'contacts' , 'photo.php' ) + '?id=' + self . id + refreshstr ) ;
2012-04-30 12:30:10 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'loadphoto.php' ) , { 'id' : this . id , 'refresh' : refresh } , function ( jsondata ) {
if ( jsondata . status == 'success' ) {
$ ( '#contacts_details_photo_wrapper' ) . data ( 'checksum' , jsondata . data . checksum ) ;
Contacts . UI . Card . loadPhotoHandlers ( ) ;
}
else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
}
} ) ;
$ ( '#file_upload_form' ) . show ( ) ;
2012-02-06 10:32:57 +04:00
} ,
2012-04-22 22:44:11 +04:00
editCurrentPhoto : function ( ) {
2012-04-24 23:33:34 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'currentphoto.php' ) , { 'id' : this . id } , function ( jsondata ) {
2012-04-22 22:44:11 +04:00
if ( jsondata . status == 'success' ) {
//alert(jsondata.data.page);
Contacts . UI . Card . editPhoto ( jsondata . data . id , jsondata . data . tmp )
$ ( '#edit_photo_dialog_img' ) . html ( jsondata . data . page ) ;
}
else {
2012-05-28 17:59:58 +04:00
wrapper . removeClass ( 'wait' ) ;
2012-04-22 22:44:11 +04:00
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
}
} ) ;
} ,
2012-06-05 22:30:22 +04:00
editPhoto : function ( id , tmpkey ) {
//alert('editPhoto: ' + tmpkey);
2012-06-14 20:12:38 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'cropphoto.php' ) , { 'tmpkey' : tmpkey , 'id' : this . id , 'requesttoken' : requesttoken } , function ( jsondata ) {
2012-02-06 10:32:57 +04:00
if ( jsondata . status == 'success' ) {
//alert(jsondata.data.page);
$ ( '#edit_photo_dialog_img' ) . html ( jsondata . data . page ) ;
}
else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
}
} ) ;
if ( $ ( '#edit_photo_dialog' ) . dialog ( 'isOpen' ) == true ) {
$ ( '#edit_photo_dialog' ) . dialog ( 'moveToTop' ) ;
} else {
$ ( '#edit_photo_dialog' ) . dialog ( 'open' ) ;
}
} ,
savePhoto : function ( ) {
var target = $ ( '#crop_target' ) ;
var form = $ ( '#cropform' ) ;
2012-05-28 17:59:58 +04:00
var wrapper = $ ( '#contacts_details_photo_wrapper' ) ;
2012-06-28 03:45:59 +04:00
var self = this ;
2012-05-28 17:59:58 +04:00
wrapper . addClass ( 'wait' ) ;
2012-02-06 10:32:57 +04:00
form . submit ( ) ;
target . load ( function ( ) {
var response = jQuery . parseJSON ( target . contents ( ) . text ( ) ) ;
if ( response != undefined && response . status == 'success' ) {
// load cropped photo.
2012-06-28 03:45:59 +04:00
self . loadPhoto ( true ) ;
2012-04-30 12:30:10 +04:00
Contacts . UI . Card . data . PHOTO = true ;
2012-02-06 10:32:57 +04:00
} else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( response . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-06-05 22:30:22 +04:00
wrapper . removeClass ( 'wait' ) ;
2012-02-06 10:32:57 +04:00
}
} ) ;
2012-03-27 12:12:30 +04:00
Contacts . UI . Contacts . refreshThumbnail ( this . id ) ;
2012-02-06 10:32:57 +04:00
} ,
addMail : function ( ) {
//alert('addMail');
2012-05-11 20:16:52 +04:00
$ ( '#emaillist li.template:first-child' ) . clone ( true ) . appendTo ( $ ( '#emaillist' ) ) . show ( ) . find ( 'a .tip' ) . tipsy ( ) ;
2012-04-30 00:10:16 +04:00
$ ( '#emaillist li.template:last-child' ) . find ( 'select' ) . addClass ( 'contacts_property' ) ;
2012-03-06 01:02:44 +04:00
$ ( '#emaillist li.template:last-child' ) . removeClass ( 'template' ) . addClass ( 'propertycontainer' ) ;
2012-02-06 10:32:57 +04:00
$ ( '#emaillist li:last-child' ) . find ( 'input[type="email"]' ) . focus ( ) ;
return false ;
} ,
loadMails : function ( ) {
$ ( '#emails' ) . hide ( ) ;
2012-03-06 01:02:44 +04:00
$ ( '#emaillist li.propertycontainer' ) . remove ( ) ;
2012-02-06 10:32:57 +04:00
for ( var mail in this . data . EMAIL ) {
this . addMail ( ) ;
//$('#emaillist li:first-child').clone().appendTo($('#emaillist')).show();
$ ( '#emaillist li:last-child' ) . data ( 'checksum' , this . data . EMAIL [ mail ] [ 'checksum' ] )
$ ( '#emaillist li:last-child' ) . find ( 'input[type="email"]' ) . val ( this . data . EMAIL [ mail ] [ 'value' ] ) ;
for ( var param in this . data . EMAIL [ mail ] [ 'parameters' ] ) {
if ( param . toUpperCase ( ) == 'PREF' ) {
$ ( '#emaillist li:last-child' ) . find ( 'input[type="checkbox"]' ) . attr ( 'checked' , 'checked' )
}
2012-04-30 00:10:16 +04:00
else if ( param . toUpperCase ( ) == 'TYPE' ) {
for ( etype in this . data . EMAIL [ mail ] [ 'parameters' ] [ param ] ) {
var et = this . data . EMAIL [ mail ] [ 'parameters' ] [ param ] [ etype ] ;
$ ( '#emaillist li:last-child' ) . find ( 'select option' ) . each ( function ( ) {
if ( $ . inArray ( $ ( this ) . val ( ) . toUpperCase ( ) , et . toUpperCase ( ) . split ( ',' ) ) > - 1 ) {
$ ( this ) . attr ( 'selected' , 'selected' ) ;
}
} ) ;
}
}
2012-02-06 10:32:57 +04:00
}
}
if ( $ ( '#emaillist li' ) . length > 1 ) {
$ ( '#emails' ) . show ( ) ;
2012-02-06 11:15:23 +04:00
$ ( '#contact_communication' ) . show ( ) ;
2012-02-06 10:32:57 +04:00
}
$ ( '#emaillist li:last-child' ) . find ( 'input[type="text"]' ) . focus ( ) ;
return false ;
} ,
addPhone : function ( ) {
2012-05-11 20:16:52 +04:00
$ ( '#phonelist li.template:first-child' ) . clone ( true ) . appendTo ( $ ( '#phonelist' ) ) ; //.show();
2012-03-06 01:02:44 +04:00
$ ( '#phonelist li.template:last-child' ) . find ( 'select' ) . addClass ( 'contacts_property' ) ;
$ ( '#phonelist li.template:last-child' ) . removeClass ( 'template' ) . addClass ( 'propertycontainer' ) ;
2012-02-06 10:32:57 +04:00
$ ( '#phonelist li:last-child' ) . find ( 'input[type="text"]' ) . focus ( ) ;
$ ( '#phonelist li:last-child' ) . find ( 'select' ) . multiselect ( {
noneSelectedText : t ( 'contacts' , 'Select type' ) ,
header : false ,
selectedList : 4 ,
classes : 'typelist'
} ) ;
$ ( '#phonelist li:last-child' ) . show ( ) ;
return false ;
} ,
loadPhones : function ( ) {
$ ( '#phones' ) . hide ( ) ;
2012-03-06 01:02:44 +04:00
$ ( '#phonelist li.propertycontainer' ) . remove ( ) ;
2012-02-06 10:32:57 +04:00
for ( var phone in this . data . TEL ) {
this . addPhone ( ) ;
$ ( '#phonelist li:last-child' ) . find ( 'select' ) . multiselect ( 'destroy' ) ;
$ ( '#phonelist li:last-child' ) . data ( 'checksum' , this . data . TEL [ phone ] [ 'checksum' ] )
$ ( '#phonelist li:last-child' ) . find ( 'input[type="text"]' ) . val ( this . data . TEL [ phone ] [ 'value' ] ) ;
for ( var param in this . data . TEL [ phone ] [ 'parameters' ] ) {
if ( param . toUpperCase ( ) == 'PREF' ) {
$ ( '#phonelist li:last-child' ) . find ( 'input[type="checkbox"]' ) . attr ( 'checked' , 'checked' ) ;
}
else if ( param . toUpperCase ( ) == 'TYPE' ) {
for ( ptype in this . data . TEL [ phone ] [ 'parameters' ] [ param ] ) {
var pt = this . data . TEL [ phone ] [ 'parameters' ] [ param ] [ ptype ] ;
$ ( '#phonelist li:last-child' ) . find ( 'select option' ) . each ( function ( ) {
2012-04-23 23:14:10 +04:00
//if ($(this).val().toUpperCase() == pt.toUpperCase()) {
if ( $ . inArray ( $ ( this ) . val ( ) . toUpperCase ( ) , pt . toUpperCase ( ) . split ( ',' ) ) > - 1 ) {
2012-02-06 10:32:57 +04:00
$ ( this ) . attr ( 'selected' , 'selected' ) ;
}
} ) ;
}
}
}
$ ( '#phonelist li:last-child' ) . find ( 'select' ) . multiselect ( {
noneSelectedText : t ( 'contacts' , 'Select type' ) ,
header : false ,
selectedList : 4 ,
classes : 'typelist'
} ) ;
}
if ( $ ( '#phonelist li' ) . length > 1 ) {
$ ( '#phones' ) . show ( ) ;
2012-02-06 11:15:23 +04:00
$ ( '#contact_communication' ) . show ( ) ;
2012-02-06 10:32:57 +04:00
}
return false ;
} ,
} ,
Addressbooks : {
overview : function ( ) {
if ( $ ( '#chooseaddressbook_dialog' ) . dialog ( 'isOpen' ) == true ) {
$ ( '#chooseaddressbook_dialog' ) . dialog ( 'moveToTop' ) ;
} else {
2012-05-28 17:59:58 +04:00
$ ( 'body' ) . append ( '<div id="addressbook_dialog"></div>' ) ;
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'chooseaddressbook.php' ) , function ( jsondata ) {
if ( jsondata . status == 'success' ) {
$ ( '#addressbook_dialog' ) . html ( jsondata . data . page ) . find ( '#chooseaddressbook_dialog' ) . dialog ( {
2012-06-07 12:54:25 +04:00
minWidth : 600 ,
2012-03-07 19:39:56 +04:00
close : function ( event , ui ) {
$ ( this ) . dialog ( 'destroy' ) . remove ( ) ;
2012-05-28 17:59:58 +04:00
$ ( '#addressbook_dialog' ) . remove ( ) ;
2012-03-07 19:39:56 +04:00
}
2012-05-17 16:29:20 +04:00
} ) . css ( 'overflow' , 'visible' ) ;
2012-03-07 19:39:56 +04:00
} else {
alert ( jsondata . data . message ) ;
2012-05-28 17:59:58 +04:00
$ ( '#addressbook_dialog' ) . remove ( ) ;
2012-03-07 19:39:56 +04:00
}
2012-02-06 10:32:57 +04:00
} ) ;
}
2012-05-02 14:10:44 +04:00
return false ;
2012-02-06 10:32:57 +04:00
} ,
2012-07-05 00:12:56 +04:00
activation : function ( checkbox , bookid ) {
var active = checkbox . checked ;
$ . post ( OC . filePath ( 'contacts' , 'ajax' , 'activation.php' ) , { bookid : bookid , active : ( active ? 1 : 0 ) } , function ( jsondata ) {
if ( jsondata . status == 'success' ) {
if ( ! active ) {
$ ( '#contacts h3[data-id="' + bookid + '"],#contacts ul[data-id="' + bookid + '"]' ) . remove ( ) ;
} else {
Contacts . UI . Contacts . update ( ) ;
}
} else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
checkbox . checked = ! active ;
2012-02-06 10:32:57 +04:00
}
} ) ;
} ,
2012-07-14 16:25:25 +04:00
addAddressbook : function ( name , description , cb ) {
$ . post ( OC . filePath ( 'contacts' , 'ajax' , 'addaddressbook.php' ) , { name : name , description : description , active : true } ,
function ( jsondata ) {
if ( jsondata . status == 'success' ) {
if ( cb ) {
cb ( jsondata . data ) ;
}
} else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
return false ;
}
} ) ;
} ,
2012-02-06 10:32:57 +04:00
newAddressbook : function ( object ) {
var tr = $ ( document . createElement ( 'tr' ) )
. load ( OC . filePath ( 'contacts' , 'ajax' , 'addbook.php' ) ) ;
$ ( object ) . closest ( 'tr' ) . after ( tr ) . hide ( ) ;
} ,
editAddressbook : function ( object , bookid ) {
var tr = $ ( document . createElement ( 'tr' ) )
. load ( OC . filePath ( 'contacts' , 'ajax' , 'editaddressbook.php' ) + "?bookid=" + bookid ) ;
$ ( object ) . closest ( 'tr' ) . after ( tr ) . hide ( ) ;
} ,
2012-04-23 23:14:10 +04:00
deleteAddressbook : function ( obj , bookid ) {
2012-02-06 10:32:57 +04:00
var check = confirm ( "Do you really want to delete this address book?" ) ;
if ( check == false ) {
return false ;
} else {
$ . post ( OC . filePath ( 'contacts' , 'ajax' , 'deletebook.php' ) , { id : bookid } ,
2012-02-20 18:24:54 +04:00
function ( jsondata ) {
if ( jsondata . status == 'success' ) {
2012-04-23 23:14:10 +04:00
$ ( obj ) . closest ( 'tr' ) . remove ( ) ;
2012-07-09 01:38:03 +04:00
$ ( '#contacts h3[data-id="' + bookid + '"],#contacts ul[data-id="' + bookid + '"]' ) . remove ( ) ;
2012-02-06 10:32:57 +04:00
Contacts . UI . Contacts . update ( ) ;
} else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
}
} ) ;
}
} ,
2012-07-15 06:15:57 +04:00
doImport : function ( file , aid ) {
$ . post ( OC . filePath ( 'contacts' , '' , 'import.php' ) , { id : aid , file : file , fstype : 'OC_FilesystemView' } ,
function ( jsondata ) {
if ( jsondata . status != 'success' ) {
2012-07-15 12:29:03 +04:00
Contacts . UI . notify ( { message : jsondata . data . message } ) ;
2012-04-26 05:20:28 +04:00
}
2012-07-15 06:15:57 +04:00
} ) ;
return false ;
2012-02-06 10:32:57 +04:00
} ,
submit : function ( button , bookid ) {
2012-02-20 18:24:54 +04:00
var displayname = $ ( "#displayname_" + bookid ) . val ( ) . trim ( ) ;
2012-02-06 10:32:57 +04:00
var active = $ ( "#edit_active_" + bookid + ":checked" ) . length ;
var description = $ ( "#description_" + bookid ) . val ( ) ;
2012-02-20 18:24:54 +04:00
if ( displayname . length == 0 ) {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( t ( 'contacts' , 'Displayname cannot be empty.' ) , t ( 'contacts' , 'Error' ) ) ;
2012-02-20 18:24:54 +04:00
return false ;
}
2012-02-06 10:32:57 +04:00
var url ;
if ( bookid == 'new' ) {
url = OC . filePath ( 'contacts' , 'ajax' , 'createaddressbook.php' ) ;
} else {
url = OC . filePath ( 'contacts' , 'ajax' , 'updateaddressbook.php' ) ;
}
$ . post ( url , { id : bookid , name : displayname , active : active , description : description } ,
2012-02-20 18:24:54 +04:00
function ( jsondata ) {
if ( jsondata . status == 'success' ) {
2012-02-22 02:15:26 +04:00
$ ( button ) . closest ( 'tr' ) . prev ( ) . html ( jsondata . page ) . show ( ) . next ( ) . remove ( ) ;
2012-02-20 18:24:54 +04:00
Contacts . UI . Contacts . update ( ) ;
} else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
}
2012-04-23 23:14:10 +04:00
} ) ;
2012-02-06 10:32:57 +04:00
} ,
cancel : function ( button , bookid ) {
$ ( button ) . closest ( 'tr' ) . prev ( ) . show ( ) . next ( ) . remove ( ) ;
}
} ,
Contacts : {
2012-06-28 00:43:57 +04:00
batchnum : 50 ,
2012-06-25 05:41:28 +04:00
drop : function ( event , ui ) {
var dragitem = ui . draggable , droptarget = $ ( this ) ;
2012-07-15 23:34:56 +04:00
if ( dragitem . is ( 'li' ) ) {
Contacts . UI . Contacts . dropContact ( event , dragitem , droptarget ) ;
} else {
Contacts . UI . Contacts . dropAddressbook ( event , dragitem , droptarget ) ;
}
} ,
dropContact : function ( event , dragitem , droptarget ) {
2012-06-25 05:41:28 +04:00
if ( dragitem . data ( 'bookid' ) == droptarget . data ( 'id' ) ) {
return false ;
}
var droplist = ( droptarget . is ( 'ul' ) ) ? droptarget : droptarget . next ( ) ;
2012-07-15 23:34:56 +04:00
$ . post ( OC . filePath ( 'contacts' , 'ajax' , 'movetoaddressbook.php' ) , { ids : dragitem . data ( 'id' ) , aid : droptarget . data ( 'id' ) } ,
2012-06-25 05:41:28 +04:00
function ( jsondata ) {
if ( jsondata . status == 'success' ) {
// Do some inserting/removing/sorting magic
var name = $ ( dragitem ) . find ( 'a' ) . html ( ) ;
var added = false ;
$ ( droplist ) . children ( ) . each ( function ( ) {
2012-07-16 00:04:33 +04:00
if ( $ ( this ) . text ( ) . toLowerCase ( ) > name . toLowerCase ( ) ) {
$ ( this ) . before ( dragitem . detach ( ) ) ; //.fadeIn('slow');
2012-06-25 05:41:28 +04:00
added = true ;
return false ;
}
} ) ;
if ( ! added ) {
$ ( droplist ) . append ( dragitem . detach ( ) ) ;
}
2012-07-15 06:15:57 +04:00
dragitem . attr ( 'data-bookid' , droptarget . data ( 'id' ) )
2012-06-25 05:41:28 +04:00
dragitem . data ( 'bookid' , droptarget . data ( 'id' ) ) ;
Contacts . UI . Contacts . scrollTo ( dragitem . data ( 'id' ) ) ;
} else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
}
} ) ;
} ,
2012-07-15 23:34:56 +04:00
dropAddressbook : function ( event , dragitem , droptarget ) {
alert ( 'Dropping address books not implemented yet' ) ;
} ,
2012-04-07 18:03:11 +04:00
// Reload the contacts list.
2012-07-15 06:15:57 +04:00
update : function ( params ) {
if ( ! params ) { params = { } ; }
if ( ! params . start ) {
if ( params . aid ) {
$ ( '#contacts h3[data-id="' + params . aid + '"],#contacts ul[data-id="' + params . aid + '"]' ) . remove ( ) ;
2012-07-14 16:25:25 +04:00
} else {
$ ( '#contacts' ) . empty ( ) ;
}
}
2012-06-28 00:43:57 +04:00
self = this ;
2012-07-15 06:15:57 +04:00
console . log ( 'update: ' + params . cid + ' ' + params . aid + ' ' + params . start ) ;
2012-06-28 00:43:57 +04:00
var firstrun = false ;
var opts = { } ;
2012-07-15 06:15:57 +04:00
opts [ 'startat' ] = ( params . start ? params . start : 0 ) ;
if ( params . aid ) {
opts [ 'aid' ] = params . aid ;
2012-06-28 00:43:57 +04:00
}
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'contacts.php' ) , opts , function ( jsondata ) {
2012-02-06 10:32:57 +04:00
if ( jsondata . status == 'success' ) {
2012-06-28 00:43:57 +04:00
var books = jsondata . data . entries ;
2012-07-15 11:54:31 +04:00
$ . each ( books , function ( b , book ) {
2012-06-28 00:43:57 +04:00
if ( $ ( '#contacts h3[data-id="' + b + '"]' ) . length == 0 ) {
firstrun = true ;
if ( $ ( '#contacts h3' ) . length == 0 ) {
2012-07-14 16:25:25 +04:00
$ ( '#contacts' ) . html ( '<h3 class="addressbook" contextmenu="addressbookmenu" data-id="' + b + '">' + book . displayname + '</h3><ul class="contacts hidden" data-id="' + b + '"></ul>' ) ;
2012-06-28 00:43:57 +04:00
} else {
if ( ! $ ( '#contacts h3[data-id="' + b + '"]' ) . length ) {
2012-07-15 11:54:31 +04:00
var item = $ ( '<h3 class="addressbook" contextmenu="addressbookmenu" data-id="' + b + '">' + book . displayname + '</h3><ul class="contacts hidden" data-id="' + b + '"></ul>' )
var added = false ;
$ ( '#contacts h3' ) . each ( function ( ) {
if ( $ ( this ) . text ( ) . toLowerCase ( ) > book . displayname . toLowerCase ( ) ) {
$ ( this ) . before ( item ) . fadeIn ( 'fast' ) ;
added = true ;
return false ;
}
} ) ;
if ( ! added ) {
$ ( '#contacts' ) . append ( item ) ;
}
2012-06-17 22:23:20 +04:00
}
2012-06-28 00:43:57 +04:00
}
$ ( '#contacts h3[data-id="' + b + '"]' ) . on ( 'click' , function ( event ) {
$ ( '#contacts h3' ) . removeClass ( 'active' ) ;
$ ( this ) . addClass ( 'active' ) ;
$ ( '#contacts ul[data-id="' + b + '"]' ) . slideToggle ( 300 ) ;
return false ;
2012-06-25 20:23:48 +04:00
} ) ;
2012-07-15 23:34:56 +04:00
var accept = 'li:not([data-bookid="' + b + '"]),h3:not([data-id="' + b + '"])' ;
2012-07-15 14:27:46 +04:00
$ ( '#contacts h3[data-id="' + b + '"],#contacts ul[data-id="' + b + '"]' ) . droppable ( {
2012-06-28 00:43:57 +04:00
drop : Contacts . UI . Contacts . drop ,
activeClass : 'ui-state-hover' ,
accept : accept
2012-06-25 19:59:49 +04:00
} ) ;
2012-06-28 00:43:57 +04:00
}
var contactlist = $ ( '#contacts ul[data-id="' + b + '"]' ) ;
for ( var c in book . contacts ) {
if ( book . contacts [ c ] . id == undefined ) { continue ; }
2012-06-30 01:17:32 +04:00
if ( $ ( '#contacts li[data-id="' + book . contacts [ c ] [ 'id' ] + '"][data-id="' + book . contacts [ c ] [ 'bookid' ] + '"]' ) . length == 0 ) {
var contact = Contacts . UI . Card . createEntry ( book . contacts [ c ] ) ;
if ( c == self . batchnum - 5 ) {
contact . bind ( 'inview' , function ( event , isInView , visiblePartX , visiblePartY ) {
$ ( this ) . unbind ( event ) ;
var bookid = $ ( this ) . data ( 'bookid' ) ;
var numsiblings = $ ( '.contacts li[data-bookid="' + bookid + '"]' ) . length ;
if ( isInView && numsiblings >= self . batchnum ) {
console . log ( 'This would be a good time to load more contacts.' ) ;
2012-07-15 06:15:57 +04:00
Contacts . UI . Contacts . update ( { cid : params . cid , aid : bookid , start : $ ( '#contacts li[data-bookid="' + bookid + '"]' ) . length } ) ;
2012-06-30 01:17:32 +04:00
}
} ) ;
}
contactlist . append ( contact ) ;
2012-06-28 00:43:57 +04:00
}
2012-06-25 19:59:49 +04:00
}
2012-06-17 22:23:20 +04:00
} ) ;
2012-06-28 00:43:57 +04:00
if ( $ ( '#contacts h3' ) . length > 1 ) {
2012-07-15 23:34:56 +04:00
$ ( '#contacts li,#contacts h3' ) . draggable ( {
2012-06-28 00:43:57 +04:00
revert : 'invalid' ,
axis : 'y' , containment : '#contacts' ,
scroll : true , scrollSensitivity : 100 ,
opacity : 0.7 , helper : 'clone'
} ) ;
} else {
$ ( '#contacts h3' ) . first ( ) . addClass ( 'active' ) ;
}
if ( opts [ 'startat' ] == 0 ) { // only update card on first load.
2012-07-15 06:15:57 +04:00
Contacts . UI . Card . update ( params ) ;
2012-06-28 00:43:57 +04:00
}
2012-02-06 10:32:57 +04:00
}
else {
2012-03-08 01:57:37 +04:00
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
}
} ) ;
2012-03-27 12:12:30 +04:00
} ,
refreshThumbnail : function ( id ) {
2012-06-20 23:00:07 +04:00
var item = $ ( '.contacts li[data-id="' + id + '"]' ) . find ( 'a' ) ;
2012-05-10 18:53:25 +04:00
item . html ( Contacts . UI . Card . fn ) ;
item . css ( 'background' , 'url(' + OC . filePath ( 'contacts' , '' , 'thumbnail.php' ) + '?id=' + id + '&refresh=1' + Math . random ( ) + ') no-repeat' ) ;
2012-05-14 13:05:50 +04:00
} ,
scrollTo : function ( id ) {
2012-06-20 23:00:07 +04:00
var item = $ ( '#contacts li[data-id="' + id + '"]' ) ;
if ( item ) {
$ ( '.contacts' ) . animate ( {
scrollTop : $ ( '#contacts li[data-id="' + id + '"]' ) . offset ( ) . top - 20 } , 'slow' , 'swing' ) ;
}
2012-02-06 10:32:57 +04:00
}
}
}
}
$ ( document ) . ready ( function ( ) {
2012-03-12 17:12:27 +04:00
OCCategories . changed = Contacts . UI . Card . categoriesChanged ;
OCCategories . app = 'contacts' ;
2012-02-06 10:32:57 +04:00
2012-04-27 12:34:45 +04:00
$ ( '#notification' ) . click ( function ( ) {
$ ( '#notification' ) . fadeOut ( ) ;
} ) ;
2012-05-02 14:10:44 +04:00
$ ( '#chooseaddressbook' ) . click ( Contacts . UI . Addressbooks . overview ) ;
$ ( '#chooseaddressbook' ) . keydown ( Contacts . UI . Addressbooks . overview ) ;
2012-02-06 10:32:57 +04:00
2012-05-02 14:10:44 +04:00
$ ( '#contacts_newcontact' ) . click ( Contacts . UI . Card . editNew ) ;
$ ( '#contacts_newcontact' ) . keydown ( Contacts . UI . Card . editNew ) ;
2012-02-06 10:32:57 +04:00
2012-05-10 22:43:40 +04:00
// Load a contact.
2012-06-17 01:37:24 +04:00
$ ( '.contacts' ) . keydown ( function ( event ) {
2012-07-15 06:15:57 +04:00
if ( event . which == 13 || event . which == 32 ) {
2012-06-17 01:37:24 +04:00
$ ( '.contacts' ) . click ( ) ;
2012-05-03 13:11:26 +04:00
}
} ) ;
2012-06-17 22:23:20 +04:00
$ ( document ) . on ( 'click' , '.contacts' , function ( event ) {
2012-05-10 22:43:40 +04:00
var $tgt = $ ( event . target ) ;
2012-05-14 19:40:16 +04:00
if ( $tgt . is ( 'li' ) || $tgt . is ( 'a' ) ) {
var item = $tgt . is ( 'li' ) ? $ ( $tgt ) : ( $tgt ) . parent ( ) ;
var id = item . data ( 'id' ) ;
2012-06-18 01:14:02 +04:00
var bookid = item . data ( 'bookid' ) ;
2012-05-14 19:40:16 +04:00
item . addClass ( 'active' ) ;
2012-05-10 22:43:40 +04:00
var oldid = $ ( '#rightcontent' ) . data ( 'id' ) ;
if ( oldid != 0 ) {
2012-07-15 23:58:45 +04:00
var olditem = $ ( '.contacts li[data-id="' + oldid + '"]' ) ;
var oldbookid = olditem . data ( 'bookid' ) ;
olditem . removeClass ( 'active' ) ;
if ( oldbookid != bookid ) {
$ ( '#contacts h3[data-id="' + oldbookid + '"]' ) . removeClass ( 'active' ) ;
$ ( '#contacts h3[data-id="' + bookid + '"]' ) . addClass ( 'active' ) ;
}
2012-02-06 10:32:57 +04:00
}
2012-05-10 22:43:40 +04:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'contactdetails.php' ) , { 'id' : id } , function ( jsondata ) {
if ( jsondata . status == 'success' ) {
2012-06-18 01:14:02 +04:00
Contacts . UI . Card . loadContact ( jsondata . data , bookid ) ;
2012-05-10 22:43:40 +04:00
}
else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
}
} ) ;
}
2012-02-06 10:32:57 +04:00
return false ;
} ) ;
2012-03-07 19:39:56 +04:00
$ ( '.contacts_property' ) . live ( 'change' , function ( ) {
2012-02-06 10:32:57 +04:00
Contacts . UI . Card . saveProperty ( this ) ;
} ) ;
2012-07-15 22:45:54 +04:00
$ ( function ( ) {
// Upload function for dropped contact photos files. Should go in the Contacts class/object.
$ . fileUpload = function ( files ) {
var file = files [ 0 ] ;
if ( file . size > $ ( '#max_upload' ) . val ( ) ) {
OC . dialogs . alert ( t ( 'contacts' , 'The file you are trying to upload exceed the maximum size for file uploads on this server.' ) , t ( 'contacts' , 'Upload too large' ) ) ;
return ;
}
if ( file . type . indexOf ( "image" ) != 0 ) {
OC . dialogs . alert ( t ( 'contacts' , 'Only image files can be used as profile picture.' ) , t ( 'contacts' , 'Wrong file type' ) ) ;
return ;
}
var xhr = new XMLHttpRequest ( ) ;
2012-02-06 10:32:57 +04:00
2012-07-15 22:45:54 +04:00
if ( ! xhr . upload ) {
OC . dialogs . alert ( t ( 'contacts' , 'Your browser doesn\'t support AJAX upload. Please click on the profile picture to select a photo to upload.' ) , t ( 'contacts' , 'Error' ) )
}
fileUpload = xhr . upload ,
xhr . onreadystatechange = function ( ) {
if ( xhr . readyState == 4 ) {
response = $ . parseJSON ( xhr . responseText ) ;
if ( response . status == 'success' ) {
if ( xhr . status == 200 ) {
Contacts . UI . Card . editPhoto ( response . data . id , response . data . tmp ) ;
} else {
OC . dialogs . alert ( xhr . status + ': ' + xhr . responseText , t ( 'contacts' , 'Error' ) ) ;
}
2012-02-06 10:32:57 +04:00
} else {
2012-07-15 22:45:54 +04:00
OC . dialogs . alert ( response . data . message , t ( 'contacts' , 'Error' ) ) ;
2012-02-06 10:32:57 +04:00
}
}
2012-07-15 22:45:54 +04:00
} ;
fileUpload . onprogress = function ( e ) {
if ( e . lengthComputable ) {
var _progress = Math . round ( ( e . loaded * 100 ) / e . total ) ;
//if (_progress != 100){
//}
}
} ;
xhr . open ( 'POST' , OC . filePath ( 'contacts' , 'ajax' , 'uploadphoto.php' ) + '?id=' + Contacts . UI . Card . id + '&requesttoken=' + requesttoken + '&imagefile=' + encodeURIComponent ( file . name ) , true ) ;
xhr . setRequestHeader ( 'Cache-Control' , 'no-cache' ) ;
xhr . setRequestHeader ( 'X-Requested-With' , 'XMLHttpRequest' ) ;
xhr . setRequestHeader ( 'X_FILE_NAME' , encodeURIComponent ( file . name ) ) ;
xhr . setRequestHeader ( 'X-File-Size' , file . size ) ;
xhr . setRequestHeader ( 'Content-Type' , file . type ) ;
xhr . send ( file ) ;
}
} ) ;
2012-02-06 10:32:57 +04:00
2012-07-15 06:15:57 +04:00
$ ( document ) . bind ( 'drop dragover' , function ( e ) {
e . preventDefault ( ) ; // prevent browser from doing anything, if file isn't dropped in dropZone
} ) ;
//add multiply file upload attribute to all browsers except konqueror (which crashes when it's used)
if ( navigator . userAgent . search ( /konqueror/i ) == - 1 ) {
$ ( '#import_upload_start' ) . attr ( 'multiple' , 'multiple' )
}
2012-07-14 16:25:25 +04:00
// Import using jquery.fileupload
$ ( function ( ) {
2012-07-15 06:15:57 +04:00
var uploadingFiles = { } , numfiles = 0 , uploadedfiles = 0 , retries = 0 ;
var aid ;
2012-07-14 16:25:25 +04:00
$ ( '#import_upload_start' ) . fileupload ( {
2012-07-15 06:15:57 +04:00
dropZone : $ ( '#contacts' ) , // restrict dropZone to contacts list.
2012-07-15 14:20:26 +04:00
acceptFileTypes : /^text\/(directory|vcard|x-vcard)$/i ,
2012-07-14 16:25:25 +04:00
add : function ( e , data ) {
var files = data . files ;
var totalSize = 0 ;
2012-07-15 06:15:57 +04:00
if ( files ) {
numfiles += files . length ; uploadedfiles = 0 ;
for ( var i = 0 ; i < files . length ; i ++ ) {
if ( files [ i ] . size == 0 && files [ i ] . type == '' ) {
2012-07-14 16:25:25 +04:00
OC . dialogs . alert ( t ( 'files' , 'Unable to upload your file as it is a directory or has 0 bytes' ) , t ( 'files' , 'Upload Error' ) ) ;
return ;
}
totalSize += files [ i ] . size ;
}
}
if ( totalSize > $ ( '#max_upload' ) . val ( ) ) {
2012-07-15 06:15:57 +04:00
OC . dialogs . alert ( t ( 'contacts' , 'The file you are trying to upload exceed the maximum size for file uploads on this server.' ) , t ( 'contacts' , 'Upload too large' ) ) ;
numfiles = uploadedfiles = retries = aid = 0 ;
uploadingFiles = { } ;
return ;
2012-07-14 16:25:25 +04:00
} else {
if ( $ . support . xhrFileUpload ) {
for ( var i = 0 ; i < files . length ; i ++ ) {
var fileName = files [ i ] . name ;
var dropTarget ;
if ( $ ( e . originalEvent . target ) . is ( 'h3' ) ) {
dropTarget = $ ( e . originalEvent . target ) . next ( 'ul' ) ;
} else {
dropTarget = $ ( e . originalEvent . target ) . closest ( 'ul' ) ;
}
if ( dropTarget && dropTarget . hasClass ( 'contacts' ) ) { // TODO: More thorough check for where we are.
2012-07-15 06:15:57 +04:00
aid = dropTarget . attr ( 'data-id' ) ;
2012-07-14 16:25:25 +04:00
} else {
2012-07-15 06:15:57 +04:00
aid = undefined ;
2012-07-14 16:25:25 +04:00
}
2012-07-15 06:15:57 +04:00
var jqXHR = $ ( '#import_upload_start' ) . fileupload ( 'send' , { files : files [ i ] ,
formData : function ( form ) {
var formArray = form . serializeArray ( ) ;
formArray [ 'aid' ] = aid ;
return formArray ;
} } )
. success ( function ( result , textStatus , jqXHR ) {
if ( result . status == 'success' ) {
// import the file
uploadedfiles += 1 ;
} else {
2012-07-15 12:29:03 +04:00
Contacts . UI . notify ( { message : jsondata . data . message } ) ;
2012-07-15 06:15:57 +04:00
}
return false ;
} )
. error ( function ( jqXHR , textStatus , errorThrown ) {
2012-07-15 14:20:26 +04:00
console . log ( textStatus ) ;
2012-07-15 12:29:03 +04:00
Contacts . UI . notify ( { message : errorThrown + ': ' + textStatus , } ) ;
2012-07-15 06:15:57 +04:00
} ) ;
uploadingFiles [ fileName ] = jqXHR ;
2012-07-14 16:25:25 +04:00
}
2012-07-15 06:15:57 +04:00
} else {
2012-07-14 16:25:25 +04:00
data . submit ( ) . success ( function ( data , status ) {
response = jQuery . parseJSON ( data [ 0 ] . body . innerText ) ;
if ( response [ 0 ] != undefined && response [ 0 ] . status == 'success' ) {
var file = response [ 0 ] ;
delete uploadingFiles [ file . name ] ;
$ ( 'tr' ) . filterAttr ( 'data-file' , file . name ) . data ( 'mime' , file . mime ) ;
var size = $ ( 'tr' ) . filterAttr ( 'data-file' , file . name ) . find ( 'td.filesize' ) . text ( ) ;
if ( size == t ( 'files' , 'Pending' ) ) {
$ ( 'tr' ) . filterAttr ( 'data-file' , file . name ) . find ( 'td.filesize' ) . text ( file . size ) ;
}
FileList . loadingDone ( file . name ) ;
} else {
2012-07-15 12:29:03 +04:00
Contacts . UI . notify ( { message : response . data . message } ) ;
2012-07-14 16:25:25 +04:00
}
} ) ;
}
}
} ,
fail : function ( e , data ) {
console . log ( 'fail' ) ;
2012-07-15 12:29:03 +04:00
Contacts . UI . notify ( { message : data . errorThrown + ': ' + data . textStatus } ) ;
2012-07-15 12:12:47 +04:00
// TODO: Remove file from upload queue.
2012-07-14 16:25:25 +04:00
} ,
progressall : function ( e , data ) {
2012-07-15 06:15:57 +04:00
var progress = ( data . loaded / data . total ) * 50 ;
2012-07-14 16:25:25 +04:00
$ ( '#uploadprogressbar' ) . progressbar ( 'value' , progress ) ;
} ,
start : function ( e , data ) {
$ ( '#uploadprogressbar' ) . progressbar ( { value : 0 } ) ;
$ ( '#uploadprogressbar' ) . fadeIn ( ) ;
if ( data . dataType != 'iframe ' ) {
$ ( '#upload input.stop' ) . show ( ) ;
}
} ,
stop : function ( e , data ) {
2012-07-15 06:15:57 +04:00
// stop only gets fired once so we collect uploaded items here.
var importFiles = function ( aid , fileList ) {
// Create a closure that can be called from different places.
if ( numfiles != uploadedfiles ) {
2012-07-15 12:29:03 +04:00
Contacts . UI . notify ( { message : t ( 'contacts' , 'Not all files uploaded. Retrying...' ) } ) ;
2012-07-15 06:15:57 +04:00
retries += 1 ;
if ( retries > 0 ) {
numfiles = uploadedfiles = retries = aid = 0 ;
uploadingFiles = { } ;
OC . dialogs . alert ( t ( 'contacts' , 'Something went wrong with the upload, please retry.' ) , t ( 'contacts' , 'Error' ) ) ;
return ;
}
setTimeout ( function ( ) { // Just to let any uploads finish
importFiles ( aid , uploadingFiles ) ;
} , 1000 ) ;
}
$ ( '#uploadprogressbar' ) . progressbar ( 'value' , 50 ) ;
var todo = uploadedfiles ;
$ . each ( fileList , function ( fileName , data ) {
Contacts . UI . Addressbooks . doImport ( fileName , aid ) ;
delete fileList [ fileName ] ;
numfiles -= 1 ; uploadedfiles -= 1 ;
$ ( '#uploadprogressbar' ) . progressbar ( 'value' , 50 + ( 50 / ( todo - uploadedfiles ) ) ) ;
} )
$ ( '#uploadprogressbar' ) . progressbar ( 'value' , 100 ) ;
$ ( '#uploadprogressbar' ) . fadeOut ( ) ;
setTimeout ( function ( ) {
Contacts . UI . Contacts . update ( { aid : aid } ) ;
numfiles = uploadedfiles = retries = aid = 0 ;
} , 1000 ) ;
}
if ( ! aid ) {
// Either selected with filepicker or dropped outside of an address book.
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'selectaddressbook.php' ) , { } , function ( jsondata ) {
if ( jsondata . status == 'success' ) {
if ( $ ( '#selectaddressbook_dialog' ) . dialog ( 'isOpen' ) == true ) {
$ ( '#selectaddressbook_dialog' ) . dialog ( 'moveToTop' ) ;
} else {
$ ( '#dialog_holder' ) . html ( jsondata . data . page ) . ready ( function ( $ ) {
$ ( '#selectaddressbook_dialog' ) . dialog ( {
modal : true , height : 'auto' , width : 'auto' ,
buttons : {
'Ok' : function ( ) {
aid = $ ( '#selectaddressbook_dialog' ) . find ( 'input:checked' ) . val ( ) ;
if ( aid == 'new' ) {
var displayname = $ ( '#selectaddressbook_dialog' ) . find ( 'input.name' ) . val ( ) ;
var description = $ ( '#selectaddressbook_dialog' ) . find ( 'input.desc' ) . val ( ) ;
if ( ! displayname . trim ( ) ) {
OC . dialogs . alert ( t ( 'contacts' , 'The address book name cannot be empty.' ) , t ( 'contacts' , 'Error' ) ) ;
return false ;
}
$ ( this ) . dialog ( 'close' ) ;
Contacts . UI . Addressbooks . addAddressbook ( displayname , description , function ( addressbook ) {
aid = addressbook . id ;
setTimeout ( function ( ) {
importFiles ( aid , uploadingFiles ) ;
} , 500 ) ;
console . log ( 'aid ' + aid ) ;
} ) ;
} else {
setTimeout ( function ( ) {
importFiles ( aid , uploadingFiles ) ;
} , 500 ) ;
console . log ( 'aid ' + aid ) ;
$ ( this ) . dialog ( 'close' ) ;
}
} ,
'Cancel' : function ( ) {
$ ( this ) . dialog ( 'close' ) ;
numfiles = uploadedfiles = retries = aid = 0 ;
uploadingFiles = { } ;
$ ( '#uploadprogressbar' ) . fadeOut ( ) ;
}
} ,
close : function ( event , ui ) {
// TODO: If numfiles != 0 delete tmp files after a timeout.
$ ( this ) . dialog ( 'destroy' ) . remove ( ) ;
}
} ) ;
} ) ;
}
} else {
OC . dialogs . alert ( jsondata . data . message , t ( 'contacts' , 'Error' ) ) ;
}
} ) ;
} else {
// Dropped on an address book or it's list.
setTimeout ( function ( ) { // Just to let any uploads finish
importFiles ( aid , uploadingFiles ) ;
} , 1000 ) ;
}
2012-07-14 16:25:25 +04:00
if ( data . dataType != 'iframe ' ) {
$ ( '#upload input.stop' ) . hide ( ) ;
}
}
} )
} ) ;
2012-06-19 16:56:21 +04:00
Contacts . UI . loadHandlers ( ) ;
2012-07-15 06:15:57 +04:00
Contacts . UI . Contacts . update ( { cid : id } ) ;
2012-02-06 10:32:57 +04:00
} ) ;