Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / webui / resources / js / cr / link_controller.js
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.
4
5 /**
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.
9  *
10  * This depends on the {@code chrome.windows} and {@code chrome.tabs}
11  * extensions API.
12  */
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
26 cr.define('cr', function() {
27   /**
28    * This class is used to handle opening of links based on user actions. The
29    * following actions are currently implemented:
30    *
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
36    *   Ctrl+Shift.
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.
40    *
41    * On Mac, uses Command instead of Ctrl.
42    * For keyboard support you need to use keydown.
43    *
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
46    *     links.
47    * @constructor
48    */
49   function LinkController(localStrings) {
50     this.localStrings_ = localStrings;
51   }
52
53   LinkController.prototype = {
54     /**
55      * The number of links that can be opened before showing a warning confirm
56      * message.
57      */
58     warningLimit: 15,
59
60     /**
61      * The DOM window that we want to open links into in case we are opening
62      * links in the same window.
63      * @type {!Window}
64      */
65     window: window,
66
67     /**
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.
72      */
73     getWarningMessage: function(count) {
74       return this.localStrings_.getStringF('should_open_all', String(count));
75     },
76
77     /**
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.
81      */
82     openUrlFromEvent: function(url, e) {
83       // We only support keydown Enter and non right click events.
84       if (e.type == 'keydown' && e.keyIdentifier == 'Enter' ||
85           e.button != 2) {
86         var kind;
87         var ctrl = cr.isMac && e.metaKey || !cr.isMac && e.ctrlKey;
88
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;
94
95         this.openUrls([url], kind);
96       }
97     },
98
99
100     /**
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.
104      */
105     openUrl: function(url, kind) {
106       this.openUrls([url], kind);
107     },
108
109     /**
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.
113      */
114     openUrls: function(urls, kind) {
115       if (urls.length < 1)
116         return;
117
118       if (urls.length > this.warningLimit) {
119         if (!this.window.confirm(this.getWarningMessage(urls.length)))
120           return;
121       }
122
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;
128       });
129
130       var incognito = kind == cr.LinkKind.INCOGNITO;
131       if (kind == cr.LinkKind.WINDOW || incognito) {
132         chrome.windows.create({
133           url: urls,
134           incognito: incognito
135         });
136       } else if (kind == cr.LinkKind.FOREGROUND_TAB ||
137                  kind == cr.LinkKind.BACKGROUND_TAB) {
138         urls.forEach(function(url, i) {
139           chrome.tabs.create({
140             url: url,
141             selected: kind == cr.LinkKind.FOREGROUND_TAB && !i
142           });
143         });
144       } else {
145         this.window.location.href = urls[0];
146       }
147     }
148   };
149
150   // Export
151   return {
152     LinkController: LinkController,
153   };
154 });