From f17cdcda9fdf977b6022b7ffc97334b0ae37893e Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 17 Sep 2019 11:33:09 +0200 Subject: [PATCH] Ability to remove window handlers added from the drive --- www/common/drive-ui.js | 312 +++++++++++++++++++++-------------------- www/team/inner.js | 3 + 2 files changed, 165 insertions(+), 150 deletions(-) diff --git a/www/common/drive-ui.js b/www/common/drive-ui.js index 1d58959ae..929634458 100644 --- a/www/common/drive-ui.js +++ b/www/common/drive-ui.js @@ -732,7 +732,8 @@ define([ }; $content.mousemove(sel.move); }); - $(window).on('mouseup', function (e) { + + var onWindowMouseUp = function (e) { if (!sel.down) { return; } if (e.which !== 1) { return; } sel.down = false; @@ -745,10 +746,146 @@ define([ selectElement($(element)); }); e.stopPropagation(); - }); + }; + + var getSelectedPaths = function ($element) { + var paths = []; + if (!$element || $element.length === 0) { return paths; } + if (findSelectedElements().length > 1) { + var $selected = findSelectedElements(); + $selected.each(function (idx, elmt) { + var ePath = $(elmt).data('path'); + if (ePath) { + paths.push({ + path: ePath, + element: $(elmt) + }); + } + }); + } + + if (!paths.length) { + var path = $element.data('path'); + if (!path) { return false; } + paths.push({ + path: path, + element: $element + }); + } + return paths; + }; + + var removeInput = function (cancel) { + if (!cancel && $('.cp-app-drive-element-row > input').length === 1) { + var $input = $('.cp-app-drive-element-row > input'); + manager.rename($input.data('path'), $input.val(), APP.refresh); + } + $('.cp-app-drive-element-row > input').remove(); + $('.cp-app-drive-element-row > span:hidden').removeAttr('style'); + }; + + var getFileNameExtension = function (name) { + var matched = /\.[^\. ]+$/.exec(name); + if (matched && matched.length) { return matched[matched.length -1]; } + return ''; + }; + + // Replace a file/folder name by an input to change its value + var displayRenameInput = function ($element, path) { + // NOTE: setTimeout(f, 0) otherwise the "rename" button in the toolbar is not working + window.setTimeout(function () { + if (!APP.editable) { return; } + if (!path || path.length < 2) { + logError("Renaming a top level element (root, trash or filesData) is forbidden."); + return; + } + removeInput(); + var $name = $element.find('.cp-app-drive-element-name'); + if (!$name.length) { + $name = $element.find('> .cp-app-drive-element'); + } + $name.hide(); + var isFolder = $element.is(".cp-app-drive-element-folder:not(.cp-app-drive-element-sharedf)"); + var el = manager.find(path); + var name = manager.isFile(el) ? manager.getTitle(el) : path[path.length - 1]; + if (manager.isSharedFolder(el)) { + name = manager.getSharedFolderData(el).title; + } + var $input = $('', { + placeholder: name, + value: name + }).data('path', path); + + + // Stop propagation on keydown to avoid issues with arrow keys + $input.on('keydown', function (e) { e.stopPropagation(); }); + + $input.on('keyup', function (e) { + e.stopPropagation(); + if (e.which === 13) { + removeInput(true); + var newName = $input.val(); + if (JSON.stringify(path) === JSON.stringify(currentPath)) { + manager.rename(path, $input.val(), function () { + if (isFolder) { + LS.renameFoldersOpened(path, newName); + path[path.length - 1] = newName; + } + APP.displayDirectory(path); + }); + } + else { + manager.rename(path, $input.val(), function () { + if (isFolder) { + LS.renameFoldersOpened(path, newName); + unselectElement($element); + $element.data("path", $element.data("path").slice(0, -1).concat(newName)); + selectElement($element); + } + APP.refresh(); + }); + } + return; + } + if (e.which === 27) { + removeInput(true); + } + }).on('keypress', function (e) { e.stopPropagation(); }); + //$element.parent().append($input); + $name.after($input); + $input.focus(); + + var extension = getFileNameExtension(name); + var input = $input[0]; + input.selectionStart = 0; + input.selectionEnd = name.length - extension.length; + + // We don't want to open the file/folder when clicking on the input + $input.on('click dblclick', function (e) { + e.stopPropagation(); + }); + // Remove the browser ability to drag text from the input to avoid + // triggering our drag/drop event handlers + $input.on('dragstart dragleave drag drop', function (e) { + e.preventDefault(); + e.stopPropagation(); + }); + // Make the parent element non-draggable when selecting text in the field + // since it would remove the input + $input.on('mousedown', function (e) { + e.stopPropagation(); + $input.parents('.cp-app-drive-element-row').attr("draggable", false); + }); + $input.on('mouseup', function (e) { + e.stopPropagation(); + $input.parents('.cp-app-drive-element-row').attr("draggable", true); + }); + },0); + }; + // Arrow keys to modify the selection - $(window).keydown(function (e) { + var onWindowKeydown = function (e) { var $searchBar = $tree.find('#cp-app-drive-tree-search-input'); if (document.activeElement && document.activeElement.nodeName === 'INPUT') { return; } if ($searchBar.is(':focus') && $searchBar.val()) { return; } @@ -784,6 +921,13 @@ define([ return; } + // F2: rename selected element + if (e.which === 113) { + var paths = getSelectedPaths(findSelectedElements().first()); + if (paths.length !== 1) { return; } + displayRenameInput(paths[0].element, paths[0].path); + } + // [Left, Up, Right, Down] if ([37, 38, 39, 40].indexOf(e.which) === -1) { return; } e.preventDefault(); @@ -843,17 +987,6 @@ define([ click($elements.get(Math.min(lastIndex+cols, length-1))); return; } - - }); - - - var removeInput = function (cancel) { - if (!cancel && $('.cp-app-drive-element-row > input').length === 1) { - var $input = $('.cp-app-drive-element-row > input'); - manager.rename($input.data('path'), $input.val(), APP.refresh); - } - $('.cp-app-drive-element-row > input').remove(); - $('.cp-app-drive-element-row > span:hidden').removeAttr('style'); }; var compareDays = function (date1, date2) { @@ -896,105 +1029,6 @@ define([ APP.displayDirectory(currentPath); }; - var getFileNameExtension = function (name) { - var matched = /\.[^\. ]+$/.exec(name); - if (matched && matched.length) { return matched[matched.length -1]; } - return ''; - }; - - // Replace a file/folder name by an input to change its value - var displayRenameInput = function ($element, path) { - // NOTE: setTimeout(f, 0) otherwise the "rename" button in the toolbar is not working - window.setTimeout(function () { - if (!APP.editable) { return; } - if (!path || path.length < 2) { - logError("Renaming a top level element (root, trash or filesData) is forbidden."); - return; - } - removeInput(); - var $name = $element.find('.cp-app-drive-element-name'); - if (!$name.length) { - $name = $element.find('> .cp-app-drive-element'); - } - $name.hide(); - var isFolder = $element.is(".cp-app-drive-element-folder:not(.cp-app-drive-element-sharedf)"); - var el = manager.find(path); - var name = manager.isFile(el) ? manager.getTitle(el) : path[path.length - 1]; - if (manager.isSharedFolder(el)) { - name = manager.getSharedFolderData(el).title; - } - var $input = $('', { - placeholder: name, - value: name - }).data('path', path); - - - // Stop propagation on keydown to avoid issues with arrow keys - $input.on('keydown', function (e) { e.stopPropagation(); }); - - $input.on('keyup', function (e) { - e.stopPropagation(); - if (e.which === 13) { - removeInput(true); - var newName = $input.val(); - if (JSON.stringify(path) === JSON.stringify(currentPath)) { - manager.rename(path, $input.val(), function () { - if (isFolder) { - LS.renameFoldersOpened(path, newName); - path[path.length - 1] = newName; - } - APP.displayDirectory(path); - }); - } - else { - manager.rename(path, $input.val(), function () { - if (isFolder) { - LS.renameFoldersOpened(path, newName); - unselectElement($element); - $element.data("path", $element.data("path").slice(0, -1).concat(newName)); - selectElement($element); - } - refresh(); - }); - } - return; - } - if (e.which === 27) { - removeInput(true); - } - }).on('keypress', function (e) { e.stopPropagation(); }); - //$element.parent().append($input); - $name.after($input); - $input.focus(); - - var extension = getFileNameExtension(name); - var input = $input[0]; - input.selectionStart = 0; - input.selectionEnd = name.length - extension.length; - - // We don't want to open the file/folder when clicking on the input - $input.on('click dblclick', function (e) { - e.stopPropagation(); - }); - // Remove the browser ability to drag text from the input to avoid - // triggering our drag/drop event handlers - $input.on('dragstart dragleave drag drop', function (e) { - e.preventDefault(); - e.stopPropagation(); - }); - // Make the parent element non-draggable when selecting text in the field - // since it would remove the input - $input.on('mousedown', function (e) { - e.stopPropagation(); - $input.parents('.cp-app-drive-element-row').attr("draggable", false); - }); - $input.on('mouseup', function (e) { - e.stopPropagation(); - $input.parents('.cp-app-drive-element-row').attr("draggable", true); - }); - },0); - }; - var pickFolderColor = function ($element, currentColor, cb) { var colors = ["", "#f23c38", "#ff0073", "#da0eba", "#9d00ac", "#6c19b3", "#4a42b1", "#3d8af0", "#30a0f1", "#1fb9d1", "#009686", "#45b354", "#84c750", "#c6e144", "#faf147", "#fbc423", "#fc9819", "#fd5227", "#775549", "#9c9c9c", "#607a89"]; @@ -1206,33 +1240,6 @@ define([ return filtered; }; - var getSelectedPaths = function ($element) { - var paths = []; - if (!$element || $element.length === 0) { return paths; } - if (findSelectedElements().length > 1) { - var $selected = findSelectedElements(); - $selected.each(function (idx, elmt) { - var ePath = $(elmt).data('path'); - if (ePath) { - paths.push({ - path: ePath, - element: $(elmt) - }); - } - }); - } - - if (!paths.length) { - var path = $element.data('path'); - if (!path) { return false; } - paths.push({ - path: path, - element: $element - }); - } - return paths; - }; - var updateContextButton = function () { if (manager.isPathIn(currentPath, [TRASH])) { $driveToolbar.find('cp-app-drive-toolbar-emptytrash').show(); @@ -3663,13 +3670,13 @@ define([ }; // Disable middle click in the context menu to avoid opening /drive/inner.html# in new tabs - $(window).click(function (e) { + var onWindowClick = function (e) { if (!e.target || !$(e.target).parents('.cp-dropdown-content').length) { return; } if (e.which !== 1) { e.stopPropagation(); return false; } - }); + }; var getProperties = APP.getProperties = function (el, cb) { if (!manager.isFile(el) && !manager.isSharedFolder(el)) { @@ -4111,13 +4118,15 @@ define([ APP.hideMenu(); }); - $(window).on("keydown", function (e) { - if (e.which === 113) { // if F2 key pressed - var paths = getSelectedPaths(findSelectedElements().first()); - if (paths.length !== 1) { return; } - displayRenameInput(paths[0].element, paths[0].path); - } - }); + $(window).on('mouseup', onWindowMouseUp); + $(window).on('keydown', onWindowKeydown); + $(window).on('click', onWindowClick); + + var removeWindowListeners = function () { + $(window).off('mouseup', onWindowMouseUp); + $(window).off('keydown', onWindowKeydown); + $(window).off('click', onWindowClick); + }; // Chrome considers the double-click means "select all" in the window $content.on('mousedown', function (e) { @@ -4342,7 +4351,10 @@ define([ } return { - refresh: refresh + refresh: refresh, + close: function () { + removeWindowListeners(); + } }; }; diff --git a/www/team/inner.js b/www/team/inner.js index d39bc2fc7..89c3ec0c3 100644 --- a/www/team/inner.js +++ b/www/team/inner.js @@ -89,7 +89,9 @@ define([ APP.module.execCommand('SUBSCRIBE', null, function () { sframeChan.query('Q_SET_TEAM', null, function (err) { if (err) { return void console.error(err); } + if (APP.drive && APP.drive.close) { APP.drive.close(); } APP.team = null; + APP.drive = null; APP.buildUI(common); }); }); @@ -206,6 +208,7 @@ define([ updateSharedFolders: updateSharedFolders, APP: driveAPP }); + APP.drive = drive; driveAPP.refresh = drive.refresh; }); };