| 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 /** | 14 cr.define('cr', function() { |
| 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 | 15 |
| 26 cr.define('cr', function() { | 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 |
| 27 /** | 28 /** |
| 28 * This class is used to handle opening of links based on user actions. The | 29 * This class is used to handle opening of links based on user actions. The |
| 29 * following actions are currently implemented: | 30 * following actions are currently implemented: |
| 30 * | 31 * |
| 31 * * Press Ctrl and click a link. Or click a link with your middle mouse | 32 * * Press Ctrl and click a link. Or click a link with your middle mouse |
| 32 * button (or mousewheel). Or press Enter while holding Ctrl. | 33 * button (or mousewheel). Or press Enter while holding Ctrl. |
| 33 * Opens the link in a new tab in the background . | 34 * Opens the link in a new tab in the background . |
| 34 * * Press Ctrl+Shift and click a link. Or press Shift and click a link with | 35 * * Press Ctrl+Shift and click a link. Or press Shift and click a link with |
| 35 * your middle mouse button (or mousewheel). Or press Enter while holding | 36 * your middle mouse button (or mousewheel). Or press Enter while holding |
| 36 * Ctrl+Shift. | 37 * Ctrl+Shift. |
| 37 * Opens the link in a new tab and switches to the newly opened tab. | 38 * Opens the link in a new tab and switches to the newly opened tab. |
| 38 * * Press Shift and click a link. Or press Enter while holding Shift. | 39 * * Press Shift and click a link. Or press Enter while holding Shift. |
| 39 * Opens the link in a new window. | 40 * Opens the link in a new window. |
| 40 * | 41 * |
| 41 * On Mac, uses Command instead of Ctrl. | 42 * On Mac, uses Command instead of Ctrl. |
| 42 * For keyboard support you need to use keydown. | 43 * For keyboard support you need to use keydown. |
| 43 * | 44 * |
| 44 * @param {!LoadTimeData} localStrings The local strings object which is used | 45 * @param {!LocalStrings} localStrings The local strings object which is used |
| 45 * to localize the warning prompt in case the user tries to open a lot of | 46 * to localize the warning prompt in case the user tries to open a lot of |
| 46 * links. | 47 * links. |
| 47 * @constructor | 48 * @constructor |
| 48 */ | 49 */ |
| 49 function LinkController(localStrings) { | 50 function LinkController(localStrings) { |
| 50 this.localStrings_ = localStrings; | 51 this.localStrings_ = localStrings; |
| 51 } | 52 } |
| 52 | 53 |
| 53 LinkController.prototype = { | 54 LinkController.prototype = { |
| 54 /** | 55 /** |
| 55 * The number of links that can be opened before showing a warning confirm | 56 * The number of links that can be opened before showing a warning confirm |
| 56 * message. | 57 * message. |
| 57 */ | 58 */ |
| 58 warningLimit: 15, | 59 warningLimit: 15, |
| 59 | 60 |
| 60 /** | 61 /** |
| 61 * The DOM window that we want to open links into in case we are opening | 62 * The DOM window that we want to open links into in case we are opening |
| 62 * links in the same window. | 63 * links in the same window. |
| 63 * @type {!Window} | 64 * @type {!Window} |
| 64 */ | 65 */ |
| 65 window: window, | 66 window: window, |
| 66 | 67 |
| 67 /** | 68 /** |
| 68 * This method is used for showing the warning confirm message when the | 69 * This method is used for showing the warning confirm message when the |
| 69 * user is trying to open a lot of links. | 70 * user is trying to open a lot of links. |
| 70 * @param {number} count The number of URLs to open. | 71 * @param {number} The number of URLs to open. |
| 71 * @return {string} The message to show the user. | 72 * @return {string} The message to show the user. |
| 72 */ | 73 */ |
| 73 getWarningMessage: function(count) { | 74 getWarningMessage: function(count) { |
| 74 return this.localStrings_.getStringF('should_open_all', String(count)); | 75 return this.localStrings_.getStringF('should_open_all', count); |
| 75 }, | 76 }, |
| 76 | 77 |
| 77 /** | 78 /** |
| 78 * Open an URL from a mouse or keyboard event. | 79 * Open an URL from a mouse or keyboard event. |
| 79 * @param {string} url The URL to open. | 80 * @param {string} url The URL to open. |
| 80 * @param {!Event} e The event triggering the opening of the URL. | 81 * @param {!Event} e The event triggering the opening of the URL. |
| 81 */ | 82 */ |
| 82 openUrlFromEvent: function(url, e) { | 83 openUrlFromEvent: function(url, e) { |
| 83 // We only support keydown Enter and non right click events. | 84 // We only support keydown Enter and non right click events. |
| 84 if (e.type == 'keydown' && e.keyIdentifier == 'Enter' || | 85 if (e.type == 'keydown' && e.keyIdentifier == 'Enter' || |
| 85 e.button != 2) { | 86 e.button != 2) { |
| 86 var kind; | 87 var kind; |
| 87 var ctrl = cr.isMac && e.metaKey || !cr.isMac && e.ctrlKey; | 88 var ctrl = cr.isMac && e.metaKey || !cr.isMac && e.ctrlKey; |
| 88 | 89 |
| 89 if (e.button == 1 || ctrl) // middle, ctrl or keyboard | 90 if (e.button == 1 || ctrl) // middle, ctrl or keyboard |
| 90 kind = e.shiftKey ? cr.LinkKind.FOREGROUND_TAB : | 91 kind = e.shiftKey ? LinkKind.FOREGROUND_TAB : LinkKind.BACKGROUND_TAB; |
| 91 cr.LinkKind.BACKGROUND_TAB; | |
| 92 else // left or keyboard | 92 else // left or keyboard |
| 93 kind = e.shiftKey ? cr.LinkKind.WINDOW : cr.LinkKind.SELF; | 93 kind = e.shiftKey ? LinkKind.WINDOW : 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 {cr.LinkKind} kind The kind of open we want to do. | 103 * @param {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 {cr.LinkKind} kind The kind of open we want to do. | 112 * @param {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 == cr.LinkKind.INCOGNITO; | 130 var incognito = kind == LinkKind.INCOGNITO; |
| 131 if (kind == cr.LinkKind.WINDOW || incognito) { | 131 if (kind == 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 == cr.LinkKind.FOREGROUND_TAB || | 136 } else if (kind == LinkKind.FOREGROUND_TAB || |
| 137 kind == cr.LinkKind.BACKGROUND_TAB) { | 137 kind == 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 == cr.LinkKind.FOREGROUND_TAB && !i | 141 selected: kind == 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 |
| 153 }; | 154 }; |
| 154 }); | 155 }); |
| OLD | NEW |