File: WorkspaceMappingTip.js

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (171 lines) | stat: -rw-r--r-- 7,607 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @constructor
 * @param {!WebInspector.SourcesPanel} sourcesPanel
 * @param {!WebInspector.Workspace} workspace
 */
WebInspector.WorkspaceMappingTip = function(sourcesPanel, workspace)
{
    this._sourcesPanel = sourcesPanel;
    this._workspace = workspace;

    this._sourcesView = this._sourcesPanel.sourcesView();
    this._sourcesView.addEventListener(WebInspector.SourcesView.Events.EditorSelected, this._editorSelected.bind(this));
    this._workspaceInfobarAllowedSetting = WebInspector.settings.createSetting("workspaceInfobarAllowed", true);
}

WebInspector.WorkspaceMappingTip._infobarSymbol = Symbol("infobar");

WebInspector.WorkspaceMappingTip.prototype = {
    /**
     * @param {!WebInspector.Event} event
     */
    _editorSelected: function(event)
    {
        var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
        if (this._editorSelectedTimer)
            clearTimeout(this._editorSelectedTimer);
        this._editorSelectedTimer = setTimeout(this._updateSuggestedMappingInfobar.bind(this, uiSourceCode), 3000);
    },

    /**
     * @param {!WebInspector.UISourceCode} uiSourceCode
     */
    _updateSuggestedMappingInfobar: function(uiSourceCode)
    {
        var uiSourceCodeFrame = this._sourcesView.viewForFile(uiSourceCode);

        if (!uiSourceCodeFrame.isShowing())
            return;
        if (uiSourceCode[WebInspector.WorkspaceMappingTip._infobarSymbol])
            return;

        // First try mapping filesystem -> network.
        if (uiSourceCode.project().type() === WebInspector.projectTypes.FileSystem) {
            var networkURL = WebInspector.networkMapping.networkURL(uiSourceCode);
            var hasMappings = !!networkURL;
            if (hasMappings)
                return;

            var networkProjects = this._workspace.projectsForType(WebInspector.projectTypes.Network);
            networkProjects = networkProjects.concat(this._workspace.projectsForType(WebInspector.projectTypes.ContentScripts));
            for (var i = 0; i < networkProjects.length; ++i) {
                if (!this._isLocalHost(networkProjects[i].url()))
                    continue;
                var name = uiSourceCode.name();
                var networkUiSourceCodes = networkProjects[i].uiSourceCodes();
                for (var j = 0; j < networkUiSourceCodes.length; ++j) {
                    if (networkUiSourceCodes[j].name() === name) {
                        this._showMappingInfobar(uiSourceCode, false);
                        return;
                    }
                }
            }
        }

        // Then map network -> filesystem.
        if (uiSourceCode.project().type() === WebInspector.projectTypes.Network || uiSourceCode.project().type() === WebInspector.projectTypes.ContentScripts) {
            // Suggest for localhost only.
            if (!this._isLocalHost(uiSourceCode.originURL()))
                return;
            var networkURL = WebInspector.networkMapping.networkURL(uiSourceCode);
            if (WebInspector.networkMapping.uiSourceCodeForURL(networkURL) !== uiSourceCode)
                return;

            var filesystemProjects = this._workspace.projectsForType(WebInspector.projectTypes.FileSystem);
            for (var i = 0; i < filesystemProjects.length; ++i) {
                var name = uiSourceCode.name();
                var fsUiSourceCodes = filesystemProjects[i].uiSourceCodes();
                for (var j = 0; j < fsUiSourceCodes.length; ++j) {
                    if (fsUiSourceCodes[j].name() === name) {
                        this._showMappingInfobar(uiSourceCode, true);
                        return;
                    }
                }
            }
            if (this._workspaceInfobarAllowedSetting.get())
                this._showWorkspaceInfobar(uiSourceCode);
        }
    },

    /**
     * @param {string} url
     * @return {boolean}
     */
    _isLocalHost: function(url)
    {
        var parsedURL = url.asParsedURL();
        return !!parsedURL && parsedURL.host === "localhost";
    },

    /**
     * @param {!WebInspector.UISourceCode} uiSourceCode
     */
    _showWorkspaceInfobar: function(uiSourceCode)
    {
        var infobar = new WebInspector.UISourceCodeFrame.Infobar(WebInspector.UISourceCodeFrame.Infobar.Level.Info, WebInspector.UIString("Serving from the file system? Add your files into the workspace."), this._onWorkspaceInfobarDispose.bind(this));
        infobar.createDetailsRowMessage(WebInspector.UIString("If you add files into your DevTools workspace, your changes will be persisted to disk."));
        infobar.createDetailsRowMessage(WebInspector.UIString("To add a folder into the workspace, drag and drop it into the Sources panel."));
        this._appendInfobar(uiSourceCode, infobar);
    },

    _onWorkspaceInfobarDispose: function()
    {
        this._workspaceInfobarAllowedSetting.set(false);
    },

    /**
     * @param {!WebInspector.UISourceCode} uiSourceCode
     * @param {boolean} isNetwork
     */
    _showMappingInfobar: function(uiSourceCode, isNetwork)
    {
        var title;
        if (isNetwork)
            title = WebInspector.UIString("Map network resource '%s' to workspace?", uiSourceCode.originURL());
        else
            title = WebInspector.UIString("Map workspace resource '%s' to network?", uiSourceCode.path());

        var infobar = new WebInspector.UISourceCodeFrame.Infobar(WebInspector.UISourceCodeFrame.Infobar.Level.Info, title);
        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."));
        infobar.createDetailsRowMessage(WebInspector.UIString("Use context menu to establish the mapping at any time."));
        var anchor = createElementWithClass("a", "link");
        anchor.textContent = WebInspector.UIString("Establish the mapping now...");
        anchor.addEventListener("click", this._establishTheMapping.bind(this, uiSourceCode), false);
        infobar.createDetailsRowMessage("").appendChild(anchor);
        this._appendInfobar(uiSourceCode, infobar);
    },

    /**
     * @param {!WebInspector.UISourceCode} uiSourceCode
     * @param {?Event} event
     */
    _establishTheMapping: function(uiSourceCode, event)
    {
        event.consume(true);
        if (uiSourceCode.project().type() === WebInspector.projectTypes.FileSystem)
            this._sourcesPanel.mapFileSystemToNetwork(uiSourceCode);
        else
            this._sourcesPanel.mapNetworkToFileSystem(uiSourceCode);
    },

    /**
     * @param {!WebInspector.UISourceCode} uiSourceCode
     * @param {!WebInspector.UISourceCodeFrame.Infobar} infobar
     */
    _appendInfobar: function(uiSourceCode, infobar)
    {
        var uiSourceCodeFrame = this._sourcesView.viewForFile(uiSourceCode);

        var rowElement = infobar.createDetailsRowMessage(WebInspector.UIString("For more information on workspaces, refer to the "));
        rowElement.appendChild(WebInspector.createDocumentationAnchor("workspaces", WebInspector.UIString("workspaces documentation")));
        rowElement.createTextChild(".");
        uiSourceCode[WebInspector.WorkspaceMappingTip._infobarSymbol] = infobar;
        uiSourceCodeFrame.attachInfobars([infobar]);
        WebInspector.runCSSAnimationOnce(infobar.element, "source-frame-infobar-animation");
    }
}