| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 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 */ | |
| 10 | |
| 11 'use strict'; | |
| 12 | |
| 13 /** @suppress {duplicate} */ | |
| 14 var remoting = remoting || {}; | |
| 15 | |
| 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 | |
| 21 */ | |
| 22 remoting.SubmenuManager = function(adapter, title, checkable) { | |
| 23 /** @private {remoting.ContextMenuAdapter} */ | |
| 24 this.adapter_ = adapter; | |
| 25 /** @private {string} */ | |
| 26 this.title_ = title; | |
| 27 /** @private {boolean} */ | |
| 28 this.checkable_ = checkable; | |
| 29 /** @private {!Object} */ | |
| 30 this.childIds_ = {}; | |
| 31 /** @private {string} */ | |
| 32 this.parentId_ = ''; | |
| 33 }; | |
| 34 | |
| 35 /** | |
| 36 * Add a submenu item, or update the title of an existing submenu item. | |
| 37 * | |
| 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. | |
| 42 */ | |
| 43 remoting.SubmenuManager.prototype.add = function(id, title) { | |
| 44 if (id in this.childIds_) { | |
| 45 this.adapter_.updateTitle(id, title); | |
| 46 return false; | |
| 47 } else { | |
| 48 this.childIds_[id] = true; | |
| 49 this.addOrRemoveParent_(); | |
| 50 this.adapter_.create(id, title, this.checkable_, this.parentId_); | |
| 51 return true; | |
| 52 } | |
| 53 }; | |
| 54 | |
| 55 /** | |
| 56 * Remove a submenu item. | |
| 57 * | |
| 58 * @param {string} id The child id. | |
| 59 */ | |
| 60 remoting.SubmenuManager.prototype.remove = function(id) { | |
| 61 this.adapter_.remove(id); | |
| 62 delete this.childIds_[id]; | |
| 63 this.addOrRemoveParent_(); | |
| 64 }; | |
| 65 | |
| 66 /** | |
| 67 * Remove all submenu items. | |
| 68 */ | |
| 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]); | |
| 73 } | |
| 74 }; | |
| 75 | |
| 76 /** | |
| 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. | |
| 79 * | |
| 80 * @private | |
| 81 */ | |
| 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); | |
| 87 } | |
| 88 } else { | |
| 89 if (this.parentId_) { | |
| 90 this.adapter_.remove(this.parentId_); | |
| 91 this.parentId_ = ''; | |
| 92 } | |
| 93 } | |
| 94 }; | |
| OLD | NEW |