TabbedEditorContainer.js [plain text]
WebInspector.TabbedEditorContainerDelegate = function() { }
WebInspector.TabbedEditorContainerDelegate.prototype = {
viewForFile: function(uiSourceCode) { }
}
WebInspector.TabbedEditorContainer = function(delegate, settingName)
{
this._delegate = delegate;
this._tabbedPane = new WebInspector.TabbedPane();
this._tabbedPane.closeableTabs = true;
this._tabbedPane.element.id = "scripts-editor-container-tabbed-pane";
this._tabbedPane.addEventListener(WebInspector.TabbedPane.EventTypes.TabClosed, this._tabClosed, this);
this._tabbedPane.addEventListener(WebInspector.TabbedPane.EventTypes.TabSelected, this._tabSelected, this);
this._tabIds = new Map();
this._files = {};
this._loadedURLs = {};
this._previouslyViewedFilesSetting = WebInspector.settings.createSetting(settingName, []);
this._history = new WebInspector.TabbedEditorContainer.History(this._previouslyViewedFilesSetting.get());
}
WebInspector.TabbedEditorContainer.Events = {
EditorSelected: "EditorSelected",
EditorClosed: "EditorClosed"
}
WebInspector.TabbedEditorContainer._tabId = 0;
WebInspector.TabbedEditorContainer.maximalPreviouslyViewedFilesCount = 30;
WebInspector.TabbedEditorContainer.prototype = {
get view()
{
return this._tabbedPane;
},
get visibleView()
{
return this._tabbedPane.visibleView;
},
show: function(parentElement)
{
this._tabbedPane.show(parentElement);
},
showFile: function(uiSourceCode)
{
this._innerShowFile(uiSourceCode, true);
},
_innerShowFile: function(uiSourceCode, userGesture)
{
if (this._currentFile === uiSourceCode)
return;
this._currentFile = uiSourceCode;
var tabId = this._tabIds.get(uiSourceCode) || this._appendFileTab(uiSourceCode, userGesture);
this._tabbedPane.selectTab(tabId, userGesture);
if (userGesture)
this._editorSelectedByUserAction();
this.dispatchEventToListeners(WebInspector.TabbedEditorContainer.Events.EditorSelected, this._currentFile);
},
_titleForFile: function(uiSourceCode)
{
const maxDisplayNameLength = 30;
const minDisplayQueryParamLength = 5;
var parsedURL = uiSourceCode.parsedURL;
if (!parsedURL.isValid)
return parsedURL.url ? parsedURL.url.trimMiddle(maxDisplayNameLength) : WebInspector.UIString("(program)");
var maxDisplayQueryParamLength = Math.max(minDisplayQueryParamLength, maxDisplayNameLength - parsedURL.lastPathComponent.length);
var displayQueryParams = parsedURL.queryParams ? "?" + parsedURL.queryParams.trimEnd(maxDisplayQueryParamLength - 1) : "";
var displayLastPathComponent = parsedURL.lastPathComponent.trimMiddle(maxDisplayNameLength - displayQueryParams.length);
var displayName = displayLastPathComponent + displayQueryParams;
return displayName || WebInspector.UIString("(program)");
},
uiSourceCodeAdded: function(uiSourceCode)
{
if (this._userSelectedFiles || this._loadedURLs[uiSourceCode.url])
return;
this._loadedURLs[uiSourceCode.url] = true;
var index = this._history.index(uiSourceCode.url)
if (index === -1)
return;
var tabId = this._tabIds.get(uiSourceCode) || this._appendFileTab(uiSourceCode, false);
if (index === 0)
this._innerShowFile(uiSourceCode, false);
},
_editorClosedByUserAction: function(uiSourceCode)
{
this._userSelectedFiles = true;
this._history.remove(uiSourceCode.url);
this._updateHistory();
},
_editorSelectedByUserAction: function()
{
this._userSelectedFiles = true;
this._updateHistory();
},
_updateHistory: function()
{
var tabIds = this._tabbedPane.lastOpenedTabIds(WebInspector.TabbedEditorContainer.maximalPreviouslyViewedFilesCount);
function tabIdToURL(tabId)
{
return this._files[tabId].url;
}
this._history.update(tabIds.map(tabIdToURL.bind(this)));
this._history.save(this._previouslyViewedFilesSetting);
},
_tooltipForFile: function(uiSourceCode)
{
return uiSourceCode.url;
},
_appendFileTab: function(uiSourceCode, userGesture)
{
var view = this._delegate.viewForFile(uiSourceCode);
var title = this._titleForFile(uiSourceCode);
var tooltip = this._tooltipForFile(uiSourceCode);
var tabId = this._generateTabId();
this._tabIds.put(uiSourceCode, tabId);
this._files[tabId] = uiSourceCode;
this._tabbedPane.appendTab(tabId, title, view, tooltip, userGesture);
return tabId;
},
_removeFileTab: function(uiSourceCode)
{
var tabId = this._tabIds.get(uiSourceCode);
if (tabId)
this._tabbedPane.closeTab(tabId);
},
_tabClosed: function(event)
{
var tabId = event.data.tabId;
var userGesture = event.data.isUserGesture;
var uiSourceCode = this._files[tabId];
this._tabIds.remove(uiSourceCode);
delete this._files[tabId];
delete this._currentFile;
this.dispatchEventToListeners(WebInspector.TabbedEditorContainer.Events.EditorClosed, uiSourceCode);
if (userGesture)
this._editorClosedByUserAction(uiSourceCode);
},
_tabSelected: function(event)
{
var tabId = event.data.tabId;
var userGesture = event.data.isUserGesture;
var uiSourceCode = this._files[tabId];
this._innerShowFile(uiSourceCode, userGesture);
},
_replaceFileTab: function(oldUISourceCode, newUISourceCode)
{
var tabId = this._tabIds.get(oldUISourceCode);
if (!tabId)
return;
delete this._files[this._tabIds.get(oldUISourceCode)]
this._tabIds.remove(oldUISourceCode);
this._tabIds.put(newUISourceCode, tabId);
this._files[tabId] = newUISourceCode;
this._tabbedPane.changeTabTitle(tabId, this._titleForFile(newUISourceCode));
this._tabbedPane.changeTabView(tabId, this._delegate.viewForFile(newUISourceCode));
this._tabbedPane.changeTabTooltip(tabId, this._tooltipForFile(newUISourceCode));
},
replaceFiles: function(oldUISourceCodeList, uiSourceCodeList)
{
var mainFile;
for (var i = 0; i < oldUISourceCodeList.length; ++i) {
var tabId = this._tabIds.get(oldUISourceCodeList[i]);
if (tabId && (!mainFile || this._tabbedPane.selectedTabId === tabId)) {
mainFile = oldUISourceCodeList[i];
break;
}
}
if (!mainFile)
return;
this._replaceFileTab(mainFile, uiSourceCodeList[0]);
for (var i = 0; i < oldUISourceCodeList.length; ++i)
this._removeFileTab(oldUISourceCodeList[i]);
},
setFileIsDirty: function(uiSourceCode, isDirty)
{
var tabId = this._tabIds.get(uiSourceCode);
if (tabId) {
var title = this._titleForFile(uiSourceCode);
if (isDirty)
title += "*";
this._tabbedPane.changeTabTitle(tabId, title);
}
},
reset: function()
{
this._tabbedPane.closeAllTabs();
this._tabIds = new Map();
this._files = {};
delete this._currentFile;
delete this._userSelectedFiles;
this._loadedURLs = {};
},
_generateTabId: function()
{
return "tab_" + (WebInspector.TabbedEditorContainer._tabId++);
}
}
WebInspector.TabbedEditorContainer.prototype.__proto__ = WebInspector.Object.prototype;
WebInspector.TabbedEditorContainer.History = function(urls)
{
this._urls = urls;
}
WebInspector.TabbedEditorContainer.History.prototype = {
index: function(url)
{
return this._urls.indexOf(url);
},
update: function(urls)
{
for (var i = urls.length - 1; i >= 0; --i) {
var index = this._urls.indexOf(urls[i]);
if (index !== -1)
this._urls.splice(index, 1);
this._urls.unshift(urls[i]);
}
},
remove: function(url)
{
var index = this._urls.indexOf(url);
if (index !== -1)
this._urls.splice(index, 1);
},
save: function(setting)
{
setting.set(this._urls);
}
}
WebInspector.TabbedEditorContainer.History.prototype.__proto__ = WebInspector.Object.prototype;