1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-18 07:52:21 +01:00

reimplemented import ui in a ocdialog

This commit is contained in:
babelouest 2014-05-07 16:54:58 -04:00
parent b89b933a98
commit f3272ad159
4 changed files with 137 additions and 76 deletions

View File

@ -253,15 +253,15 @@ OC.Contacts = OC.Contacts || {};
this.$bookList = this.$bookTemplate.find('.addressbooklist');
this.$bookItemTemplate = bookItemTemplate;
this.$importIntoSelect = $('#import_into');
this.$importFormatSelect = this.$bookTemplate.find('#import_format');
this.$importProgress = this.$bookTemplate.find('#import-status-progress');
this.$importStatusText = this.$bookTemplate.find('#import-status-text');
this.$importFormatSelect = $('#import_format');
this.$importProgress = $('#import-status-progress');
this.$importStatusText = $('#import-status-text');
this.addressBooks = [];
if(this.isFileAction) {
return;
}
this.$importFileInput = this.$bookTemplate.find('#import_upload_start');
this.$importFileInput = $('#import_upload_start');
var $addInput = this.$bookTemplate.find('#add-address-book');
$addInput.addnew({
ok: function(event, name) {
@ -287,43 +287,71 @@ OC.Contacts = OC.Contacts || {};
$(document).bind('status.addressbook.added', function() {
self.buildImportSelect();
})
this.$importFormatSelect.on('change', function() {
self.$importIntoSelect.trigger('change');
$('#oc-import-nocontact').unbind('click').click(function(event) {
console.log("triggered", event);
self.importDialog();
});
this.$importIntoSelect.on('change', function() {
// Disable file input if no address book selected
var value = $(this).val();
self.$importFileInput.prop('disabled', value === '-1' );
if(value !== '-1') {
var url = OC.generateUrl(
'apps/contacts/addressbook/{backend}/{addressBookId}/{importType}/import/upload',
{
addressBookId:value,
importType:self.$importFormatSelect.find('option:selected').val(),
backend: $(this).find('option:selected').data('backend')
$('#import-contacts').unbind('click').click(function() {
console.log("Import clicked");
self.importDialog();
});
};
AddressBookList.prototype.importDialog = function() {
var $rightContent = $('#app-content');
$rightContent.append('<div id="import-dialog"></div>');
var $dlg = $('#contactsImportTemplate').clone().octemplate();
var $divDlg = $('#import-dialog');
var self = this;
$divDlg.html($dlg).ocdialog({
modal: true,
closeOnEscape: true,
title: t('contacts', 'Import contacts'),
height: '220',
width: 'auto',
buttons: [
{
text: t('contacts', 'Close'),
click: function() {
$('#import-dialog').ocdialog('close').ocdialog('destroy').remove();
}
);
self.$importFileInput.fileupload('option', 'url', url);
}
],
close: function(/*event, ui*/) {
$('#import-dialog').ocdialog('close').ocdialog('destroy').remove();
},
open: function(/*event, ui*/) {
self.openImportDialog();
}
});
}
AddressBookList.prototype.openImportDialog = function() {
this.$importIntoSelect = $('#import_into');
this.$importFormatSelect = $('#import_format');
this.$importProgress = $('#import-status-progress');
this.$importStatusText = $('#import-status-text');
this.$importFileInput = $('#import_upload_start');
var me = this;
var self = this;
this.$importFileInput.fileupload({
dataType: 'json',
start: function(e, data) {
self.$importProgress.progressbar({value:false});
me.$importProgress.progressbar({value:false});
$('.tipsy').remove();
$('.import-upload').hide();
$('.import-status').show();
self.$importProgress.fadeIn();
self.$importStatusText.text(t('contacts', 'Starting file import'));
me.$importProgress.fadeIn();
me.$importStatusText.text(t('contacts', 'Starting file import'));
},
done: function (e, data) {
if ($('#import_format').find('option:selected').val() != 'automatic') {
$('#import-status-text').text(t('contacts', 'Format selected: {format}',
if (me.$importFormatSelect.find('option:selected').val() != 'automatic') {
me.$importStatusText.text(t('contacts', 'Format selected: {format}',
{format: $('#import_format').find('option:selected').text() }));
} else {
$('#import-status-text').text(t('contacts', 'Automatic format detection'));
me.$importStatusText.text(t('contacts', 'Automatic format detection'));
}
console.log('Upload done:', data);
console.log('Upload done:', self.addressBooks);
self.doImport(self.storage.formatResponse(data.jqXHR));
},
fail: function(e, data) {
@ -333,46 +361,68 @@ OC.Contacts = OC.Contacts || {};
$('.import-status').hide();
}
});
$('#import-contacts').on('click keypress', function() {
var $rightContent = $('#app-content');
$rightContent.append('<div id="import-dialog"></div>');
var $dlg = $('#contactsImportTemplate').octemplate();
var $divDlg = $('#import-dialog');
$divDlg.html($dlg).ocdialog({
modal: true,
closeOnEscape: true,
title: t('contacts', 'Import contacts'),
height: '220',
width: 'auto',
buttons: [
var $import_into = $('#import_into');
$import_into.change(function() {
if ($(this).val() != '-1') {
var url = OC.generateUrl(
'apps/contacts/addressbook/{backend}/{addressBookId}/{importType}/import/upload',
{
text: t('contacts', 'Close'),
click: function() {
$divDlg.ocdialog().ocdialog('close');
$divDlg.ocdialog().ocdialog('destroy').remove();
}
addressBookId:$import_into.val(),
importType:me.$importFormatSelect.find('option:selected').val(),
backend: $import_into.find('option:selected').attr('backend')
}
],
close: function(/*event, ui*/) {
$divDlg.ocdialog().ocdialog('close');
$divDlg.ocdialog().ocdialog('destroy').remove();
},
open: function(/*event, ui*/) {
self.buildImportSelect();
}
});
);
me.$importFileInput.fileupload('option', 'url', url);
me.$importFileInput.attr('disabled', false);
} else {
me.$importFileInput.attr('disabled', true);
}
});
};
$.when(self.storage.getAddressBooksForUser()).then(function(response) {
if(!response.error) {
$import_into.empty();
var $option = $('<option value="-1">' + t('contacts', 'Import into...') + '</option>');
$import_into.append($option);
var nbOptions = 0;
$.each(response.data.addressbooks, function(idx, addressBook) {
if (addressBook.permissions & OC.PERMISSION_UPDATE) {
var $option=$('<option></option>').val(addressBook.id).html(addressBook.displayname).attr('backend', addressBook.backend);
self.insertAddressBook(addressBook);
$import_into.append($option);
nbOptions++;
}
});
if (nbOptions === 1) {
$import_into.val($import_into.find('option:not([value="-1"])').first().val());
$import_into.attr('disabled', true);
me.$importFileInput.attr('disabled', false);
var url = OC.generateUrl(
'apps/contacts/addressbook/{backend}/{addressBookId}/{importType}/import/upload',
{
addressBookId:$import_into.val(),
importType:me.$importFormatSelect.find('option:selected').val(),
backend: $import_into.find('option:selected').attr('backend')
}
);
me.$importFileInput.fileupload('option', 'url', url);
}
} else {
console.log('status.contacts.error', response);
}
});
}
AddressBookList.prototype.count = function() {
return this.addressBooks.length;
};
/**
* For importing from oC filesyatem
* For importing from oC filesystem
*/
AddressBookList.prototype.prepareImport = function(backend, addressBookId, importType, path, fileName) {
console.log('prepareImport', backend, addressBookId, importType, path, fileName);
this.$importStatusText = $('#import-status-text');
this.$importProgress = $('#import-status-progress');
this.$importProgress.progressbar({value:false});
if (importType != 'automatic') {
this.$importStatusText.text(t('contacts', 'Format selected: {format}',
@ -388,21 +438,14 @@ OC.Contacts = OC.Contacts || {};
AddressBookList.prototype.doImport = function(response) {
console.log('doImport', response);
this.$importProgress = $('#import-status-progress');
this.$importStatusText = $('#import-status-text');
var defer = $.Deferred();
var done = false;
var interval = null, isChecking = false;
var self = this;
var closeImport = function() {
defer.resolve();
self.$importProgress.fadeOut();
setTimeout(function() {
$('.import-upload').show();
$('.import-status').hide();
self.importCount = null;
if(self.$importProgress.hasClass('ui-progressbar')) {
self.$importProgress.progressbar('destroy');
}
}, 3000);
};
if(!response.error) {
this.$importProgress.progressbar('value', 0);
@ -426,6 +469,7 @@ OC.Contacts = OC.Contacts || {};
if(!response.error) {
console.log('status, response: ', response);
if (response.data.total != null && response.data.progress != null) {
console.log('response.data', response.data);
self.$importProgress.progressbar('option', 'max', Number(response.data.total));
self.$importProgress.progressbar('value', Number(response.data.progress));
self.$importStatusText.text(t('contacts', 'Processing {count}/{total} cards',
@ -442,6 +486,7 @@ OC.Contacts = OC.Contacts || {};
isChecking = false;
});
};
console.log('vertige', data);
$.when(
self.storage.startImport(
data.backend, data.addressBookId, data.importType,
@ -454,7 +499,11 @@ OC.Contacts = OC.Contacts || {};
console.log('Import done');
self.$importStatusText.text(t('contacts', 'Total:{total}, Success:{imported}, Errors:{failed}',
{total: response.data.total, imported:response.data.imported, failed: response.data.failed}));
var addressBook = self.find({id:response.data.addressBookId, backend: response.data.backend});
self.$importProgress.progressbar('option', 'max', response.data.total);
self.$importProgress.progressbar('value', response.data.total);
self.buildImportSelect();
var addressBook = self.find({id:data.addressBookId, backend: data.backend});
console.log('addressBook', self.count(), self.addressBooks);
$(document).trigger('status.addressbook.imported', {
addressbook: addressBook
});
@ -471,9 +520,9 @@ OC.Contacts = OC.Contacts || {};
$(document).trigger('status.contacts.error', response);
done = true;
});
interval = setInterval(function() {
/*interval = setInterval(function() {
getStatus(data.backend, data.addressBookId, data.importType, data.progresskey, interval, done);
}, 1500);
}, 1500);*/
} else {
defer.reject(response);
done = true;
@ -489,12 +538,12 @@ OC.Contacts = OC.Contacts || {};
*/
AddressBookList.prototype.buildImportSelect = function() {
console.log('buildImportSelect', this);
var self = this;
/*var self = this;
this.$importIntoSelect.find('option:not([value="-1"])').remove();
var addressBooks = self.selectByPermission(OC.PERMISSION_UPDATE);
if (addressBooks.length > 0) {
console.log('ImportInto Select', self.$importIntoSelect);
//console.log('addressbooks', addressBooks);
console.log('addressbooks', addressBooks);
$.each(addressBooks, function(idx, book) {
var $opt = $('<option />');
$opt.val(book.getId()).text(book.getDisplayName()).data('backend', book.getBackend());
@ -512,7 +561,7 @@ OC.Contacts = OC.Contacts || {};
self.$importFileInput.prop('disabled', true);
}
}
}
}*/
};
/**

View File

@ -45,7 +45,10 @@ class ImportVCardConnector extends ImportConnector{
$elements = array();
foreach($parts as $part) {
$elements[] = $this->convertElementToVCard($part);
$converted = $this->convertElementToVCard($part);
if ($converted) {
$elements[] = $converted;
}
}
return array_values($elements);
@ -100,10 +103,14 @@ class ImportVCardConnector extends ImportConnector{
/**
* @brief converts a VCard into a owncloud VCard
* @param $element the VCard element to convert
* @return VCard
* @return VCard|false
*/
public function convertElementToVCard($element) {
$source = VObject\Reader::read($element);
try {
$source = VObject\Reader::read($element, VObject\Reader::OPTION_FORGIVING);
} catch (VObject\ParseException $error) {
return false;
}
$dest = \Sabre\VObject\Component::create('VCARD');
foreach ($source->children() as $sourceProperty) {

View File

@ -258,7 +258,7 @@ class ImportController extends Controller {
$favourites = $part->select('X-FAVOURITES');
foreach ($favourites as $favourite) {
if ($favourite->getValue() == 'yes') {
$tagMgr = $this->server->getTagManager()->load('contact');
$tagMgr = \OC::$server->getTagManager()->load('contact');
$tagMgr->addToFavorites($id);
}
}
@ -307,9 +307,13 @@ class ImportController extends Controller {
$response->bailOut(App::$l10n->t('Progress key missing from request.'));
return $response;
}
error_log("progresskey: ".$this->cache->get($progresskey)." total: ".$this->cache->get($progresskey.'_total') );
$response->setParams(array('progress' => $this->cache->get($progresskey), 'total' => $this->cache->get($progresskey.'_total') ));
return $response;
}
/**
* @NoAdminRequired
*/
public function detectFileType() {
}
}

View File

@ -106,6 +106,7 @@ use OCA\Contacts\ImportManager;
<p><?php p($l->t('Add a new contact or import existing contacts from a VCF file.')); ?></p>
<div id="selections">
<input type="button" class="add-contact text" value="<?php p($l->t('New contact')) ?>">
<input type="button" id="oc-import-nocontact" value="<?php p($l->t('Import contacts')); ?>">
</div>
</div>
</div>
@ -507,7 +508,7 @@ use OCA\Contacts\ImportManager;
?>
</select>
<button class="icon-upload svg tooltipped import-upload-button" title="<?php p($l->t('Select file...')); ?>"></button>
<input id="import_upload_start" class="tooltipped" title="<?php p($l->t('Select file...')); ?>" type="file" accept="text/vcard,text/x-vcard,text/directory" name="file" disabled />
<input id="import_upload_start" class="tooltipped" title="<?php p($l->t('Select file...')); ?>" type="file" name="file" disabled />
</li>
<li class="import-status">
<label id="import-status-text"></label>