From d56ef4f0b64e3bd27a92fbd8b2214dc3cbea7b3f Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Thu, 5 Dec 2013 14:04:36 +0100 Subject: [PATCH] Make it possible to rename documents by clicking on title --- ajax/sessionController.php | 18 ++++++++++++++++ appinfo/routes.php | 4 ++++ css/style.css | 7 ++++++- js/documents.js | 43 ++++++++++++++++++++++++++++++++++++-- lib/file.php | 11 ++++++++++ 5 files changed, 80 insertions(+), 3 deletions(-) diff --git a/ajax/sessionController.php b/ajax/sessionController.php index 008f1a97..df2ba10d 100644 --- a/ajax/sessionController.php +++ b/ajax/sessionController.php @@ -22,6 +22,24 @@ class SessionController extends Controller{ self::join($uid, $file); } + public static function renameDocument($args){ + $fileId = intval(@$args['file_id']); + $name = @$_POST['name']; + $file = new File($fileId); + $l = new \OC_L10n('documents'); + + if (isset($name) && $file->getPermissions() & \OCP\PERMISSION_UPDATE) { + if ($file->renameTo($name)) { + // TODO: propagate to other clients + \OCP\JSON::success(); + return; + } + } + \OCP\JSON::error(array( + 'message' => $l->t('You don\'t have permission to rename this document') + )); + } + public static function joinAsUser($args){ $uid = self::preDispatch(); $fileId = intval(@$args['file_id']); diff --git a/appinfo/routes.php b/appinfo/routes.php index 6ea9b07f..8c4c2d53 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -73,6 +73,10 @@ $this->create('documents_session_joinasguest', 'ajax/session/joinasguest/{token} ->post() ->action('\OCA\Documents\SessionController', 'joinAsGuest') ; +$this->create('documents_session_renamedocument', 'ajax/session/renamedocument/{file_id}') + ->post() + ->action('\OCA\Documents\SessionController', 'renameDocument') +; $this->create('documents_session_save', 'ajax/session/save') ->post() diff --git a/css/style.css b/css/style.css index 5146f731..9f376c5d 100755 --- a/css/style.css +++ b/css/style.css @@ -280,4 +280,9 @@ margin-top:-1px; .dojoTabular {border-collapse: collapse; border-spacing: 0; border: 1px solid #ccc; margin: 0 1.5em;} .dojoTabular th {text-align: center; font-weight: bold;} .dojoTabular thead,.dojoTabular tfoot {background-color: #efefef; border: 1px solid #ccc; border-width: 1px 0;} -.dojoTabular th,.dojoTabular td {padding: 0.25em 0.5em;} \ No newline at end of file +.dojoTabular th,.dojoTabular td {padding: 0.25em 0.5em;} + +/* raise notification z-index above the documents app */ +#odf-toolbar + #notification-container { + z-index: 501; +} diff --git a/js/documents.js b/js/documents.js index decb3ff4..be054eff 100644 --- a/js/documents.js +++ b/js/documents.js @@ -9,6 +9,7 @@ var documentsMain = { memberId : false, esId : false, ready :false, + fileName: null, UI : { /* Overlay HTML */ @@ -186,8 +187,10 @@ var documentsMain = { // fade out file list and show WebODF canvas $('#content').fadeOut('fast').promise().done(function() { + documentsMain.fileId = response.file_id; + documentsMain.fileName = documentsMain.getNameByFileid(response.file_id); documentsMain.UI.showEditor( - documentsMain.getNameByFileid(response.file_id), + documentsMain.fileName, response.permissions & OC.PERMISSION_SHARE && !documentsMain.isGuest ); var serverFactory = new ServerFactory(); @@ -281,7 +284,42 @@ var documentsMain = { }); $.post(OC.Router.generate('documents_user_invite'), {users: users}); }, - + + renameDocument: function(name) { + var url = OC.Router.generate('documents_session_renamedocument') + '/' + documentsMain.fileId; + $.post( + url, + { name : name }, + function(result) { + if (result && result.status === 'error') { + if (result.message){ + OC.Notification.show(result.message); + setTimeout(function() { + OC.Notification.hide(); + }, 10000); + } + return; + } + documentsMain.fileName = name; + $('title').text(documentsMain.UI.mainTitle + '| ' + name); + $('#document-title>div').text(name); + } + ); + }, + + onRenamePrompt: function() { + var name = documentsMain.fileName; + var lastPos = name.lastIndexOf('.'); + var extension = name.substr(lastPos + 1); + name = name.substr(0, lastPos); + // FIXME: don't use an ugly prompt but an inline field + // FIXME: check for invalid characters + var newName = prompt(t('document', 'Please enter the document name'), name); + if (newName !== null && newName !== '') { + documentsMain.renameDocument(newName + '.' + extension); + } + }, + onClose: function() { "use strict"; @@ -433,6 +471,7 @@ $(document).ready(function() { } }); + $(document.body).on('click', '#document-title', documentsMain.onRenamePrompt); $(document.body).on('click', '#odf-close', documentsMain.onClose); $(document.body).on('click', '#odf-invite', documentsMain.onInvite); $(document.body).on('click', '#odf-join', function(event){ diff --git a/lib/file.php b/lib/file.php index 84125bda..af6fc287 100644 --- a/lib/file.php +++ b/lib/file.php @@ -151,6 +151,17 @@ class File { return $permissions; } + /** + * Rename this file to the given name + * @param string $newName name to give (without path) + * @return boolean true if rename succeeded, false otherwise + */ + public function renameTo($newName) { + list($owner, $path) = $this->getOwnerViewAndPath(); + $newPath = dirname($path) . '/' . $newName; + return \OC\Files\Filesystem::rename($path, $newPath); + } + /** * * @return string owner of the current file item