Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / sources / WorkspaceMappingTip.js
blobf829c4e7cd7b906f2da5c1ede4975bccb619dff1
2 // Copyright 2014 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 /**
7 * @constructor
8 * @param {!WebInspector.SourcesPanel} sourcesPanel
9 * @param {!WebInspector.Workspace} workspace
11 WebInspector.WorkspaceMappingTip = function(sourcesPanel, workspace)
13 this._sourcesPanel = sourcesPanel;
14 this._workspace = workspace;
16 this._sourcesView = this._sourcesPanel.sourcesView();
17 this._workspaceInfobarDisabledSetting = WebInspector.settings.createSetting("workspaceInfobarDisabled", false);
18 this._workspaceMappingInfobarDisabledSetting = WebInspector.settings.createSetting("workspaceMappingInfobarDisabled", false);
20 if (this._workspaceInfobarDisabledSetting.get() && this._workspaceMappingInfobarDisabledSetting.get())
21 return;
22 this._sourcesView.addEventListener(WebInspector.SourcesView.Events.EditorSelected, this._editorSelected.bind(this));
25 WebInspector.WorkspaceMappingTip._infobarSymbol = Symbol("infobar");
27 WebInspector.WorkspaceMappingTip.prototype = {
28 /**
29 * @param {!WebInspector.Event} event
31 _editorSelected: function(event)
33 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
34 if (this._editorSelectedTimer)
35 clearTimeout(this._editorSelectedTimer);
36 this._editorSelectedTimer = setTimeout(this._updateSuggestedMappingInfobar.bind(this, uiSourceCode), 3000);
39 /**
40 * @param {!WebInspector.UISourceCode} uiSourceCode
42 _updateSuggestedMappingInfobar: function(uiSourceCode)
44 var uiSourceCodeFrame = this._sourcesView.viewForFile(uiSourceCode);
46 if (!uiSourceCodeFrame.isShowing())
47 return;
48 if (uiSourceCode[WebInspector.WorkspaceMappingTip._infobarSymbol])
49 return;
51 // First try mapping filesystem -> network.
52 if (!this._workspaceMappingInfobarDisabledSetting.get() && uiSourceCode.project().type() === WebInspector.projectTypes.FileSystem) {
53 var networkURL = WebInspector.networkMapping.networkURL(uiSourceCode);
54 var hasMappings = !!networkURL;
55 if (hasMappings)
56 return;
58 var networkProjects = this._workspace.projectsForType(WebInspector.projectTypes.Network);
59 networkProjects = networkProjects.concat(this._workspace.projectsForType(WebInspector.projectTypes.ContentScripts));
60 for (var i = 0; i < networkProjects.length; ++i) {
61 if (!this._isLocalHost(networkProjects[i].url()))
62 continue;
63 var name = uiSourceCode.name();
64 var networkUiSourceCodes = networkProjects[i].uiSourceCodes();
65 for (var j = 0; j < networkUiSourceCodes.length; ++j) {
66 if (networkUiSourceCodes[j].name() === name) {
67 this._showMappingInfobar(uiSourceCode, false);
68 return;
74 // Then map network -> filesystem.
75 if (uiSourceCode.project().type() === WebInspector.projectTypes.Network || uiSourceCode.project().type() === WebInspector.projectTypes.ContentScripts) {
76 // Suggest for localhost only.
77 if (!this._isLocalHost(uiSourceCode.originURL()))
78 return;
79 var networkURL = WebInspector.networkMapping.networkURL(uiSourceCode);
80 if (WebInspector.networkMapping.uiSourceCodeForURLForAnyTarget(networkURL) !== uiSourceCode)
81 return;
83 var filesystemProjects = this._workspace.projectsForType(WebInspector.projectTypes.FileSystem);
84 for (var i = 0; i < filesystemProjects.length; ++i) {
85 var name = uiSourceCode.name();
86 var fsUiSourceCodes = filesystemProjects[i].uiSourceCodes();
87 for (var j = 0; j < fsUiSourceCodes.length; ++j) {
88 if (fsUiSourceCodes[j].name() === name) {
89 if (!this._workspaceMappingInfobarDisabledSetting.get())
90 this._showMappingInfobar(uiSourceCode, true);
91 return;
96 if (!this._workspaceInfobarDisabledSetting.get())
97 this._showWorkspaceInfobar(uiSourceCode);
102 * @param {string} url
103 * @return {boolean}
105 _isLocalHost: function(url)
107 var parsedURL = url.asParsedURL();
108 return !!parsedURL && parsedURL.host === "localhost";
112 * @param {!WebInspector.UISourceCode} uiSourceCode
114 _showWorkspaceInfobar: function(uiSourceCode)
116 var infobar = new WebInspector.UISourceCodeFrame.Infobar(WebInspector.Infobar.Type.Info, WebInspector.UIString("Serving from the file system? Add your files into the workspace."), this._workspaceInfobarDisabledSetting);
117 infobar.createDetailsRowMessage(WebInspector.UIString("If you add files into your DevTools workspace, your changes will be persisted to disk."));
118 infobar.createDetailsRowMessage(WebInspector.UIString("To add a folder into the workspace, drag and drop it into the Sources panel."));
119 this._appendInfobar(uiSourceCode, infobar);
123 * @param {!WebInspector.UISourceCode} uiSourceCode
124 * @param {boolean} isNetwork
126 _showMappingInfobar: function(uiSourceCode, isNetwork)
128 var title;
129 if (isNetwork)
130 title = WebInspector.UIString("Map network resource '%s' to workspace?", uiSourceCode.originURL());
131 else
132 title = WebInspector.UIString("Map workspace resource '%s' to network?", uiSourceCode.path());
134 var infobar = new WebInspector.UISourceCodeFrame.Infobar(WebInspector.Infobar.Type.Info, title, this._workspaceMappingInfobarDisabledSetting);
135 infobar.createDetailsRowMessage(WebInspector.UIString("You can map files in your workspace to the ones loaded over the network. As a result, changes made in DevTools will be persisted to disk."));
136 infobar.createDetailsRowMessage(WebInspector.UIString("Use context menu to establish the mapping at any time."));
137 var anchor = createElementWithClass("a", "link");
138 anchor.textContent = WebInspector.UIString("Establish the mapping now...");
139 anchor.addEventListener("click", this._establishTheMapping.bind(this, uiSourceCode), false);
140 infobar.createDetailsRowMessage("").appendChild(anchor);
141 this._appendInfobar(uiSourceCode, infobar);
145 * @param {!WebInspector.UISourceCode} uiSourceCode
146 * @param {?Event} event
148 _establishTheMapping: function(uiSourceCode, event)
150 event.consume(true);
151 if (uiSourceCode.project().type() === WebInspector.projectTypes.FileSystem)
152 this._sourcesPanel.mapFileSystemToNetwork(uiSourceCode);
153 else
154 this._sourcesPanel.mapNetworkToFileSystem(uiSourceCode);
158 * @param {!WebInspector.UISourceCode} uiSourceCode
159 * @param {!WebInspector.UISourceCodeFrame.Infobar} infobar
161 _appendInfobar: function(uiSourceCode, infobar)
163 var uiSourceCodeFrame = this._sourcesView.viewForFile(uiSourceCode);
165 var rowElement = infobar.createDetailsRowMessage(WebInspector.UIString("For more information on workspaces, refer to the "));
166 rowElement.appendChild(WebInspector.linkifyDocumentationURLAsNode("setup/workspace/setup-workflow", WebInspector.UIString("workspaces documentation")));
167 rowElement.createTextChild(".");
168 uiSourceCode[WebInspector.WorkspaceMappingTip._infobarSymbol] = infobar;
169 uiSourceCodeFrame.attachInfobars([infobar]);
170 WebInspector.runCSSAnimationOnce(infobar.element, "source-frame-infobar-animation");