OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview This file provides a class that can be used to open URLs based | 6 * @fileoverview This file provides a class that can be used to open URLs based |
7 * on user interactions. It ensures a consistent behavior when it comes to | 7 * on user interactions. It ensures a consistent behavior when it comes to |
8 * holding down Ctrl and Shift while clicking or activating the a link. | 8 * holding down Ctrl and Shift while clicking or activating the a link. |
9 * | 9 * |
10 * This depends on the {@code chrome.windows} and {@code chrome.tabs} | 10 * This depends on the {@code chrome.windows} and {@code chrome.tabs} |
11 * extensions API. | 11 * extensions API. |
12 */ | 12 */ |
13 | 13 |
| 14 /** |
| 15 * The kind of link open we want to perform. |
| 16 * @enum {number} |
| 17 */ |
| 18 cr.LinkKind = { |
| 19 FOREGROUND_TAB: 0, |
| 20 BACKGROUND_TAB: 1, |
| 21 WINDOW: 2, |
| 22 SELF: 3, |
| 23 INCOGNITO: 4 |
| 24 }; |
| 25 |
14 cr.define('cr', function() { | 26 cr.define('cr', function() { |
15 | |
16 /** | |
17 * The kind of link open we want to perform. | |
18 * @enum {number} | |
19 */ | |
20 var LinkKind = { | |
21 FOREGROUND_TAB: 0, | |
22 BACKGROUND_TAB: 1, | |
23 WINDOW: 2, | |
24 SELF: 3, | |
25 INCOGNITO: 4 | |
26 }; | |
27 | |
28 /** | 27 /** |
29 * This class is used to handle opening of links based on user actions. The | 28 * This class is used to handle opening of links based on user actions. The |
30 * following actions are currently implemented: | 29 * following actions are currently implemented: |
31 * | 30 * |
32 * * Press Ctrl and click a link. Or click a link with your middle mouse | 31 * * Press Ctrl and click a link. Or click a link with your middle mouse |
33 * button (or mousewheel). Or press Enter while holding Ctrl. | 32 * button (or mousewheel). Or press Enter while holding Ctrl. |
34 * Opens the link in a new tab in the background . | 33 * Opens the link in a new tab in the background . |
35 * * Press Ctrl+Shift and click a link. Or press Shift and click a link with | 34 * * Press Ctrl+Shift and click a link. Or press Shift and click a link with |
36 * your middle mouse button (or mousewheel). Or press Enter while holding | 35 * your middle mouse button (or mousewheel). Or press Enter while holding |
37 * Ctrl+Shift. | 36 * Ctrl+Shift. |
38 * Opens the link in a new tab and switches to the newly opened tab. | 37 * Opens the link in a new tab and switches to the newly opened tab. |
39 * * Press Shift and click a link. Or press Enter while holding Shift. | 38 * * Press Shift and click a link. Or press Enter while holding Shift. |
40 * Opens the link in a new window. | 39 * Opens the link in a new window. |
41 * | 40 * |
42 * On Mac, uses Command instead of Ctrl. | 41 * On Mac, uses Command instead of Ctrl. |
43 * For keyboard support you need to use keydown. | 42 * For keyboard support you need to use keydown. |
44 * | 43 * |
45 * @param {!LocalStrings} localStrings The local strings object which is used | 44 * @param {!(LocalStrings|LoadTimeData)} localStrings The local strings object |
46 * to localize the warning prompt in case the user tries to open a lot of | 45 * which is used to localize the warning prompt in case the user tries to |
47 * links. | 46 * open a lot of links. |
48 * @constructor | 47 * @constructor |
49 */ | 48 */ |
50 function LinkController(localStrings) { | 49 function LinkController(localStrings) { |
51 this.localStrings_ = localStrings; | 50 this.localStrings_ = localStrings; |
52 } | 51 } |
53 | 52 |
54 LinkController.prototype = { | 53 LinkController.prototype = { |
55 /** | 54 /** |
56 * The number of links that can be opened before showing a warning confirm | 55 * The number of links that can be opened before showing a warning confirm |
57 * message. | 56 * message. |
58 */ | 57 */ |
59 warningLimit: 15, | 58 warningLimit: 15, |
60 | 59 |
61 /** | 60 /** |
62 * The DOM window that we want to open links into in case we are opening | 61 * The DOM window that we want to open links into in case we are opening |
63 * links in the same window. | 62 * links in the same window. |
64 * @type {!Window} | 63 * @type {!Window} |
65 */ | 64 */ |
66 window: window, | 65 window: window, |
67 | 66 |
68 /** | 67 /** |
69 * This method is used for showing the warning confirm message when the | 68 * This method is used for showing the warning confirm message when the |
70 * user is trying to open a lot of links. | 69 * user is trying to open a lot of links. |
71 * @param {number} The number of URLs to open. | 70 * @param {number} count The number of URLs to open. |
72 * @return {string} The message to show the user. | 71 * @return {string} The message to show the user. |
73 */ | 72 */ |
74 getWarningMessage: function(count) { | 73 getWarningMessage: function(count) { |
75 return this.localStrings_.getStringF('should_open_all', count); | 74 return this.localStrings_.getStringF('should_open_all', String(count)); |
76 }, | 75 }, |
77 | 76 |
78 /** | 77 /** |
79 * Open an URL from a mouse or keyboard event. | 78 * Open an URL from a mouse or keyboard event. |
80 * @param {string} url The URL to open. | 79 * @param {string} url The URL to open. |
81 * @param {!Event} e The event triggering the opening of the URL. | 80 * @param {!Event} e The event triggering the opening of the URL. |
82 */ | 81 */ |
83 openUrlFromEvent: function(url, e) { | 82 openUrlFromEvent: function(url, e) { |
84 // We only support keydown Enter and non right click events. | 83 // We only support keydown Enter and non right click events. |
85 if (e.type == 'keydown' && e.keyIdentifier == 'Enter' || | 84 if (e.type == 'keydown' && e.keyIdentifier == 'Enter' || |
86 e.button != 2) { | 85 e.button != 2) { |
87 var kind; | 86 var kind; |
88 var ctrl = cr.isMac && e.metaKey || !cr.isMac && e.ctrlKey; | 87 var ctrl = cr.isMac && e.metaKey || !cr.isMac && e.ctrlKey; |
89 | 88 |
90 if (e.button == 1 || ctrl) // middle, ctrl or keyboard | 89 if (e.button == 1 || ctrl) // middle, ctrl or keyboard |
91 kind = e.shiftKey ? LinkKind.FOREGROUND_TAB : LinkKind.BACKGROUND_TAB; | 90 kind = e.shiftKey ? cr.LinkKind.FOREGROUND_TAB : |
| 91 cr.LinkKind.BACKGROUND_TAB; |
92 else // left or keyboard | 92 else // left or keyboard |
93 kind = e.shiftKey ? LinkKind.WINDOW : LinkKind.SELF; | 93 kind = e.shiftKey ? cr.LinkKind.WINDOW : cr.LinkKind.SELF; |
94 | 94 |
95 this.openUrls([url], kind); | 95 this.openUrls([url], kind); |
96 } | 96 } |
97 }, | 97 }, |
98 | 98 |
99 | 99 |
100 /** | 100 /** |
101 * Opens a URL in a new tab, window or incognito window. | 101 * Opens a URL in a new tab, window or incognito window. |
102 * @param {string} url The URL to open. | 102 * @param {string} url The URL to open. |
103 * @param {LinkKind} kind The kind of open we want to do. | 103 * @param {cr.LinkKind} kind The kind of open we want to do. |
104 */ | 104 */ |
105 openUrl: function(url, kind) { | 105 openUrl: function(url, kind) { |
106 this.openUrls([url], kind); | 106 this.openUrls([url], kind); |
107 }, | 107 }, |
108 | 108 |
109 /** | 109 /** |
110 * Opens URLs in new tab, window or incognito mode. | 110 * Opens URLs in new tab, window or incognito mode. |
111 * @param {!Array.<string>} urls The URLs to open. | 111 * @param {!Array.<string>} urls The URLs to open. |
112 * @param {LinkKind} kind The kind of open we want to do. | 112 * @param {cr.LinkKind} kind The kind of open we want to do. |
113 */ | 113 */ |
114 openUrls: function(urls, kind) { | 114 openUrls: function(urls, kind) { |
115 if (urls.length < 1) | 115 if (urls.length < 1) |
116 return; | 116 return; |
117 | 117 |
118 if (urls.length > this.warningLimit) { | 118 if (urls.length > this.warningLimit) { |
119 if (!this.window.confirm(this.getWarningMessage(urls.length))) | 119 if (!this.window.confirm(this.getWarningMessage(urls.length))) |
120 return; | 120 return; |
121 } | 121 } |
122 | 122 |
123 // Fix '#124' URLs since opening those in a new window does not work. We | 123 // Fix '#124' URLs since opening those in a new window does not work. We |
124 // prepend the base URL when we encounter those. | 124 // prepend the base URL when we encounter those. |
125 var base = this.window.location.href.split('#')[0]; | 125 var base = this.window.location.href.split('#')[0]; |
126 urls = urls.map(function(url) { | 126 urls = urls.map(function(url) { |
127 return url[0] == '#' ? base + url : url; | 127 return url[0] == '#' ? base + url : url; |
128 }); | 128 }); |
129 | 129 |
130 var incognito = kind == LinkKind.INCOGNITO; | 130 var incognito = kind == cr.LinkKind.INCOGNITO; |
131 if (kind == LinkKind.WINDOW || incognito) { | 131 if (kind == cr.LinkKind.WINDOW || incognito) { |
132 chrome.windows.create({ | 132 chrome.windows.create({ |
133 url: urls, | 133 url: urls, |
134 incognito: incognito | 134 incognito: incognito |
135 }); | 135 }); |
136 } else if (kind == LinkKind.FOREGROUND_TAB || | 136 } else if (kind == cr.LinkKind.FOREGROUND_TAB || |
137 kind == LinkKind.BACKGROUND_TAB) { | 137 kind == cr.LinkKind.BACKGROUND_TAB) { |
138 urls.forEach(function(url, i) { | 138 urls.forEach(function(url, i) { |
139 chrome.tabs.create({ | 139 chrome.tabs.create({ |
140 url: url, | 140 url: url, |
141 selected: kind == LinkKind.FOREGROUND_TAB && !i | 141 selected: kind == cr.LinkKind.FOREGROUND_TAB && !i |
142 }); | 142 }); |
143 }); | 143 }); |
144 } else { | 144 } else { |
145 this.window.location.href = urls[0]; | 145 this.window.location.href = urls[0]; |
146 } | 146 } |
147 } | 147 } |
148 }; | 148 }; |
149 | 149 |
150 // Export | 150 // Export |
151 return { | 151 return { |
152 LinkController: LinkController, | 152 LinkController: LinkController, |
153 LinkKind: LinkKind | |
154 }; | 153 }; |
155 }); | 154 }); |
OLD | NEW |