1 // Copyright (c) 2012 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.
6 * This view displays options for importing data from a log file.
8 var ImportView
= (function() {
11 // This is defined in index.html, but for all intents and purposes is part
13 var LOAD_LOG_FILE_DROP_TARGET_ID
= 'import-view-drop-target';
15 // We inherit from DivView.
16 var superClass
= DivView
;
21 function ImportView() {
22 assertFirstConstructorCall(ImportView
);
24 // Call superclass's constructor.
25 superClass
.call(this, ImportView
.MAIN_BOX_ID
);
27 this.loadedDiv_
= $(ImportView
.LOADED_DIV_ID
);
29 this.loadFileElement_
= $(ImportView
.LOAD_LOG_FILE_ID
);
30 this.loadFileElement_
.onchange
= this.logFileChanged
.bind(this);
31 this.loadStatusText_
= $(ImportView
.LOAD_STATUS_TEXT_ID
);
33 var dropTarget
= $(LOAD_LOG_FILE_DROP_TARGET_ID
);
34 dropTarget
.ondragenter
= this.onDrag
.bind(this);
35 dropTarget
.ondragover
= this.onDrag
.bind(this);
36 dropTarget
.ondrop
= this.onDrop
.bind(this);
38 this.loadedInfoBuildName_
= $(ImportView
.LOADED_INFO_BUILD_NAME_ID
);
39 this.loadedInfoExportDate_
= $(ImportView
.LOADED_INFO_EXPORT_DATE_ID
);
40 this.loadedInfoOsType_
= $(ImportView
.LOADED_INFO_OS_TYPE_ID
);
41 this.loadedInfoCommandLine_
= $(ImportView
.LOADED_INFO_COMMAND_LINE_ID
);
42 this.loadedInfoUserComments_
= $(ImportView
.LOADED_INFO_USER_COMMENTS_ID
);
45 ImportView
.TAB_HANDLE_ID
= 'tab-handle-import';
47 // IDs for special HTML elements in import_view.html
48 ImportView
.MAIN_BOX_ID
= 'import-view-tab-content';
49 ImportView
.LOADED_DIV_ID
= 'import-view-loaded-div';
50 ImportView
.LOAD_LOG_FILE_ID
= 'import-view-load-log-file';
51 ImportView
.LOAD_STATUS_TEXT_ID
= 'import-view-load-status-text';
52 ImportView
.LOADED_INFO_EXPORT_DATE_ID
= 'import-view-export-date';
53 ImportView
.LOADED_INFO_BUILD_NAME_ID
= 'import-view-build-name';
54 ImportView
.LOADED_INFO_OS_TYPE_ID
= 'import-view-os-type';
55 ImportView
.LOADED_INFO_COMMAND_LINE_ID
= 'import-view-command-line';
56 ImportView
.LOADED_INFO_USER_COMMENTS_ID
= 'import-view-user-comments';
58 cr
.addSingletonGetter(ImportView
);
60 ImportView
.prototype = {
61 // Inherit the superclass's methods.
62 __proto__
: superClass
.prototype,
65 * Called when a log file is loaded, after clearing the old log entries and
66 * loading the new ones. Returns true to indicate the view should
69 onLoadLogFinish: function(data
, unused
, logDump
) {
70 setNodeDisplay(this.loadedDiv_
, true);
71 this.updateLoadedClientInfo(logDump
.userComments
);
76 * Called when something is dragged over the drop target.
78 * Returns false to cancel default browser behavior when a single file is
79 * being dragged. When this happens, we may not receive a list of files for
80 * security reasons, which is why we allow the |files| array to be empty.
82 onDrag: function(event
) {
83 // NOTE: Use Array.prototype.indexOf here is necessary while WebKit
84 // decides which type of data structure dataTransfer.types will be
85 // (currently between DOMStringList and Array). These have different APIs
86 // so assuming one type or the other was breaking things. See
87 // http://crbug.com/115433. TODO(dbeam): Remove when standardized more.
88 var indexOf
= Array
.prototype.indexOf
;
89 return indexOf
.call(event
.dataTransfer
.types
, 'Files') == -1 ||
90 event
.dataTransfer
.files
.length
> 1;
94 * Called when something is dropped onto the drop target. If it's a single
95 * file, tries to load it as a log file.
97 onDrop: function(event
) {
98 var indexOf
= Array
.prototype.indexOf
;
99 if (indexOf
.call(event
.dataTransfer
.types
, 'Files') == -1 ||
100 event
.dataTransfer
.files
.length
!= 1) {
103 event
.preventDefault();
105 // Loading a log file may hide the currently active tab. Switch to the
106 // import tab to prevent this.
107 document
.location
.hash
= 'import';
109 this.loadLogFile(event
.dataTransfer
.files
[0]);
113 * Called when a log file is selected.
115 * Gets the log file from the input element and tries to read from it.
117 logFileChanged: function() {
118 this.loadLogFile(this.loadFileElement_
.files
[0]);
122 * Attempts to read from the File |logFile|.
124 loadLogFile: function(logFile
) {
126 this.setLoadFileStatus('Loading log...', true);
127 var fileReader
= new FileReader();
129 fileReader
.onload
= this.onLoadLogFile
.bind(this, logFile
);
130 fileReader
.onerror
= this.onLoadLogFileError
.bind(this);
132 fileReader
.readAsText(logFile
);
137 * Displays an error message when unable to read the selected log file.
138 * Also clears the file input control, so the same file can be reloaded.
140 onLoadLogFileError: function(event
) {
141 this.loadFileElement_
.value
= null;
142 this.setLoadFileStatus(
143 'Error ' + getKeyWithValue(FileError
, event
.target
.error
.code
) +
144 '. Unable to read file.',
148 onLoadLogFile: function(logFile
, event
) {
149 var result
= log_util
.loadLogFile(event
.target
.result
, logFile
.name
);
150 this.setLoadFileStatus(result
, false);
154 * Sets the load from file status text, displayed below the load file
155 * button, to |text|. Also enables or disables the load buttons based on
156 * the value of |isLoading|, which must be true if the load process is still
157 * ongoing, and false when the operation has stopped, regardless of success
158 * of failure. Also, when loading is done, replaces the load button so the
159 * same file can be loaded again.
161 setLoadFileStatus: function(text
, isLoading
) {
162 this.enableLoadFileElement_(!isLoading
);
163 this.loadStatusText_
.textContent
= text
;
166 // Clear the button, so the same file can be reloaded. Recreating the
167 // element seems to be the only way to do this.
168 var loadFileElementId
= this.loadFileElement_
.id
;
169 var loadFileElementOnChange
= this.loadFileElement_
.onchange
;
170 this.loadFileElement_
.outerHTML
= this.loadFileElement_
.outerHTML
;
171 this.loadFileElement_
= $(loadFileElementId
);
172 this.loadFileElement_
.onchange
= loadFileElementOnChange
;
175 // Style the log output differently depending on what just happened.
176 var pos
= text
.indexOf('Log loaded.');
178 this.loadStatusText_
.className
= 'import-view-pending-log';
179 } else if (pos
== 0) {
180 this.loadStatusText_
.className
= 'import-view-success-log';
181 } else if (pos
!= -1) {
182 this.loadStatusText_
.className
= 'import-view-warning-log';
184 this.loadStatusText_
.className
= 'import-view-error-log';
188 enableLoadFileElement_: function(enabled
) {
189 this.loadFileElement_
.disabled
= !enabled
;
193 * Prints some basic information about the environment when the log was
196 updateLoadedClientInfo: function(userComments
) {
197 // Reset all the fields (in case we early-return).
198 this.loadedInfoExportDate_
.innerText
= '';
199 this.loadedInfoBuildName_
.innerText
= '';
200 this.loadedInfoOsType_
.innerText
= '';
201 this.loadedInfoCommandLine_
.innerText
= '';
202 this.loadedInfoUserComments_
.innerText
= '';
204 if (typeof(ClientInfo
) != 'object')
207 timeutil
.addNodeWithDate(this.loadedInfoExportDate_
,
208 new Date(ClientInfo
.numericDate
));
212 ' ' + ClientInfo
.version
+
213 ' (' + ClientInfo
.official
+
214 ' ' + ClientInfo
.cl
+
215 ') ' + ClientInfo
.version_mod
;
217 this.loadedInfoBuildName_
.innerText
= buildName
;
219 this.loadedInfoOsType_
.innerText
= ClientInfo
.os_type
;
220 this.loadedInfoCommandLine_
.innerText
= ClientInfo
.command_line
;
222 // User comments will not be available when dumped from command line.
223 this.loadedInfoUserComments_
.innerText
= userComments
|| '';