2012-12-17 22:43:24 +01:00
Modernizr . load ( {
2013-01-02 02:22:16 +01:00
test : Modernizr . input . placeholder ,
nope : [
OC . filePath ( 'contacts' , 'css' , 'placeholder_polyfill.min.css' ) ,
OC . filePath ( 'contacts' , 'js' , 'placeholder_polyfill.jquery.min.combo.js' )
]
2012-12-17 22:43:24 +01:00
} ) ;
2013-01-25 14:30:53 +01:00
( function ( $ ) {
$ . QueryString = ( function ( a ) {
if ( a == "" ) return { } ;
var b = { } ;
for ( var i = 0 ; i < a . length ; ++ i )
{
var p = a [ i ] . split ( '=' ) ;
if ( p . length != 2 ) continue ;
b [ p [ 0 ] ] = decodeURIComponent ( p [ 1 ] . replace ( /\+/g , " " ) ) ;
}
return b ;
} ) ( window . location . search . substr ( 1 ) . split ( '&' ) )
} ) ( jQuery ) ;
2012-11-01 15:31:58 +01:00
var utils = { } ;
/ * *
* utils . isArray
*
* Best guess if object is an array .
* /
utils . isArray = function ( obj ) {
// do an instanceof check first
if ( obj instanceof Array ) {
return true ;
}
// then check for obvious falses
if ( typeof obj !== 'object' ) {
return false ;
}
if ( utils . type ( obj ) === 'array' ) {
return true ;
}
return false ;
2012-11-15 00:28:43 +01:00
} ;
utils . isInt = function ( s ) {
return typeof s === 'number' && ( s . toString ( ) . search ( /^-?[0-9]+$/ ) === 0 ) ;
2012-12-17 15:19:22 +01:00
} ;
2012-11-15 00:28:43 +01:00
utils . isUInt = function ( s ) {
return typeof s === 'number' && ( s . toString ( ) . search ( /^[0-9]+$/ ) === 0 ) ;
2012-12-17 15:19:22 +01:00
} ;
2012-11-01 15:31:58 +01:00
/ * *
* utils . type
*
* Attempt to ascertain actual object type .
* /
utils . type = function ( obj ) {
if ( obj === null || typeof obj === 'undefined' ) {
return String ( obj ) ;
}
return Object . prototype . toString . call ( obj )
. replace ( /\[object ([a-zA-Z]+)\]/ , '$1' ) . toLowerCase ( ) ;
} ;
utils . moveCursorToEnd = function ( el ) {
2012-11-22 08:17:57 +01:00
if ( typeof el . selectionStart === 'number' ) {
2012-11-01 15:31:58 +01:00
el . selectionStart = el . selectionEnd = el . value . length ;
2012-11-22 08:17:57 +01:00
} else if ( typeof el . createTextRange !== 'undefined' ) {
2012-11-01 15:31:58 +01:00
el . focus ( ) ;
var range = el . createTextRange ( ) ;
range . collapse ( false ) ;
range . select ( ) ;
}
2012-12-17 15:19:22 +01:00
} ;
2012-11-01 15:31:58 +01:00
2012-09-17 16:15:31 +02:00
if ( typeof Object . create !== 'function' ) {
2012-10-05 05:05:49 +02:00
Object . create = function ( o ) {
function F ( ) { }
F . prototype = o ;
return new F ( ) ;
} ;
2012-09-17 16:15:31 +02:00
}
2012-11-22 00:24:31 +01:00
Array . prototype . clone = function ( ) {
return this . slice ( 0 ) ;
} ;
2012-09-17 16:15:31 +02:00
Array . prototype . clean = function ( deleteValue ) {
2012-11-22 00:24:31 +01:00
var arr = this . clone ( ) ;
for ( var i = 0 ; i < arr . length ; i ++ ) {
if ( arr [ i ] == deleteValue ) {
arr . splice ( i , 1 ) ;
2012-09-17 16:15:31 +02:00
i -- ;
}
}
2012-11-22 00:24:31 +01:00
return arr ;
2012-09-17 16:15:31 +02:00
} ;
2012-10-05 05:05:49 +02:00
// Keep it DRY ;)
var wrongKey = function ( event ) {
2012-12-17 21:03:10 +01:00
return ( ( event . type === 'keydown' || event . type === 'keypress' )
&& ( event . keyCode !== 32 && event . keyCode !== 13 ) ) ;
2012-12-17 15:19:22 +01:00
} ;
2012-10-05 05:05:49 +02:00
/ * *
* Simply notifier
* Arguments :
* @ param message The text message to show .
* @ param timeout The timeout in seconds before the notification disappears . Default 10.
* @ param timeouthandler A function to run on timeout .
* @ param clickhandler A function to run on click . If a timeouthandler is given it will be cancelled on click .
* @ param data An object that will be passed as argument to the timeouthandler and clickhandler functions .
* @ param cancel If set cancel all ongoing timer events and hide the notification .
* /
OC . notify = function ( params ) {
var self = this ;
if ( ! self . notifier ) {
self . notifier = $ ( '#notification' ) ;
}
if ( params . cancel ) {
self . notifier . off ( 'click' ) ;
for ( var id in self . notifier . data ( ) ) {
if ( $ . isNumeric ( id ) ) {
clearTimeout ( parseInt ( id ) ) ;
}
}
self . notifier . text ( '' ) . fadeOut ( ) . removeData ( ) ;
return ;
}
self . notifier . text ( params . message ) ;
2013-02-08 19:11:25 +01:00
self . notifier . fadeIn ( ) . css ( 'display' , 'inline' ) ;
2012-10-05 05:05:49 +02:00
self . notifier . on ( 'click' , function ( ) { $ ( this ) . fadeOut ( ) ; } ) ;
var timer = setTimeout ( function ( ) {
self . notifier . fadeOut ( ) ;
if ( params . timeouthandler && $ . isFunction ( params . timeouthandler ) ) {
params . timeouthandler ( self . notifier . data ( dataid ) ) ;
self . notifier . off ( 'click' ) ;
self . notifier . removeData ( dataid ) ;
}
} , params . timeout && $ . isNumeric ( params . timeout ) ? parseInt ( params . timeout ) * 1000 : 10000 ) ;
var dataid = timer . toString ( ) ;
if ( params . data ) {
self . notifier . data ( dataid , params . data ) ;
}
if ( params . clickhandler && $ . isFunction ( params . clickhandler ) ) {
self . notifier . on ( 'click' , function ( ) {
clearTimeout ( timer ) ;
self . notifier . off ( 'click' ) ;
params . clickhandler ( self . notifier . data ( dataid ) ) ;
self . notifier . removeData ( dataid ) ;
} ) ;
}
2012-12-17 15:19:22 +01:00
} ;
2012-10-05 05:05:49 +02:00
2012-10-15 21:33:21 +02:00
2012-09-17 16:15:31 +02:00
OC . Contacts = OC . Contacts || {
2013-01-25 14:30:53 +01:00
init : function ( ) {
2012-11-22 00:25:40 +01:00
if ( oc _debug === true ) {
2013-03-28 03:19:01 +01:00
$ . error = console . error ;
$ ( document ) . ajaxError ( function ( e , xhr , settings , exception ) {
console . error ( 'Error in: ' , settings . url , ' : ' , xhr . responseText , exception ) ;
2012-10-25 23:15:47 +02:00
} ) ;
2012-11-22 00:25:40 +01:00
}
2013-01-25 14:30:53 +01:00
2012-09-17 16:15:31 +02:00
this . scrollTimeoutMiliSecs = 100 ;
this . isScrolling = false ;
this . cacheElements ( ) ;
2013-03-22 14:54:47 +01:00
this . storage = new OC . Contacts . Storage ( ) ;
2012-12-13 02:30:20 +01:00
this . contacts = new OC . Contacts . ContactList (
2013-03-22 14:54:47 +01:00
this . storage ,
2012-10-22 15:35:33 +02:00
this . $contactList ,
this . $contactListItemTemplate ,
2013-03-12 00:28:41 +01:00
this . $contactDragItemTemplate ,
2012-09-17 16:15:31 +02:00
this . $contactFullTemplate ,
this . detailTemplates
) ;
2013-03-23 02:07:17 +01:00
this . groups = new OC . Contacts . GroupList ( this . storage , this . $groupList , this . $groupListItemTemplate ) ;
2012-12-13 02:30:20 +01:00
OCCategories . changed = this . groups . categoriesChanged ;
2012-10-19 22:09:59 +02:00
OCCategories . app = 'contacts' ;
OCCategories . type = 'contact' ;
2012-09-17 16:15:31 +02:00
this . bindEvents ( ) ;
2012-10-05 05:05:49 +02:00
this . $toggleAll . show ( ) ;
2012-12-14 02:52:03 +01:00
this . showActions ( [ 'add' ] ) ;
2012-09-17 16:15:31 +02:00
} ,
loading : function ( obj , state ) {
2012-10-24 20:31:29 +02:00
$ ( obj ) . toggleClass ( 'loading' , state ) ;
2012-09-17 16:15:31 +02:00
} ,
2012-10-05 05:05:49 +02:00
/ * *
* Show / hide elements in the header
* @ param act An array of actions to show based on class name e . g [ 'add' , 'delete' ]
* /
2012-11-13 17:01:14 +01:00
hideActions : function ( ) {
this . showActions ( false ) ;
} ,
2012-10-05 05:05:49 +02:00
showActions : function ( act ) {
2012-12-13 02:38:58 +01:00
console . log ( 'showActions' , act ) ;
//console.trace();
2012-10-05 05:05:49 +02:00
this . $headeractions . children ( ) . hide ( ) ;
2012-11-13 17:01:14 +01:00
if ( act && act . length > 0 ) {
this . $headeractions . children ( '.' + act . join ( ',.' ) ) . show ( ) ;
}
2012-10-05 05:05:49 +02:00
} ,
showAction : function ( act , show ) {
2012-10-24 20:31:29 +02:00
this . $headeractions . find ( '.' + act ) . toggle ( show ) ;
2012-10-05 05:05:49 +02:00
} ,
2012-09-17 16:15:31 +02:00
cacheElements : function ( ) {
var self = this ;
this . detailTemplates = { } ;
// Load templates for contact details.
// The weird double loading is because jquery apparently doesn't
// create a searchable object from a script element.
$ . each ( $ ( $ ( '#contactDetailsTemplate' ) . html ( ) ) , function ( idx , node ) {
2012-12-20 22:58:04 +01:00
var $node = $ ( node ) ;
if ( $node . is ( 'div' ) ) {
2012-09-17 16:15:31 +02:00
var $tmpl = $ ( node . innerHTML ) ;
2012-12-20 22:58:04 +01:00
self . detailTemplates [ $tmpl . data ( 'element' ) ] = $node ;
2012-09-17 16:15:31 +02:00
}
} ) ;
this . $groupListItemTemplate = $ ( '#groupListItemTemplate' ) ;
this . $contactListItemTemplate = $ ( '#contactListItemTemplate' ) ;
2013-03-12 00:28:41 +01:00
this . $contactDragItemTemplate = $ ( '#contactDragItemTemplate' ) ;
2012-09-17 16:15:31 +02:00
this . $contactFullTemplate = $ ( '#contactFullTemplate' ) ;
this . $contactDetailsTemplate = $ ( '#contactDetailsTemplate' ) ;
this . $rightContent = $ ( '#rightcontent' ) ;
this . $header = $ ( '#contactsheader' ) ;
2012-10-05 05:05:49 +02:00
this . $headeractions = this . $header . find ( 'div.actions' ) ;
2012-09-17 16:15:31 +02:00
this . $groupList = $ ( '#grouplist' ) ;
this . $contactList = $ ( '#contactlist' ) ;
this . $contactListHeader = $ ( '#contactlistheader' ) ;
this . $toggleAll = $ ( '#toggle_all' ) ;
2012-10-05 05:05:49 +02:00
this . $groups = this . $headeractions . find ( '.groups' ) ;
this . $ninjahelp = $ ( '#ninjahelp' ) ;
2012-11-15 02:40:47 +01:00
this . $firstRun = $ ( '#firstrun' ) ;
2012-11-13 17:01:14 +01:00
this . $settings = $ ( '#contacts-settings' ) ;
2013-03-29 05:00:03 +01:00
this . $importFileInput = $ ( '#import_upload_start' ) ;
2012-12-03 19:39:28 +01:00
this . $importIntoSelect = $ ( '#import_into' ) ;
2012-10-05 05:05:49 +02:00
} ,
// Build the select to add/remove from groups.
buildGroupSelect : function ( ) {
2012-10-24 20:31:29 +02:00
// If a contact is open we know which categories it's in
2012-10-05 05:05:49 +02:00
if ( this . currentid ) {
2013-02-03 15:40:14 +01:00
var contact = this . contacts . findById ( this . currentid ) ;
if ( contact === null ) {
return false ;
}
2012-10-05 05:05:49 +02:00
this . $groups . find ( 'optgroup,option:not([value="-1"])' ) . remove ( ) ;
var addopts = '' , rmopts = '' ;
2012-12-13 02:30:20 +01:00
$ . each ( this . groups . categories , function ( i , category ) {
2012-10-05 05:05:49 +02:00
if ( contact . inGroup ( category . name ) ) {
rmopts += '<option value="' + category . id + '">' + category . name + '</option>' ;
} else {
addopts += '<option value="' + category . id + '">' + category . name + '</option>' ;
}
} ) ;
if ( addopts . length ) {
$ ( addopts ) . appendTo ( this . $groups )
. wrapAll ( '<optgroup data-action="add" label="' + t ( 'contacts' , 'Add to...' ) + '"/>' ) ;
}
if ( rmopts . length ) {
$ ( rmopts ) . appendTo ( this . $groups )
. wrapAll ( '<optgroup data-action="remove" label="' + t ( 'contacts' , 'Remove from...' ) + '"/>' ) ;
}
2012-12-13 02:30:20 +01:00
} else if ( this . contacts . getSelectedContacts ( ) . length > 0 ) { // Otherwise add all categories to both add and remove
2012-10-05 05:05:49 +02:00
this . $groups . find ( 'optgroup,option:not([value="-1"])' ) . remove ( ) ;
var addopts = '' , rmopts = '' ;
2012-12-13 02:30:20 +01:00
$ . each ( this . groups . categories , function ( i , category ) {
2012-10-05 05:05:49 +02:00
rmopts += '<option value="' + category . id + '">' + category . name + '</option>' ;
addopts += '<option value="' + category . id + '">' + category . name + '</option>' ;
} ) ;
$ ( addopts ) . appendTo ( this . $groups )
. wrapAll ( '<optgroup data-action="add" label="' + t ( 'contacts' , 'Add to...' ) + '"/>' ) ;
$ ( rmopts ) . appendTo ( this . $groups )
. wrapAll ( '<optgroup data-action="remove" label="' + t ( 'contacts' , 'Remove from...' ) + '"/>' ) ;
2012-11-05 12:39:49 +01:00
} else {
// 3rd option: No contact open, none checked, just show "Add group..."
this . $groups . find ( 'optgroup,option:not([value="-1"])' ) . remove ( ) ;
2012-10-05 05:05:49 +02:00
}
$ ( '<option value="add">' + t ( 'contacts' , 'Add group...' ) + '</option>' ) . appendTo ( this . $groups ) ;
this . $groups . val ( - 1 ) ;
2012-09-17 16:15:31 +02:00
} ,
bindEvents : function ( ) {
var self = this ;
2012-10-22 15:35:33 +02:00
2012-11-05 20:45:08 +01:00
// Should fix Opera check for delayed delete.
$ ( window ) . unload ( function ( ) {
$ ( window ) . trigger ( 'beforeunload' ) ;
} ) ;
2013-02-23 22:27:04 +01:00
this . hashChange = function ( ) {
2013-01-25 18:59:28 +01:00
console . log ( 'hashchange' , window . location . hash )
var id = parseInt ( window . location . hash . substr ( 1 ) ) ;
2013-02-23 22:45:21 +01:00
if ( id && id !== self . currentid ) {
2013-01-25 18:59:28 +01:00
self . openContact ( id ) ;
2013-02-23 22:51:28 +01:00
} else if ( ! id && self . currentid ) {
self . closeContact ( self . currentid ) ;
2013-01-25 18:59:28 +01:00
}
2013-02-23 22:27:04 +01:00
}
2013-02-23 22:45:21 +01:00
$ ( window ) . bind ( 'popstate' , this . hashChange ) ;
2013-02-23 22:27:04 +01:00
$ ( window ) . bind ( 'hashchange' , this . hashChange ) ;
2013-01-25 18:59:28 +01:00
2012-10-05 05:05:49 +02:00
// App specific events
2012-09-17 16:15:31 +02:00
$ ( document ) . bind ( 'status.contact.deleted' , function ( e , data ) {
var id = parseInt ( data . id ) ;
2013-02-20 13:14:31 +01:00
if ( id === self . currentid ) {
delete self . currentid ;
}
2012-09-17 16:15:31 +02:00
console . log ( 'contact' , data . id , 'deleted' ) ;
2012-10-05 05:05:49 +02:00
// update counts on group lists
2012-12-17 15:19:22 +01:00
self . groups . removeFromAll ( data . id , true ) ;
2012-10-05 05:05:49 +02:00
} ) ;
2012-11-13 22:42:55 +01:00
$ ( document ) . bind ( 'status.contact.added' , function ( e , data ) {
self . currentid = parseInt ( data . id ) ;
2012-11-15 20:35:35 +01:00
self . buildGroupSelect ( ) ;
2012-12-11 06:18:57 +01:00
self . hideActions ( ) ;
2012-11-13 22:42:55 +01:00
} ) ;
2013-03-25 17:08:47 +01:00
// Keep error messaging at one place to be able to replace it.
2012-10-05 05:05:49 +02:00
$ ( document ) . bind ( 'status.contact.error' , function ( e , data ) {
OC . notify ( { message : data . message } ) ;
2012-09-17 16:15:31 +02:00
} ) ;
2012-11-13 22:42:55 +01:00
2012-09-17 16:15:31 +02:00
$ ( document ) . bind ( 'status.contact.enabled' , function ( e , enabled ) {
2012-12-17 15:19:22 +01:00
console . log ( 'status.contact.enabled' , enabled ) ;
2012-12-11 06:18:57 +01:00
/ * i f ( e n a b l e d ) {
2012-12-10 00:02:06 +01:00
self . showActions ( [ 'back' , 'download' , 'delete' , 'groups' ] ) ;
2012-09-17 16:15:31 +02:00
} else {
2012-10-05 05:05:49 +02:00
self . showActions ( [ 'back' ] ) ;
2012-12-11 06:18:57 +01:00
} * /
2012-09-17 16:15:31 +02:00
} ) ;
2012-11-13 22:42:55 +01:00
2012-09-30 06:38:53 +02:00
$ ( document ) . bind ( 'status.contacts.loaded' , function ( e , result ) {
console . log ( 'status.contacts.loaded' , result ) ;
2012-09-17 16:15:31 +02:00
if ( result . status !== true ) {
alert ( 'Error loading contacts!' ) ;
2012-10-05 05:05:49 +02:00
} else {
self . numcontacts = result . numcontacts ;
2012-10-30 07:03:09 +01:00
self . loading ( self . $rightContent , false ) ;
2012-12-13 02:30:20 +01:00
self . groups . loadGroups ( self . numcontacts , function ( ) {
2012-10-30 07:03:09 +01:00
self . loading ( $ ( '#leftcontent' ) , false ) ;
2013-01-25 18:59:28 +01:00
var id = $ . QueryString [ 'id' ] ; // Keep for backwards compatible links.
2013-01-25 14:30:53 +01:00
self . currentid = parseInt ( id ) ;
2013-01-25 18:59:28 +01:00
if ( ! self . currentid ) {
self . currentid = parseInt ( window . location . hash . substr ( 1 ) ) ;
}
2012-11-01 02:01:00 +01:00
console . log ( 'Groups loaded, currentid' , self . currentid ) ;
2012-10-05 05:05:49 +02:00
if ( self . currentid ) {
self . openContact ( self . currentid ) ;
}
} ) ;
2013-01-25 15:08:56 +01:00
if ( ! result . is _indexed ) {
// Wait a couple of mins then check if contacts are indexed.
setTimeout ( function ( ) {
OC . notify ( { message : t ( 'contacts' , 'Indexing contacts' ) , timeout : 20 } ) ;
$ . post ( OC . filePath ( 'contacts' , 'ajax' , 'indexproperties.php' ) ) ;
} , 10000 ) ;
} else {
console . log ( 'contacts are indexed.' ) ;
}
2012-09-17 16:15:31 +02:00
}
2012-10-05 05:05:49 +02:00
} ) ;
2012-11-13 22:42:55 +01:00
2012-10-05 05:05:49 +02:00
$ ( document ) . bind ( 'status.contact.currentlistitem' , function ( e , result ) {
//console.log('status.contact.currentlistitem', result, self.$rightContent.height());
if ( self . dontScroll !== true ) {
if ( result . pos > self . $rightContent . height ( ) ) {
self . $rightContent . scrollTop ( result . pos - self . $rightContent . height ( ) + result . height ) ;
}
else if ( result . pos < self . $rightContent . offset ( ) . top ) {
self . $rightContent . scrollTop ( result . pos ) ;
}
} else {
setTimeout ( function ( ) {
self . dontScroll = false ;
} , 100 ) ;
}
2012-12-17 15:19:22 +01:00
self . currentlistid = result . id ;
2012-10-05 05:05:49 +02:00
} ) ;
2012-11-23 01:16:46 +01:00
2012-10-05 05:05:49 +02:00
$ ( document ) . bind ( 'status.nomorecontacts' , function ( e , result ) {
console . log ( 'status.nomorecontacts' , result ) ;
2012-11-15 02:40:47 +01:00
self . $contactList . hide ( ) ;
self . $firstRun . show ( ) ;
2012-10-05 05:05:49 +02:00
// TODO: Show a first-run page.
} ) ;
2012-11-23 01:16:46 +01:00
2012-10-05 05:05:49 +02:00
$ ( document ) . bind ( 'status.visiblecontacts' , function ( e , result ) {
console . log ( 'status.visiblecontacts' , result ) ;
// TODO: To be decided.
} ) ;
2012-11-23 01:16:46 +01:00
2012-10-05 05:05:49 +02:00
// A contact id was in the request
$ ( document ) . bind ( 'request.loadcontact' , function ( e , result ) {
console . log ( 'request.loadcontact' , result ) ;
if ( self . numcontacts ) {
self . openContact ( result . id ) ;
} else {
// Contacts are not loaded yet, try again.
console . log ( 'waiting for contacts to load' ) ;
setTimeout ( function ( ) {
$ ( document ) . trigger ( 'request.loadcontact' , {
2012-12-17 15:19:22 +01:00
id : result . id
2012-10-05 05:05:49 +02:00
} ) ;
} , 1000 ) ;
}
} ) ;
2012-11-23 01:16:46 +01:00
2012-12-10 00:02:06 +01:00
$ ( document ) . bind ( 'request.contact.setasfavorite' , function ( e , data ) {
console . log ( 'contact' , data . id , 'request.contact.setasfavorite' ) ;
2012-12-13 02:30:20 +01:00
self . groups . setAsFavorite ( data . id , data . state ) ;
2012-12-10 00:02:06 +01:00
} ) ;
2012-12-14 02:53:06 +01:00
$ ( document ) . bind ( 'request.contact.addtogroup' , function ( e , data ) {
2013-01-16 13:34:30 +01:00
self . groups . addTo ( data . id , data . groupid , function ( response ) {
console . log ( 'contact' , data . id , 'request.contact.addtogroup' , response ) ;
} ) ;
} ) ;
$ ( document ) . bind ( 'request.contact.removefromgroup' , function ( e , data ) {
console . log ( 'contact' , data . id , 'request.contact.removefromgroup' ) ;
self . groups . removeFrom ( data . id , data . groupid ) ;
2012-12-14 02:53:06 +01:00
} ) ;
2012-12-11 04:05:34 +01:00
$ ( document ) . bind ( 'request.contact.export' , function ( e , data ) {
var id = parseInt ( data . id ) ;
console . log ( 'contact' , data . id , 'request.contact.export' ) ;
2013-03-25 17:08:47 +01:00
document . location . href = OC . linkTo ( 'contacts' , 'export.php' ) + '?' + $ . param ( data ) ;
2012-12-11 04:05:34 +01:00
} ) ;
2012-12-10 00:02:06 +01:00
$ ( document ) . bind ( 'request.contact.close' , function ( e , data ) {
var id = parseInt ( data . id ) ;
console . log ( 'contact' , data . id , 'request.contact.close' ) ;
self . closeContact ( id ) ;
} ) ;
2012-12-11 04:05:34 +01:00
$ ( document ) . bind ( 'request.contact.delete' , function ( e , data ) {
2013-03-28 03:19:01 +01:00
var id = parseInt ( data . contactid ) ;
console . log ( 'contact' , data , 'request.contact.delete' ) ;
2013-02-20 13:14:31 +01:00
self . closeContact ( id ) ;
2013-03-28 03:19:01 +01:00
self . contacts . delayedDelete ( data ) ;
2012-12-11 04:05:34 +01:00
self . $contactList . removeClass ( 'dim' ) ;
2012-12-11 06:18:57 +01:00
self . showActions ( [ 'add' ] ) ;
2012-12-11 04:05:34 +01:00
} ) ;
2012-10-15 21:33:21 +02:00
$ ( document ) . bind ( 'request.select.contactphoto.fromlocal' , function ( e , result ) {
console . log ( 'request.select.contactphoto.fromlocal' , result ) ;
$ ( '#contactphoto_fileupload' ) . trigger ( 'click' ) ;
} ) ;
2012-11-23 01:16:46 +01:00
2012-10-15 21:33:21 +02:00
$ ( document ) . bind ( 'request.select.contactphoto.fromcloud' , function ( e , result ) {
console . log ( 'request.select.contactphoto.fromcloud' , result ) ;
OC . dialogs . filepicker ( t ( 'contacts' , 'Select photo' ) , function ( path ) {
self . cloudPhotoSelected ( self . currentid , path ) ;
} , false , 'image' , true ) ;
} ) ;
2012-11-23 01:16:46 +01:00
2012-10-15 21:33:21 +02:00
$ ( document ) . bind ( 'request.edit.contactphoto' , function ( e , result ) {
console . log ( 'request.edit.contactphoto' , result ) ;
self . editCurrentPhoto ( result . id ) ;
} ) ;
2012-11-23 01:16:46 +01:00
2012-10-05 05:05:49 +02:00
$ ( document ) . bind ( 'request.addressbook.activate' , function ( e , result ) {
console . log ( 'request.addressbook.activate' , result ) ;
2012-12-13 02:30:20 +01:00
self . contacts . showFromAddressbook ( result . id , result . activate ) ;
2012-09-17 16:15:31 +02:00
} ) ;
2012-10-22 15:35:33 +02:00
2012-11-23 01:16:46 +01:00
$ ( document ) . bind ( 'status.contact.removedfromgroup' , function ( e , result ) {
console . log ( 'status.contact.removedfromgroup' , result ) ;
if ( self . currentgroup == result . groupid ) {
2012-12-13 02:30:20 +01:00
self . contacts . hideContact ( result . contactid ) ;
2012-11-23 01:16:46 +01:00
self . closeContact ( result . contactid ) ;
}
} ) ;
$ ( document ) . bind ( 'status.group.groupremoved' , function ( e , result ) {
console . log ( 'status.group.groupremoved' , result ) ;
if ( parseInt ( result . groupid ) === parseInt ( self . currentgroup ) ) {
2012-12-13 02:30:20 +01:00
self . contacts . showContacts ( [ ] ) ;
2012-11-23 01:16:46 +01:00
self . currentgroup = 'all' ;
}
$ . each ( result . contacts , function ( idx , contactid ) {
2012-12-13 02:30:20 +01:00
var contact = self . contacts . findById ( contactid ) ;
2012-11-23 02:25:48 +01:00
console . log ( 'contactid' , contactid , contact ) ;
2012-12-17 15:19:22 +01:00
2012-12-13 02:30:20 +01:00
self . contacts . findById ( contactid ) . removeFromGroup ( result . groupname ) ;
2012-11-23 01:16:46 +01:00
} ) ;
2012-11-22 19:32:47 +01:00
} ) ;
2012-10-24 20:31:29 +02:00
$ ( document ) . bind ( 'status.group.contactadded' , function ( e , result ) {
console . log ( 'status.group.contactadded' , result ) ;
2013-03-11 20:27:16 +01:00
var contact = self . contacts . findById ( result . contactid ) ;
if ( ! contact ) {
2013-02-03 15:40:14 +01:00
return false ;
}
contact . addToGroup ( result . groupname ) ;
2012-10-24 20:31:29 +02:00
} ) ;
2012-10-26 01:57:16 +02:00
// Group sorted, save the sort order
$ ( document ) . bind ( 'status.groups.sorted' , function ( e , result ) {
console . log ( 'status.groups.sorted' , result ) ;
2013-03-25 17:08:47 +01:00
$ . when ( self . storage . setPreference ( 'groupsort' , result . sortorder ) ) . then ( function ( response ) {
2013-03-28 03:19:01 +01:00
if ( response . error ) {
OC . notify ( { message : response ? response . message : t ( 'contacts' , 'Network or server error. Please inform administrator.' ) } ) ;
2012-10-26 01:57:16 +02:00
}
2013-03-25 17:08:47 +01:00
} )
. fail ( function ( jqxhr , textStatus , error ) {
var err = textStatus + ', ' + error ;
console . log ( "Request Failed: " + err ) ;
$ ( document ) . trigger ( 'status.contact.error' , {
message : t ( 'contacts' , 'Failed saving sort order: {error}' , { error : err } )
} ) ;
2012-10-26 01:57:16 +02:00
} ) ;
} ) ;
2012-10-23 10:55:18 +02:00
// Group selected, only show contacts from that group
$ ( document ) . bind ( 'status.group.selected' , function ( e , result ) {
console . log ( 'status.group.selected' , result ) ;
self . currentgroup = result . id ;
// Close any open contact.
if ( self . currentid ) {
var id = self . currentid ;
self . closeContact ( id ) ;
2012-12-07 15:08:25 +01:00
self . jumpToContact ( id ) ;
2012-10-23 10:55:18 +02:00
}
self . $contactList . show ( ) ;
self . $toggleAll . show ( ) ;
2012-12-14 02:52:03 +01:00
self . showActions ( [ 'add' ] ) ;
2012-10-23 10:55:18 +02:00
if ( result . type === 'category' || result . type === 'fav' ) {
2012-12-13 02:30:20 +01:00
self . contacts . showContacts ( result . contacts ) ;
2012-10-24 20:31:29 +02:00
} else if ( result . type === 'shared' ) {
2012-12-13 02:30:20 +01:00
self . contacts . showFromAddressbook ( self . currentgroup , true , true ) ;
2012-10-23 10:55:18 +02:00
} else {
2012-12-13 02:30:20 +01:00
self . contacts . showContacts ( self . currentgroup ) ;
2012-10-23 10:55:18 +02:00
}
2013-03-25 17:08:47 +01:00
$ . when ( self . storage . setPreference ( 'lastgroup' , self . currentgroup ) ) . then ( function ( response ) {
2013-03-28 03:19:01 +01:00
if ( response . error ) {
OC . notify ( { message : response . message } ) ;
2012-10-23 10:55:18 +02:00
}
2013-03-25 17:08:47 +01:00
} )
. fail ( function ( jqxhr , textStatus , error ) {
var err = textStatus + ', ' + error ;
console . log ( "Request Failed: " + err ) ;
$ ( document ) . trigger ( 'status.contact.error' , {
message : t ( 'contacts' , 'Failed saving last group: {error}' , { error : err } )
} ) ;
2012-10-23 10:55:18 +02:00
} ) ;
self . $rightContent . scrollTop ( 0 ) ;
} ) ;
2012-09-17 16:15:31 +02:00
// mark items whose title was hid under the top edge as read
/ * t h i s . $ r i g h t C o n t e n t . s c r o l l ( f u n c t i o n ( ) {
// prevent too many scroll requests;
if ( ! self . isScrolling ) {
self . isScrolling = true ;
var num = self . $contactList . find ( 'tr' ) . length ;
//console.log('num', num);
var offset = self . $contactList . find ( 'tr:eq(' + ( num - 20 ) + ')' ) . offset ( ) . top ;
if ( offset < self . $rightContent . height ( ) ) {
console . log ( 'load more' ) ;
2012-12-13 02:30:20 +01:00
self . contacts . loadContacts ( num , function ( ) {
2012-09-17 16:15:31 +02:00
self . isScrolling = false ;
} ) ;
} else {
setTimeout ( function ( ) {
self . isScrolling = false ;
} , self . scrollTimeoutMiliSecs ) ;
}
//console.log('scroll, unseen:', offset, self.$rightContent.height());
}
} ) ; * /
2012-11-13 17:01:14 +01:00
this . $settings . find ( '.settings' ) . on ( 'click keydown' , function ( event ) {
if ( wrongKey ( event ) ) {
return ;
}
2012-11-14 00:42:02 +01:00
var bodyListener = function ( e ) {
if ( self . $settings . find ( $ ( e . target ) ) . length == 0 ) {
self . $settings . switchClass ( 'open' , '' ) ;
}
2012-12-17 15:19:22 +01:00
} ;
2012-11-13 17:01:14 +01:00
if ( self . $settings . hasClass ( 'open' ) ) {
2012-11-14 00:42:02 +01:00
self . $settings . switchClass ( 'open' , '' ) ;
$ ( 'body' ) . unbind ( 'click' , bodyListener ) ;
2012-11-13 17:01:14 +01:00
} else {
2013-02-08 15:23:52 +01:00
// FIXME: Settings needs to be refactored
self . $settings . find ( 'h2' ) . trigger ( 'click' ) ;
2012-11-14 00:42:02 +01:00
self . $settings . switchClass ( '' , 'open' ) ;
$ ( 'body' ) . bind ( 'click' , bodyListener ) ;
2012-11-13 17:01:14 +01:00
}
} ) ;
2012-11-05 12:39:12 +01:00
$ ( '#contactphoto_fileupload' ) . on ( 'change' , function ( ) {
self . uploadPhoto ( this . files ) ;
} ) ;
2012-12-17 21:27:01 +01:00
$ ( '#groupsheader > .addgroup' ) . on ( 'click keydown' , function ( event ) {
2012-11-14 15:27:38 +01:00
if ( wrongKey ( event ) ) {
return ;
}
2012-12-13 02:30:20 +01:00
self . groups . editGroup ( ) ;
2012-12-07 14:47:28 +01:00
//self.addGroup();
2012-11-14 15:27:38 +01:00
} ) ;
2012-10-05 05:05:49 +02:00
this . $ninjahelp . find ( '.close' ) . on ( 'click keydown' , function ( event ) {
if ( wrongKey ( event ) ) {
return ;
}
self . $ninjahelp . hide ( ) ;
} ) ;
2012-11-01 15:31:58 +01:00
2012-10-05 05:05:49 +02:00
this . $toggleAll . on ( 'change' , function ( ) {
2012-10-30 07:03:09 +01:00
var isChecked = $ ( this ) . is ( ':checked' ) ;
2012-11-15 02:41:52 +01:00
self . setAllChecked ( isChecked ) ;
2012-10-05 05:05:49 +02:00
if ( self . $groups . find ( 'option' ) . length === 1 ) {
self . buildGroupSelect ( ) ;
}
2012-11-13 17:01:14 +01:00
if ( isChecked ) {
2012-12-14 02:52:03 +01:00
self . showActions ( [ 'add' , 'download' , 'groups' , 'delete' , 'favorite' ] ) ;
2012-11-13 17:01:14 +01:00
} else {
2012-12-14 02:52:03 +01:00
self . showActions ( [ 'add' ] ) ;
2012-11-13 17:01:14 +01:00
}
2012-10-05 05:05:49 +02:00
} ) ;
2012-11-01 15:31:58 +01:00
2012-10-05 05:05:49 +02:00
this . $contactList . on ( 'change' , 'input:checkbox' , function ( event ) {
if ( $ ( this ) . is ( ':checked' ) ) {
if ( self . $groups . find ( 'option' ) . length === 1 ) {
self . buildGroupSelect ( ) ;
}
2012-12-14 02:52:03 +01:00
self . showActions ( [ 'add' , 'download' , 'groups' , 'delete' , 'favorite' ] ) ;
2012-12-13 02:38:58 +01:00
} else if ( self . contacts . getSelectedContacts ( ) . length === 0 ) {
2012-12-14 02:52:03 +01:00
self . showActions ( [ 'add' ] ) ;
2012-10-05 05:05:49 +02:00
}
} ) ;
2012-11-01 15:31:58 +01:00
2012-12-13 02:38:58 +01:00
// Add to/remove from group multiple contacts.
2012-10-05 05:05:49 +02:00
this . $groups . on ( 'change' , function ( ) {
var $opt = $ ( this ) . find ( 'option:selected' ) ;
var action = $opt . parent ( ) . data ( 'action' ) ;
2013-03-30 17:33:58 +01:00
var groupName , groupId , buildnow = false ;
2012-10-22 15:35:33 +02:00
2013-03-30 17:33:58 +01:00
var contacts = self . contacts . getSelectedContacts ( ) ;
var ids = $ . map ( contacts , function ( c ) { return c . getId ( ) ; } ) ;
2012-11-14 15:27:38 +01:00
2012-11-15 02:41:52 +01:00
self . setAllChecked ( false ) ;
2012-11-23 01:19:16 +01:00
self . $toggleAll . prop ( 'checked' , false ) ;
2012-11-24 01:40:29 +01:00
if ( ! self . currentid ) {
2012-12-14 02:52:03 +01:00
self . showActions ( [ 'add' ] ) ;
2012-11-24 01:40:29 +01:00
}
2012-12-17 15:19:22 +01:00
2012-11-14 15:27:38 +01:00
if ( $opt . val ( ) === 'add' ) { // Add new group
action = 'add' ;
2012-10-05 05:05:49 +02:00
console . log ( 'add group...' ) ;
self . $groups . val ( - 1 ) ;
2012-11-14 15:27:38 +01:00
self . addGroup ( function ( response ) {
2013-03-29 23:23:47 +01:00
if ( ! response . error ) {
2012-11-14 15:27:38 +01:00
groupId = response . id ;
groupName = response . name ;
2012-12-13 02:30:20 +01:00
self . groups . addTo ( ids , groupId , function ( result ) {
2012-11-14 15:27:38 +01:00
if ( result . status === 'success' ) {
$ . each ( ids , function ( idx , id ) {
// Delay each contact to not trigger too many ajax calls
// at a time.
setTimeout ( function ( ) {
2013-02-03 15:40:14 +01:00
var contact = self . contacts . findById ( id ) ;
if ( contact === null ) {
return true ;
}
contact . addToGroup ( groupName ) ;
2012-11-14 15:27:38 +01:00
// I don't think this is used...
if ( buildnow ) {
self . buildGroupSelect ( ) ;
2012-10-19 22:09:59 +02:00
}
2012-11-14 15:27:38 +01:00
$ ( document ) . trigger ( 'status.contact.addedtogroup' , {
contactid : id ,
groupid : groupId ,
2012-12-17 15:19:22 +01:00
groupname : groupName
2012-11-14 15:27:38 +01:00
} ) ;
} , 1000 ) ;
2012-10-19 22:09:59 +02:00
} ) ;
2012-11-14 15:27:38 +01:00
} else {
2012-12-13 02:30:20 +01:00
// TODO: Use message returned from groups object.
2012-11-14 15:27:38 +01:00
OC . notify ( { message : t ( 'contacts' , t ( 'contacts' , 'Error adding to group.' ) ) } ) ;
}
2012-10-19 22:09:59 +02:00
} ) ;
2012-11-14 15:27:38 +01:00
} else {
OC . notify ( { message : response . message } ) ;
}
2012-10-19 22:09:59 +02:00
} ) ;
2012-10-05 05:05:49 +02:00
return ;
}
2012-12-17 15:19:22 +01:00
2012-11-14 15:27:38 +01:00
groupName = $opt . text ( ) , groupId = $opt . val ( ) ;
2012-10-22 15:35:33 +02:00
2012-10-05 05:05:49 +02:00
if ( action === 'add' ) {
2012-12-13 02:30:20 +01:00
self . groups . addTo ( ids , $opt . val ( ) , function ( result ) {
2012-11-01 15:31:58 +01:00
console . log ( 'after add' , result ) ;
if ( result . status === 'success' ) {
$ . each ( result . ids , function ( idx , id ) {
// Delay each contact to not trigger too many ajax calls
// at a time.
setTimeout ( function ( ) {
2012-11-14 15:27:38 +01:00
console . log ( 'adding' , id , 'to' , groupName ) ;
2013-02-03 15:40:14 +01:00
var contact = self . contacts . findById ( id ) ;
if ( contact === null ) {
return true ;
}
contact . addToGroup ( groupName ) ;
2012-11-01 15:31:58 +01:00
// I don't think this is used...
2012-11-05 12:39:49 +01:00
if ( buildnow ) {
self . buildGroupSelect ( ) ;
}
2012-11-01 15:31:58 +01:00
$ ( document ) . trigger ( 'status.contact.addedtogroup' , {
contactid : id ,
2012-11-14 15:27:38 +01:00
groupid : groupId ,
2012-12-17 15:19:22 +01:00
groupname : groupName
2012-11-01 15:31:58 +01:00
} ) ;
} , 1000 ) ;
} ) ;
} else {
2012-11-14 15:27:38 +01:00
var msg = result . message ? result . message : t ( 'contacts' , 'Error adding to group.' ) ;
OC . notify ( { message : msg } ) ;
2012-11-01 15:31:58 +01:00
}
2012-10-05 05:05:49 +02:00
} ) ;
if ( ! buildnow ) {
self . $groups . val ( - 1 ) . hide ( ) . find ( 'optgroup,option:not([value="-1"])' ) . remove ( ) ;
}
} else if ( action === 'remove' ) {
2012-12-13 02:30:20 +01:00
self . groups . removeFrom ( ids , $opt . val ( ) , function ( result ) {
2012-11-01 15:31:58 +01:00
console . log ( 'after remove' , result ) ;
if ( result . status === 'success' ) {
var groupname = $opt . text ( ) , groupid = $opt . val ( ) ;
$ . each ( result . ids , function ( idx , id ) {
2013-02-03 15:40:14 +01:00
var contact = self . contacts . findById ( id ) ;
if ( contact === null ) {
return true ;
}
contact . removeFromGroup ( groupname ) ;
2012-11-05 12:39:49 +01:00
if ( buildnow ) {
self . buildGroupSelect ( ) ;
}
2012-10-05 05:05:49 +02:00
// If a group is selected the contact has to be removed from the list
$ ( document ) . trigger ( 'status.contact.removedfromgroup' , {
contactid : id ,
2012-11-14 15:27:38 +01:00
groupid : groupId ,
2012-12-17 15:19:22 +01:00
groupname : groupName
2012-10-05 05:05:49 +02:00
} ) ;
2012-11-01 15:31:58 +01:00
} ) ;
} else {
2012-11-14 15:27:38 +01:00
var msg = result . message ? result . message : t ( 'contacts' , 'Error removing from group.' ) ;
OC . notify ( { message : msg } ) ;
2012-11-01 15:31:58 +01:00
}
2012-10-05 05:05:49 +02:00
} ) ;
if ( ! buildnow ) {
self . $groups . val ( - 1 ) . hide ( ) . find ( 'optgroup,option:not([value="-1"])' ) . remove ( ) ;
}
} // else something's wrong ;)
2012-11-15 02:41:52 +01:00
self . setAllChecked ( false ) ;
2012-10-05 05:05:49 +02:00
} ) ;
2012-11-01 15:31:58 +01:00
2012-10-05 05:05:49 +02:00
// Contact list. Either open a contact or perform an action (mailto etc.)
2012-09-17 16:15:31 +02:00
this . $contactList . on ( 'click' , 'tr' , function ( event ) {
if ( $ ( event . target ) . is ( 'input' ) ) {
return ;
}
2012-12-09 19:38:43 +01:00
if ( event . ctrlKey || event . metaKey ) {
2012-10-05 05:05:49 +02:00
event . stopPropagation ( ) ;
event . preventDefault ( ) ;
console . log ( 'select' , event ) ;
self . dontScroll = true ;
2012-12-13 02:30:20 +01:00
self . contacts . select ( $ ( this ) . data ( 'id' ) , true ) ;
2012-10-05 05:05:49 +02:00
return ;
}
2012-09-17 16:15:31 +02:00
if ( $ ( event . target ) . is ( 'a.mailto' ) ) {
2013-02-12 11:56:09 +01:00
var mailto = 'mailto:' + $ . trim ( $ ( this ) . find ( '.email' ) . text ( ) ) ;
2012-10-05 05:05:49 +02:00
console . log ( 'mailto' , mailto ) ;
try {
window . location . href = mailto ;
} catch ( e ) {
alert ( t ( 'contacts' , 'There was an error opening a mail composer.' ) ) ;
}
2012-09-17 16:15:31 +02:00
return ;
}
2012-10-05 05:05:49 +02:00
self . openContact ( $ ( this ) . data ( 'id' ) ) ;
2012-09-17 16:15:31 +02:00
} ) ;
2012-12-17 15:19:22 +01:00
2013-03-14 23:14:48 +01:00
/ * * A p p e n d s a n a d d r e s s b o o k e n t r y t o $ l i s t a n d a d d s t h e i d t o
* internal list .
*
* @ param object $list A jquery object of an unordered list
* @ param object book An object with the properties 'id' , 'name' and 'permissions' .
* /
var appendAddressBook = function ( $list , book ) {
self . contacts . setAddressbook ( book ) ;
var $li = self . $addressbookTmpl . octemplate ( {
id : book . id ,
permissions : book . permissions ,
2013-03-29 20:09:48 +01:00
displayname : book . displayname
2013-03-14 23:14:48 +01:00
} ) ;
$li . find ( 'a.action' ) . tipsy ( { gravity : 'w' } ) ;
$li . find ( 'a.action.delete' ) . on ( 'click keypress' , function ( ) {
$ ( '.tipsy' ) . remove ( ) ;
var id = parseInt ( $ ( this ) . parents ( 'li' ) . first ( ) . data ( 'id' ) ) ;
console . log ( 'delete' , id ) ;
var $li = $ ( this ) . parents ( 'li' ) . first ( ) ;
2013-03-29 05:00:03 +01:00
$ . when ( self . storage . deleteAddressBook ( 'database' , id ) )
2013-03-25 17:08:47 +01:00
. then ( function ( response ) {
2013-03-28 03:19:01 +01:00
if ( ! response . error ) {
2013-03-25 17:08:47 +01:00
self . contacts . unsetAddressbook ( id ) ;
$li . remove ( ) ;
OC . notify ( {
message : t ( 'contacts' , 'Deleting done. Click here to cancel reloading.' ) ,
timeout : 5 ,
timeouthandler : function ( ) {
console . log ( 'reloading' ) ;
window . location . href = OC . Router . generate ( 'contacts_index' ) ;
} ,
clickhandler : function ( ) {
console . log ( 'reloading cancelled' ) ;
OC . notify ( { cancel : true } ) ;
}
} ) ;
} else {
2013-03-28 03:19:01 +01:00
OC . notify ( { message : response . message } ) ;
2013-03-14 23:14:48 +01:00
}
} ) ;
} ) ;
$li . find ( 'a.action.globe' ) . on ( 'click keypress' , function ( ) {
var id = parseInt ( $ ( this ) . parents ( 'li' ) . first ( ) . data ( 'id' ) ) ;
var book = self . contacts . addressbooks [ id ] ;
var uri = ( book . owner === oc _current _user ) ? book . uri : book . uri + '_shared_by_' + book . owner ;
var link = OC . linkToRemote ( 'carddav' ) + '/addressbooks/' + encodeURIComponent ( oc _current _user ) + '/' + encodeURIComponent ( uri ) ;
var $dropdown = $ ( '<div id="dropdown" class="drop"><input type="text" value="' + link + '" /></div>' ) ;
$dropdown . appendTo ( $ ( this ) . parents ( 'li' ) . first ( ) ) ;
var $input = $dropdown . find ( 'input' ) ;
$input . focus ( ) . get ( 0 ) . select ( ) ;
$input . on ( 'blur' , function ( ) {
$dropdown . hide ( 'blind' , function ( ) {
$dropdown . remove ( ) ;
} ) ;
} ) ;
} ) ;
$list . append ( $li ) ;
} ;
this . $settings . find ( '.addaddressbook' ) . on ( 'click keydown' , function ( event ) {
if ( wrongKey ( event ) ) {
return ;
}
$ ( this ) . hide ( ) ;
var $addAddressbookPart = $ ( this ) . next ( 'ul' ) . show ( ) ;
var $addinput = $addAddressbookPart . find ( 'input.addaddressbookinput' ) . focus ( ) ;
$addAddressbookPart . on ( 'click keydown' , 'button' , function ( event ) {
if ( wrongKey ( event ) ) {
return ;
}
if ( $ ( this ) . is ( '.addaddressbookok' ) ) {
if ( $addinput . val ( ) . trim ( ) === '' ) {
return false ;
} else {
var name = $addinput . val ( ) . trim ( ) ;
$addinput . addClass ( 'loading' ) ;
$addAddressbookPart . find ( 'button input' ) . prop ( 'disabled' , true ) ;
console . log ( 'adding' , name ) ;
2013-03-30 07:27:51 +01:00
$ . when ( self . storage . addAddressBook ( 'database' ,
{ displayname : name , description : '' } ) ) . then ( function ( response ) {
2013-03-28 03:19:01 +01:00
if ( response . error ) {
2013-03-14 23:14:48 +01:00
OC . notify ( { message : response . message } ) ;
return false ;
2013-03-28 03:19:01 +01:00
} else {
2013-03-30 07:27:51 +01:00
var book = response . data ;
2013-03-14 23:14:48 +01:00
var $list = self . $settings . find ( '[data-id="addressbooks"]' ) . next ( 'ul' ) ;
appendAddressBook ( $list , book ) ;
}
$addinput . removeClass ( 'loading' ) ;
$addAddressbookPart . find ( 'button input' ) . prop ( 'disabled' , false ) ;
$addAddressbookPart . hide ( ) . prev ( 'button' ) . show ( ) ;
2013-03-25 17:08:47 +01:00
} )
. fail ( function ( jqxhr , textStatus , error ) {
var err = textStatus + ', ' + error ;
console . log ( "Request Failed: " + err ) ;
$ ( document ) . trigger ( 'status.contact.error' , {
message : t ( 'contacts' , 'Failed adding address book: {error}' , { error : err } )
} ) ;
2013-03-14 23:14:48 +01:00
} ) ;
}
} else if ( $ ( this ) . is ( '.addaddressbookcancel' ) ) {
$addAddressbookPart . hide ( ) . prev ( 'button' ) . show ( ) ;
}
} ) ;
} ) ;
2013-02-08 15:23:52 +01:00
this . $settings . find ( 'h2' ) . on ( 'click keydown' , function ( event ) {
2012-11-15 02:42:33 +01:00
if ( wrongKey ( event ) ) {
return ;
}
2012-11-29 03:10:34 +01:00
if ( $ ( this ) . next ( 'ul' ) . is ( ':visible' ) ) {
return ;
}
2012-12-03 19:39:28 +01:00
console . log ( 'settings' ) ;
2012-12-07 14:40:28 +01:00
var $list = $ ( this ) . next ( 'ul' ) ;
if ( $ ( this ) . data ( 'id' ) === 'addressbooks' ) {
console . log ( 'addressbooks' ) ;
2012-12-17 15:19:22 +01:00
2012-12-07 14:40:28 +01:00
if ( ! self . $addressbookTmpl ) {
self . $addressbookTmpl = $ ( '#addressbookTemplate' ) ;
}
$list . empty ( ) ;
2012-12-13 02:30:20 +01:00
$ . each ( self . contacts . addressbooks , function ( id , book ) {
2013-03-14 23:14:48 +01:00
appendAddressBook ( $list , book ) ;
2012-12-07 14:40:28 +01:00
} ) ;
2012-12-11 05:27:03 +01:00
if ( typeof OC . Share !== 'undefined' ) {
OC . Share . loadIcons ( 'addressbook' ) ;
} else {
$list . find ( 'a.action.share' ) . css ( 'display' , 'none' ) ;
}
2012-12-07 14:40:28 +01:00
} else if ( $ ( this ) . data ( 'id' ) === 'import' ) {
console . log ( 'import' ) ;
$ ( '.import-upload' ) . show ( ) ;
$ ( '.import-select' ) . hide ( ) ;
var addAddressbookCallback = function ( select , name ) {
2013-03-14 23:14:48 +01:00
var id = false ;
2013-03-25 17:08:47 +01:00
$ . when ( this . storage . addAddressBook ( 'database' ,
{ name : name , description : '' } ) ) . then ( function ( response ) {
2013-03-28 03:19:01 +01:00
if ( response . error ) {
2013-03-14 23:14:48 +01:00
OC . notify ( { message : response . message } ) ;
return false ;
2013-03-28 03:19:01 +01:00
} else {
2013-03-25 17:08:47 +01:00
id = response . data . id ;
2012-12-17 15:19:22 +01:00
}
2013-03-25 17:08:47 +01:00
} )
. fail ( function ( jqxhr , textStatus , error ) {
var err = textStatus + ', ' + error ;
console . log ( "Request Failed: " + err ) ;
$ ( document ) . trigger ( 'status.contact.error' , {
message : t ( 'contacts' , 'Failed adding addres books: {error}' , { error : err } )
} ) ;
2012-12-07 14:40:28 +01:00
} ) ;
return id ;
2012-12-17 15:19:22 +01:00
} ;
2012-12-07 14:40:28 +01:00
self . $importIntoSelect . empty ( ) ;
2012-12-13 02:30:20 +01:00
$ . each ( self . contacts . addressbooks , function ( id , book ) {
2012-12-07 14:40:28 +01:00
self . $importIntoSelect . append ( '<option value="' + id + '">' + book . displayname + '</option>' ) ;
} ) ;
self . $importIntoSelect . multiSelect ( {
createCallback : addAddressbookCallback ,
singleSelect : true ,
createText : String ( t ( 'contacts' , 'Add address book' ) ) ,
2012-12-17 15:19:22 +01:00
minWidth : 120
2012-12-07 14:40:28 +01:00
} ) ;
}
2012-11-29 03:10:34 +01:00
$ ( this ) . parents ( 'ul' ) . first ( ) . find ( 'ul:visible' ) . slideUp ( ) ;
2012-12-07 14:40:28 +01:00
$list . toggle ( 'slow' ) ;
2012-11-15 02:42:33 +01:00
} ) ;
2013-02-16 23:00:11 +01:00
var addContact = function ( ) {
2012-12-14 02:53:06 +01:00
console . log ( 'add' ) ;
self . $toggleAll . hide ( ) ;
$ ( this ) . hide ( ) ;
self . currentid = 'new' ;
// Properties that the contact doesn't know
2012-12-17 15:19:22 +01:00
console . log ( 'addContact, groupid' , self . currentgroup ) ;
2013-03-23 13:19:14 +01:00
self . $contactList . addClass ( 'dim' ) ;
2012-12-14 02:53:06 +01:00
var groupprops = {
favorite : false ,
groups : self . groups . categories ,
2012-12-17 15:19:22 +01:00
currentgroup : { id : self . currentgroup , name : self . groups . nameById ( self . currentgroup ) }
2012-12-14 02:53:06 +01:00
} ;
2013-03-04 17:57:46 +01:00
self . $firstRun . hide ( ) ;
self . $contactList . show ( ) ;
2012-12-14 02:53:06 +01:00
self . tmpcontact = self . contacts . addContact ( groupprops ) ;
self . $rightContent . prepend ( self . tmpcontact ) ;
self . hideActions ( ) ;
2013-02-16 23:00:11 +01:00
} ;
this . $firstRun . on ( 'click keydown' , '.import' , function ( event ) {
event . preventDefault ( ) ;
event . stopPropagation ( ) ;
self . $settings . find ( '.settings' ) . click ( ) ;
} ) ;
this . $firstRun . on ( 'click keydown' , '.addcontact' , function ( event ) {
if ( wrongKey ( event ) ) {
return ;
}
addContact ( ) ;
} ) ;
this . $header . on ( 'click keydown' , '.add' , function ( event ) {
if ( wrongKey ( event ) ) {
return ;
}
addContact ( ) ;
2012-11-15 02:42:33 +01:00
} ) ;
2012-10-05 05:05:49 +02:00
this . $header . on ( 'click keydown' , '.delete' , function ( event ) {
if ( wrongKey ( event ) ) {
return ;
}
2012-09-17 16:15:31 +02:00
console . log ( 'delete' ) ;
if ( self . currentid ) {
2013-03-30 17:33:58 +01:00
console . assert ( typeof self . currentid === 'string' , 'self.currentid is not a string' ) ;
contactInfo = self . contacts [ self . currentid ] . metaData ( ) ;
self . contacts . delayedDelete ( contactInfo ) ;
2012-10-05 05:05:49 +02:00
} else {
2012-12-13 02:30:20 +01:00
self . contacts . delayedDelete ( self . contacts . getSelectedContacts ( ) ) ;
2012-10-05 05:05:49 +02:00
}
2012-12-14 02:52:03 +01:00
self . showActions ( [ 'add' ] ) ;
2012-10-05 05:05:49 +02:00
} ) ;
2012-11-15 00:28:43 +01:00
2012-10-05 05:05:49 +02:00
this . $header . on ( 'click keydown' , '.download' , function ( event ) {
if ( wrongKey ( event ) ) {
return ;
}
console . log ( 'download' ) ;
2013-03-30 17:33:58 +01:00
var contacts = self . contacts . getSelectedContacts ( ) ;
var ids = $ . map ( contacts , function ( c ) { return c . getId ( ) ; } ) ;
2012-12-17 15:19:22 +01:00
document . location . href = OC . linkTo ( 'contacts' , 'export.php' )
2013-03-30 17:33:58 +01:00
+ '?selectedids=' + ids . join ( ',' ) ;
2012-09-17 16:15:31 +02:00
} ) ;
2012-11-15 00:28:43 +01:00
2012-11-22 05:34:37 +01:00
this . $header . on ( 'click keydown' , '.favorite' , function ( event ) {
2012-10-05 05:05:49 +02:00
if ( wrongKey ( event ) ) {
return ;
}
2013-03-30 20:13:52 +01:00
var contacts = self . contacts . getSelectedContacts ( ) ;
self . setAllChecked ( false ) ;
self . $toggleAll . prop ( 'checked' , false ) ;
if ( ! self . currentid ) {
self . showActions ( [ 'add' ] ) ;
2012-09-17 16:15:31 +02:00
}
2013-03-30 20:13:52 +01:00
$ . each ( contacts , function ( idx , contact ) {
if ( ! self . groups . isFavorite ( contact . getId ( ) ) ) {
self . groups . setAsFavorite ( contact . getId ( ) , true , function ( result ) {
if ( result . status !== 'success' ) {
OC . notify ( { message :
t ( 'contacts' ,
'Error setting {name} as favorite.' ,
{ name : contact . getDisplayName ( ) } )
} ) ;
}
} ) ;
2012-11-22 05:34:37 +01:00
}
} ) ;
2013-03-30 20:13:52 +01:00
self . showActions ( [ 'add' ] ) ;
2012-09-17 16:15:31 +02:00
} ) ;
2012-11-15 00:28:43 +01:00
2012-09-17 16:15:31 +02:00
this . $contactList . on ( 'mouseenter' , 'td.email' , function ( event ) {
2013-02-12 11:56:09 +01:00
if ( $ . trim ( $ ( this ) . text ( ) ) . length > 3 ) {
2012-10-15 21:33:21 +02:00
$ ( this ) . find ( '.mailto' ) . css ( 'display' , 'inline-block' ) ; //.fadeIn(100);
2012-09-17 16:15:31 +02:00
}
} ) ;
this . $contactList . on ( 'mouseleave' , 'td.email' , function ( event ) {
$ ( this ) . find ( '.mailto' ) . fadeOut ( 100 ) ;
} ) ;
2012-10-05 05:05:49 +02:00
2012-12-05 00:59:04 +01:00
// Import using jquery.fileupload
$ ( function ( ) {
2013-03-29 05:00:03 +01:00
var uploadingFiles = { } ,
numfiles = 0 ,
uploadedfiles = 0 ,
importedfiles = 0 ,
retries = 0 ,
succeded = 0
failed = 0 ;
2012-12-05 00:59:04 +01:00
var aid , importError = false ;
var $progressbar = $ ( '#import-progress' ) ;
2012-12-05 03:39:16 +01:00
var $status = $ ( '#import-status-text' ) ;
2012-12-05 00:59:04 +01:00
2013-03-29 05:00:03 +01:00
self . $importFileInput . on ( 'fileuploaddone' , function ( e , data ) {
console . log ( 'fileuploaddone' , data ) ;
var file = data . result . file ;
console . log ( 'fileuploaddone, file' , file ) ;
uploadedfiles += 1 ;
} ) ;
2012-12-05 00:59:04 +01:00
var waitForImport = function ( ) {
2013-03-29 05:00:03 +01:00
console . log ( 'waitForImport' , numfiles , uploadedfiles , importedfiles ) ;
if ( uploadedfiles === importedfiles && importedfiles === numfiles ) {
$progressbar . progressbar ( 'value' , 100 ) ;
2012-12-05 00:59:04 +01:00
if ( ! importError ) {
2012-12-05 03:39:16 +01:00
OC . notify ( {
message : t ( 'contacts' , 'Import done. Click here to cancel reloading.' ) ,
2012-12-10 00:05:52 +01:00
timeout : 5 ,
2012-12-05 03:39:16 +01:00
timeouthandler : function ( ) {
console . log ( 'reloading' ) ;
window . location . href = OC . linkTo ( 'contacts' , 'index.php' ) ;
} ,
clickhandler : function ( ) {
console . log ( 'reloading cancelled' ) ;
OC . notify ( { cancel : true } ) ;
}
} ) ;
2012-12-05 00:59:04 +01:00
}
2013-03-29 05:00:03 +01:00
$status . text ( t ( 'contacts' , '{success} imported, {failed} failed.' ,
{ success : succeded , failed : failed } ) ) . fadeIn ( ) ;
numfiles = uploadedfiles = importedfiles = retries = aid = failed = succeded = 0 ;
2012-12-05 00:59:04 +01:00
$progressbar . fadeOut ( ) ;
2012-12-05 03:39:16 +01:00
setTimeout ( function ( ) {
$status . fadeOut ( 'slow' ) ;
$ ( '.import-upload' ) . show ( ) ;
} , 3000 ) ;
2012-12-05 00:59:04 +01:00
} else {
2012-12-05 03:39:16 +01:00
setTimeout ( function ( ) {
2012-12-05 00:59:04 +01:00
waitForImport ( ) ;
} , 1000 ) ;
}
} ;
var doImport = function ( file , aid , cb ) {
$ . post ( OC . filePath ( 'contacts' , '' , 'import.php' ) , { id : aid , file : file , fstype : 'OC_FilesystemView' } ,
function ( jsondata ) {
if ( jsondata . status != 'success' ) {
importError = true ;
OC . notify ( { message : jsondata . data . message } ) ;
}
if ( typeof cb == 'function' ) {
2012-12-05 03:39:16 +01:00
cb ( jsondata ) ;
2012-12-05 00:59:04 +01:00
}
} ) ;
return false ;
} ;
var importFiles = function ( aid , uploadingFiles ) {
2012-12-05 03:39:16 +01:00
console . log ( 'importFiles' , aid , uploadingFiles ) ;
2013-03-29 05:00:03 +01:00
if ( numfiles !== uploadedfiles ) {
2012-12-05 00:59:04 +01:00
OC . notify ( { message : t ( 'contacts' , 'Not all files uploaded. Retrying...' ) } ) ;
retries += 1 ;
if ( retries > 3 ) {
2013-03-29 05:00:03 +01:00
numfiles = uploadedfiles = importedfiles = retries = failed = succeded = aid = 0 ;
2012-12-05 00:59:04 +01:00
uploadingFiles = { } ;
$progressbar . fadeOut ( ) ;
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 ) ;
}
2012-12-05 03:39:16 +01:00
$progressbar . progressbar ( 'value' , 50 ) ;
2012-12-05 00:59:04 +01:00
var todo = uploadedfiles ;
$ . each ( uploadingFiles , function ( fileName , data ) {
2012-12-05 03:39:16 +01:00
$status . text ( t ( 'contacts' , 'Importing from {filename}...' , { filename : fileName } ) ) . fadeIn ( ) ;
doImport ( fileName , aid , function ( response ) {
if ( response . status === 'success' ) {
2013-03-29 05:00:03 +01:00
importedfiles += 1 ;
succeded += response . data . imported ;
failed += response . data . failed ;
OC . notify ( {
message : t ( 'contacts' , '{success} imported, {failed} failed from {file}' ,
{ success : response . data . imported , failed : response . data . failed , file : response . data . failed }
)
} ) ;
2013-03-09 16:41:45 +01:00
} else {
$ ( '.import-upload' ) . show ( ) ;
$ ( '.import-select' ) . hide ( ) ;
2012-12-05 03:39:16 +01:00
}
2012-12-05 00:59:04 +01:00
delete uploadingFiles [ fileName ] ;
$progressbar . progressbar ( 'value' , 50 + ( 50 / ( todo - uploadedfiles ) ) ) ;
} ) ;
2012-12-17 15:19:22 +01:00
} ) ;
2012-12-05 03:39:16 +01:00
//$status.text(t('contacts', 'Importing...')).fadeIn();
2012-12-05 00:59:04 +01:00
waitForImport ( ) ;
} ;
2012-12-05 03:39:16 +01:00
// Start the actual import.
$ ( '.doImport' ) . on ( 'click keypress' , function ( event ) {
2012-12-05 00:59:04 +01:00
if ( wrongKey ( event ) ) {
return ;
}
aid = $ ( this ) . prev ( 'select' ) . val ( ) ;
$ ( '.import-select' ) . hide ( ) ;
importFiles ( aid , uploadingFiles ) ;
2012-12-03 19:39:28 +01:00
} ) ;
2013-03-29 05:00:03 +01:00
self . $importFileInput . fileupload ( {
2012-12-05 00:59:04 +01:00
acceptFileTypes : /^text\/(directory|vcard|x-vcard)$/i ,
add : function ( e , data ) {
2013-03-29 05:00:03 +01:00
var file = data . files [ 0 ] ;
console . log ( 'add' , file . name ) ;
2012-12-05 00:59:04 +01:00
var totalSize = 0 ;
2013-03-29 05:00:03 +01:00
if ( file ) {
numfiles += 1 ;
if ( file . size === 0 || file . type == '' ) {
OC . dialogs . alert ( t ( 'contacts' , 'Unable to upload your file as it is a directory or has 0 bytes' ) , t ( 'contacts' , 'Upload Error' ) ) ;
return ;
2012-12-05 00:59:04 +01:00
}
2013-03-29 05:00:03 +01:00
totalSize += file . size ;
2012-12-05 00:59:04 +01:00
}
if ( totalSize > $ ( '#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' ) ) ;
2013-03-29 05:00:03 +01:00
numfiles = uploadedfiles = importedfiles = retries = failed = succeded = aid = 0 ;
2012-12-05 00:59:04 +01:00
uploadingFiles = { } ;
return ;
}
2013-03-29 05:00:03 +01:00
var jqXHR = data . submit ( ) ;
uploadingFiles [ file . name ] = jqXHR ;
2012-12-05 00:59:04 +01:00
} ,
fail : function ( e , data ) {
console . log ( 'fail' ) ;
OC . notify ( { message : data . errorThrown + ': ' + data . textStatus } ) ;
2013-03-29 05:00:03 +01:00
numfiles = uploadedfiles = importedfiles = retries = failed = succeded = aid = 0 ;
2013-03-09 16:41:45 +01:00
$ ( '.import-upload' ) . show ( ) ;
$ ( '.import-select' ) . hide ( ) ;
2012-12-05 00:59:04 +01:00
// TODO: Remove file from upload queue.
} ,
progressall : function ( e , data ) {
var progress = ( data . loaded / data . total ) * 50 ;
$progressbar . progressbar ( 'value' , progress ) ;
} ,
start : function ( e , data ) {
$progressbar . progressbar ( { value : 0 } ) ;
$progressbar . fadeIn ( ) ;
} ,
stop : function ( e , data ) {
console . log ( 'stop, data' , data ) ;
// stop only gets fired once so we collect uploaded items here.
$ ( '.import-upload' ) . hide ( ) ;
$ ( '.import-select' ) . show ( ) ;
}
2012-12-17 15:19:22 +01:00
} ) ;
2012-12-05 00:59:04 +01:00
} ) ;
2012-12-17 15:19:22 +01:00
2013-01-14 09:15:49 +01:00
$ ( 'body' ) . on ( 'touchmove' , function ( event ) {
event . preventDefault ( ) ;
} ) ;
2013-03-29 22:40:59 +01:00
$ ( document ) . on ( 'keyup' , function ( event ) {
2012-12-11 17:41:28 +01:00
if ( ! $ ( event . target ) . is ( 'body' ) ) {
2012-10-05 05:05:49 +02:00
return ;
}
2012-12-03 19:39:28 +01:00
var keyCode = Math . max ( event . keyCode , event . which ) ;
2012-10-30 07:03:09 +01:00
// TODO: This should go in separate method
2012-12-03 19:39:28 +01:00
console . log ( event , keyCode + ' ' + event . target . nodeName ) ;
2012-10-05 05:05:49 +02:00
/ * *
* To add :
* Shift - a : add addressbook
* u ( 85 ) : hide / show leftcontent
* f ( 70 ) : add field
* /
2012-12-03 19:39:28 +01:00
switch ( keyCode ) {
2012-10-05 05:05:49 +02:00
case 13 : // Enter?
console . log ( 'Enter?' ) ;
if ( ! self . currentid && self . currentlistid ) {
self . openContact ( self . currentlistid ) ;
}
break ;
case 27 : // Esc
if ( self . $ninjahelp . is ( ':visible' ) ) {
self . $ninjahelp . hide ( ) ;
} else if ( self . currentid ) {
self . closeContact ( self . currentid ) ;
}
break ;
case 46 : // Delete
if ( event . shiftKey ) {
2012-12-13 02:30:20 +01:00
self . contacts . delayedDelete ( self . currentid ) ;
2012-10-05 05:05:49 +02:00
}
break ;
case 40 : // down
case 74 : // j
console . log ( 'next' ) ;
if ( ! self . currentid && self . currentlistid ) {
2012-12-13 02:30:20 +01:00
self . contacts . contacts [ self . currentlistid ] . next ( ) ;
2012-10-05 05:05:49 +02:00
}
break ;
case 65 : // a
if ( event . shiftKey ) {
console . log ( 'add group?' ) ;
break ;
}
self . addContact ( ) ;
break ;
case 38 : // up
case 75 : // k
console . log ( 'previous' ) ;
if ( ! self . currentid && self . currentlistid ) {
2012-12-13 02:30:20 +01:00
self . contacts . contacts [ self . currentlistid ] . prev ( ) ;
2012-10-05 05:05:49 +02:00
}
break ;
case 34 : // PageDown
case 78 : // n
2012-12-17 15:19:22 +01:00
console . log ( 'page down' ) ;
2012-10-05 05:05:49 +02:00
break ;
case 79 : // o
console . log ( 'open contact?' ) ;
break ;
case 33 : // PageUp
case 80 : // p
// prev addressbook
2012-12-13 02:30:20 +01:00
//OC.contacts.contacts.previousAddressbook();
2012-10-05 05:05:49 +02:00
break ;
case 82 : // r
console . log ( 'refresh - what?' ) ;
break ;
case 63 : // ? German.
if ( event . shiftKey ) {
self . $ninjahelp . toggle ( 'fast' ) ;
}
break ;
case 171 : // ? Danish
case 191 : // ? Standard qwerty
2012-10-26 14:03:42 +02:00
self . $ninjahelp . toggle ( 'fast' ) . position ( { my : "center" , at : "center" , of : "#content" } ) ;
2012-10-05 05:05:49 +02:00
break ;
}
} ) ;
2012-10-22 15:35:33 +02:00
2012-12-17 15:19:22 +01:00
// find all with a title attribute and tipsy them
2012-12-10 20:58:34 +01:00
$ ( '.tooltipped.downwards:not(.onfocus)' ) . tipsy ( { gravity : 'n' } ) ;
$ ( '.tooltipped.upwards:not(.onfocus)' ) . tipsy ( { gravity : 's' } ) ;
$ ( '.tooltipped.rightwards:not(.onfocus)' ) . tipsy ( { gravity : 'w' } ) ;
$ ( '.tooltipped.leftwards:not(.onfocus)' ) . tipsy ( { gravity : 'e' } ) ;
$ ( '.tooltipped.downwards.onfocus' ) . tipsy ( { trigger : 'focus' , gravity : 'n' } ) ;
$ ( '.tooltipped.rightwards.onfocus' ) . tipsy ( { trigger : 'focus' , gravity : 'w' } ) ;
2012-09-17 16:15:31 +02:00
} ,
2012-11-14 15:27:38 +01:00
addGroup : function ( cb ) {
var self = this ;
$ ( 'body' ) . append ( '<div id="add_group_dialog"></div>' ) ;
if ( ! this . $addGroupTmpl ) {
this . $addGroupTmpl = $ ( '#addGroupTemplate' ) ;
}
var $dlg = this . $addGroupTmpl . octemplate ( ) ;
$ ( '#add_group_dialog' ) . html ( $dlg ) . dialog ( {
modal : true ,
closeOnEscape : true ,
title : t ( 'contacts' , 'Add group' ) ,
height : 'auto' , width : 'auto' ,
buttons : {
'Ok' : function ( ) {
2012-12-13 02:30:20 +01:00
self . groups . addGroup (
2012-12-07 14:47:28 +01:00
{ name : $dlg . find ( 'input:text' ) . val ( ) } ,
2012-11-14 15:27:38 +01:00
function ( response ) {
if ( typeof cb === 'function' ) {
cb ( response ) ;
} else {
2013-03-29 23:23:47 +01:00
if ( response . error ) {
2012-11-14 15:27:38 +01:00
OC . notify ( { message : response . message } ) ;
}
}
} ) ;
$ ( this ) . dialog ( 'close' ) ;
} ,
2012-12-17 15:19:22 +01:00
'Cancel' : function ( ) {
$ ( this ) . dialog ( 'close' ) ;
2012-11-14 15:27:38 +01:00
return false ;
}
} ,
close : function ( event , ui ) {
$ ( this ) . dialog ( 'destroy' ) . remove ( ) ;
$ ( '#add_group_dialog' ) . remove ( ) ;
} ,
open : function ( event , ui ) {
$dlg . find ( 'input' ) . focus ( ) ;
2012-12-17 15:19:22 +01:00
}
2012-11-14 15:27:38 +01:00
} ) ;
} ,
2012-11-15 02:41:52 +01:00
setAllChecked : function ( checked ) {
var selector = checked ? 'input:checkbox:visible:not(checked)' : 'input:checkbox:visible:checked' ;
2012-12-20 22:58:46 +01:00
$ . each ( this . $contactList . find ( selector ) , function ( ) {
2012-11-15 02:41:52 +01:00
$ ( this ) . prop ( 'checked' , checked ) ;
} ) ;
} ,
2012-10-05 05:05:49 +02:00
jumpToContact : function ( id ) {
2013-02-12 20:56:27 +01:00
this . $rightContent . scrollTop ( this . contacts . contactPos ( id ) - 30 ) ;
2012-10-05 05:05:49 +02:00
} ,
closeContact : function ( id ) {
2013-02-23 22:27:04 +01:00
$ ( window ) . unbind ( 'hashchange' , this . hashChange ) ;
2012-10-24 20:31:29 +02:00
if ( typeof this . currentid === 'number' ) {
2012-12-13 02:30:20 +01:00
var contact = this . contacts . findById ( id ) ;
if ( contact && contact . close ( ) ) {
2012-11-01 02:01:00 +01:00
this . $contactList . show ( ) ;
this . jumpToContact ( id ) ;
}
2012-11-15 20:35:35 +01:00
} else if ( this . currentid === 'new' ) {
this . tmpcontact . remove ( ) ;
this . $contactList . show ( ) ;
2012-10-05 05:05:49 +02:00
}
2012-12-10 00:02:06 +01:00
this . $contactList . removeClass ( 'dim' ) ;
2012-11-15 20:35:35 +01:00
delete this . currentid ;
2012-12-14 02:52:03 +01:00
this . showActions ( [ 'add' ] ) ;
2012-10-05 05:05:49 +02:00
this . $groups . find ( 'optgroup,option:not([value="-1"])' ) . remove ( ) ;
2013-03-04 17:57:46 +01:00
if ( this . contacts . length === 0 ) {
$ ( document ) . trigger ( 'status.nomorecontacts' ) ;
}
2013-03-04 14:46:53 +01:00
//$('body').unbind('click', this.bodyListener);
2013-02-23 22:27:04 +01:00
window . location . hash = '' ;
$ ( window ) . bind ( 'hashchange' , this . hashChange ) ;
2012-10-05 05:05:49 +02:00
} ,
openContact : function ( id ) {
2013-03-30 20:13:52 +01:00
self . hideActions ( ) ;
2012-11-01 02:01:00 +01:00
console . log ( 'Contacts.openContact' , id ) ;
if ( this . currentid ) {
this . closeContact ( this . currentid ) ;
}
2013-02-23 22:27:04 +01:00
$ ( window ) . unbind ( 'hashchange' , this . hashChange ) ;
2012-10-05 05:05:49 +02:00
this . currentid = parseInt ( id ) ;
2012-12-13 02:30:20 +01:00
console . log ( 'Contacts.openContact, Favorite' , this . currentid , this . groups . isFavorite ( this . currentid ) , this . groups ) ;
2012-11-15 02:41:52 +01:00
this . setAllChecked ( false ) ;
2012-12-10 00:02:06 +01:00
//this.$contactList.hide();
this . $contactList . addClass ( 'dim' ) ;
2012-10-05 05:05:49 +02:00
this . $toggleAll . hide ( ) ;
2012-12-10 00:02:06 +01:00
this . jumpToContact ( this . currentid ) ;
2012-12-14 02:53:06 +01:00
// Properties that the contact doesn't know
var groupprops = {
2012-12-13 02:30:20 +01:00
favorite : this . groups . isFavorite ( this . currentid ) ,
groups : this . groups . categories ,
2012-12-17 15:19:22 +01:00
currentgroup : { id : this . currentgroup , name : this . groups . nameById ( this . currentgroup ) }
2012-12-10 00:02:06 +01:00
} ;
2012-12-14 02:53:06 +01:00
var $contactelem = this . contacts . showContact ( this . currentid , groupprops ) ;
2012-12-10 00:02:06 +01:00
var self = this ;
var $contact = $contactelem . find ( '#contact' ) ;
var adjustElems = function ( ) {
var maxheight = document . documentElement . clientHeight - 200 ; // - ($contactelem.offset().top+70);
console . log ( 'contact maxheight' , maxheight ) ;
$contactelem . find ( 'ul' ) . first ( ) . css ( { 'max-height' : maxheight , 'overflow-y' : 'auto' , 'overflow-x' : 'hidden' } ) ;
} ;
$ ( window ) . resize ( adjustElems ) ;
//$contact.resizable({ minWidth: 400, minHeight: 400, maxHeight: maxheight});
2012-10-05 05:05:49 +02:00
this . $rightContent . prepend ( $contactelem ) ;
2012-12-10 00:02:06 +01:00
adjustElems ( ) ;
2013-03-04 14:46:53 +01:00
/ * t h i s . b o d y L i s t e n e r = f u n c t i o n ( e ) {
2013-02-12 10:52:58 +01:00
if ( ! self . currentid ) {
return ;
}
var $contactelem = self . contacts . findById ( self . currentid ) . $fullelem ;
if ( $contactelem . find ( $ ( e . target ) ) . length === 0 ) {
self . closeContact ( self . currentid ) ;
}
2013-02-23 22:27:04 +01:00
} ; * /
window . location . hash = this . currentid . toString ( ) ;
2013-02-12 10:52:58 +01:00
setTimeout ( function ( ) {
2013-02-23 22:27:04 +01:00
//$('body').bind('click', self.bodyListener);
$ ( window ) . bind ( 'hashchange' , this . hashChange ) ;
} , 500 ) ;
2012-10-05 05:05:49 +02:00
} ,
2012-09-17 16:15:31 +02:00
update : function ( ) {
console . log ( 'update' ) ;
} ,
2012-11-05 12:39:12 +01:00
uploadPhoto : function ( filelist ) {
var self = this ;
if ( ! filelist ) {
OC . notify ( { message : t ( 'contacts' , 'No files selected for upload.' ) } ) ;
return ;
}
var file = filelist [ 0 ] ;
var target = $ ( '#file_upload_target' ) ;
var form = $ ( '#file_upload_form' ) ;
2013-03-04 15:11:55 +01:00
form . find ( 'input[name="id"]' ) . val ( this . currentid ) ;
2012-11-05 12:39:12 +01:00
var totalSize = 0 ;
if ( file . size > $ ( '#max_upload' ) . val ( ) ) {
OC . notify ( {
message : t (
'contacts' ,
2012-12-17 15:19:22 +01:00
'The file you are trying to upload exceed the maximum size for file uploads on this server.' )
2012-11-05 12:39:12 +01:00
} ) ;
return ;
} else {
target . load ( function ( ) {
var response = jQuery . parseJSON ( target . contents ( ) . text ( ) ) ;
if ( response != undefined && response . status == 'success' ) {
console . log ( 'response' , response ) ;
self . editPhoto ( self . currentid , response . data . tmp ) ;
//alert('File: ' + file.tmp + ' ' + file.name + ' ' + file.mime);
} else {
OC . notify ( { message : response . data . message } ) ;
}
} ) ;
form . submit ( ) ;
}
} ,
2012-10-15 21:33:21 +02:00
cloudPhotoSelected : function ( id , path ) {
var self = this ;
2012-12-17 15:19:22 +01:00
console . log ( 'cloudPhotoSelected, id' , id ) ;
2012-10-22 15:35:33 +02:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'oc_photo.php' ) ,
2012-10-15 21:33:21 +02:00
{ path : path , id : id } , function ( jsondata ) {
if ( jsondata . status == 'success' ) {
//alert(jsondata.data.page);
2012-12-17 15:19:22 +01:00
self . editPhoto ( jsondata . data . id , jsondata . data . tmp ) ;
2012-10-15 21:33:21 +02:00
$ ( '#edit_photo_dialog_img' ) . html ( jsondata . data . page ) ;
}
else {
OC . notify ( { message : jsondata . data . message } ) ;
}
} ) ;
} ,
editCurrentPhoto : function ( id ) {
2012-09-17 16:15:31 +02:00
var self = this ;
2012-10-15 21:33:21 +02:00
$ . getJSON ( OC . filePath ( 'contacts' , 'ajax' , 'currentphoto.php' ) ,
{ id : id } , function ( jsondata ) {
if ( jsondata . status == 'success' ) {
//alert(jsondata.data.page);
2012-12-17 15:19:22 +01:00
self . editPhoto ( jsondata . data . id , jsondata . data . tmp ) ;
2012-10-15 21:33:21 +02:00
$ ( '#edit_photo_dialog_img' ) . html ( jsondata . data . page ) ;
}
else {
OC . notify ( { message : jsondata . data . message } ) ;
}
} ) ;
} ,
editPhoto : function ( id , tmpkey ) {
2012-12-17 15:19:22 +01:00
console . log ( 'editPhoto' , id , tmpkey ) ;
2012-10-15 21:33:21 +02:00
$ ( '.tipsy' ) . remove ( ) ;
// Simple event handler, called from onChange and onSelect
// event handlers, as per the Jcrop invocation above
var showCoords = function ( c ) {
$ ( '#x1' ) . val ( c . x ) ;
$ ( '#y1' ) . val ( c . y ) ;
$ ( '#x2' ) . val ( c . x2 ) ;
$ ( '#y2' ) . val ( c . y2 ) ;
$ ( '#w' ) . val ( c . w ) ;
$ ( '#h' ) . val ( c . h ) ;
} ;
var clearCoords = function ( ) {
$ ( '#coords input' ) . val ( '' ) ;
} ;
2012-10-22 15:35:33 +02:00
2012-10-15 21:33:21 +02:00
var self = this ;
if ( ! this . $cropBoxTmpl ) {
this . $cropBoxTmpl = $ ( '#cropBoxTemplate' ) ;
}
$ ( 'body' ) . append ( '<div id="edit_photo_dialog"></div>' ) ;
var $dlg = this . $cropBoxTmpl . octemplate ( { id : id , tmpkey : tmpkey } ) ;
2012-09-17 16:15:31 +02:00
2012-10-15 21:33:21 +02:00
var cropphoto = new Image ( ) ;
$ ( cropphoto ) . load ( function ( ) {
$ ( this ) . attr ( 'id' , 'cropbox' ) ;
$ ( this ) . prependTo ( $dlg ) . fadeIn ( ) ;
$ ( this ) . Jcrop ( {
onChange : showCoords ,
onSelect : showCoords ,
onRelease : clearCoords ,
maxSize : [ 399 , 399 ] ,
bgColor : 'black' ,
bgOpacity : . 4 ,
2012-12-17 15:19:22 +01:00
boxWidth : 400 ,
2012-10-15 21:33:21 +02:00
boxHeight : 400 ,
setSelect : [ 100 , 130 , 50 , 50 ] //,
//aspectRatio: 0.8
} ) ;
$ ( '#edit_photo_dialog' ) . html ( $dlg ) . dialog ( {
modal : true ,
closeOnEscape : true ,
title : t ( 'contacts' , 'Edit profile picture' ) ,
height : 'auto' , width : 'auto' ,
buttons : {
'Ok' : function ( ) {
self . savePhoto ( $ ( this ) ) ;
$ ( this ) . dialog ( 'close' ) ;
} ,
'Cancel' : function ( ) { $ ( this ) . dialog ( 'close' ) ; }
} ,
close : function ( event , ui ) {
$ ( this ) . dialog ( 'destroy' ) . remove ( ) ;
2012-10-19 22:09:59 +02:00
$ ( '#edit_photo_dialog' ) . remove ( ) ;
2012-10-15 21:33:21 +02:00
} ,
open : function ( event , ui ) {
// Jcrop maybe?
}
} ) ;
} ) . error ( function ( ) {
OC . notify ( { message : t ( 'contacts' , 'Error loading profile picture.' ) } ) ;
} ) . attr ( 'src' , OC . linkTo ( 'contacts' , 'tmpphoto.php' ) + '?tmpkey=' + tmpkey ) ;
} ,
savePhoto : function ( $dlg ) {
var form = $dlg . find ( '#cropform' ) ;
q = form . serialize ( ) ;
console . log ( 'savePhoto' , q ) ;
$ . post ( OC . filePath ( 'contacts' , 'ajax' , 'savecrop.php' ) , q , function ( response ) {
var jsondata = $ . parseJSON ( response ) ;
console . log ( 'savePhoto, jsondata' , typeof jsondata ) ;
if ( jsondata && jsondata . status === 'success' ) {
// load cropped photo.
$ ( document ) . trigger ( 'status.contact.photoupdated' , {
2012-12-17 15:19:22 +01:00
id : jsondata . data . id
2012-09-17 16:15:31 +02:00
} ) ;
2012-10-15 21:33:21 +02:00
} else {
if ( ! jsondata ) {
OC . notify ( { message : t ( 'contacts' , 'Network or server error. Please inform administrator.' ) } ) ;
} else {
OC . notify ( { message : jsondata . data . message } ) ;
}
2012-10-05 05:05:49 +02:00
}
2012-09-17 16:15:31 +02:00
} ) ;
} ,
} ;
$ ( document ) . ready ( function ( ) {
2013-03-22 14:53:27 +01:00
OC . Router . registerLoadedCallback ( function ( ) {
OC . Contacts . init ( ) ;
} ) ;
2012-09-17 16:15:31 +02:00
} ) ;