function ucwords (str) { return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) { return $1.toUpperCase(); }); } String.prototype.strip_tags = function(){ tags = this; stripped = tags.replace(/<(.|\n)*?>/g, ''); return stripped; }; OC.Contacts={ /** * Arguments: * message: The text message to show. * timeout: The timeout in seconds before the notification disappears. Default 10. * timeouthandler: A function to run on timeout. * clickhandler: A function to run on click. If a timeouthandler is given it will be cancelled. * data: An object that will be passed as argument to the timeouthandler and clickhandler functions. * cancel: If set cancel all ongoing timer events and hide the notification. */ 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); self.notifier.fadeIn(); self.notifier.on('click', function() { $(this).fadeOut();}); var timer = setTimeout(function() { if(!self || !self.notifier) { var self = OC.Contacts; self.notifier = $('#notification'); } 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() { if(!self || !self.notifier) { var self = OC.Contacts; self.notifier = $(this); } clearTimeout(timer); self.notifier.off('click'); params.clickhandler(self.notifier.data(dataid)); self.notifier.removeData(dataid); }); } }, notImplemented:function() { OC.dialogs.alert(t('contacts', 'Sorry, this functionality has not been implemented yet'), t('contacts', 'Not implemented')); }, searchOSM:function(obj) { var adr = OC.Contacts.propertyContainerFor(obj).find('.adr').val(); if(adr == undefined) { OC.dialogs.alert(t('contacts', 'Couldn\'t get a valid address.'), t('contacts', 'Error')); 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); var uri = 'http://open.mapquestapi.com/nominatim/v1/search.php?q=' + adrstr + '&limit=10&addressdetails=1&polygon=1&zoom='; var newWindow = window.open(uri,'_blank'); newWindow.focus(); }, mailTo:function(obj) { var adr = OC.Contacts.propertyContainerFor($(obj)).find('input[type="email"]').val().trim(); if(adr == '') { OC.dialogs.alert(t('contacts', 'Please enter an email address.'), t('contacts', 'Error')); 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){ $('#carddav_url').val(totalurl + '/' + username + '/' + decodeURIComponent(bookname)); $('#carddav_url').show(); $('#carddav_url_close').show(); }, loadListHandlers:function() { $('.propertylist li a.delete').unbind('click'); $('.propertylist li a.delete').unbind('keydown'); var deleteItem = function(obj) { obj.tipsy('hide'); OC.Contacts.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() { OC.Contacts.mailTo(this) }); $('.propertylist li a.mail').keydown(function() { OC.Contacts.mailTo(this) }); $('.addresscard .globe').click(function() { $(this).tipsy('hide');OC.Contacts.searchOSM(this); }); $('.addresscard .globe').keydown(function() { $(this).tipsy('hide');OC.Contacts.searchOSM(this); }); $('.addresscard .edit').click(function() { $(this).tipsy('hide');OC.Contacts.Card.editAddress(this, false); }); $('.addresscard .edit').keydown(function() { $(this).tipsy('hide');OC.Contacts.Card.editAddress(this, false); }); $('.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() {}); } ); }, loadHandlers:function() { var deleteItem = function(obj) { obj.tipsy('hide'); OC.Contacts.Card.deleteProperty(obj, 'single'); } var goToUrl = function(obj) { var url = OC.Contacts.propertyContainerFor(obj).find('#url').val(); if(url != '') { var newWindow = window.open(url,'_blank'); newWindow.focus(); } } $('#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(); } ); $('#url_value a.globe').click( function() { $(this).tipsy('hide');goToUrl($(this)); } ); $('#url_value a.globe').keydown( function() { $(this).tipsy('hide');goToUrl($(this)); } ); $('#fn_select').combobox({ 'id': 'fn', 'name': 'value', 'classes': ['contacts_property', 'nonempty', 'huge', 'tip', 'float'], 'attributes': {'placeholder': t('contacts', 'Enter name')}, 'title': t('contacts', 'Format custom, Short name, Full name, Reverse or Reverse with comma')}); $('#bday').datepicker({ dateFormat : 'dd-mm-yy' }); // Style phone types $('#phonelist').find('select.contacts_property').multiselect({ noneSelectedText: t('contacts', 'Select type'), header: false, selectedList: 4, classes: 'typelist' }); $('#edit_name').click(function(){OC.Contacts.Card.editName()}); $('#edit_name').keydown(function(){OC.Contacts.Card.editName()}); $('#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'), OC.Contacts.Card.cloudPhotoSelected, false, 'image', true); }); /* Initialize the photo edit dialog */ $('#edit_photo_dialog').dialog({ autoOpen: false, modal: true, height: 'auto', width: 'auto' }); $('#edit_photo_dialog' ).dialog( 'option', 'buttons', [ { text: "Ok", click: function() { OC.Contacts.Card.savePhoto(this); $(this).dialog('close'); } }, { text: "Cancel", click: function() { $(this).dialog('close'); } } ] ); // Name has changed. Update it and reorder. $('#fn').change(function(){ var name = $('#fn').val().strip_tags(); var item = $('.contacts li[data-id="'+OC.Contacts.Card.id+'"]').detach(); $(item).find('a').html(name); OC.Contacts.Card.fn = name; OC.Contacts.Contacts.insertContact({contact:item}); OC.Contacts.Contacts.scrollTo(OC.Contacts.Card.id); }); $('#contacts_deletecard').click( function() { OC.Contacts.Card.delayedDelete();return false;} ); $('#contacts_deletecard').keydown( function(event) { if(event.which == 13 || event.which == 32) { OC.Contacts.Card.delayedDelete(); } return false; }); $('#contacts_downloadcard').click( function() { OC.Contacts.Card.doExport();return false;} ); $('#contacts_downloadcard').keydown( function(event) { if(event.which == 13 || event.which == 32) { OC.Contacts.Card.doExport(); } return false; }); // Profile picture upload handling // New profile picture selected $('#file_upload_start').change(function(){ OC.Contacts.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'); }); $('#contacts_details_photo_wrapper').bind('drop',function(event){ event.stopPropagation(); event.preventDefault(); $(event.target).removeClass('droppable'); $.fileUpload(event.originalEvent.dataTransfer.files); }); $('#categories').multiple_autocomplete({source: categories}); $('#contacts_deletecard').tipsy({gravity: 'ne'}); $('#contacts_downloadcard').tipsy({gravity: 'ne'}); $('#contacts_propertymenu_button').tipsy(); $('#contacts_newcontact, #contacts_import, #bottomcontrols .settings').tipsy({gravity: 'sw'}); $('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'); OC.Contacts.Card.addProperty(type); $('#contacts_propertymenu_dropdown').hide(); } $('#contacts_propertymenu_dropdown a').click(propertyMenuItem); $('#contacts_propertymenu_dropdown a').keydown(propertyMenuItem); }, Card:{ update:function(params) { // params {cid:int, aid:int} if(!params) { params = {}; } $('#contacts li,#contacts h3').removeClass('active'); console.log('Card, cid: ' + params.cid + ' aid: ' + params.aid); var newid, bookid, firstitem; if(!parseInt(params.cid) && !parseInt(params.aid)) { firstitem = $('#contacts ul').find('li:first-child'); if(firstitem.length > 0) { if(firstitem.length > 1) { firstitem = firstitem.first(); } newid = parseInt(firstitem.data('id')); bookid = parseInt(firstitem.data('bookid')); } } 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); var listitem = OC.Contacts.Contacts.getContact(newid); //$('#contacts li[data-id="'+newid+'"]'); console.log('Is contact in list? ' + listitem.length); if(listitem.length) { //bookid = parseInt($('#contacts li[data-id="'+newid+'"]').data('bookid')); bookid = parseInt(OC.Contacts.Contacts.getContact(newid).data('bookid')); } else { // contact isn't in list yet. bookid = 'unknown'; } } else { newid = parseInt(params.cid); bookid = parseInt(params.aid); } if(!bookid || !newid) { bookid = parseInt($('#contacts h3').first().data('id')); newid = parseInt($('#contacts').find('li[data-bookid="'+bookid+'"]').first().data('id')); } console.log('newid: ' + newid + ' bookid: ' +bookid); var localLoadContact = function(newid, bookid) { if($('.contacts li').length > 0) { $.getJSON(OC.filePath('contacts', 'ajax', 'contact/details.php'),{'id':newid},function(jsondata){ if(jsondata.status == 'success'){ if(bookid == 'unknown') { bookid = jsondata.data.addressbookid; var contact = OC.Contacts.Contacts.insertContact({ contactlist:$('#contacts ul[data-id="'+bookid+'"]'), data:jsondata.data }); } $('#contacts li[data-id="'+newid+'"],#contacts h3[data-id="'+bookid+'"]').addClass('active'); $('#contacts ul[data-id="'+bookid+'"]').slideDown(300); OC.Contacts.Card.loadContact(jsondata.data, bookid); OC.Contacts.Contacts.scrollTo(newid); } else { OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); } }); } } // Make sure proper DOM is loaded. if(!$('#card').length && newid) { console.log('Loading card DOM'); $.getJSON(OC.filePath('contacts', 'ajax', 'loadcard.php'),{requesttoken:requesttoken},function(jsondata){ if(jsondata.status == 'success'){ $('#rightcontent').html(jsondata.data.page).ready(function() { OC.Contacts.loadHandlers(); localLoadContact(newid, bookid); }); } else { OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); } }); } else if(!newid) { console.log('Loading intro'); // 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 { OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); } }); } else { localLoadContact(newid, bookid); } }, doExport:function() { document.location.href = OC.linkTo('contacts', 'export.php') + '?contactid=' + this.id; }, editNew:function(){ // add a new contact this.id = ''; this.fn = ''; this.fullname = ''; this.givname = ''; this.famname = ''; this.addname = ''; this.honpre = ''; this.honsuf = ''; OC.Contacts.Card.add(';;;;;', '', '', true); return false; }, add:function(n, fn, aid, isnew){ // add a new contact console.log('Adding ' + fn); aid = aid?aid:$('#contacts h3.active').first().data('id'); var localAddcontact = function(n, fn, aid, isnew) { $.post(OC.filePath('contacts', 'ajax', 'contact/add.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; var aid = jsondata.data.aid; $.getJSON(OC.filePath('contacts', 'ajax', 'contact/details.php'),{'id':id},function(jsondata){ if(jsondata.status == 'success'){ OC.Contacts.Card.loadContact(jsondata.data, aid); var item = OC.Contacts.Contacts.insertContact({data:jsondata.data}); if(isnew) { // add some default properties OC.Contacts.Card.addProperty('EMAIL'); OC.Contacts.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')); } }); } if(!$('#card').length) { console.log('Loading card DOM'); $.getJSON(OC.filePath('contacts', 'ajax', 'loadcard.php'),{'requesttoken': requesttoken},function(jsondata){ if(jsondata.status == 'success'){ $('#rightcontent').html(jsondata.data.page).ready(function() { OC.Contacts.loadHandlers(); localAddcontact(n, fn, aid, isnew); }); } else{ OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); } }); } else { localAddcontact(n, fn, aid, isnew); } }, delayedDelete:function() { $('#contacts_deletecard').tipsy('hide'); var newid = '', bookid; var curlistitem = OC.Contacts.Contacts.getContact(this.id); curlistitem.removeClass('active'); var newlistitem = curlistitem.prev('li'); if(!newlistitem) { newlistitem = curlistitem.next('li'); } curlistitem.detach(); if($(newlistitem).is('li')) { newid = newlistitem.data('id'); bookid = newlistitem.data('bookid'); } $('#rightcontent').data('id', newid); OC.Contacts.Contacts.deletionQueue.push(this.id); if(!window.onbeforeunload) { window.onbeforeunload = OC.Contacts.Contacts.warnNotDeleted; } with(this) { delete id; delete fn; delete fullname; delete shortname; delete famname; delete givname; delete addname; delete honpre; delete honsuf; delete data; } if($('.contacts li').length > 0) { OC.Contacts.Card.update({cid:newid, aid:bookid}); } else { // load intro page $.getJSON(OC.filePath('contacts', 'ajax', 'loadintro.php'),{},function(jsondata){ if(jsondata.status == 'success'){ id = ''; $('#rightcontent').html(jsondata.data.page).removeData('id'); } else { OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); } }); } OC.Contacts.notify({ data:curlistitem, message:t('contacts','Click to undo deletion of "') + curlistitem.find('a').text() + '"', timeouthandler:function(contact) { OC.Contacts.Card.doDelete(contact.data('id'), true); delete contact; }, clickhandler:function(contact) { OC.Contacts.Contacts.insertContact({contact:contact}); OC.Contacts.notify({message:t('contacts', 'Cancelled deletion of: "') + curlistitem.find('a').text() + '"'}); window.onbeforeunload = null; } }); }, doDelete:function(id, removeFromQueue) { if(OC.Contacts.Contacts.deletionQueue.indexOf(id) == -1 && removeFromQueue) { return; } $.post(OC.filePath('contacts', 'ajax', 'contact/delete.php'),{'id':id},function(jsondata) { if(jsondata.status == 'error'){ OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); } if(removeFromQueue) { OC.Contacts.Contacts.deletionQueue.splice(OC.Contacts.Contacts.deletionQueue.indexOf(id), 1); } if(OC.Contacts.Contacts.deletionQueue.length == 0) { window.onbeforeunload = null; } }); }, loadContact:function(jsondata, bookid){ this.data = jsondata; this.id = this.data.id; this.bookid = bookid; $('#rightcontent').data('id',this.id); this.populateNameFields(); this.loadPhoto(); this.loadMails(); this.loadPhones(); this.loadAddresses(); this.loadSingleProperties(); OC.Contacts.loadListHandlers(); var note = $('#note'); if(this.data.NOTE) { note.data('checksum', this.data.NOTE[0]['checksum']); var textarea = note.find('textarea'); var txt = this.data.NOTE[0]['value']; var nheight = txt.split('\n').length > 4 ? txt.split('\n').length+2 : 5; textarea.css('min-height', nheight+'em'); textarea.attr('rows', nheight); textarea.val(txt); note.show(); textarea.expandingTextarea(); $('#contacts_propertymenu_dropdown a[data-type="NOTE"]').parent().hide(); } else { note.removeData('checksum'); note.find('textarea').val(''); note.hide(); $('#contacts_propertymenu_dropdown a[data-type="NOTE"]').parent().show(); } }, loadSingleProperties:function() { var props = ['BDAY', 'NICKNAME', 'ORG', 'URL', 'CATEGORIES']; // Clear all elements $('#ident .propertycontainer').each(function(){ if(props.indexOf($(this).data('element')) > -1) { $(this).data('checksum', ''); $(this).find('input').val(''); $(this).hide(); $(this).prev().hide(); } }); for(var prop in props) { var propname = props[prop]; if(this.data[propname] != undefined) { $('#contacts_propertymenu_dropdown a[data-type="'+propname+'"]').parent().hide(); var property = this.data[propname][0]; var value = property['value'], checksum = property['checksum']; if(propname == 'BDAY') { var val = $.datepicker.parseDate('yy-mm-dd', value.substring(0, 10)); value = $.datepicker.formatDate('dd-mm-yy', val); } var identcontainer = $('#contact_identity'); identcontainer.find('#'+propname.toLowerCase()).val(value); identcontainer.find('#'+propname.toLowerCase()+'_value').data('checksum', checksum); identcontainer.find('#'+propname.toLowerCase()+'_label').show(); identcontainer.find('#'+propname.toLowerCase()+'_value').show(); } else { $('#contacts_propertymenu_dropdown a[data-type="'+propname+'"]').parent().show(); } } }, populateNameFields:function() { 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(''); } }); with(this) { delete fn; delete fullname; delete givname; delete famname; delete addname; delete honpre; delete honsuf; } if(this.data.FN) { this.fn = this.data.FN[0]['value']; } else { this.fn = ''; } if(this.data.N == undefined) { narray = [this.fn,'','','','']; // Checking for non-existing 'N' property :-P } else { narray = this.data.N[0]['value']; } this.famname = narray[0] || ''; this.givname = narray[1] || ''; this.addname = narray[2] || ''; this.honpre = narray[3] || ''; this.honsuf = narray[4] || ''; 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; } $('#n').val(narray.join(';')); $('#fn_select option').remove(); var names = [this.fn, this.fullname, this.givname + ' ' + this.famname, this.famname + ' ' + this.givname, this.famname + ', ' + this.givname]; if(this.data.ORG) { names[names.length]=this.data.ORG[0].value; } $.each(names, function(key, value) { $('#fn_select') .append($('') .text(value)); }); $('#fn_select').combobox('value', this.fn); $('#contact_identity').find('*[data-element="N"]').data('checksum', this.data.N[0]['checksum']); if(this.data.FN) { $('#contact_identity').find('*[data-element="FN"]').data('checksum', this.data.FN[0]['checksum']); } $('#contact_identity').show(); }, hasCategory:function(category) { if(this.data.CATEGORIES) { var categories = this.data.CATEGORIES[0]['value'].split(/,\s*/); for(var c in categories) { var cat = this.data.CATEGORIES[0]['value'][c]; if(typeof cat === 'string' && (cat.toUpperCase() === category.toUpperCase())) { return true; } } } return false; }, categoriesChanged:function(newcategories) { // Categories added/deleted. categories = $.map(newcategories, function(v) {return v;}); $('#categories').multiple_autocomplete('option', 'source', categories); var categorylist = $('#categories_value').find('input'); $.getJSON(OC.filePath('contacts', 'ajax', 'categories/categoriesfor.php'),{'id':OC.Contacts.Card.id},function(jsondata){ if(jsondata.status == 'success'){ $('#categories_value').data('checksum', jsondata.data.checksum); categorylist.val(jsondata.data.value); } else { OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); } }); }, savePropertyInternal:function(name, fields, oldchecksum, checksum) { // TODO: Add functionality for new fields. //console.log('savePropertyInternal: ' + name + ', fields: ' + fields + 'checksum: ' + checksum); //console.log('savePropertyInternal: ' + this.data[name]); var multivalue = ['CATEGORIES']; var params = {}; var value = multivalue.indexOf(name) != -1 ? new Array() : undefined; jQuery.each(fields, function(i, field){ //.substring(11,'parameters[TYPE][]'.indexOf(']')) if(field.name.substring(0, 5) === 'value') { if(multivalue.indexOf(name) != -1) { value.push(field.value); } else { value = field.value; } } 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; } } }, saveProperty:function(obj) { if(!$(obj).hasClass('contacts_property')) { return false; } if($(obj).hasClass('nonempty') && $(obj).val().trim() == '') { OC.dialogs.alert(t('contacts', 'This property has to be non-empty.'), t('contacts', 'Error')); return false; } container = $(obj).parents('.propertycontainer').first(); // get the parent holding the metadata. OC.Contacts.loading(obj, true); var checksum = container.data('checksum'); var name = container.data('element'); var fields = container.find('input.contacts_property,select.contacts_property').serializeArray(); switch(name) { case 'FN': var nempty = true; for(var i in OC.Contacts.Card.data.N[0]['value']) { if(OC.Contacts.Card.data.N[0]['value'][i] != '') { nempty = false; break; } } if(nempty) { $('#n').val(fields[0].value + ';;;;'); OC.Contacts.Card.data.N[0]['value'] = Array(fields[0].value, '', '', '', ''); setTimeout(function() {OC.Contacts.Card.saveProperty($('#n'))}, 500); } break; } var q = container.find('input.contacts_property,select.contacts_property,textarea.contacts_property').serialize(); if(q == '' || q == undefined) { OC.dialogs.alert(t('contacts', 'Couldn\'t serialize elements.'), t('contacts', 'Error')); OC.Contacts.loading(obj, false); return false; } q = q + '&id=' + this.id + '&name=' + name; if(checksum != undefined && checksum != '') { // save q = q + '&checksum=' + checksum; console.log('Saving: ' + q); $(obj).attr('disabled', 'disabled'); $.post(OC.filePath('contacts', 'ajax', 'contact/saveproperty.php'),q,function(jsondata){ if(jsondata.status == 'success'){ container.data('checksum', jsondata.data.checksum); OC.Contacts.Card.savePropertyInternal(name, fields, checksum, jsondata.data.checksum); OC.Contacts.loading(obj, false); $(obj).removeAttr('disabled'); return true; } else{ OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); OC.Contacts.loading(obj, false); $(obj).removeAttr('disabled'); return false; } },'json'); } else { // add console.log('Adding: ' + q); $(obj).attr('disabled', 'disabled'); $.post(OC.filePath('contacts', 'ajax', 'contact/addproperty.php'),q,function(jsondata){ if(jsondata.status == 'success'){ container.data('checksum', jsondata.data.checksum); // TODO: savePropertyInternal doesn't know about new fields //OC.Contacts.Card.savePropertyInternal(name, fields, checksum, jsondata.data.checksum); OC.Contacts.loading(obj, false); $(obj).removeAttr('disabled'); return true; } else{ OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); OC.Contacts.loading(obj, false); $(obj).removeAttr('disabled'); return false; } },'json'); } }, addProperty:function(type) { switch (type) { case 'NOTE': $('#contacts_propertymenu_dropdown a[data-type="'+type+'"]').parent().hide(); $('#note').find('textarea').expandingTextarea().show().focus(); break; case 'EMAIL': if($('#emaillist>li').length == 1) { $('#emails').show(); } OC.Contacts.Card.addMail(); break; case 'TEL': if($('#phonelist>li').length == 1) { $('#phones').show(); } OC.Contacts.Card.addPhone(); break; case 'ADR': if($('#addressdisplay>dl').length == 1) { $('#addresses').show(); } OC.Contacts.Card.editAddress('new', true); break; case 'NICKNAME': case 'URL': case 'ORG': case 'BDAY': case 'CATEGORIES': $('dl dt[data-element="'+type+'"],dd[data-element="'+type+'"]').show(); $('dd[data-element="'+type+'"]').find('input').focus(); $('#contacts_propertymenu_dropdown a[data-type="'+type+'"]').parent().hide(); break; } }, deleteProperty:function(obj, type) { console.log('deleteProperty'); OC.Contacts.loading(obj, true); var checksum = OC.Contacts.checksumFor(obj); if(checksum) { $.post(OC.filePath('contacts', 'ajax', 'contact/deleteproperty.php'),{'id': this.id, 'checksum': checksum },function(jsondata){ if(jsondata.status == 'success'){ if(type == 'list') { OC.Contacts.propertyContainerFor(obj).remove(); } else if(type == 'single') { var proptype = OC.Contacts.propertyTypeFor(obj); OC.Contacts.Card.data[proptype] = null; var othertypes = ['NOTE', 'PHOTO']; if(othertypes.indexOf(proptype) != -1) { OC.Contacts.propertyContainerFor(obj).data('checksum', ''); if(proptype == 'PHOTO') { OC.Contacts.Contacts.refreshThumbnail(OC.Contacts.Card.id); OC.Contacts.Card.loadPhoto(true); } else if(proptype == 'NOTE') { $('#note').find('textarea').val(''); OC.Contacts.propertyContainerFor(obj).hide(); } } else { $('dl dt[data-element="'+proptype+'"],dd[data-element="'+proptype+'"]').hide(); $('dl dd[data-element="'+proptype+'"]').data('checksum', '').find('input').val(''); } $('#contacts_propertymenu_dropdown a[data-type="'+proptype+'"]').parent().show(); OC.Contacts.loading(obj, false); } else { OC.dialogs.alert(t('contacts', '\'deleteProperty\' called without type argument. Please report at bugs.owncloud.org'), t('contacts', 'Error')); OC.Contacts.loading(obj, false); } } else{ OC.Contacts.loading(obj, false); OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); } }); } else { // Property hasn't been saved so there's nothing to delete. if(type == 'list') { OC.Contacts.propertyContainerFor(obj).remove(); } else if(type == 'single') { var proptype = OC.Contacts.propertyTypeFor(obj); $('dl dt[data-element="'+proptype+'"],dd[data-element="'+proptype+'"]').hide(); $('#contacts_propertymenu_dropdown a[data-type="'+proptype+'"]').parent().show(); OC.Contacts.loading(obj, false); } else { OC.dialogs.alert(t('contacts', '\'deleteProperty\' called without type argument. Please report at bugs.owncloud.org'), t('contacts', 'Error')); } } }, editName:function() { var params = {id: this.id}; /* Initialize the name edit dialog */ if($('#edit_name_dialog').dialog('isOpen') == true) { $('#edit_name_dialog').dialog('moveToTop'); } else { $.getJSON(OC.filePath('contacts', 'ajax', 'editname.php'),{id: this.id},function(jsondata) { if(jsondata.status == 'success') { $('body').append('
'); $('#name_dialog').html(jsondata.data.page).find('#edit_name_dialog' ).dialog({ modal: true, closeOnEscape: true, title: t('contacts', 'Edit name'), height: 'auto', width: 'auto', buttons: { 'Ok':function() { OC.Contacts.Card.saveName(this); $(this).dialog('close'); }, 'Cancel':function() { $(this).dialog('close'); } }, close: function(event, ui) { $(this).dialog('destroy').remove(); $('#name_dialog').remove(); }, open: function(event, ui) { // load 'N' property - maybe :-P } }); } else { OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); } }); } }, saveName:function(dlg) { //console.log('saveName, id: ' + this.id); 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()); this.famname = n[0]; this.givname = n[1]; this.addname = n[2]; this.honpre = n[3]; this.honsuf = n[4]; this.fullname = ''; $('#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]; } $('#fn_select option').remove(); //$('#fn_select').combobox('value', this.fn); 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]); } } $.each(names, function(key, value) { $('#fn_select') .append($('') .text(value)); }); if(this.id == '') { var aid = $(dlg).find('#aid').val(); OC.Contacts.Card.add(n.join(';'), $('#short').text(), aid); } else { OC.Contacts.Card.saveProperty($('#n')); } }, loadAddresses:function() { $('#addresses').hide(); $('#addressdisplay dl.propertycontainer').remove(); var addresscontainer = $('#addressdisplay'); for(var adr in this.data.ADR) { addresscontainer.find('dl').first().clone().insertAfter($('#addressdisplay dl').last()).show(); addresscontainer.find('dl').last().removeClass('template').addClass('propertycontainer'); addresscontainer.find('dl').last().data('checksum', this.data.ADR[adr]['checksum']); var adrarray = this.data.ADR[adr]['value']; var adrtxt = ''; if(adrarray[0] && adrarray[0].length > 0) { adrtxt = adrtxt + '