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
3 // found in the LICENSE file.
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
8 * holding down Ctrl and Shift while clicking or activating the a link.
10 * This depends on the {@code chrome.windows} and {@code chrome.tabs}
15 * The kind of link open we want to perform.
26 cr.define('cr', function() {
28 * This class is used to handle opening of links based on user actions. The
29 * following actions are currently implemented:
31 * * 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 * 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 * your middle mouse button (or mousewheel). Or press Enter while holding
37 * 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 * Opens the link in a new window.
41 * On Mac, uses Command instead of Ctrl.
42 * For keyboard support you need to use keydown.
44 * @param {!LoadTimeData} localStrings The local strings object which is used
45 * to localize the warning prompt in case the user tries to open a lot of
49 function LinkController(localStrings) {
50 this.localStrings_ = localStrings;
53 LinkController.prototype = {
55 * The number of links that can be opened before showing a warning confirm
61 * The DOM window that we want to open links into in case we are opening
62 * links in the same window.
68 * This method is used for showing the warning confirm message when the
69 * user is trying to open a lot of links.
70 * @param {number} count The number of URLs to open.
71 * @return {string} The message to show the user.
73 getWarningMessage: function(count) {
74 return this.localStrings_.getStringF('should_open_all', String(count));
78 * Open an URL from a mouse or keyboard event.
79 * @param {string} url The URL to open.
80 * @param {!Event} e The event triggering the opening of the URL.
82 openUrlFromEvent: function(url, e) {
83 // We only support keydown Enter and non right click events.
84 if (e.type == 'keydown' && e.keyIdentifier == 'Enter' ||
87 var ctrl = cr.isMac && e.metaKey || !cr.isMac && e.ctrlKey;
89 if (e.button == 1 || ctrl) // middle, ctrl or keyboard
90 kind = e.shiftKey ? cr.LinkKind.FOREGROUND_TAB :
91 cr.LinkKind.BACKGROUND_TAB;
92 else // left or keyboard
93 kind = e.shiftKey ? cr.LinkKind.WINDOW : cr.LinkKind.SELF;
95 this.openUrls([url], kind);
101 * Opens a URL in a new tab, window or incognito window.
102 * @param {string} url The URL to open.
103 * @param {cr.LinkKind} kind The kind of open we want to do.
105 openUrl: function(url, kind) {
106 this.openUrls([url], kind);
110 * Opens URLs in new tab, window or incognito mode.
111 * @param {!Array<string>} urls The URLs to open.
112 * @param {cr.LinkKind} kind The kind of open we want to do.
114 openUrls: function(urls, kind) {
118 if (urls.length > this.warningLimit) {
119 if (!this.window.confirm(this.getWarningMessage(urls.length)))
123 // Fix '#124' URLs since opening those in a new window does not work. We
124 // prepend the base URL when we encounter those.
125 var base = this.window.location.href.split('#')[0];
126 urls = urls.map(function(url) {
127 return url[0] == '#' ? base + url : url;
130 var incognito = kind == cr.LinkKind.INCOGNITO;
131 if (kind == cr.LinkKind.WINDOW || incognito) {
132 chrome.windows.create({
136 } else if (kind == cr.LinkKind.FOREGROUND_TAB ||
137 kind == cr.LinkKind.BACKGROUND_TAB) {
138 urls.forEach(function(url, i) {
141 selected: kind == cr.LinkKind.FOREGROUND_TAB && !i
145 this.window.location.href = urls[0];
152 LinkController: LinkController,