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
) {
24 * @type {remoting.ContextMenuAdapter}
27 this.adapter_
= adapter
;
37 this.checkable_
= checkable
;
51 * Add a submenu item, or update the title of an existing submenu item.
53 * @param {string} id The child id.
54 * @param {string} title The window title.
55 * @return {boolean} True if a new menu item was created, false if an existing
56 * menu item was renamed.
58 remoting
.SubmenuManager
.prototype.add = function(id
, title
) {
59 if (id
in this.childIds_
) {
60 this.adapter_
.updateTitle(id
, title
);
63 this.childIds_
[id
] = true;
64 this.addOrRemoveParent_();
65 this.adapter_
.create(id
, title
, this.checkable_
, this.parentId_
);
71 * Remove a submenu item.
73 * @param {string} id The child id.
75 remoting
.SubmenuManager
.prototype.remove = function(id
) {
76 this.adapter_
.remove(id
);
77 delete this.childIds_
[id
];
78 this.addOrRemoveParent_();
82 * Remove all submenu items.
84 remoting
.SubmenuManager
.prototype.removeAll = function() {
85 var submenus
= Object
.keys(this.childIds_
);
86 for (var i
= 0; i
< submenus
.length
; ++i
) {
87 this.remove(submenus
[i
]);
92 * Add the parent menu item if it doesn't exist but there are submenus items,
93 * or remove it if it exists but there are no submenus.
97 remoting
.SubmenuManager
.prototype.addOrRemoveParent_ = function() {
98 if (Object
.getOwnPropertyNames(this.childIds_
).length
!= 0) {
99 if (!this.parentId_
) {
100 this.parentId_
= base
.generateXsrfToken(); // Use a random id
101 this.adapter_
.create(this.parentId_
, this.title_
, false);
104 if (this.parentId_
) {
105 this.adapter_
.remove(this.parentId_
);