From 97ebba6f2bbe7bf0c203a84fa0432e9cbdae85cf Mon Sep 17 00:00:00 2001 From: llj Date: Mon, 13 Jul 2015 18:14:01 +0800 Subject: [PATCH] [file lock] 'dir view' & 'file view': improvement & bugfix --- media/css/seahub.css | 4 - seahub/templates/js/templates.html | 33 +++---- seahub/templates/view_file_base.html | 35 ++----- static/css/backbone.css | 8 ++ static/scripts/app/views/dirent.js | 140 ++++++++++++++------------- static/scripts/common.js | 2 +- 6 files changed, 105 insertions(+), 117 deletions(-) diff --git a/media/css/seahub.css b/media/css/seahub.css index 773827f690..20d14c1bf5 100644 --- a/media/css/seahub.css +++ b/media/css/seahub.css @@ -3655,10 +3655,6 @@ textarea:-moz-placeholder {/* for FF */ } #user-profile .info-detail { padding-left: 6px; - -/* file lock */ -#view-hd .file-locked-icon { - vertical-align: middle; } /* shared file view */ #shared-file-view-logo { diff --git a/seahub/templates/js/templates.html b/seahub/templates/js/templates.html index 8e0aac058e..cefca90e5b 100644 --- a/seahub/templates/js/templates.html +++ b/seahub/templates/js/templates.html @@ -258,6 +258,10 @@ <% } else { %> <% } %> + + <% if (is_pro && dirent.is_locked) { %> + {% trans + <% } %> @@ -266,13 +270,6 @@ <% } else { %> <%- dirent.obj_name %> <% } %> - <% if (is_pro) { %> - <% if (dirent.is_locked) { %> - locked - <% } else { %> - locked - <% } %> - <% } %> @@ -283,32 +280,28 @@ <% } %> <% if (dirent.perm == 'rw') { %> - <% if (!dirent.is_locked) { %> + <% if (enable_some_op) { %> - <% } else { %> - <% if (dirent.locked_by_me) { %> - - <% } %> <% } %> <% } %> <% if (dirent.perm == 'rw') { %> {% trans <% } %> diff --git a/seahub/templates/view_file_base.html b/seahub/templates/view_file_base.html index 5ed1acd47e..8bf36f48c5 100644 --- a/seahub/templates/view_file_base.html +++ b/seahub/templates/view_file_base.html @@ -13,7 +13,7 @@

{{ filename }} {% if is_pro %} - {% trans + {% trans {% endif %}

@@ -151,44 +151,27 @@ $('#file-star').click(function() { }); {% if can_lock_unlock_file %} -// lock file -$('#lock-file').click(function() { +// lock/unlock file +$('#lock-file, #unlock-file').click(function() { + var op = $(this).attr('id') == 'lock-file' ? 'lock' : 'unlock'; $.ajax({ url: '{% url 'FileView' repo.id %}', type: 'PUT', dataType: 'json', cache: 'false', beforeSend: prepareCSRFToken, - data: {'operation': 'lock', 'p': '{{path}}'}, + data: {'operation': op, 'p': '{{path|escapejs}}'}, success: function() { - $('#lock-file').hide(); - $('#unlock-file').show(); - $('.file-locked-icon').show(); + $('#lock-file, #unlock-file, .file-locked-icon').toggleClass('hide'); }, - error: ajaxErrorHandler - }); -}); - -$('#unlock-file').click(function() { - $.ajax({ - url: '{% url 'FileView' repo.id %}', - type: 'PUT', - dataType: 'json', - cache: 'false', - beforeSend: prepareCSRFToken, - data: {'operation': 'unlock', 'p': '{{path}}'}, - success: function() { - $('#lock-file').show(); - $('#unlock-file').hide(); - $('.file-locked-icon').hide(); - }, - error: ajaxErrorHandler + error: function () { + feedback("{% trans "Failed" %}", 'error'); + } }); }); {% endif %} // set 'side toolbar' position - function setToolbarPos() { var toolbar = $('#side-toolbar'); diff --git a/static/css/backbone.css b/static/css/backbone.css index c8b6d84bee..ca8b6b0e1d 100644 --- a/static/css/backbone.css +++ b/static/css/backbone.css @@ -1,4 +1,12 @@ /* lib view */ +.repo-file-list .dirent-icon { + position:relative; +} +.repo-file-list .file-locked-icon { + position:absolute; + right:0; + bottom:0; +} .repo-file-list .dirent-name { width:220px; } diff --git a/static/scripts/app/views/dirent.js b/static/scripts/app/views/dirent.js index 765bec000b..8592352265 100644 --- a/static/scripts/app/views/dirent.js +++ b/static/scripts/app/views/dirent.js @@ -31,6 +31,18 @@ define([ render: function() { var dir = this.dir; var dirent_path = Common.pathJoin([dir.path, this.model.get('obj_name')]); + var is_pro = app.pageOptions.is_pro; + + // for 'file lock' + var enable_some_op = false; // op: 'del', 'rename', 'mv' + if (!is_pro) { + enable_some_op = true; + } else { + if (!this.model.get('is_locked') || this.model.get('locked_by_me')) { + enable_some_op = true; + } + } + this.$el.html(this.template({ dirent: this.model.attributes, dirent_path: dirent_path, @@ -39,20 +51,11 @@ define([ repo_id: dir.repo_id, is_repo_owner: dir.is_repo_owner, can_generate_shared_link: app.pageOptions.can_generate_shared_link, - is_pro: app.pageOptions.is_pro, + is_pro: is_pro, + enable_some_op: enable_some_op, repo_encrypted: dir.encrypted })); - - if (this.model.get('is_locked')) { - if (this.model.get('locked_by_me')) { - this.$('.file-lock').hide(); - } else { - this.$('.file-lock').hide(); - this.$('.file-unlock').hide(); - } - } else { - this.$('.file-unlock').hide(); - } + this.$('.file-locked-icon').attr('title', gettext("locked by {placeholder}").replace('{placeholder}', Common.HTMLescape(this.model.get('lock_owner')))); return this; }, @@ -69,59 +72,9 @@ define([ 'click .rename': 'rename', 'click .mv': 'mvcp', 'click .cp': 'mvcp', - 'click .file-lock': 'fileLock', - 'click .file-unlock': 'fileUnlock', - 'click .set-folder-permission': 'setFolderPerm' - }, - - fileLock: function() { - var dir = this.dirView.dir, - _this = this, - filepath = Common.pathJoin([dir.path, this.model.get('obj_name')]); - - $.ajax({ - url: Common.getUrl({name: 'file_lock', repo_id: dir.repo_id}), - type: 'PUT', - dataType: 'json', - data: {'operation': 'lock', 'p': filepath}, - cache: 'false', - beforeSend: Common.prepareCSRFToken, - success: function(data) { - _this.$('.file-locked-icon').show(); - _this.$('.file-unlock').show(); - _this.$('.file-lock').hide(); - }, - error: function (xhr) { - Common.ajaxErrorHandler(xhr); - } - }); - - return false; - }, - - fileUnlock: function() { - var dir = this.dirView.dir, - _this = this, - filepath = Common.pathJoin([dir.path, this.model.get('obj_name')]); - - $.ajax({ - url: Common.getUrl({name: 'file_lock', repo_id: dir.repo_id}), - type: 'PUT', - dataType: 'json', - data: {'operation': 'unlock', 'p': filepath}, - cache: 'false', - beforeSend: Common.prepareCSRFToken, - success: function(data) { - _this.$('.file-locked-icon').hide(); - _this.$('.file-lock').show(); - _this.$('.file-unlock').hide(); - }, - error: function (xhr) { - Common.ajaxErrorHandler(xhr); - } - }); - - return false; + 'click .set-folder-permission': 'setFolderPerm', + 'click .lock-file': 'lockFile', + 'click .unlock-file': 'unlockFile' }, highlight: function() { @@ -516,8 +469,63 @@ define([ }; new FolderPermView(options); return false; - } + }, + lockOrUnlockFile: function(params) { + var dir = this.dir, + filepath = Common.pathJoin([dir.path, this.model.get('obj_name')]), + callback = params.after_success; + + $.ajax({ + url: Common.getUrl({name: 'lock_or_unlock_file', repo_id: dir.repo_id}), + type: 'PUT', + dataType: 'json', + data: { + 'operation': params.op, + 'p': filepath + }, + cache: false, + beforeSend: Common.prepareCSRFToken, + success: function() { + callback(); + }, + error: function (xhr) { + Common.ajaxErrorHandler(xhr); + } + }); + }, + + lockFile: function() { + var _this = this; + this.lockOrUnlockFile({ + 'op': 'lock', + 'after_success': function() { + _this.model.set({ + 'is_locked': true, + 'locked_by_me': true, + 'lock_owner': app.pageOptions.username + }); + app.globalState.noFileOpPopup = true; + _this.$el.removeClass('hl'); + } + }); + return false; + }, + + unlockFile: function() { + var _this = this; + this.lockOrUnlockFile({ + 'op': 'unlock', + 'after_success': function() { + _this.model.set({ + 'is_locked': false + }); + app.globalState.noFileOpPopup = true; + _this.$el.removeClass('hl'); + } + }); + return false; + } }); return DirentView; diff --git a/static/scripts/common.js b/static/scripts/common.js index e5935c863f..fed5c695b4 100644 --- a/static/scripts/common.js +++ b/static/scripts/common.js @@ -67,13 +67,13 @@ define([ case 'unstar_file': return siteRoot + 'ajax/repo/' + options.repo_id + '/file/unstar/'; case 'del_dir': return siteRoot + 'ajax/repo/' + options.repo_id + '/dir/delete/'; case 'del_file': return siteRoot + 'ajax/repo/' + options.repo_id + '/file/delete/'; - case 'file_lock': return siteRoot + 'api2/repos/' + options.repo_id + '/file/'; case 'rename_dir': return siteRoot + 'ajax/repo/' + options.repo_id + '/dir/rename/'; case 'rename_file': return siteRoot + 'ajax/repo/' + options.repo_id + '/file/rename/'; case 'mv_dir': return siteRoot + 'ajax/repo/' + options.repo_id + '/dir/mv/'; case 'cp_dir': return siteRoot + 'ajax/repo/' + options.repo_id + '/dir/cp/'; case 'mv_file': return siteRoot + 'ajax/repo/' + options.repo_id + '/file/mv/'; case 'cp_file': return siteRoot + 'ajax/repo/' + options.repo_id + '/file/cp/'; + case 'lock_or_unlock_file': return siteRoot + 'api2/repos/' + options.repo_id + '/file/'; case 'new_dir': return siteRoot + 'ajax/repo/' + options.repo_id + '/dir/new/'; case 'new_file': return siteRoot + 'ajax/repo/' + options.repo_id + '/file/new/'; case 'del_dirents': return siteRoot + 'ajax/repo/' + options.repo_id + '/dirents/delete/';