From f3272ad1594c00735406a5e4ba2961af72ebcf05 Mon Sep 17 00:00:00 2001 From: babelouest Date: Wed, 7 May 2014 16:54:58 -0400 Subject: [PATCH] reimplemented import ui in a ocdialog --- js/addressbooks.js | 187 ++++++++++++++++--------- lib/connector/importvcardconnector.php | 13 +- lib/controller/importcontroller.php | 10 +- templates/contacts.php | 3 +- 4 files changed, 137 insertions(+), 76 deletions(-) diff --git a/js/addressbooks.js b/js/addressbooks.js index fa571cc6..76016349 100644 --- a/js/addressbooks.js +++ b/js/addressbooks.js @@ -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('
'); + 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('
'); - 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 = $(''); + $import_into.append($option); + var nbOptions = 0; + $.each(response.data.addressbooks, function(idx, addressBook) { + if (addressBook.permissions & OC.PERMISSION_UPDATE) { + var $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 = $('