1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-11-29 11:24:11 +01:00
OwncloudContactsOfficial/js/loader.js

168 lines
4.8 KiB
JavaScript
Raw Normal View History

2012-09-07 14:32:45 +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.
*/
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);
/*OC.addScript('contacts', 'addressbooks', function() {
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
});
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
});
})
.fail(function(jqxhr, settings, exception) {
console.warn('Error loading address book backend', jqxhr, settings, exception);
});*/
},
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;
}
});
self.$importIntoSelect = $('#contacts-import-into');
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());
}
}]);
}
});
} else {
console.warn('response.message');
2012-09-07 14:32:45 +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) {
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))
.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
}
})
.fail(function(response) {
console.warn(response);
2012-09-07 14:32:45 +02:00
});
},
_getTemplate: function() {
2014-03-08 06:30:29 +01:00
var defer = $.Deferred();
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-09-07 14:32:45 +02:00
$(document).ready(function(){
2014-10-01 18:08:24 +02:00
// translate search result type
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;
}
$(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'){
FileActions.register('text/vcard','importaddressbook', OC.PERMISSION_READ, '', OC.ContactsImporter.init);
2012-09-07 14:32:45 +02:00
FileActions.setDefault('text/vcard','importaddressbook');
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');
}
2013-09-20 16:24:29 +02:00
});