Bug 1121463 - Support copy link on context menu. r=kanru

Add a special menuitem, copy-link, to pass the url and do command.
This commit is contained in:
Boris Chiou 2015-08-31 20:37:00 -04:00
Родитель 288dfa7ea3
Коммит b5773495c8
2 изменённых файлов: 27 добавлений и 8 удалений

Просмотреть файл

@ -64,6 +64,7 @@ const COMMAND_MAP = {
'cut': 'cmd_cut',
'copy': 'cmd_copyAndCollapseToEnd',
'copyImage': 'cmd_copyImage',
'copyLink': 'cmd_copyLink',
'paste': 'cmd_paste',
'selectall': 'cmd_selectAll'
};
@ -865,7 +866,13 @@ BrowserElementChild.prototype = {
var elem = e.target;
var menuData = {systemTargets: [], contextmenu: null};
var ctxMenuId = null;
var hasImgElement = false;
var copyableElements = {
image: false,
link: false,
hasElements: function() {
return this.image || this.link;
}
};
// Set the event target as the copy image command needs it to
// determine what was context-clicked on.
@ -884,20 +891,22 @@ BrowserElementChild.prototype = {
ctxMenuId = elem.getAttribute('contextmenu');
}
// Enable copy image option
// Enable copy image/link option
if (elem.nodeName == 'IMG') {
hasImgElement = true;
copyableElements.image = true;
} else if (elem.nodeName == 'A') {
copyableElements.link = true;
}
elem = elem.parentNode;
}
if (ctxMenuId || hasImgElement) {
if (ctxMenuId || copyableElements.hasElements()) {
var menu = null;
if (ctxMenuId) {
menu = e.target.ownerDocument.getElementById(ctxMenuId);
}
menuData.contextmenu = this._buildMenuObj(menu, '', hasImgElement);
menuData.contextmenu = this._buildMenuObj(menu, '', copyableElements);
}
// Pass along the position where the context menu should be located
@ -1235,6 +1244,10 @@ BrowserElementChild.prototype = {
// Set command
data.json.command = 'copyImage';
this._recvDoCommand(data);
} else if (data.json.menuitem == 'copy-link') {
// Set command
data.json.command = 'copyLink';
this._recvDoCommand(data);
} else if (data.json.menuitem in this._ctxHandlers) {
this._ctxHandlers[data.json.menuitem].click();
this._ctxHandlers = {};
@ -1244,7 +1257,7 @@ BrowserElementChild.prototype = {
}
},
_buildMenuObj: function(menu, idPrefix, hasImgElement) {
_buildMenuObj: function(menu, idPrefix, copyableElements) {
var menuObj = {type: 'menu', items: []};
// Customized context menu
if (menu) {
@ -1263,8 +1276,14 @@ BrowserElementChild.prototype = {
}
}
}
// Note: Display "Copy Link" first in order to make sure "Copy Image" is
// put together with other image options if elem is an image link.
// "Copy Link" menu item
if (copyableElements.link) {
menuObj.items.push({id: 'copy-link'});
}
// "Copy Image" menu item
if (hasImgElement) {
if (copyableElements.image) {
menuObj.items.push({id: 'copy-image'});
}

Просмотреть файл

@ -24,7 +24,7 @@ function checkEmptyContextMenu() {
function checkInnerContextMenu() {
sendContextMenuTo('#inner-link', function onContextMenu(detail) {
is(detail.systemTargets.length, 1, 'Includes anchor data');
is(detail.contextmenu.items.length, 2, 'Inner clicks trigger correct menu');
is(detail.contextmenu.items.length, 3, 'Inner clicks trigger correct menu');
var target = detail.systemTargets[0];
is(target.nodeName, 'A', 'Reports correct nodeName');
is(target.data.uri, 'foo.html', 'Reports correct uri');