|
1 // This file contains the functions used to create the context menu for the browser's |
|
2 // main content web view. |
|
3 |
|
4 |
|
5 // Context menu spec: |
|
6 // ------------------ |
|
7 //Images---Options to be shown under an "Links/Pictures" tab |
|
8 // Save |
|
9 // Share |
|
10 // |
|
11 //Images that are also links---Options to be shown under an "Links/Pictures" tab |
|
12 // Open link in new window |
|
13 // Save |
|
14 // Share |
|
15 // |
|
16 //URLs-- Options to be shown under a "Links/Picture" tab |
|
17 // Open link in new window |
|
18 // Share |
|
19 // |
|
20 // Note: the options offered for a link or a picture or a combo |
|
21 // link/picture are slightly different, but the right set should |
|
22 // always be shown under a single tab contextual to what's been |
|
23 // pressed. |
|
24 // |
|
25 // If no link or picture has been long-pressed and the user is in |
|
26 // the CMB - say to see page options or the view switcher--the user |
|
27 // can still press on the Links/Pictures tab. Because there is no |
|
28 // known context to present options, we instead show menu items for |
|
29 // the combined link/image object as grayed out with text on top |
|
30 // approximating this: "Long-press on a web link or picture to use |
|
31 // these actions." |
|
32 |
|
33 function viewMenu_getImageItems(imageUrl) { |
|
34 var items = new Array(); |
|
35 items = |
|
36 [ |
|
37 { |
|
38 "text": window.localeDelegate.translateText("txt_browser_content_view_menu_image_save_image"), // "Save Image", |
|
39 "onclick": function() { |
|
40 if (window.downloads != null) { |
|
41 window.downloads.downloadImage(imageUrl); |
|
42 } else { |
|
43 alert("Save " + imageUrl); |
|
44 } |
|
45 }, |
|
46 }, |
|
47 ] |
|
48 ; |
|
49 return items; |
|
50 } |
|
51 |
|
52 function viewMenu_getLinkItems(linkUrl) { |
|
53 const maxWindowCount = 5; |
|
54 var items = new Array(); |
|
55 items = |
|
56 [ |
|
57 { |
|
58 "text": window.localeDelegate.translateText("txt_browser_content_view_menu_link_open_link"), // "Open Link In New Window", |
|
59 "onclick": function() { |
|
60 pageController.LoadInNewWindow(linkUrl); |
|
61 }, |
|
62 "disabled": (pageController.pageCount() >= maxWindowCount) ? "true" : "false", |
|
63 }, |
|
64 ] |
|
65 ; |
|
66 return items; |
|
67 } |
|
68 |
|
69 function viewMenu_getNavMenuData(current) { |
|
70 return { |
|
71 "text": window.localeDelegate.translateText("txt_browser_content_view_menu_tab_navigation"), // "Navigation", |
|
72 "iconHighlighted": "contextmenu.snippet/icons/nav_selected.png", |
|
73 "icon": "contextmenu.snippet/icons/nav_deselected.png", |
|
74 "current": current ? "true" : "false", |
|
75 "menuItems": |
|
76 [ |
|
77 { |
|
78 "text": window.localeDelegate.translateText("txt_browser_content_view_menu_nav_bookmarks"), |
|
79 "onclick": chrome_showBookmarksView, |
|
80 }, |
|
81 { |
|
82 "text": window.localeDelegate.translateText("txt_browser_content_view_menu_nav_history"), |
|
83 "onclick": chrome_showHistoryView, |
|
84 }, |
|
85 { |
|
86 "text": window.localeDelegate.translateText("txt_browser_content_view_menu_nav_windows"), |
|
87 "onclick": chrome_showWindowsView, |
|
88 }, |
|
89 { |
|
90 "text": window.localeDelegate.translateText("txt_browser_content_view_menu_nav_settings"), |
|
91 "onclick": chrome_showSettingsView, |
|
92 }, |
|
93 ], |
|
94 }; |
|
95 } |
|
96 |
|
97 function viewMenu_getPageMenuData(current) { |
|
98 |
|
99 var popupsBlocked = pageController.getPopupSettings(); |
|
100 return { |
|
101 "text": window.localeDelegate.translateText("txt_browser_content_view_menu_tab_page"), // "Page", |
|
102 "iconHighlighted": "contextmenu.snippet/icons/page_selected.png", |
|
103 "icon": "contextmenu.snippet/icons/page_deselected.png", |
|
104 "current": current ? "true" : "false", |
|
105 "menuItems": |
|
106 [ |
|
107 { |
|
108 "text": window.localeDelegate.translateText("txt_browser_content_view_menu_page_add_bookmark"), // "Add Bookmark", |
|
109 "onclick": function() { |
|
110 launchBookmarkDialog(pageController.currentDocTitle, pageController.currentDocUrl,0), |
|
111 }, |
|
112 }, |
|
113 { |
|
114 "text": (popupsBlocked ) ? |
|
115 window.localeDelegate.translateText("txt_browser_content_view_menu_page_allow_popups"): |
|
116 window.localeDelegate.translateText("txt_browser_content_view_menu_page_block_popups"), // "Block/Allow Pop-ups" |
|
117 "onclick": function() { |
|
118 pageController.savePopupSettings(!popupsBlocked); |
|
119 }, |
|
120 }, |
|
121 { |
|
122 "text": window.localeDelegate.translateText("txt_browser_content_view_menu_page_share"), // "Share", |
|
123 "onclick": function() { |
|
124 var tempUrl = pageController.currentDocUrl; |
|
125 pageController.share(tempUrl); |
|
126 }, |
|
127 }, |
|
128 ], |
|
129 }; |
|
130 } |
|
131 |
|
132 // Return a data structure representing the context menu for the main web view based |
|
133 // on the information in contextEvent. |
|
134 function viewMenu_getWebViewContextMenuData(contextEvent) { |
|
135 var tabList = new Array; |
|
136 var linkUrl = contextEvent != undefined ? contextEvent.linkUrl : ""; |
|
137 var imageUrl = contextEvent != undefined ? contextEvent.imageUrl : ""; |
|
138 var isLink = linkUrl != ""; |
|
139 var isImage = imageUrl != ""; |
|
140 var isLinkOrImage = isLink || isImage; |
|
141 |
|
142 // Build Navigation tab and its menu. |
|
143 if (contextEvent == undefined) |
|
144 tabList = tabList.concat(viewMenu_getNavMenuData(true)); |
|
145 else |
|
146 tabList = tabList.concat(viewMenu_getNavMenuData(false)); |
|
147 |
|
148 var linkMenuItems = new Array(); |
|
149 |
|
150 // Get image related menu items. |
|
151 if (isImage || !isLinkOrImage) |
|
152 linkMenuItems = linkMenuItems.concat(viewMenu_getImageItems(imageUrl)); |
|
153 |
|
154 // Get link related menu items. |
|
155 if (isLink || !isLinkOrImage) |
|
156 linkMenuItems = linkMenuItems.concat(viewMenu_getLinkItems(linkUrl)); |
|
157 |
|
158 linkMenuItems = linkMenuItems.concat( |
|
159 [ |
|
160 { |
|
161 "text": window.localeDelegate.translateText("txt_browser_content_view_menu_link_share_link"), // "Share" |
|
162 "onclick": function() { |
|
163 if ( isImage || !isLinkOrImage) |
|
164 { |
|
165 pageController.share(imageUrl); |
|
166 } |
|
167 else |
|
168 { |
|
169 pageController.share(linkUrl); |
|
170 } |
|
171 }, |
|
172 } |
|
173 ]); |
|
174 |
|
175 // Build image/link tab and menu data. |
|
176 tabList = tabList.concat( { |
|
177 "text": (isLink && isImage || !isLink && !isImage) |
|
178 ? window.localeDelegate.translateText("txt_browser_content_view_menu_tab_link_image") // "Link/image" |
|
179 : (isLink |
|
180 ? window.localeDelegate.translateText("txt_browser_content_view_menu_tab_link") // "Link" |
|
181 : window.localeDelegate.translateText("txt_browser_content_view_menu_tab_image")), // "image" |
|
182 "icon": "contextmenu.snippet/icons/links_deselected.png", |
|
183 "iconHighlighted": "contextmenu.snippet/icons/links_selected.png", |
|
184 "current": isLinkOrImage ? "true" : "false", |
|
185 "disabled": isLinkOrImage ? "false" : "true", |
|
186 "menuItems": linkMenuItems, |
|
187 }); |
|
188 |
|
189 // Build Page tab and menu data. |
|
190 if (contextEvent == undefined) |
|
191 tabList = tabList.concat(viewMenu_getPageMenuData(false)); |
|
192 else |
|
193 tabList = tabList.concat(viewMenu_getPageMenuData(!isLinkOrImage)); |
|
194 |
|
195 // Combine the tabs and return them. |
|
196 return { |
|
197 "tabs": tabList |
|
198 }; |
|
199 } |
|
200 |
|
201 // Initialize the WebView's context menu. |
|
202 function viewMenu_initWebViewContextMenu() { |
|
203 // Connect to the view's contextEvent signal. |
|
204 views.WebView.contextEvent.connect( |
|
205 function(contextEvent) { |
|
206 //printProp(contextEvent); |
|
207 cm_TheContextMenu.show(viewMenu_getWebViewContextMenuData(contextEvent)); |
|
208 } |
|
209 ); |
|
210 } |
|
211 |
|
212 chrome.chromeComplete.connect(viewMenu_initWebViewContextMenu); |