blob: f6c2dc5d77df4542551ccd00d8dcc32ced7f753a [file] [log] [blame]
Kyle Milka39a55eba2018-06-05 18:55:501// Copyright 2018 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'use strict';
7
8let customBackgrounds = {};
9
10/**
11 * Enum for HTML element ids.
12 * @enum {string}
13 * @const
14 */
15customBackgrounds.IDS = {
16 BACK: 'bg-sel-back',
17 CONNECT_GOOGLE_PHOTOS: 'edit-bg-google-photos',
18 CONNECT_GOOGLE_PHOTOS_TEXT: 'edit-bg-google-photos-text',
19 DEFAULT_WALLPAPERS: 'edit-bg-default-wallpapers',
20 DEFAULT_WALLPAPERS_TEXT: 'edit-bg-default-wallpapers-text',
21 DONE: 'bg-sel-footer-done',
22 EDIT_BG: 'edit-bg',
23 EDIT_BG_GEAR: 'edit-bg-gear',
24 EDIT_BG_OVERLAY: 'edit-bg-overlay',
25 MENU: 'bg-sel-menu',
26 OPTIONS_TITLE: 'edit-bg-title',
27 OVERLAY: 'bg-sel-menu-overlay',
28 RESTORE_DEFAULT: 'edit-bg-restore-default',
29 RESTORE_DEFAULT_TEXT: 'edit-bg-restore-default-text',
30 REFRESH_TEXT: 'bg-sel-refresh-text',
31 REFRESH_TOGGLE: 'bg-daily-refresh',
32 UPLOAD_IMAGE: 'edit-bg-upload-image',
33 UPLOAD_IMAGE_TEXT: 'edit-bg-upload-image-text',
34 TILES: 'bg-sel-tiles',
35 TITLE: 'bg-sel-title',
36};
37
38/**
39 * Enum for classnames.
40 * @enum {string}
41 * @const
42 */
43customBackgrounds.CLASSES = {
44 COLLECTION_DIALOG: 'is-col-sel',
45 COLLECTION_SELECTED: 'bg-selected', // Highlight selected tile
46 COLLECTION_TILE: 'bg-sel-tile', // Preview tile for background customization
47 COLLECTION_TITLE: 'bg-sel-tile-title', // Title of a background image
48 DONE_AVAILABLE: 'done-available',
49 IMAGE_DIALOG: 'is-img-sel',
50 SHOW_OVERLAY: 'show-overlay',
51};
52
53/**
54 * Alias for document.getElementById.
55 * @param {string} id The ID of the element to find.
56 * @return {HTMLElement} The found element or null if not found.
57 */
58function $(id) {
59 // eslint-disable-next-line no-restricted-properties
60 return document.getElementById(id);
61}
62
63/**
64 * Remove all collection tiles from the container when the dialog
65 * is closed.
66 */
67customBackgrounds.resetSelectionDialog = function() {
68 $(customBackgrounds.IDS.TILES).scrollTop = 0;
69 var tileContainer = $(customBackgrounds.IDS.TILES);
70 while (tileContainer.firstChild) {
71 tileContainer.removeChild(tileContainer.firstChild);
72 }
73 $(customBackgrounds.IDS.DONE)
74 .classList.remove(customBackgrounds.CLASSES.DONE_AVAILABLE);
75};
76
77/**
78 * Show dialog for selecting a collection. Populates the dialog
79 * with data from |coll|.
80 * @param {string} dialogTitle The title to be displayed at the top of the
81 * dialog.
82 */
83customBackgrounds.showCollectionSelectionDialog = function() {
84 var doneButton = $(customBackgrounds.IDS.DONE);
85 var tileContainer = $(customBackgrounds.IDS.TILES);
86 var overlay = $(customBackgrounds.IDS.OVERLAY);
87
88 overlay.classList.add(customBackgrounds.CLASSES.SHOW_OVERLAY);
89
90 // Create dialog header
91 $(customBackgrounds.IDS.TITLE).textContent =
92 configData.translatedStrings.selectChromeWallpaper;
93 $(customBackgrounds.IDS.MENU)
94 .classList.add(customBackgrounds.CLASSES.COLLECTION_DIALOG);
95 $(customBackgrounds.IDS.MENU)
96 .classList.remove(customBackgrounds.CLASSES.IMAGE_DIALOG);
97
98 // Create dialog tiles
99 for (var i = 0; i < coll.length; ++i) {
100 var tile = document.createElement('div');
101 tile.classList.add(customBackgrounds.CLASSES.COLLECTION_TILE);
102 tile.style.backgroundImage = 'url(' + coll[i].previewImageUrl + ')';
103 tile.dataset.id = coll[i].collectionId;
104 tile.dataset.name = coll[i].collectionName;
105
106 var title = document.createElement('div');
107 title.classList.add(customBackgrounds.CLASSES.COLLECTION_TITLE);
108 title.textContent = tile.dataset.name;
109
110 tile.onclick = function(event) {
111 var tile = this;
112
113 // Load images for selected collection
114 var imgElement = $('ntp-images-loader');
115 if (imgElement) {
116 imgElement.parentNode.removeChild(imgElement);
117 }
118 var imgScript = document.createElement('script');
119 imgScript.id = 'ntp-images-loader';
120 imgScript.src = 'chrome-search://local-ntp/ntp-background-images.js?' +
121 'collection_id=' + this.dataset.id;
122 document.body.appendChild(imgScript);
123
124 imgScript.onload = function() {
125 customBackgrounds.resetSelectionDialog();
126 if (coll_img.length > 0 &&
127 coll_img[0].collectionId == tile.dataset.id) {
128 customBackgrounds.showImageSelectionDialog(tile.dataset.name);
129 } else {
130 overlay.classList.remove(customBackgrounds.CLASSES.SHOW_OVERLAY);
131 customBackgrounds.resetSelectionDialog();
132 }
133 };
134 };
135
136 tile.appendChild(title);
137 tileContainer.appendChild(tile);
138 }
139
140 // Create dialog footer
141 $(customBackgrounds.IDS.DONE).textContent =
142 configData.translatedStrings.selectionDone;
143
144 overlay.onclick = function(event) {
145 if (event.target == overlay) {
146 overlay.classList.remove(customBackgrounds.CLASSES.SHOW_OVERLAY);
147 customBackgrounds.resetSelectionDialog();
148 }
149 };
150};
151
152
153/**
154 * Show dialog for selecting an image or toggling on daily refresh. Image
155 * data should previous have been loaded into coll_img via
156 * chrome-search://local-ntp/ntp-background-images.js?collection_id=<collection_id>
157 * @param {string} dialogTitle The title to be displayed at the top of the
158 * @param {string} prevDialogTitle The title for the previous dialog, needed
159 * when the back button is clicked.
160 */
161customBackgrounds.showImageSelectionDialog = function(dialogTitle) {
162 var backButton = $(customBackgrounds.IDS.BACK);
163 var dailyRefresh = $(customBackgrounds.IDS.REFRESH_TOGGLE);
164 var overlay = $(customBackgrounds.IDS.OVERLAY);
165 var selectedTile = null;
166 var tileContainer = $(customBackgrounds.IDS.TILES);
167
168 $(customBackgrounds.IDS.TITLE).textContent = dialogTitle;
169 $(customBackgrounds.IDS.MENU)
170 .classList.remove(customBackgrounds.CLASSES.COLLECTION_DIALOG);
171 $(customBackgrounds.IDS.MENU)
172 .classList.add(customBackgrounds.CLASSES.IMAGE_DIALOG);
173
174 for (var i = 0; i < coll_img.length; ++i) {
175 var tile = document.createElement('div');
176 tile.classList.add(customBackgrounds.CLASSES.COLLECTION_TILE);
177 tile.style.backgroundImage = 'url(' + coll_img[i].imageUrl + ')';
178 tile.id = 'img_tile_' + i;
179
180 tile.onclick = function(event) {
181 var tile = event.target;
182 if (selectedTile) {
183 selectedTile.classList.remove(
184 customBackgrounds.CLASSES.COLLECTION_SELECTED);
185 }
186 tile.classList.add(customBackgrounds.CLASSES.COLLECTION_SELECTED);
187 selectedTile = tile;
188
189 // Turn toggle off when an image is selected.
190 $(customBackgrounds.IDS.REFRESH_TOGGLE).children[0].checked = false;
191 $(customBackgrounds.IDS.DONE)
192 .classList.add(customBackgrounds.CLASSES.DONE_AVAILABLE);
193 };
194
195 tileContainer.appendChild(tile);
196 }
197
198 dailyRefresh.onclick = function(event) {
199 if (selectedTile) {
200 selectedTile.classList.remove(
201 customBackgrounds.CLASSES.COLLECTION_SELECTED);
202 }
203 $(customBackgrounds.IDS.DONE)
204 .classList.add(customBackgrounds.CLASSES.DONE_AVAILABLE);
205 };
206
207 backButton.onclick = function(event) {
208 customBackgrounds.resetSelectionDialog();
209 customBackgrounds.showCollectionSelectionDialog();
210 };
211};
212
213/**
214 * Display dialog with various options for custom background source.
215 */
216customBackgrounds.initBackgroundOptionDialog = function() {
217 var editDialogOverlay = $(customBackgrounds.IDS.EDIT_BG_OVERLAY);
218
219 $(customBackgrounds.IDS.CONNECT_GOOGLE_PHOTOS_TEXT).textContent =
220 configData.translatedStrings.connectGooglePhotos;
221 $(customBackgrounds.IDS.DEFAULT_WALLPAPERS_TEXT).textContent =
222 configData.translatedStrings.defaultWallpapers;
223 $(customBackgrounds.IDS.UPLOAD_IMAGE_TEXT).textContent =
224 configData.translatedStrings.uploadImage;
225 $(customBackgrounds.IDS.RESTORE_DEFAULT_TEXT).textContent =
226 configData.translatedStrings.restoreDefaultBackground;
227 $(customBackgrounds.IDS.OPTIONS_TITLE).textContent =
228 configData.translatedStrings.customizeBackground;
229 $(customBackgrounds.IDS.REFRESH_TEXT).textContent =
230 configData.translatedStrings.dailyRefresh;
231
232 $(customBackgrounds.IDS.EDIT_BG_GEAR).onclick = function() {
233 $(customBackgrounds.IDS.CONNECT_GOOGLE_PHOTOS).hidden = true;
234 $(customBackgrounds.IDS.DEFAULT_WALLPAPERS).hidden = false;
235 $(customBackgrounds.IDS.UPLOAD_IMAGE).hidden = true;
236 $(customBackgrounds.IDS.RESTORE_DEFAULT).hidden = true;
237
238 $(customBackgrounds.IDS.DEFAULT_WALLPAPERS).onclick = function() {
239 $(customBackgrounds.IDS.EDIT_BG_OVERLAY)
240 .classList.remove(customBackgrounds.CLASSES.SHOW_OVERLAY);
241 if (typeof coll != 'undefined') {
242 customBackgrounds.showCollectionSelectionDialog(
243 configData.translatedStrings.selectChromeWallpaper);
244 }
245 };
246
247 editDialogOverlay.classList.add(customBackgrounds.CLASSES.SHOW_OVERLAY);
248 };
249
250 editDialogOverlay.onclick = function(event) {
251 if (event.target == editDialogOverlay) {
252 editDialogOverlay.classList.remove(
253 customBackgrounds.CLASSES.SHOW_OVERLAY);
254 }
255 };
256};