From: Robin Appelman Date: Tue, 17 Jun 2014 11:28:27 +0000 (+0200) Subject: organize js a bit better X-Git-Tag: v7.0.0alpha2~16^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2219087df6b3226e257547e5a02e120486ab4579;p=nextcloud-server.git organize js a bit better --- diff --git a/apps/files_sharing/js/external.js b/apps/files_sharing/js/external.js index bc02ecfaf97..5c476b2d43d 100644 --- a/apps/files_sharing/js/external.js +++ b/apps/files_sharing/js/external.js @@ -1,4 +1,13 @@ -$(document).ready(function () { +/* + * Copyright (c) 2014 Robin Appelman + * + * This file is licensed under the Affero General Public License version 3 + * or later. + * + * See the COPYING-README file. + * + */ +(function () { var getParameterByName = function (query, name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new RegExp("[\\#&]" + name + "=([^&#]*)"), @@ -8,10 +17,10 @@ $(document).ready(function () { var addExternalShare = function (remote, token, owner, name, password) { return $.post(OC.generateUrl('apps/files_sharing/external'), { - remote : remote, - token : token, - owner : owner, - name : name, + remote: remote, + token: token, + owner: owner, + name: name, password: password }); }; @@ -31,15 +40,15 @@ $(document).ready(function () { } }; if (!passwordProtected) { - OC.dialogs.confirm('Add ' + name + ' from ' + owner + '@' + remoteClean, 'Add Share', callback, true); + OC.dialogs.confirm(t('files_sharing', 'Add {name} from {owner}@{remote}', {name: name, owner: owner, remote: remoteClean}) + , 'Add Share', callback, true); } else { - OC.dialogs.prompt('Add ' + name + ' from ' + owner + '@' + remoteClean, 'Add Share', callback, true, 'Password', true); + OC.dialogs.prompt(t('files_sharing', 'Add {name} from {owner}@{remote}', {name: name, owner: owner, remote: remoteClean}) + , 'Add Share', callback, true, 'Password', true); } }; - if (OCA.Files) {// only run in the files app - var hash = location.hash; - location.hash = ''; + OCA.Sharing.showAddExternalDialog = function (hash) { var remote = getParameterByName(hash, 'remote'); var owner = getParameterByName(hash, 'owner'); var name = getParameterByName(hash, 'name'); @@ -49,5 +58,14 @@ $(document).ready(function () { if (remote && token && owner && name) { showAddExternalDialog(remote, token, owner, name, passwordProtected); } + }; +})(); + +$(document).ready(function () { + // FIXME: HACK: do not init when running unit tests, need a better way + if (!window.TESTING && OCA.Files) {// only run in the files app + var hash = location.hash; + location.hash = ''; + OCA.Sharing.showAddExternalDialog(hash); } }); diff --git a/apps/files_sharing/js/public.js b/apps/files_sharing/js/public.js index 359087c0f94..80631908d24 100644 --- a/apps/files_sharing/js/public.js +++ b/apps/files_sharing/js/public.js @@ -148,6 +148,24 @@ OCA.Sharing.PublicApp = { $(this).select(); }); + $('.save-form').submit(function (event) { + event.preventDefault(); + + var remote = $(this).find('input[type="text"]').val(); + var token = $('#sharingToken').val(); + var owner = $('#save').data('owner'); + var name = $('#save').data('name'); + var isProtected = $('#save').data('protected') ? 1 : 0; + OCA.Sharing.PublicApp._saveToOwnCloud(remote, token, owner, name, isProtected); + }); + + $('#save > button').click(function () { + $(this).hide(); + $('.header-right').addClass('active'); + $('.save-form').css('display', 'inline'); + $('#remote_address').focus(); + }); + // legacy window.FileList = this.fileList; }, @@ -163,6 +181,29 @@ OCA.Sharing.PublicApp = { _onUrlChanged: function (params) { this.fileList.changeDirectory(params.path || params.dir, false, true); + }, + + _saveToOwnCloud: function(remote, token, owner, name, isProtected) { + var location = window.location.protocol + '//' + window.location.host + OC.webroot; + + var url = remote + '/index.php/apps/files#' + 'remote=' + encodeURIComponent(location) // our location is the remote for the other server + + "&token=" + encodeURIComponent(token) + "&owner=" + encodeURIComponent(owner) + "&name=" + encodeURIComponent(name) + "&protected=" + isProtected; + + + if (remote.indexOf('://') > 0) { + OC.redirect(url); + } else { + // if no protocol is specified, we automatically detect it by testing https and http + // this check needs to happen on the server due to the Content Security Policy directive + $.get(OC.generateUrl('apps/files_sharing/testremote'), {remote: remote}).then(function (protocol) { + if (protocol !== 'http' && protocol !== 'https') { + OC.dialogs.alert(t('files_sharing', 'No ownCloud installation found at {remote}', {remote: remote}), + t('files_sharing', 'Invalid ownCloud url')); + } else { + OC.redirect(protocol + '://' + url); + } + }); + } } }; @@ -186,42 +227,5 @@ $(document).ready(function () { }); }; } - - $('.save-form').submit(function (event) { - event.preventDefault(); - - var remote = $(this).find('input[type="text"]').val(); - var token = $('#sharingToken').val(); - var location = window.location.protocol + '//' + window.location.host + OC.webroot; - var owner = $('#save').data('owner'); - var name = $('#save').data('name'); - var isProtected = $('#save').data('protected') ? 1 : 0; - - var url = remote + '/index.php/apps/files#' + 'remote=' + encodeURIComponent(location) // our location is the remote for the other server - + "&token=" + encodeURIComponent(token) + "&owner=" + encodeURIComponent(owner) + "&name=" + encodeURIComponent(name) + "&protected=" + isProtected; - - - if (remote.indexOf('://') > 0) { - window.location = url; - } else { - // if no protocol is specified, we automatically detect it by testing https and http - // this check needs to happen on the server due to the Content Security Policy directive - $.get(OC.generateUrl('apps/files_sharing/testremote'), {remote: remote}).then(function (protocol) { - if (protocol !== 'http' && protocol !== 'https') { - OC.dialogs.alert(t('files_sharing', 'No ownCloud installation found at {remote}', {remote: remote}), - t('files_sharing', 'Invalid ownCloud url')); - } else { - window.location = protocol + '://' + url; - } - }); - } - }); - - $('#save > button').click(function () { - $(this).hide(); - $('.header-right').addClass('active'); - $('.save-form').css('display', 'inline'); - $('#remote_address').focus(); - }); });