Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(505)

Side by Side Diff: chrome/browser/resources/settings/people_page/change_picture.js

Issue 1615093003: Settings People Revamp: Switch Change Picture to use iron-selector. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@0073-settings-add-preview-pic-and-camera-button
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 cr.define('settings_test', function() { 5 cr.define('settings_test', function() {
6 var changePictureOptions = settings_test.changePictureOptions || { 6 var changePictureOptions = settings_test.changePictureOptions || {
7 /** 7 /**
8 * True if property changes should fire events for testing purposes. 8 * True if property changes should fire events for testing purposes.
9 * @type {boolean} 9 * @type {boolean}
10 */ 10 */
(...skipping 12 matching lines...) Expand all
23 */ 23 */
24 Polymer({ 24 Polymer({
25 is: 'settings-change-picture', 25 is: 'settings-change-picture',
26 26
27 behaviors: [ 27 behaviors: [
28 I18nBehavior, 28 I18nBehavior,
29 ], 29 ],
30 30
31 properties: { 31 properties: {
32 /** 32 /**
33 * The currently selected profile image URL. May be a data URL. 33 * The currently selected item. This property is bound to the iron-selector
34 * @private {string} 34 * and never directly assigned.
35 * @private {Element}
35 */ 36 */
36 selectedImageUrl_: { 37 selectedItem_: {
37 type: String, 38 type: Element,
38 notify: settings_test.changePictureOptions.notifyPropertyChangesForTest, 39 notify: settings_test.changePictureOptions.notifyPropertyChangesForTest,
39 }, 40 },
40 41
41 /** 42 /**
42 * True only if the user has selected the camera icon. Old photos captured
43 * from the camera are represented as the 'old' image.
44 * @private {boolean}
45 */
46 cameraActive_: {
47 type: Boolean,
48 value: false,
49 },
50
51 /**
52 * This differs from its default value only if the user has just captured 43 * This differs from its default value only if the user has just captured
53 * a new photo from the camera. 44 * a new photo from the camera.
54 * @private {string} 45 * @private {!string}
dpapad 2016/01/22 02:35:32 Nit, "!" is implied with string, number and boolea
tommycli 2016/01/22 19:24:40 Done. Oops! Thanks!
55 */ 46 */
56 cameraImageUrl_: { 47 cameraImageUrl_: {
57 type: String, 48 type: String,
58 value: settings.ChangePicturePrivateApi.ButtonImages.TAKE_PHOTO, 49 value: settings.ChangePicturePrivateApi.ButtonImages.TAKE_PHOTO,
59 }, 50 },
60 51
61 /** 52 /**
62 * This differs from its default value only if the user has just captured 53 * This differs from its default value only if the user has just captured
63 * a new photo from the camera. 54 * a new photo from the camera.
64 * @private {string} 55 * @private {!string}
65 */ 56 */
66 cameraImageTitle_: { 57 cameraImageTitle_: {
67 type: String, 58 type: String,
68 value: function() { return loadTimeData.getString('takePhoto'); }, 59 value: function() { return loadTimeData.getString('takePhoto'); },
69 }, 60 },
70 61
71 /** 62 /**
72 * The url of the 'old' image, which is the existing image sourced from 63 * The url of the 'old' image, which is the existing image sourced from
73 * the camera, a file, or a deprecated default image. 64 * the camera, a file, or a deprecated default image. It defaults to an
74 * @private {string} 65 * empty string instead of undefined, because Polymer bindings don't behave
66 * as expected with undefined properties.
67 * @private {!string}
75 */ 68 */
76 oldImageUrl_: String, 69 oldImageUrl_: {
70 type: String,
71 value: '',
72 },
77 73
78 /** 74 /**
79 * The url of the profile image. 75 * The url of the profile image.
80 * @private {string} 76 * @private {!string}
81 */ 77 */
82 profileImageUrl_: { 78 profileImageUrl_: {
83 type: String, 79 type: String,
84 value: settings.ChangePicturePrivateApi.ButtonImages.PROFILE_PICTURE, 80 value: settings.ChangePicturePrivateApi.ButtonImages.PROFILE_PICTURE,
85 }, 81 },
86 82
87 /** 83 /**
88 * The default user images. Populated by ChangePicturePrivateApi. 84 * The default user images. Populated by ChangePicturePrivateApi.
89 * @private {!Array<!settings.DefaultImage>} 85 * @private {!Array<!settings.DefaultImage>}
90 */ 86 */
91 defaultImages_: { 87 defaultImages_: {
92 type: Array, 88 type: Array,
93 value: function() { return []; }, 89 value: function() { return []; },
94 }, 90 },
95 }, 91 },
96 92
97 /** @override */ 93 /** @override */
98 attached: function() { 94 attached: function() {
99 // This is the interface called by the C++ handler. 95 // This is the interface called by the C++ handler.
100 var nativeInterface = { 96 var nativeInterface = {
101 /** 97 /**
102 * Called from C++ to provide the default set of images. 98 * Called from C++ to provide the default set of images.
103 * @param {!Array<!settings.DefaultImage>} images 99 * @param {!Array<!settings.DefaultImage>} images
104 */ 100 */
105 receiveDefaultImages: function(images) { 101 receiveDefaultImages: function(images) {
106 this.defaultImages_ = images; 102 this.defaultImages_ = images;
107 }.bind(this), 103 }.bind(this),
108 104
109 /** 105 /**
110 * Called from C++ to provide the URL of the selected image. 106 * Called from C++ to provide the URL of the selected image. Is only
107 * called with default images.
111 * @param {string} imageUrl 108 * @param {string} imageUrl
112 */ 109 */
113 receiveSelectedImage: function(imageUrl) { 110 receiveSelectedImage: function(imageUrl) {
114 this.cameraActive_ = false; 111 var images = this.$.selector.items;
dpapad 2016/01/22 02:35:32 Nit (optional): You can also do it as follows (I t
tommycli 2016/01/22 19:24:40 Done.
115 this.selectedImageUrl_ = imageUrl; 112 for (var i = 0; i < images.length; i++) {
113 if (images[i].dataset.type == 'default' &&
114 images[i].src == imageUrl) {
115 this.$.selector.select(i);
116 return;
117 }
118 }
119 assertNotReached('Default image with URL ' + imageUrl + ' not found.');
116 }.bind(this), 120 }.bind(this),
117 121
118 /** 122 /**
119 * Called from C++ to provide the URL of the 'old' image. The 'old' 123 * Called from C++ to provide the URL of the 'old' image. The 'old'
120 * image is any selected non-profile and non-default image. It can be 124 * image is any selected non-profile and non-default image. It can be
121 * from the camera, a file, or a deprecated default image. When this 125 * from the camera, a file, or a deprecated default image. When this
122 * method is called, it's implied that the old image is selected. 126 * method is called, it's implied that the old image is selected.
123 * @param {string} imageUrl 127 * @param {string} imageUrl
124 */ 128 */
125 receiveOldImage: function(imageUrl) { 129 receiveOldImage: function(imageUrl) {
126 this.cameraActive_ = false;
127 this.oldImageUrl_ = imageUrl; 130 this.oldImageUrl_ = imageUrl;
128 this.selectedImageUrl_ = imageUrl; 131 this.$.selector.select(this.$.selector.indexOf(this.$.oldImage));
dpapad 2016/01/22 02:35:32 iron-selector#indexOf returns a number, but iron-s
tommycli 2016/01/22 19:24:40 Yes... exactly. The selection key can be a string
129 }.bind(this), 132 }.bind(this),
130 133
131 /** 134 /**
132 * Called from C++ to provide the URL of the profile image. 135 * Called from C++ to provide the URL of the profile image.
133 * @param {string} imageUrl 136 * @param {string} imageUrl
134 * @param {boolean} selected 137 * @param {boolean} selected
135 */ 138 */
136 receiveProfileImage: function(imageUrl, selected) { 139 receiveProfileImage: function(imageUrl, selected) {
137 this.profileImageUrl_ = imageUrl; 140 this.profileImageUrl_ = imageUrl;
138 if (selected) { 141 if (selected)
139 this.cameraActive_ = false; 142 this.$.selector.select(this.$.selector.indexOf(this.$.profileImage));
140 this.selectedImageUrl_ = imageUrl;
141 }
142 }.bind(this), 143 }.bind(this),
143 144
144 /** 145 /**
145 * Called from the C++ to notify page about camera presence. 146 * Called from the C++ to notify page about camera presence.
146 * @param {boolean} cameraPresent 147 * @param {boolean} cameraPresent
147 */ 148 */
148 receiveCameraPresence: function(cameraPresent) { 149 receiveCameraPresence: function(cameraPresent) {
149 // TODO(tommycli): Implement camera functionality. 150 // TODO(tommycli): Implement camera functionality.
150 }.bind(this), 151 }.bind(this),
151 }; 152 };
152 153
153 cr.define('settings', function() { 154 cr.define('settings', function() {
154 var ChangePicturePage = nativeInterface; 155 var ChangePicturePage = nativeInterface;
155 return { 156 return {
156 ChangePicturePage: ChangePicturePage, 157 ChangePicturePage: ChangePicturePage,
157 }; 158 };
158 }); 159 });
159 160
160 settings.ChangePicturePrivateApi.initialize(); 161 settings.ChangePicturePrivateApi.initialize();
161 }, 162 },
162 163
163 /** 164 /**
164 * Handler for when the user clicks the camera image. 165 * Handler for when the an image is activated.
166 * @param {!Event} event
165 * @private 167 * @private
166 */ 168 */
167 onCameraImageTap_: function() { 169 onImageActivate_: function(event) {
168 this.cameraActive_ = true; 170 var selectedImage = event.detail.item;
169 }, 171 switch (selectedImage.dataset.type) {
170 172 case 'camera':
171 /** 173 // TODO(tommycli): Implement camera functionality.
172 * Handler for when the user clicks a new profile image. 174 break;
173 * @private 175 case 'profile':
174 * @param {!Event} event 176 settings.ChangePicturePrivateApi.selectProfileImage();
175 */ 177 break;
176 onDefaultImageTap_: function(event) { 178 case 'old':
177 var element = Polymer.dom(event).rootTarget; 179 settings.ChangePicturePrivateApi.selectOldImage();
178 180 break;
179 var imageUrl = null; 181 case 'default':
180 if (element.nodeName == 'IMG') 182 settings.ChangePicturePrivateApi.selectDefaultImage(selectedImage.src);
181 imageUrl = element.src; 183 break;
182 else if (element.dataset && element.dataset.imageUrl) 184 default:
183 imageUrl = element.dataset.imageUrl; 185 assertNotReached('Selected unknown image type');
184
185 if (imageUrl != null) {
186 settings.ChangePicturePrivateApi.selectDefaultImage(imageUrl);
187 // Button toggle state is instead controlled by the selected image URL.
188 event.preventDefault();
189 } 186 }
190 }, 187 },
191 188
192 /** 189 /**
193 * Handler for when the user clicks the 'old' image. 190 * True if there is no old image and the selection icon should be hidden.
191 * @param {!string} oldImageUrl
194 * @private 192 * @private
195 * @param {!Event} event
196 */ 193 */
197 onOldImageTap_: function(event) { 194 isOldImageHidden_: function(oldImageUrl) {
198 settings.ChangePicturePrivateApi.selectOldImage(); 195 return !oldImageUrl;
dpapad 2016/01/22 02:35:32 This can only return false if oldImageUrl is the e
tommycli 2016/01/22 19:24:40 Done.
199 // Button toggle state is instead controlled by the selected image URL.
200 event.preventDefault();
201 }, 196 },
202 197
203 /** 198 /**
204 * Handler for when the user clicks the 'profile' image. 199 * True if the preview image is hidden and the camera controls are shown.
200 * @param {!Element} selectedItem
205 * @private 201 * @private
206 * @param {!Event} event
207 */ 202 */
208 onProfileImageTap_: function(event) { 203 isPreviewImageHidden_: function(selectedItem) {
209 settings.ChangePicturePrivateApi.selectProfileImage(); 204 // The selected item can be undefined on initial load and between selection
210 // Button toggle state is instead controlled by the selected image URL. 205 // changes. In those cases, show the preview image.
211 event.preventDefault(); 206 return selectedItem != undefined && selectedItem.dataset.type == 'camera';
212 },
213
214 /**
215 * Computed binding determining which profile image button is toggled on.
216 * @private
217 * @param {string} imageUrl
218 * @param {string} selectedImageUrl
219 * @return {boolean}
220 */
221 isActiveImage_: function(cameraActive, imageUrl, selectedImageUrl) {
222 return !cameraActive && (imageUrl == selectedImageUrl);
223 }, 207 },
224 }); 208 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698