forked from dazemc/ffxiv-craft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettingsimport.js
89 lines (78 loc) · 2.45 KB
/
settingsimport.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
(function () {
'use strict';
angular
.module('ffxivCraftOptWeb.controllers')
.controller('SettingsImportController', controller);
function controller($scope, $window, $translate) {
//noinspection AssignmentResultUsedJS
var vm = $scope.vm = {};
vm.generateFile = generateFile;
vm.handleDrop = handleDrop;
//////////////////////////////////////////////////////////////////////////
function generateFile() {
var zip = new JSZip();
zip.file("settings.json", generateExportText());
zip.generateAsync({type:"blob"})
.then(function(content) {
// see FileSaver.js
saveAs(content, "settings.zip");
});
}
function handleDrop(file) {
JSZip.loadAsync(file.content)
.then(function (zip) {
var settingsJsonFile = zip.file('settings.json');
if (!settingsJsonFile) {
throw new Error($translate.instant('SETTINGS_NOT_FOUND'));
}
settingsJsonFile.async('text')
.then(function (text) {
$scope.$apply(function () {
try {
importSettings(text);
}
catch (err) {
$window.alert($translate.instant('INVALID_FILE') + '\n\n' + err.message);
}
});
})
.catch(function (err) {
$window.alert($translate.instant('INVALID_FILE') + '\n\n' + err.message);
});
})
.catch(function (err) {
$window.alert($translate.instant('INVALID_FILE') + '\n\n' + err.message);
});
}
function importSettings(text) {
var data;
data = JSON.parse(text);
if (!$window.confirm($translate.instant('CONFIRM_IMPORT_SETTINGS'))) {
return;
}
console.log('Importing settings into local storage:', data);
for (var key in data) {
if (data.hasOwnProperty(key)) {
localStorage[key] = data[key];
}
}
$window.alert($translate.instant('SETTINGS_IMPORTED'));
$window.location.reload();
}
function generateExportText() {
var settings = {};
var keys = [
'NG_TRANSLATE_LANG_KEY',
'crafterStats',
'pageStage_v2',
'synths',
'character'
];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
settings[key] = localStorage[key];
}
return JSON.stringify(settings);
}
}
})();