]> source.dussan.org Git - nextcloud-server.git/commitdiff
Defer initialisation of data until after complete construction
authorRobin McCorkell <rmccorkell@owncloud.com>
Sun, 22 Nov 2015 17:25:32 +0000 (17:25 +0000)
committerRobin McCorkell <rmccorkell@owncloud.com>
Sun, 22 Nov 2015 17:25:32 +0000 (17:25 +0000)
apps/files_external/js/public_key.js
apps/files_external/js/settings.js

index e35d7ad37899ad10d5b70a543c0516d3e3c2c214..5f9658381f0d0d238d1ba2b05a86f110600507a8 100644 (file)
@@ -1,10 +1,16 @@
 $(document).ready(function() {
 
-       OCA.External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme) {
+       OCA.External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme, onCompletion) {
                if (scheme === 'publickey') {
                        var config = $tr.find('.configuration');
                        if ($(config).find('[name="public_key_generate"]').length === 0) {
                                setupTableRow($tr, config);
+                               onCompletion.then(function() {
+                                       // If there's no private key, build one
+                                       if (0 === $(config).find('[data-parameter="private_key"]').val().length) {
+                                               generateKeys($tr);
+                                       }
+                               });
                        }
                }
        });
@@ -22,10 +28,6 @@ $(document).ready(function() {
                        .attr('value', t('files_external', 'Generate keys'))
                        .attr('name', 'public_key_generate')
                );
-               // If there's no private key, build one
-               if (0 === $(config).find('[data-parameter="private_key"]').val().length) {
-                       generateKeys(tr);
-               }
        }
 
        function generateKeys(tr) {
index e16944eaa19162b3a2604246d8dcc18d07342f8b..f712ecf432853ba99fe83d38862c85a300744b85 100644 (file)
@@ -703,7 +703,9 @@ MountConfigListView.prototype = _.extend({
                storageConfig.backend = $target.val();
                $tr.find('.mountPoint input').val('');
 
-               $tr = this.newStorage(storageConfig);
+               var onCompletion = jQuery.Deferred();
+               $tr = this.newStorage(storageConfig, onCompletion);
+               onCompletion.resolve();
 
                $tr.find('td.configuration').children().not('[type=hidden]').first().focus();
                this.saveStorageConfig($tr);
@@ -712,8 +714,23 @@ MountConfigListView.prototype = _.extend({
        _onSelectAuthMechanism: function(event) {
                var $target = $(event.target);
                var $tr = $target.closest('tr');
-
                var authMechanism = $target.val();
+
+               var onCompletion = jQuery.Deferred();
+               this.configureAuthMechanism($tr, authMechanism, onCompletion);
+               onCompletion.resolve();
+
+               this.saveStorageConfig($tr);
+       },
+
+       /**
+        * Configure the storage config with a new authentication mechanism
+        *
+        * @param {jQuery} $tr config row
+        * @param {string} authMechanism
+        * @param {jQuery.Deferred} onCompletion
+        */
+       configureAuthMechanism: function($tr, authMechanism, onCompletion) {
                var authMechanismConfiguration = this._allAuthMechanisms[authMechanism];
                var $td = $tr.find('td.configuration');
                $td.find('.auth-param').remove();
@@ -723,22 +740,18 @@ MountConfigListView.prototype = _.extend({
                ));
 
                this.trigger('selectAuthMechanism',
-                       $tr, authMechanism, authMechanismConfiguration['scheme']
+                       $tr, authMechanism, authMechanismConfiguration['scheme'], onCompletion
                );
-
-               if ($tr.data('constructing') !== true) {
-                       // row is ready, trigger recheck
-                       this.saveStorageConfig($tr);
-               }
        },
 
        /**
         * Create a config row for a new storage
         *
         * @param {StorageConfig} storageConfig storage config to pull values from
+        * @param {jQuery.Deferred} onCompletion
         * @return {jQuery} created row
         */
-       newStorage: function(storageConfig) {
+       newStorage: function(storageConfig, onCompletion) {
                var mountPoint = storageConfig.mountPoint;
                var backend = this._allBackends[storageConfig.backend];
 
@@ -753,8 +766,6 @@ MountConfigListView.prototype = _.extend({
                $tr.find('select#selectBackend');
                addSelect2($tr.find('.applicableUsers'), this._userListLimit);
 
-               $tr.data('constructing', true);
-
                if (storageConfig.id) {
                        $tr.data('id', storageConfig.id);
                }
@@ -777,15 +788,16 @@ MountConfigListView.prototype = _.extend({
                });
                if (storageConfig.authMechanism) {
                        selectAuthMechanism.val(storageConfig.authMechanism);
+               } else {
+                       storageConfig.authMechanism = selectAuthMechanism.val();
                }
                $tr.find('td.authentication').append(selectAuthMechanism);
 
                var $td = $tr.find('td.configuration');
                $.each(backend.configuration, _.partial(this.writeParameterInput, $td));
 
-               this.trigger('selectBackend', $tr, backend.identifier);
-
-               selectAuthMechanism.trigger('change'); // generate configuration parameters for auth mechanism
+               this.trigger('selectBackend', $tr, backend.identifier, onCompletion);
+               this.configureAuthMechanism($tr, storageConfig.authMechanism, onCompletion);
 
                if (storageConfig.backendOptions) {
                        $td.children().each(function() {
@@ -825,7 +837,6 @@ MountConfigListView.prototype = _.extend({
                        }));
                }
 
-               $tr.removeData('constructing');
                return $tr;
        },
 
@@ -842,11 +853,12 @@ MountConfigListView.prototype = _.extend({
                                url: OC.generateUrl('apps/files_external/userglobalstorages'),
                                contentType: 'application/json',
                                success: function(result) {
+                                       var onCompletion = jQuery.Deferred();
                                        $.each(result, function(i, storageParams) {
                                                storageParams.mountPoint = storageParams.mountPoint.substr(1); // trim leading slash
                                                var storageConfig = new self._storageConfigClass();
                                                _.extend(storageConfig, storageParams);
-                                               var $tr = self.newStorage(storageConfig);
+                                               var $tr = self.newStorage(storageConfig, onCompletion);
 
                                                // userglobal storages must be at the top of the list
                                                $tr.detach();
@@ -862,6 +874,7 @@ MountConfigListView.prototype = _.extend({
                                                $tr.find('.mountOptionsToggle, .remove').empty();
                                                $tr.find('input, select, button').attr('disabled', 'disabled');
                                        });
+                                       onCompletion.resolve();
                                }
                        });
                }
@@ -873,13 +886,15 @@ MountConfigListView.prototype = _.extend({
                        url: OC.generateUrl(url),
                        contentType: 'application/json',
                        success: function(result) {
+                               var onCompletion = jQuery.Deferred();
                                $.each(result, function(i, storageParams) {
                                        storageParams.mountPoint = storageParams.mountPoint.substr(1); // trim leading slash
                                        var storageConfig = new self._storageConfigClass();
                                        _.extend(storageConfig, storageParams);
-                                       var $tr = self.newStorage(storageConfig);
+                                       var $tr = self.newStorage(storageConfig, onCompletion);
                                        self.recheckStorageConfig($tr);
                                });
+                               onCompletion.resolve();
                        }
                });
        },