OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All Rights Reserved. | 2 * Copyright (C) 2011 Google Inc. All Rights Reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 30 matching lines...) Expand all Loading... |
41 * @param {number} x | 41 * @param {number} x |
42 * @param {number} y | 42 * @param {number} y |
43 */ | 43 */ |
44 show: function(x, y) | 44 show: function(x, y) |
45 { | 45 { |
46 this._x = x; | 46 this._x = x; |
47 this._y = y; | 47 this._y = y; |
48 this._time = new Date().getTime(); | 48 this._time = new Date().getTime(); |
49 | 49 |
50 // Create context menu. | 50 // Create context menu. |
51 this._contextMenuElement = document.createElementWithClass("div", "soft-
context-menu"); | 51 this._contextMenuElement = createElementWithClass("div", "soft-context-m
enu"); |
52 this._contextMenuElement.tabIndex = 0; | 52 this._contextMenuElement.tabIndex = 0; |
53 this._contextMenuElement.style.top = y + "px"; | 53 this._contextMenuElement.style.top = y + "px"; |
54 this._contextMenuElement.style.left = x + "px"; | 54 this._contextMenuElement.style.left = x + "px"; |
55 | 55 |
56 this._contextMenuElement.addEventListener("mouseup", consumeEvent, false
); | 56 this._contextMenuElement.addEventListener("mouseup", consumeEvent, false
); |
57 this._contextMenuElement.addEventListener("keydown", this._menuKeyDown.b
ind(this), false); | 57 this._contextMenuElement.addEventListener("keydown", this._menuKeyDown.b
ind(this), false); |
58 | 58 |
59 for (var i = 0; i < this._items.length; ++i) | 59 for (var i = 0; i < this._items.length; ++i) |
60 this._contextMenuElement.appendChild(this._createMenuItem(this._item
s[i])); | 60 this._contextMenuElement.appendChild(this._createMenuItem(this._item
s[i])); |
61 | 61 |
62 // Install glass pane capturing events. | 62 // Install glass pane capturing events. |
63 if (!this._parentMenu) { | 63 if (!this._parentMenu) { |
64 this._glassPaneElement = document.createElementWithClass("div", "sof
t-context-menu-glass-pane"); | 64 this._glassPaneElement = createElementWithClass("div", "soft-context
-menu-glass-pane"); |
65 this._glassPaneElement.tabIndex = 0; | 65 this._glassPaneElement.tabIndex = 0; |
66 this._glassPaneElement.addEventListener("mouseup", this._glassPaneMo
useUp.bind(this), false); | 66 this._glassPaneElement.addEventListener("mouseup", this._glassPaneMo
useUp.bind(this), false); |
67 this._glassPaneElement.appendChild(this._contextMenuElement); | 67 this._glassPaneElement.appendChild(this._contextMenuElement); |
68 document.body.appendChild(this._glassPaneElement); | 68 document.body.appendChild(this._glassPaneElement); |
69 this._focus(); | 69 this._focus(); |
70 } else { | 70 } else { |
71 this._parentMenu._parentGlassPaneElement().appendChild(this._context
MenuElement); | 71 this._parentMenu._parentGlassPaneElement().appendChild(this._context
MenuElement); |
72 } | 72 } |
73 | 73 |
74 // Re-position menu in case it does not fit. | 74 // Re-position menu in case it does not fit. |
(...skipping 13 matching lines...) Expand all Loading... |
88 }, | 88 }, |
89 | 89 |
90 _createMenuItem: function(item) | 90 _createMenuItem: function(item) |
91 { | 91 { |
92 if (item.type === "separator") | 92 if (item.type === "separator") |
93 return this._createSeparator(); | 93 return this._createSeparator(); |
94 | 94 |
95 if (item.type === "subMenu") | 95 if (item.type === "subMenu") |
96 return this._createSubMenu(item); | 96 return this._createSubMenu(item); |
97 | 97 |
98 var menuItemElement = document.createElementWithClass("div", "soft-conte
xt-menu-item"); | 98 var menuItemElement = createElementWithClass("div", "soft-context-menu-i
tem"); |
99 var checkMarkElement = menuItemElement.createChild("span", "soft-context
-menu-item-checkmark"); | 99 var checkMarkElement = menuItemElement.createChild("span", "soft-context
-menu-item-checkmark"); |
100 checkMarkElement.textContent = "\u2713 "; // Checkmark Unicode symbol | 100 checkMarkElement.textContent = "\u2713 "; // Checkmark Unicode symbol |
101 if (!item.checked) | 101 if (!item.checked) |
102 checkMarkElement.style.opacity = "0"; | 102 checkMarkElement.style.opacity = "0"; |
103 | 103 |
104 menuItemElement.createTextChild(item.label); | 104 menuItemElement.createTextChild(item.label); |
105 | 105 |
106 menuItemElement.addEventListener("mousedown", this._menuItemMouseDown.bi
nd(this), false); | 106 menuItemElement.addEventListener("mousedown", this._menuItemMouseDown.bi
nd(this), false); |
107 menuItemElement.addEventListener("mouseup", this._menuItemMouseUp.bind(t
his), false); | 107 menuItemElement.addEventListener("mouseup", this._menuItemMouseUp.bind(t
his), false); |
108 | 108 |
109 // Manually manage hover highlight since :hover does not work in case of
click-and-hold menu invocation. | 109 // Manually manage hover highlight since :hover does not work in case of
click-and-hold menu invocation. |
110 menuItemElement.addEventListener("mouseover", this._menuItemMouseOver.bi
nd(this), false); | 110 menuItemElement.addEventListener("mouseover", this._menuItemMouseOver.bi
nd(this), false); |
111 menuItemElement.addEventListener("mouseout", this._menuItemMouseOut.bind
(this), false); | 111 menuItemElement.addEventListener("mouseout", this._menuItemMouseOut.bind
(this), false); |
112 | 112 |
113 menuItemElement._actionId = item.id; | 113 menuItemElement._actionId = item.id; |
114 return menuItemElement; | 114 return menuItemElement; |
115 }, | 115 }, |
116 | 116 |
117 _createSubMenu: function(item) | 117 _createSubMenu: function(item) |
118 { | 118 { |
119 var menuItemElement = document.createElementWithClass("div", "soft-conte
xt-menu-item"); | 119 var menuItemElement = createElementWithClass("div", "soft-context-menu-i
tem"); |
120 menuItemElement._subItems = item.subItems; | 120 menuItemElement._subItems = item.subItems; |
121 | 121 |
122 // Occupy the same space on the left in all items. | 122 // Occupy the same space on the left in all items. |
123 var checkMarkElement = menuItemElement.createChild("span", "soft-context
-menu-item-checkmark"); | 123 var checkMarkElement = menuItemElement.createChild("span", "soft-context
-menu-item-checkmark"); |
124 checkMarkElement.textContent = "\u2713 "; // Checkmark Unicode symbol | 124 checkMarkElement.textContent = "\u2713 "; // Checkmark Unicode symbol |
125 checkMarkElement.style.opacity = "0"; | 125 checkMarkElement.style.opacity = "0"; |
126 | 126 |
127 menuItemElement.createTextChild(item.label); | 127 menuItemElement.createTextChild(item.label); |
128 | 128 |
129 var subMenuArrowElement = menuItemElement.createChild("span", "soft-cont
ext-menu-item-submenu-arrow"); | 129 var subMenuArrowElement = menuItemElement.createChild("span", "soft-cont
ext-menu-item-submenu-arrow"); |
130 subMenuArrowElement.textContent = "\u25B6"; // BLACK RIGHT-POINTING TRIA
NGLE | 130 subMenuArrowElement.textContent = "\u25B6"; // BLACK RIGHT-POINTING TRIA
NGLE |
131 | 131 |
132 menuItemElement.addEventListener("mousedown", this._menuItemMouseDown.bi
nd(this), false); | 132 menuItemElement.addEventListener("mousedown", this._menuItemMouseDown.bi
nd(this), false); |
133 menuItemElement.addEventListener("mouseup", this._menuItemMouseUp.bind(t
his), false); | 133 menuItemElement.addEventListener("mouseup", this._menuItemMouseUp.bind(t
his), false); |
134 | 134 |
135 // Manually manage hover highlight since :hover does not work in case of
click-and-hold menu invocation. | 135 // Manually manage hover highlight since :hover does not work in case of
click-and-hold menu invocation. |
136 menuItemElement.addEventListener("mouseover", this._menuItemMouseOver.bi
nd(this), false); | 136 menuItemElement.addEventListener("mouseover", this._menuItemMouseOver.bi
nd(this), false); |
137 menuItemElement.addEventListener("mouseout", this._menuItemMouseOut.bind
(this), false); | 137 menuItemElement.addEventListener("mouseout", this._menuItemMouseOut.bind
(this), false); |
138 | 138 |
139 return menuItemElement; | 139 return menuItemElement; |
140 }, | 140 }, |
141 | 141 |
142 _createSeparator: function() | 142 _createSeparator: function() |
143 { | 143 { |
144 var separatorElement = document.createElementWithClass("div", "soft-cont
ext-menu-separator"); | 144 var separatorElement = createElementWithClass("div", "soft-context-menu-
separator"); |
145 separatorElement._isSeparator = true; | 145 separatorElement._isSeparator = true; |
146 separatorElement.addEventListener("mouseover", this._hideSubMenu.bind(th
is), false); | 146 separatorElement.addEventListener("mouseover", this._hideSubMenu.bind(th
is), false); |
147 separatorElement.createChild("div", "separator-line"); | 147 separatorElement.createChild("div", "separator-line"); |
148 return separatorElement; | 148 return separatorElement; |
149 }, | 149 }, |
150 | 150 |
151 _menuItemMouseDown: function(event) | 151 _menuItemMouseDown: function(event) |
152 { | 152 { |
153 // Do not let separator's mouse down hit menu's handler - we need to rec
eive mouse up! | 153 // Do not let separator's mouse down hit menu's handler - we need to rec
eive mouse up! |
154 event.consume(true); | 154 event.consume(true); |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 | 336 |
337 _discardSubMenus: function() | 337 _discardSubMenus: function() |
338 { | 338 { |
339 if (this._subMenu) | 339 if (this._subMenu) |
340 this._subMenu._discardSubMenus(); | 340 this._subMenu._discardSubMenus(); |
341 this._contextMenuElement.remove(); | 341 this._contextMenuElement.remove(); |
342 if (this._parentMenu) | 342 if (this._parentMenu) |
343 delete this._parentMenu._subMenu; | 343 delete this._parentMenu._subMenu; |
344 } | 344 } |
345 } | 345 } |
OLD | NEW |