OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 cr.define('options', function() { |
| 6 var OptionsPage = options.OptionsPage; |
| 7 var ArrayDataModel = cr.ui.ArrayDataModel; |
| 8 |
| 9 const localStrings = new LocalStrings(); |
| 10 |
| 11 /** |
| 12 * ManageProfileOverlay class |
| 13 * Encapsulated handling of the 'Manage profile...' overlay page. |
| 14 * @constructor |
| 15 * @class |
| 16 */ |
| 17 function ManageProfileOverlay() { |
| 18 OptionsPage.call(this, |
| 19 'manageProfile', |
| 20 templateData.manageProfileOverlayTabTitle, |
| 21 'manage-profile-overlay'); |
| 22 }; |
| 23 |
| 24 cr.addSingletonGetter(ManageProfileOverlay); |
| 25 |
| 26 ManageProfileOverlay.prototype = { |
| 27 // Inherit from OptionsPage. |
| 28 __proto__: OptionsPage.prototype, |
| 29 |
| 30 // Info about the currently managed/deleted profile. |
| 31 profileInfo_: null, |
| 32 |
| 33 // An object containing all known profile names. |
| 34 profileNames_: {}, |
| 35 |
| 36 /** |
| 37 * Initialize the page. |
| 38 */ |
| 39 initializePage: function() { |
| 40 // Call base class implementation to start preference initialization. |
| 41 OptionsPage.prototype.initializePage.call(this); |
| 42 |
| 43 var self = this; |
| 44 var iconGrid = $('manage-profile-icon-grid'); |
| 45 options.ProfilesIconGrid.decorate(iconGrid); |
| 46 |
| 47 $('manage-profile-name').oninput = this.onNameChanged_.bind(this); |
| 48 $('manage-profile-cancel').onclick = |
| 49 $('delete-profile-cancel').onclick = function(event) { |
| 50 OptionsPage.closeOverlay(); |
| 51 }; |
| 52 $('manage-profile-ok').onclick = function(event) { |
| 53 OptionsPage.closeOverlay(); |
| 54 self.submitManageChanges_(); |
| 55 }; |
| 56 $('delete-profile-ok').onclick = function(event) { |
| 57 OptionsPage.closeOverlay(); |
| 58 chrome.send('deleteProfile', [self.profileInfo_.filePath]); |
| 59 }; |
| 60 }, |
| 61 |
| 62 /** @inheritDoc */ |
| 63 didShowPage: function() { |
| 64 $('manage-profile-icon-grid').redraw(); |
| 65 $('manage-profile-name').focus(); |
| 66 }, |
| 67 |
| 68 /** |
| 69 * Set the profile info used in the dialog. |
| 70 * @param {Object} profileInfo An object of the form: |
| 71 * profileInfo = { |
| 72 * name: "Profile Name", |
| 73 * iconURL: "chrome://path/to/icon/image", |
| 74 * filePath: "/path/to/profile/data/on/disk" |
| 75 * isCurrentProfile: false, |
| 76 * }; |
| 77 * @private |
| 78 */ |
| 79 setProfileInfo_: function(profileInfo) { |
| 80 this.profileInfo_ = profileInfo; |
| 81 $('manage-profile-name').value = profileInfo.name; |
| 82 $('manage-profile-icon-grid').selectedItem = profileInfo.iconURL; |
| 83 }, |
| 84 |
| 85 /** |
| 86 * Set an array of default icon URLs. These will be added to the grid that |
| 87 * the user will use to choose their profile icon. |
| 88 * @param {Array.<string>} iconURLs An array of icon URLs. |
| 89 * @private |
| 90 */ |
| 91 receiveDefaultProfileIcons_: function(iconURLs) { |
| 92 $('manage-profile-icon-grid').dataModel = new ArrayDataModel(iconURLs); |
| 93 }, |
| 94 |
| 95 /** |
| 96 * Set a dictionary of all profile names. These are used to prevent the |
| 97 * user from naming two profiles the same. |
| 98 * @param {Object} profileNames A dictionary of profile names. |
| 99 * @private |
| 100 */ |
| 101 receiveProfileNames_: function(profileNames) { |
| 102 this.profileNames_ = profileNames; |
| 103 }, |
| 104 |
| 105 /** |
| 106 * Determine whether |name| is valid; i.e. not equal to any other profile |
| 107 * name. |
| 108 * @param {string} name The profile name to validate. |
| 109 * @return true if the name is not equal to any other profile name. |
| 110 * @private |
| 111 */ |
| 112 isNameValid_: function(name) { |
| 113 // if the name hasn't changed, assume it is valid. |
| 114 if (name == this.profileInfo_.name) |
| 115 return true; |
| 116 |
| 117 return this.profileNames_[name] == undefined; |
| 118 }, |
| 119 |
| 120 /** |
| 121 * Update the UI elements accordingly if the profile name is valid/invalid. |
| 122 * @param {boolean} isValid True if the UI should be updated as if the name |
| 123 * were valid. |
| 124 * @private |
| 125 */ |
| 126 setNameIsValid_: function(isValid) { |
| 127 var dupeNameErrorEl = $('manage-profile-duplicate-name-error'); |
| 128 if (isValid) |
| 129 dupeNameErrorEl.classList.add('hiding'); |
| 130 else |
| 131 dupeNameErrorEl.classList.remove('hiding'); |
| 132 |
| 133 $('manage-profile-ok').disabled = !isValid; |
| 134 }, |
| 135 |
| 136 /** |
| 137 * oninput callback for <input> field. |
| 138 * @param event The event object |
| 139 * @private |
| 140 */ |
| 141 onNameChanged_: function(event) { |
| 142 this.setNameIsValid_(this.isNameValid_(event.target.value)); |
| 143 }, |
| 144 |
| 145 /** |
| 146 * Called when the user clicks "OK". Saves the newly changed profile info. |
| 147 * @private |
| 148 */ |
| 149 submitManageChanges_: function() { |
| 150 var name = $('manage-profile-name').value; |
| 151 var iconURL = $('manage-profile-icon-grid').selectedItem; |
| 152 chrome.send('setProfileNameAndIcon', |
| 153 [this.profileInfo_.filePath, name, iconURL]); |
| 154 }, |
| 155 |
| 156 /** |
| 157 * Display the "Manage Profile" dialog. |
| 158 * @param {Object} profileInfo The profile object of the profile to manage. |
| 159 * @private |
| 160 */ |
| 161 showManageDialog_: function(profileInfo) { |
| 162 ManageProfileOverlay.setProfileInfo(profileInfo); |
| 163 $('manage-profile-overlay-manage').hidden = false; |
| 164 $('manage-profile-overlay-delete').hidden = true; |
| 165 ManageProfileOverlay.getInstance().setNameIsValid_(true); |
| 166 |
| 167 // Intentionally don't show the URL in the location bar as we don't want |
| 168 // people trying to navigate here by hand. |
| 169 OptionsPage.showPageByName('manageProfile', false); |
| 170 }, |
| 171 |
| 172 /** |
| 173 * Display the "Delete Profile" dialog. |
| 174 * @param {Object} profileInfo The profile object of the profile to delete. |
| 175 * @private |
| 176 */ |
| 177 showDeleteDialog_: function(profileInfo) { |
| 178 ManageProfileOverlay.setProfileInfo(profileInfo); |
| 179 $('manage-profile-overlay-manage').hidden = true; |
| 180 $('manage-profile-overlay-delete').hidden = false; |
| 181 $('delete-profile-message').textContent = |
| 182 localStrings.getStringF('deleteProfileMessage', profileInfo.name); |
| 183 |
| 184 // Intentionally don't show the URL in the location bar as we don't want |
| 185 // people trying to navigate here by hand. |
| 186 OptionsPage.showPageByName('manageProfile', false); |
| 187 }, |
| 188 }; |
| 189 |
| 190 // Forward public APIs to private implementations. |
| 191 [ |
| 192 'receiveDefaultProfileIcons', |
| 193 'receiveProfileNames', |
| 194 'setProfileInfo', |
| 195 'showManageDialog', |
| 196 'showDeleteDialog', |
| 197 ].forEach(function(name) { |
| 198 ManageProfileOverlay[name] = function(value) { |
| 199 ManageProfileOverlay.getInstance()[name + '_'](value); |
| 200 }; |
| 201 }); |
| 202 |
| 203 // Export |
| 204 return { |
| 205 ManageProfileOverlay: ManageProfileOverlay |
| 206 }; |
| 207 }); |
OLD | NEW |