Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 //<include src="../../../../ui/webui/resources/js/cr.js"> | |
| 6 //<include src="../../../../ui/webui/resources/js/load_time_data.js"> | |
| 7 //<include src="../../../../ui/webui/resources/js/i18n_template_no_process.js"> | |
| 8 //<include src="../../../../ui/webui/resources/js/cr/event_target.js"> | |
| 9 //<include src="../../../../ui/webui/resources/js/cr/ui/dialogs.js"> | |
|
stevenjb
2015/05/14 23:28:04
Note: We should avoid committing commented out cod
Dan Beam
2015/05/14 23:59:31
these are functional. <include> is pre-processed.
| |
| 10 | |
| 11 (function() { | |
| 12 'use strict'; | |
| 13 | |
| 14 //<include src="../cws_widget/app_installer.js"> | |
| 15 //<include src="../cws_widget/cws_webview_client.js"> | |
| 16 //<include src="../cws_widget/cws_widget_container.js"> | |
| 17 //<include src="../cws_widget/cws_widget_container_error_dialog.js"> | |
| 18 | |
| 19 /** | |
| 20 * @type {?{ | |
| 21 * filter: !Object.<string, *>, | |
| 22 * webstoreUrl: ?string | |
| 23 * }} | |
| 24 */ | |
| 25 window.params = window.params || null; | |
| 26 | |
| 27 /** | |
| 28 * @param {string} id Element id. | |
| 29 * @return {HTMLElement} The found element, or null. | |
| 30 */ | |
| 31 function $(id) { | |
| 32 return document.getElementById(id); | |
| 33 } | |
| 34 | |
| 35 /** | |
| 36 * Default strings. | |
| 37 */ | |
| 38 var defaultStrings = { | |
| 39 'language': 'en', | |
| 40 'LINK_TO_WEBSTORE': '[LOCALIZE ME] Learn more...', | |
| 41 'INSTALLATION_FAILED_MESSAGE': '[LOCALIZE ME] Installation failed!', | |
| 42 'OK_BUTTON': '[LOCALIZE ME] OK', | |
| 43 'TITLE_PRINTER_PROVIDERS': '[LOCALIZE ME] Select app for your printer' | |
| 44 }; | |
| 45 | |
| 46 /** | |
| 47 * @param {string} id The string id. | |
| 48 * @return {string} | |
| 49 */ | |
| 50 function getString(id) { | |
| 51 return loadTimeData.getString(id) || defaultStrings[id] || ''; | |
| 52 } | |
| 53 | |
| 54 /** | |
| 55 * @param {Object.<string, string>} strings Localized strings used by the | |
| 56 * container. | |
| 57 * @return {!CWSWidgetContainer.PlatformDelegate} | |
| 58 */ | |
| 59 function createPlatformDelegate(strings) { | |
| 60 return { | |
| 61 strings: { | |
| 62 UI_LOCALE: getString('language'), | |
| 63 LINK_TO_WEBSTORE: getString('LINK_TO_WEBSTORE'), | |
| 64 INSTALLATION_FAILED_MESSAGE: getString('INSTALLATION_FAILED_MESSAGE') | |
| 65 }, | |
| 66 | |
| 67 metricsImpl: { | |
| 68 /** | |
| 69 * @param {string} enumName | |
| 70 * @param {number} value | |
| 71 * @param {number} enumSize | |
| 72 */ | |
| 73 recordEnum: function(enumName, value, enumSize) {}, | |
| 74 | |
| 75 /** @param {string} actionName */ | |
| 76 recordUserAction: function(actionName) {}, | |
| 77 | |
| 78 /** @param {string} intervalName */ | |
| 79 startInterval: function(intervalName) {}, | |
| 80 | |
| 81 /** @param {string} intervalName */ | |
| 82 recordInterval: function(intervalName) {} | |
| 83 }, | |
| 84 | |
| 85 /** | |
| 86 * @param {string} itemId Item to be installed. | |
| 87 * @param {function(?string)} callback Callback param is the error message, | |
| 88 * which is set to null on success. | |
| 89 */ | |
| 90 installWebstoreItem: function(itemId, callback) { | |
| 91 chrome.webstoreWidgetPrivate.installWebstoreItem( | |
| 92 itemId, | |
| 93 false, | |
| 94 function() { | |
| 95 callback(chrome.runtime.lastError ? | |
| 96 chrome.runtime.lastError.message || 'UNKNOWN_ERROR' : null); | |
| 97 }); | |
| 98 }, | |
| 99 | |
| 100 /** @param {function(Array.<string>)} callback */ | |
| 101 getInstalledItems: function(callback) { callback([]); }, | |
| 102 | |
| 103 /** | |
| 104 * @param {function(?string)} callback The argument is the fetche3d access | |
| 105 * token. Null on error. | |
| 106 */ | |
| 107 requestWebstoreAccessToken: function(callback) { | |
| 108 chrome.fileManagerPrivate.requestWebStoreAccessToken(function(token) { | |
| 109 if (chrome.runtime.lastError) { | |
| 110 console.error('Error getting access token: ' + | |
| 111 chrome.runtime.lastError.message); | |
| 112 callback(null); | |
| 113 return; | |
| 114 } | |
| 115 callback(token); | |
| 116 }); | |
| 117 } | |
| 118 }; | |
| 119 } | |
| 120 | |
| 121 function initializeTopbarButtons() { | |
| 122 $('close-button').addEventListener('click', function(e) { | |
| 123 e.preventDefault(); | |
| 124 chrome.app.window.current().close(); | |
| 125 }); | |
| 126 | |
| 127 $('close-button').addEventListener('mousedown', function(e) { | |
| 128 e.preventDefault(); | |
| 129 }); | |
| 130 | |
| 131 $('minimize-button').addEventListener('click', function(e) { | |
| 132 e.preventDefault(); | |
| 133 chrome.app.window.current().minimize(); | |
| 134 }); | |
| 135 | |
| 136 $('minimize-button').addEventListener('mousedown', function(e) { | |
| 137 e.preventDefault(); | |
| 138 }); | |
| 139 } | |
| 140 | |
| 141 window.addEventListener('DOMContentLoaded', function() { | |
| 142 initializeTopbarButtons(); | |
| 143 | |
| 144 chrome.webstoreWidgetPrivate.getStrings(function(strings) { | |
| 145 loadTimeData.data = strings; | |
| 146 i18nTemplate.process(document, loadTimeData); | |
| 147 | |
| 148 cr.ui.dialogs.BaseDialog.OK_LABEL = getString('OK_BUTTON'); | |
| 149 | |
| 150 document.title = getString('TITLE_PRINTER_PROVIDERS'); | |
| 151 $('title').textContent = document.title; | |
| 152 | |
| 153 if (!window.params) { | |
| 154 console.error('Params not set!'); | |
| 155 return; | |
| 156 } | |
| 157 | |
| 158 /** @type {!CWSWidgetContainer.PlatformDelegate} */ | |
| 159 var platformDelegate = createPlatformDelegate(strings); | |
| 160 | |
| 161 var root = $('widget-container-root'); | |
| 162 if (!root) { | |
| 163 console.error('No root element'); | |
| 164 return; | |
| 165 } | |
| 166 | |
| 167 /** @type {!CWSWidgetContainer} */ | |
| 168 var widgetContainer = new CWSWidgetContainer( | |
| 169 document, root, platformDelegate, {} /* state */); | |
| 170 | |
| 171 widgetContainer.ready() | |
| 172 /** @return {!Promise.<CWSWidgetContainer.ResolveReason>} */ | |
| 173 .then(function() { | |
| 174 return widgetContainer.start(window.params.filter, | |
| 175 window.params.webstoreUrl); | |
| 176 }) | |
| 177 /** @param {!CWSWidgetContainer.ResolveReason} reason */ | |
| 178 .then(function(reason) { | |
| 179 chrome.app.window.current().close(); | |
| 180 }) | |
| 181 /** @param {*} error */ | |
| 182 .catch(function(error) { | |
| 183 // TODO(tbarzic): Add error UI. | |
| 184 console.error(error); | |
| 185 }); | |
| 186 }); | |
| 187 }); | |
| 188 })(); | |
| OLD | NEW |