OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 'use strict'; | |
6 | |
7 /** | |
8 * Creates a new Navigator for navigating to links inside or outside the PDF. | |
9 */ | |
10 function Navigator() { | |
11 } | |
12 | |
13 Navigator.prototype = { | |
14 /** | |
15 * @private | |
16 * Function to navigate to the given URL. This might involve navigating | |
17 * within the PDF page or opening a new url (in the same tab or a new tab). | |
18 * @param {string} url The URL to navigate to. | |
19 * @param {boolean} newTab Whether to perform the navigation in a new tab or | |
20 * in the current tab. | |
raymes
2015/01/21 07:03:44
nit: 4 space indent
Deepak
2015/01/21 08:33:41
Done.
| |
21 * @param {string} originalUrl The original page URL. | |
22 * @param {Object} viewPort The viewport info of the page. | |
raymes
2015/01/21 07:03:44
nit: viewPort->viewport (and below)
Deepak
2015/01/21 08:33:41
Done.
| |
23 * @param {Object} paramsParser The parsed object having URL parsed info. | |
24 */ | |
25 navigate: function(url, newTab, originalUrl, viewPort, paramsParser) { | |
26 if (url.length == 0) | |
27 return; | |
28 // If |urlFragment| starts with '#', then it's for the same URL with a | |
29 // different URL fragment. | |
30 if (url.charAt(0) == '#') { | |
31 // if '#' is already present in |originalUrl| then remove old fragment | |
32 // and add new url fragment. | |
33 var hashIndex = originalUrl.search('#'); | |
34 if (hashIndex != -1) | |
35 url = originalUrl.substring(0, hashIndex) + url; | |
36 else | |
37 url = originalUrl + url; | |
38 } | |
39 | |
40 // If there's no scheme, add http. | |
41 if (url.indexOf('://') == -1 && url.indexOf('mailto:') == -1) | |
42 url = 'http://' + url; | |
43 | |
44 // Make sure inputURL starts with a valid scheme. | |
45 if (url.indexOf('http://') != 0 && | |
46 url.indexOf('https://') != 0 && | |
47 url.indexOf('ftp://') != 0 && | |
48 url.indexOf('file://') != 0 && | |
49 url.indexOf('mailto:') != 0) { | |
50 return; | |
51 } | |
52 // Make sure inputURL is not only a scheme. | |
53 if (url == 'http://' || | |
54 url == 'https://' || | |
55 url == 'ftp://' || | |
56 url == 'file://' || | |
57 url == 'mailto:') { | |
58 return; | |
59 } | |
60 | |
61 if (newTab) { | |
62 // Prefer the tabs API because it guarantees we can just open a new tab. | |
63 // window.open doesn't have this guarantee. | |
64 if (chrome.tabs) | |
65 chrome.tabs.create({ url: url }); | |
66 else | |
67 window.open(url); | |
68 } else { | |
69 var pageNumber = | |
70 paramsParser.getViewportFromUrlParams(url).page; | |
71 if (pageNumber != undefined) | |
72 viewPort.goToPage(pageNumber); | |
73 else | |
74 window.location.href = url; | |
75 } | |
76 } | |
77 }; | |
OLD | NEW |