Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / app_remoting / js / submenu_manager.js
blobd5f4a8f6e91d5bf127f8be6486b64a3c0a3044d8
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.
5 /**
6 * @fileoverview
7 * Helper class for submenus that should add or remove the parent menu entry
8 * depending on whether or not any child items exist.
9 */
11 'use strict';
13 /** @suppress {duplicate} */
14 var remoting = remoting || {};
16 /**
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.
20 * @constructor
22 remoting.SubmenuManager = function(adapter, title, checkable) {
23 /**
24 * @type {remoting.ContextMenuAdapter}
25 * @private
27 this.adapter_ = adapter;
28 /**
29 * @type {string}
30 * @private
32 this.title_ = title;
33 /**
34 * @type {boolean}
35 * @private
37 this.checkable_ = checkable;
38 /**
39 * @type {!Object}
40 * @private
42 this.childIds_ = {};
43 /**
44 * @type {string}
45 * @private
47 this.parentId_ = '';
50 /**
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);
61 return false;
62 } else {
63 this.childIds_[id] = true;
64 this.addOrRemoveParent_();
65 this.adapter_.create(id, title, this.checkable_, this.parentId_);
66 return true;
70 /**
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_();
81 /**
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]);
91 /**
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.
95 * @private
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);
103 } else {
104 if (this.parentId_) {
105 this.adapter_.remove(this.parentId_);
106 this.parentId_ = '';