2012-09-07 14:32:45 +02:00
|
|
|
/**
|
2013-09-10 00:28:16 +02:00
|
|
|
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
|
2012-09-07 14:32:45 +02:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
2013-09-10 00:28:16 +02:00
|
|
|
OC.ContactsImporter = OC.ContactsImporter || {
|
|
|
|
init:function(fileName) {
|
|
|
|
var self = OC.ContactsImporter;
|
|
|
|
self.path = $('#dir').val();
|
|
|
|
self.fileName = fileName;
|
|
|
|
console.log('fileName', self.path, self.fileName);
|
2014-07-04 17:16:33 +02:00
|
|
|
/*OC.addScript('contacts', 'addressbooks', function() {
|
2013-09-10 00:28:16 +02:00
|
|
|
OC.addScript('contacts', 'storage', function() {
|
|
|
|
$.when(self._getTemplate()).then(function($tmpl) {
|
|
|
|
self.$template = $tmpl;
|
|
|
|
if(self.$dialog) {
|
|
|
|
self.$dialog.ocdialog('close');
|
|
|
|
}
|
|
|
|
self.$dialog = self.$template.octemplate({
|
|
|
|
selectText: t('contacts', 'Please choose the addressbook'),
|
|
|
|
defaultText: t('contacts', 'Import into...')
|
2012-09-07 14:32:45 +02:00
|
|
|
});
|
2013-09-10 00:28:16 +02:00
|
|
|
self.$dialog.appendTo($('body'));
|
|
|
|
self.addressBooks = new OC.Contacts.AddressBookList(
|
|
|
|
new OC.Contacts.Storage(), self.$dialog, null, true
|
|
|
|
);
|
|
|
|
self.showDialog();
|
|
|
|
})
|
|
|
|
.fail(function() {
|
|
|
|
alert(t('contacts', 'Error loading import template'));
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.fail(function(jqxhr, settings, exception) {
|
|
|
|
console.warn('Error loading storage backend', jqxhr, settings, exception);
|
2012-09-07 14:32:45 +02:00
|
|
|
});
|
2013-09-10 00:28:16 +02:00
|
|
|
})
|
|
|
|
.fail(function(jqxhr, settings, exception) {
|
|
|
|
console.warn('Error loading address book backend', jqxhr, settings, exception);
|
2014-07-04 17:19:55 +02:00
|
|
|
});*/
|
2013-09-10 00:28:16 +02:00
|
|
|
},
|
|
|
|
showDialog:function() {
|
|
|
|
var self = this;
|
|
|
|
$.when(self.addressBooks.loadAddressBooks()).then(function(response) {
|
|
|
|
if(!response.error) {
|
|
|
|
self.$dialog.ocdialog({
|
|
|
|
modal: true,
|
|
|
|
title: t('contacts', 'Import contacts'),
|
|
|
|
close: function() {
|
|
|
|
$(this).ocdialog('destroy').remove();
|
|
|
|
self.$dialog = null;
|
|
|
|
}
|
|
|
|
});
|
2014-05-08 04:46:22 +02:00
|
|
|
self.$importIntoSelect = $('#contacts-import-into');
|
2013-09-10 00:28:16 +02:00
|
|
|
self.$importIntoSelect.on('change', function() {
|
|
|
|
var $selected = $(this).find('option:selected');
|
|
|
|
if($(this).val() === '-1') {
|
|
|
|
self.$dialog.ocdialog('option', 'buttons', []);
|
|
|
|
} else {
|
|
|
|
self.$dialog.ocdialog('option', 'buttons', [{
|
|
|
|
text: t('contacts', 'Import'),
|
|
|
|
defaultButton: true,
|
|
|
|
click:function() {
|
|
|
|
console.log('Selected', $selected);
|
|
|
|
self.$dialog.ocdialog('option', {
|
|
|
|
buttons: [],
|
|
|
|
closeButton: false,
|
|
|
|
title: t('contacts', 'Importing...')
|
|
|
|
});
|
2014-12-08 21:56:12 +01:00
|
|
|
self.startImport($selected.data('backend'), $selected.val(), self.$importFormatSelect.find('option:selected').val());
|
2013-09-10 00:28:16 +02:00
|
|
|
}
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.warn('response.message');
|
2012-09-07 14:32:45 +02:00
|
|
|
}
|
2013-09-10 00:28:16 +02:00
|
|
|
})
|
|
|
|
.fail(function(response) {
|
|
|
|
console.warn(response);
|
2012-09-07 14:32:45 +02:00
|
|
|
});
|
|
|
|
},
|
2013-09-20 16:24:29 +02:00
|
|
|
startImport: function(backend, addressBookId, importType) {
|
2013-09-10 00:28:16 +02:00
|
|
|
var self = this;
|
|
|
|
$('.import-select').hide();
|
|
|
|
$('.import-status').show();
|
2013-09-20 16:24:29 +02:00
|
|
|
$.when(self.addressBooks.prepareImport(backend, addressBookId, importType, this.path, this.fileName))
|
2013-09-10 00:28:16 +02:00
|
|
|
.then(function(response) {
|
|
|
|
if(!response.error) {
|
|
|
|
$.when(self.addressBooks.doImport(response)).then(function(response) {
|
|
|
|
self.$dialog.ocdialog('option', {
|
|
|
|
title: t('contacts', 'Import done'),
|
|
|
|
closeButton: true,
|
|
|
|
buttons: [{
|
|
|
|
text: t('contacts', 'Close'),
|
|
|
|
defaultButton: true,
|
|
|
|
click:function() {
|
|
|
|
self.$dialog.ocdialog('close');
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.fail(function(response) {
|
|
|
|
console.warn(response);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.warn('response.message');
|
2012-09-07 14:32:45 +02:00
|
|
|
}
|
2013-09-10 00:28:16 +02:00
|
|
|
})
|
|
|
|
.fail(function(response) {
|
|
|
|
console.warn(response);
|
2012-09-07 14:32:45 +02:00
|
|
|
});
|
2013-09-10 00:28:16 +02:00
|
|
|
},
|
|
|
|
_getTemplate: function() {
|
2014-03-08 06:30:29 +01:00
|
|
|
var defer = $.Deferred();
|
2013-09-10 00:28:16 +02:00
|
|
|
if(!this.$template) {
|
|
|
|
$.get(OC.filePath('contacts', 'templates', 'importdialog.html'), function(tmpl) {
|
|
|
|
defer.resolve($(tmpl));
|
|
|
|
})
|
|
|
|
.fail(function() {
|
|
|
|
defer.reject();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
defer.resolve(this.$template);
|
|
|
|
}
|
|
|
|
return defer.promise();
|
2012-11-01 02:01:00 +01:00
|
|
|
}
|
2012-12-17 15:19:22 +01:00
|
|
|
};
|
2012-11-01 02:01:00 +01:00
|
|
|
|
2012-09-07 14:32:45 +02:00
|
|
|
$(document).ready(function(){
|
2013-09-10 00:28:16 +02:00
|
|
|
|
2014-10-01 18:08:24 +02:00
|
|
|
// translate search result type
|
2014-10-27 18:08:54 +01:00
|
|
|
OC.search.resultTypes.contact = t('contacts', 'Contact');
|
2014-10-01 18:08:24 +02:00
|
|
|
|
|
|
|
OC.search.customResults.contact = function (row, item){
|
|
|
|
var text = '';
|
|
|
|
if (item.email) {
|
|
|
|
text += '✉ ' + item.email;
|
|
|
|
if (item.phone) {
|
|
|
|
text += ', '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (item.phone) {
|
|
|
|
text += '☎ ' + item.phone
|
|
|
|
}
|
|
|
|
row.find('td.result .text').text(text);
|
|
|
|
};
|
|
|
|
|
2013-09-17 18:46:59 +02:00
|
|
|
// If the app is already active there's no need for the FileActions
|
|
|
|
if(OC.Contacts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-10 00:28:16 +02:00
|
|
|
$(document).bind('status.contacts.error', function(e, data) {
|
|
|
|
console.warn(data.message);
|
|
|
|
//console.trace();
|
|
|
|
//OC.notify({message:data.message});
|
|
|
|
});
|
|
|
|
|
2012-09-07 14:32:45 +02:00
|
|
|
if(typeof FileActions !== 'undefined'){
|
2013-09-10 00:28:16 +02:00
|
|
|
FileActions.register('text/vcard','importaddressbook', OC.PERMISSION_READ, '', OC.ContactsImporter.init);
|
2012-09-07 14:32:45 +02:00
|
|
|
FileActions.setDefault('text/vcard','importaddressbook');
|
2013-09-10 00:28:16 +02:00
|
|
|
FileActions.register('text/x-vcard','importaddressbook', OC.PERMISSION_READ, '', OC.ContactsImporter.init);
|
2012-09-07 14:32:45 +02:00
|
|
|
FileActions.setDefault('text/x-vcard','importaddressbook');
|
2012-12-17 15:19:22 +01:00
|
|
|
}
|
2014-03-29 06:22:52 +01:00
|
|
|
|
2013-09-20 16:24:29 +02:00
|
|
|
});
|