Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | Persistence.WorkspaceSettingsTab = class extends UI.VBox { |
| 6 | constructor() { |
| 7 | super(); |
| 8 | this.registerRequiredCSS('persistence/workspaceSettingsTab.css'); |
| 9 | |
| 10 | const header = this.element.createChild('header'); |
| 11 | header.createChild('h3').createTextChild(Common.UIString('Workspace')); |
| 12 | |
| 13 | this.containerElement = this.element.createChild('div', 'settings-container-wrapper') |
| 14 | .createChild('div', 'settings-tab settings-content settings-container'); |
| 15 | |
| 16 | Persistence.isolatedFileSystemManager.addEventListener( |
| 17 | Persistence.IsolatedFileSystemManager.Events.FileSystemAdded, |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 18 | event => this._fileSystemAdded(/** @type {!Persistence.PlatformFileSystem} */ (event.data)), this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 19 | Persistence.isolatedFileSystemManager.addEventListener( |
| 20 | Persistence.IsolatedFileSystemManager.Events.FileSystemRemoved, |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 21 | event => this._fileSystemRemoved(/** @type {!Persistence.PlatformFileSystem} */ (event.data)), this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 22 | |
| 23 | const folderExcludePatternInput = this._createFolderExcludePatternInput(); |
| 24 | folderExcludePatternInput.classList.add('folder-exclude-pattern'); |
| 25 | this.containerElement.appendChild(folderExcludePatternInput); |
| 26 | |
| 27 | const div = this.containerElement.createChild('div', 'settings-info-message'); |
Andrey Lushnikov | b179546 | 2018-08-30 14:29:34 | [diff] [blame] | 28 | div.createTextChild(Common.UIString('Mappings are inferred automatically.')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 29 | |
| 30 | this._fileSystemsListContainer = this.containerElement.createChild('div', ''); |
| 31 | |
Joel Einbinder | 4b9d350 | 2019-03-13 01:43:38 | [diff] [blame] | 32 | const addButton = UI.createTextButton(ls`Add folder\u2026`, this._addFileSystemClicked.bind(this)); |
| 33 | this.containerElement.appendChild(addButton); |
| 34 | this.setDefaultFocusedElement(addButton); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 35 | |
| 36 | /** @type {!Map<string, !Element>} */ |
| 37 | this._elementByPath = new Map(); |
| 38 | |
| 39 | /** @type {!Map<string, !Persistence.EditFileSystemView>} */ |
| 40 | this._mappingViewByPath = new Map(); |
| 41 | |
| 42 | const fileSystems = Persistence.isolatedFileSystemManager.fileSystems(); |
| 43 | for (let i = 0; i < fileSystems.length; ++i) |
| 44 | this._addItem(fileSystems[i]); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return {!Element} |
| 49 | */ |
| 50 | _createFolderExcludePatternInput() { |
| 51 | const p = createElement('p'); |
| 52 | const labelElement = p.createChild('label'); |
| 53 | labelElement.textContent = Common.UIString('Folder exclude pattern'); |
| 54 | const inputElement = UI.createInput('', 'text'); |
| 55 | p.appendChild(inputElement); |
| 56 | inputElement.style.width = '270px'; |
| 57 | const folderExcludeSetting = Persistence.isolatedFileSystemManager.workspaceFolderExcludePatternSetting(); |
| 58 | const setValue = |
| 59 | UI.bindInput(inputElement, folderExcludeSetting.set.bind(folderExcludeSetting), regexValidator, false); |
| 60 | folderExcludeSetting.addChangeListener(() => setValue.call(null, folderExcludeSetting.get())); |
| 61 | setValue(folderExcludeSetting.get()); |
| 62 | return p; |
| 63 | |
| 64 | /** |
| 65 | * @param {string} value |
| 66 | * @return {boolean} |
| 67 | */ |
| 68 | function regexValidator(value) { |
| 69 | let regex; |
| 70 | try { |
| 71 | regex = new RegExp(value); |
| 72 | } catch (e) { |
| 73 | } |
| 74 | return !!regex; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 79 | * @param {!Persistence.PlatformFileSystem} fileSystem |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 80 | */ |
| 81 | _addItem(fileSystem) { |
Andrey Lushnikov | 3091403 | 2018-11-21 07:17:33 | [diff] [blame] | 82 | // Support managing only instances of IsolatedFileSystem. |
| 83 | if (!(fileSystem instanceof Persistence.IsolatedFileSystem)) |
| 84 | return; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 85 | const networkPersistenceProject = Persistence.networkPersistenceManager.project(); |
| 86 | if (networkPersistenceProject && |
| 87 | Persistence.isolatedFileSystemManager.fileSystem(networkPersistenceProject.fileSystemPath()) === fileSystem) |
| 88 | return; |
| 89 | const element = this._renderFileSystem(fileSystem); |
| 90 | this._elementByPath.set(fileSystem.path(), element); |
| 91 | |
| 92 | this._fileSystemsListContainer.appendChild(element); |
| 93 | |
| 94 | const mappingView = new Persistence.EditFileSystemView(fileSystem.path()); |
| 95 | this._mappingViewByPath.set(fileSystem.path(), mappingView); |
| 96 | mappingView.element.classList.add('file-system-mapping-view'); |
| 97 | mappingView.show(element); |
| 98 | } |
| 99 | |
| 100 | /** |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 101 | * @param {!Persistence.PlatformFileSystem} fileSystem |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 102 | * @return {!Element} |
| 103 | */ |
| 104 | _renderFileSystem(fileSystem) { |
| 105 | const fileSystemPath = fileSystem.path(); |
| 106 | const lastIndexOfSlash = fileSystemPath.lastIndexOf(Host.isWin() ? '\\' : '/'); |
| 107 | const folderName = fileSystemPath.substr(lastIndexOfSlash + 1); |
| 108 | |
| 109 | const element = createElementWithClass('div', 'file-system-container'); |
| 110 | const header = element.createChild('div', 'file-system-header'); |
| 111 | |
| 112 | header.createChild('div', 'file-system-name').textContent = folderName; |
| 113 | const path = header.createChild('div', 'file-system-path'); |
| 114 | path.textContent = fileSystemPath; |
| 115 | path.title = fileSystemPath; |
| 116 | |
| 117 | const toolbar = new UI.Toolbar(''); |
| 118 | const button = new UI.ToolbarButton(Common.UIString('Remove'), 'largeicon-delete'); |
| 119 | button.addEventListener(UI.ToolbarButton.Events.Click, this._removeFileSystemClicked.bind(this, fileSystem)); |
| 120 | toolbar.appendToolbarItem(button); |
| 121 | header.appendChild(toolbar.element); |
| 122 | |
| 123 | return element; |
| 124 | } |
| 125 | |
| 126 | /** |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 127 | * @param {!Persistence.PlatformFileSystem} fileSystem |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 128 | */ |
| 129 | _removeFileSystemClicked(fileSystem) { |
| 130 | Persistence.isolatedFileSystemManager.removeFileSystem(fileSystem); |
| 131 | } |
| 132 | |
| 133 | _addFileSystemClicked() { |
| 134 | Persistence.isolatedFileSystemManager.addFileSystem(); |
| 135 | } |
| 136 | |
| 137 | /** |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 138 | * @param {!Persistence.PlatformFileSystem} fileSystem |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 139 | */ |
| 140 | _fileSystemAdded(fileSystem) { |
| 141 | this._addItem(fileSystem); |
| 142 | } |
| 143 | |
| 144 | /** |
Alexey Kozyatinskiy | 3a2eb28 | 2018-08-21 16:22:02 | [diff] [blame] | 145 | * @param {!Persistence.PlatformFileSystem} fileSystem |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 146 | */ |
| 147 | _fileSystemRemoved(fileSystem) { |
| 148 | const mappingView = this._mappingViewByPath.get(fileSystem.path()); |
| 149 | if (mappingView) { |
| 150 | mappingView.dispose(); |
| 151 | this._mappingViewByPath.delete(fileSystem.path()); |
| 152 | } |
| 153 | |
| 154 | const element = this._elementByPath.get(fileSystem.path()); |
| 155 | if (element) { |
| 156 | this._elementByPath.delete(fileSystem.path()); |
| 157 | element.remove(); |
| 158 | } |
| 159 | } |
| 160 | }; |