-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-dialog.js
138 lines (126 loc) · 4.2 KB
/
load-dialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview This file contains implementation for the load dialog.
*/
/**
* Namespace for load dialog.
*/
var loadDialog = {};
/**
* Called to initialize the load dialog.
*/
loadDialog.init = function() {
$('#load-file-form', '#load-dialog')[0].reset();
$('#apply-errors', '#load-dialog').html('');
$('#apply-warnings', '#load-dialog').html('');
var applyButton = $('#apply-button', '#load-dialog');
applyButton.click(loadDialog.onApplyPress);
main.disableInput(applyButton);
// Reset the file picker.
ui.resetFilePicker('#load-file');
loadDialog.configureLoadFilePicker();
$('#load-file').focus();
};
loadDialog.setUiVisibility = function() {
return;
};
/**
* Load the given ONC configuration and show any errors in errorDom.
* @param {String} config ONC formatted string.
*/
loadDialog.loadConfig = function(configString) {
if (!configString || configString.constructor != String) {
return;
}
var passphrase = $('#load-passphrase', '#load-dialog');
var result = { 'errors': [], 'warnings': [], 'hasOpaqueEntity': false };
// Check and see if the config is encrypted or not, and decrypt it
// using the passphrase if it is.
var config;
try {
config = JSON.parse(configString);
} catch(e) {
result.errors.push(['errorDuringLoad', e.toString()]);
}
if (!result.errors.length && onc.isEncrypted(config)) {
result = onc.validateEncryptedConfiguration(config, result);
if (!result.errors.length) {
try {
config = onc.decryptConfig(passphrase.val(), config);
} catch(e) {
result.errors.push(['errorDuringLoad', e.toString()]);
}
}
}
if (!result.errors.length && typeof(config) != 'object') {
result.errors.push(['errorLoadUnknown']);
}
if (!result.errors.length) {
result = onc.validateUnencryptedConfiguration(config, result);
}
// Display the errors we found (if any)
if (result.errors.length == 0) {
$('#apply-header', '#load-dialog').html(chrome.i18n.getMessage
('loadSucceeded'));
} else {
// Clear everything out if we had errors.
$('#load-file-form')[0].reset();
config = undefined;
}
ui.showMessages(result, '#load-dialog');
loadDialog.oncToLoad = config;
loadDialog.oncToLoadResult = result;
};
/**
* Handle ONC file load event.
* @param {Array.<Object>} files Array of files in file upload format.
* @returns {Boolean} Indicates the event needs to be passed to other objects.
*/
loadDialog.handleLoadFile = function(files) {
var applyButton = $('#apply-button', '#load-dialog');
$('#apply-errors').html('');
for (var i = 0; i < files.length; ++i) {
var file = files[i];
var reader = new FileReader();
reader.onload = function(theFile) {
// Enable the Load File button when done reading from disk.
loadDialog.oncToLoad = this.result;
$('#apply-errors', '#load-dialog').html('');
$('#apply-warnings', '#load-dialog').html('');
main.enableInput(applyButton);
};
reader.readAsBinaryString(file);
}
return false;
};
/**
* Configures the ONC load file picker.
*/
loadDialog.configureLoadFilePicker = function() {
$('#load-file').change(function(event) {
loadDialog.handleLoadFile(event.target.files);
});
};
/**
* Handles apply button press in load dialog.
*/
loadDialog.onApplyPress = function() {
loadDialog.loadConfig(loadDialog.oncToLoad);
// Ignore apply button if nothing was loaded or there are errors.
if (!loadDialog.oncToLoadResult || loadDialog.oncToLoadResult.errors.length) {
// Clear the filename and passphrase out if we had errors.
$('#load-file-form')[0].reset();
main.disableInput($('#apply-button', '#load-dialog'));
ui.showMessages(loadDialog.oncToLoadResult, '#load-dialog');
loadDialog.oncToLoad = undefined;
loadDialog.oncToLoadResult = undefined;
return;
}
main.oncCurrent = loadDialog.oncToLoad;
main.externalEntitySet = onc.getEntitySet(main.oncCurrent);
loadDialog.oncToLoad = undefined;
loadDialog.oncToLoadResult = undefined;
ui.dismissDialog();
};