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.
7 * Helper class for submenus that should add or remove the parent menu entry
8 * depending on whether or not any child items exist.
13 /** @suppress {duplicate} */
14 var remoting
= remoting
|| {};
17 * @param {remoting.ContextMenuAdapter} adapter
18 * @param {string} title The title of the parent menu item.
19 * @param {boolean} checkable True if the child menus should be checkable.
22 remoting
.SubmenuManager = function(adapter
, title
, checkable
) {
23 /** @private {remoting.ContextMenuAdapter} */
24 this.adapter_
= adapter
;
25 /** @private {string} */
27 /** @private {boolean} */
28 this.checkable_
= checkable
;
29 /** @private {!Object} */
31 /** @private {string} */
36 * Add a submenu item, or update the title of an existing submenu item.
38 * @param {string} id The child id.
39 * @param {string} title The window title.
40 * @return {boolean} True if a new menu item was created, false if an existing
41 * menu item was renamed.
43 remoting
.SubmenuManager
.prototype.add = function(id
, title
) {
44 if (id
in this.childIds_
) {
45 this.adapter_
.updateTitle(id
, title
);
48 this.childIds_
[id
] = true;
49 this.addOrRemoveParent_();
50 this.adapter_
.create(id
, title
, this.checkable_
, this.parentId_
);
56 * Remove a submenu item.
58 * @param {string} id The child id.
60 remoting
.SubmenuManager
.prototype.remove = function(id
) {
61 this.adapter_
.remove(id
);
62 delete this.childIds_
[id
];
63 this.addOrRemoveParent_();
67 * Remove all submenu items.
69 remoting
.SubmenuManager
.prototype.removeAll = function() {
70 var submenus
= Object
.keys(this.childIds_
);
71 for (var i
= 0; i
< submenus
.length
; ++i
) {
72 this.remove(submenus
[i
]);
77 * Add the parent menu item if it doesn't exist but there are submenus items,
78 * or remove it if it exists but there are no submenus.
82 remoting
.SubmenuManager
.prototype.addOrRemoveParent_ = function() {
83 if (Object
.getOwnPropertyNames(this.childIds_
).length
!= 0) {
84 if (!this.parentId_
) {
85 this.parentId_
= base
.generateXsrfToken(); // Use a random id
86 this.adapter_
.create(this.parentId_
, this.title_
, false);
90 this.adapter_
.remove(this.parentId_
);