diff --git a/media/scripts/app/views/repo.js b/media/scripts/app/views/repo.js index bcd76b6409..72118fff54 100644 --- a/media/scripts/app/views/repo.js +++ b/media/scripts/app/views/repo.js @@ -12,18 +12,16 @@ define([ tagName: 'tr', template: _.template(reposTemplate), + repoDelConfirmTemplate: _.template($('#repo-del-confirm-template').html()), events: { - 'mouseenter': 'showAction', - 'mouseleave': 'hideAction', - 'click .repo-delete-btn': 'delete', + 'mouseenter': 'highlight', + 'mouseleave': 'rmHighlight', + 'click .repo-delete-btn': 'del', 'click .repo-share-btn': 'share' }, initialize: function() { - console.log('init RepoView'); - - this.listenTo(this.model, 'destroy', this.remove); }, render: function() { @@ -31,67 +29,62 @@ define([ return this; }, - showAction: function() { - this.$el.addClass('hl'); - this.$el.find('.op-icon').removeClass('vh'); - }, - - hideAction: function() { - this.$el.removeClass('hl'); - this.$el.find('.op-icon').addClass('vh'); - }, - - showDelConfirm: function($target, msg, yesCallback) { - // TODO: need to refactor, copied from repo_del_js.html - - var op = $target, - cont = op.parent().css({'position': 'relative'}), - cfm; - - // only show 1 popup each time. - $('.repo-del-cfm', op.parents('table')).addClass('hide'); - - if (cont.find('.repo-del-cfm').length == 1) { - cfm = cont.find('.repo-del-cfm'); - } else { - cfm = $('#repo-del-cfm-popup').clone().removeAttr('id'); - cont.append(cfm); - cfm.css({'left': op.position().left, 'top': op.position().top + op.height() + 2, 'width':202}); + // disable 'hover' when 'repo-del-confirm' popup is shown + highlight: function() { + if ($('#my-own-repos .repo-del-confirm').length == 0) { + this.$el.addClass('hl').find('.op-icon').removeClass('vh'); } + }, - var con = $('.con', cfm); - con.html(msg.replace('{placeholder}', '' + this.model.get("name") + '')); - cfm.removeClass('hide'); - $('.no',cfm).click(function() { - cfm.addClass('hide'); + rmHighlight: function() { + if ($('#my-own-repos .repo-del-confirm').length == 0) { + this.$el.removeClass('hl').find('.op-icon').addClass('vh'); + } + }, + + del: function() { + var del_icon = this.$('.repo-delete-btn'); + var op_container = this.$('.op-container').css({'position': 'relative'}); + + var confirm_msg = gettext("Really want to delete {lib_name}?") + .replace('{lib_name}', '' + Common.HTMLescape(this.model.get('name')) + ''); + var confirm_popup = $(this.repoDelConfirmTemplate({ + content: confirm_msg + })) + .appendTo(op_container) + .css({ + 'left': del_icon.position().left, + 'top': del_icon.position().top + del_icon.height() + 2, + 'width': 180 }); - $('.yes', cfm).click(yesCallback); - }, - - delete: function(e) { - e.preventDefault(); - - var that = this; - var yesCallback = function() { - Common.feedback(gettext('Loading...'), 'info', Common.INFO_TIMEOUT); // TODO: what if there is still response after 10 secs ? - that.model.destroy({ - wait: true, - success: function(model, rep) { - Common.feedback(gettext('Delete succeeded'), 'success', Common.SUCCESS_TIMOUT); + var _this = this; + $('.no', confirm_popup).click(function() { + confirm_popup.addClass('hide').remove(); // `addClass('hide')`: to rm cursor + _this.rmHighlight(); + }); + $('.yes', confirm_popup).click(function() { + $.ajax({ + url: Common.getUrl({'name':'repo_del', 'repo_id': _this.model.get('id')}), + dataType: 'json', + success: function(data) { + _this.remove(); + Common.feedback(gettext("Delete succeeded."), 'success'); }, - error: function() { - Common.feedback(gettext('Error'), 'error', Common.ERROR_TIMEOUT); + error: function(xhr) { + confirm_popup.addClass('hide').remove(); + _this.rmHighlight(); + + var err; + if (xhr.responseText) { + err = $.parseJSON(xhr.responseText).error; + } else { + err = gettext("Failed. Please check the network."); + } + Common.feedback(err, 'error'); } }); - } - - this.showDelConfirm( - $(e.target), - gettext('Really want to delete {placeholder} ?') - .replace(/\{placeholder\}/g, '' + this.model.get('name') + ''), - yesCallback - ); + }); }, share: function() { diff --git a/media/scripts/app/views/sub-lib.js b/media/scripts/app/views/sub-lib.js index f0d8c8ff7b..614ceb6978 100644 --- a/media/scripts/app/views/sub-lib.js +++ b/media/scripts/app/views/sub-lib.js @@ -12,16 +12,16 @@ define([ tagName: 'tr', template: _.template(reposTemplate), + repoDelConfirmTemplate: _.template($('#repo-del-confirm-template').html()), events: { - 'mouseenter': 'showAction', - 'mouseleave': 'hideAction', - 'click .repo-delete-btn': 'delete', + 'mouseenter': 'highlight', + 'mouseleave': 'rmHighlight', + 'click .repo-delete-btn': 'del', 'click .repo-share-btn': 'share' }, initialize: function() { - this.listenTo(this.model, 'destroy', this.remove); }, render: function() { @@ -29,67 +29,62 @@ define([ return this; }, - showAction: function() { - this.$el.addClass('hl'); - this.$el.find('.op-icon').removeClass('vh'); - }, - - hideAction: function() { - this.$el.removeClass('hl'); - this.$el.find('.op-icon').addClass('vh'); - }, - - showDelConfirm: function($target, msg, yesCallback) { - // TODO: need to refactor, copied from repo_del_js.html - - var op = $target, - cont = op.parent().css({'position': 'relative'}), - cfm; - - // only show 1 popup each time. - $('.repo-del-cfm', op.parents('table')).addClass('hide'); - - if (cont.find('.repo-del-cfm').length == 1) { - cfm = cont.find('.repo-del-cfm'); - } else { - cfm = $('#repo-del-cfm-popup').clone().removeAttr('id'); - cont.append(cfm); - cfm.css({'left': op.position().left, 'top': op.position().top + op.height() + 2, 'width':202}); + // disable 'hover' when 'repo-del-confirm' popup is shown + highlight: function() { + if ($('#my-sub-repos .repo-del-confirm').length == 0) { + this.$el.addClass('hl').find('.op-icon').removeClass('vh'); } + }, - var con = $('.con', cfm); - con.html(msg.replace('{placeholder}', '' + this.model.get("name") + '')); - cfm.removeClass('hide'); - $('.no',cfm).click(function() { - cfm.addClass('hide'); + rmHighlight: function() { + if ($('#my-sub-repos .repo-del-confirm').length == 0) { + this.$el.removeClass('hl').find('.op-icon').addClass('vh'); + } + }, + + del: function() { + var del_icon = this.$('.repo-delete-btn'); + var op_container = this.$('.op-container').css({'position': 'relative'}); + + var confirm_msg = gettext("Really want to delete {lib_name}?") + .replace('{lib_name}', '' + Common.HTMLescape(this.model.get('name')) + ''); + var confirm_popup = $(this.repoDelConfirmTemplate({ + content: confirm_msg + })) + .appendTo(op_container) + .css({ + 'left': del_icon.position().left, + 'top': del_icon.position().top + del_icon.height() + 2, + 'width': 180 }); - $('.yes', cfm).click(yesCallback); - }, + var _this = this; + $('.no', confirm_popup).click(function() { + confirm_popup.addClass('hide').remove(); // `addClass('hide')`: to rm cursor + _this.rmHighlight(); + }); + $('.yes', confirm_popup).click(function() { + $.ajax({ + url: Common.getUrl({'name':'repo_del', 'repo_id': _this.model.get('id')}), + dataType: 'json', + success: function(data) { + _this.remove(); + Common.feedback(gettext("Delete succeeded."), 'success'); + }, + error: function(xhr) { + confirm_popup.addClass('hide').remove(); + _this.rmHighlight(); - delete: function(e) { - e.preventDefault(); - - var that = this; - var yesCallback = function() { - Common.feedback(gettext('Loading...'), 'info', Common.INFO_TIMEOUT); // TODO: what if there is still response after 10 secs ? - that.model.destroy({ - wait: true, - success: function(model, rep) { - Common.feedback(gettext('Delete succeeded'), 'success', Common.SUCCESS_TIMOUT); - }, - error: function() { - Common.feedback(gettext('Error'), 'error', Common.ERROR_TIMEOUT); + var err; + if (xhr.responseText) { + err = $.parseJSON(xhr.responseText).error; + } else { + err = gettext("Failed. Please check the network."); + } + Common.feedback(err, 'error'); } - }); - } - - this.showDelConfirm( - $(e.target), - gettext('Really want to delete {placeholder} ?') - .replace(/\{placeholder\}/g, '' + this.model.get('name') + ''), - yesCallback - ); + }); + }); }, share: function() { diff --git a/media/scripts/common.js b/media/scripts/common.js index 5fdd4fc1f0..24b5f2f064 100644 --- a/media/scripts/common.js +++ b/media/scripts/common.js @@ -75,7 +75,10 @@ define([ case 'cp_dirents': return siteRoot + 'ajax/repo/' + options.repo_id + '/dirents/copy/'; case 'get_file_op_url': return siteRoot + 'ajax/repo/' + options.repo_id + '/file_op_url/'; case 'get_dirents': return siteRoot + 'ajax/repo/' + options.repo_id + '/dirents/'; + case 'repo_del': return siteRoot + 'ajax/repo/' + options.repo_id + '/remove/'; + case 'thumbnail_create': return siteRoot + 'thumbnail/' + options.repo_id + '/create/'; + case 'unenc_rw_repos': return siteRoot + 'ajax/unenc-rw-repos/'; case 'get_cp_progress': return siteRoot + 'ajax/cp_progress/'; case 'cancel_cp': return siteRoot + 'ajax/cancel_cp/'; diff --git a/seahub/templates/js/lib-op-popups.html b/seahub/templates/js/lib-op-popups.html index 0d1eaedc38..3e3f370185 100644 --- a/seahub/templates/js/lib-op-popups.html +++ b/seahub/templates/js/lib-op-popups.html @@ -122,3 +122,11 @@ + + diff --git a/seahub/templates/js/repo.html b/seahub/templates/js/repo.html index 7a3f5f3b5a..55a161f581 100644 --- a/seahub/templates/js/repo.html +++ b/seahub/templates/js/repo.html @@ -10,7 +10,7 @@