From aff38ea0bc4558b4baa4c283ab25a71b4ff83dea Mon Sep 17 00:00:00 2001 From: ronggang Date: Thu, 6 Sep 2018 14:53:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add configuration import and export function --- src/index.html | 28 ++-- src/tr-web-control/i18n/en.json | 19 ++- src/tr-web-control/i18n/zh_CN.json | 19 ++- src/tr-web-control/script/min/public.min.js | 8 +- src/tr-web-control/script/min/system.min.js | 8 +- src/tr-web-control/script/public.js | 86 ++++++++++ src/tr-web-control/script/system.js | 10 +- .../template/dialog-export-config.html | 102 ++++++++++++ .../template/dialog-import-config.html | 119 ++++++++++++++ .../template/dialog-system-config.html | 149 ++++++++---------- 10 files changed, 437 insertions(+), 111 deletions(-) create mode 100644 src/tr-web-control/template/dialog-export-config.html create mode 100644 src/tr-web-control/template/dialog-import-config.html diff --git a/src/index.html b/src/index.html index 7ebd67c..f0a1400 100644 --- a/src/index.html +++ b/src/index.html @@ -8,13 +8,13 @@ Transmission Web Control - - + + - - - - + + + + @@ -22,20 +22,20 @@ - + - + - - - + + + - - - + + + \ No newline at end of file diff --git a/src/tr-web-control/template/dialog-import-config.html b/src/tr-web-control/template/dialog-import-config.html new file mode 100644 index 0000000..16bd6a2 --- /dev/null +++ b/src/tr-web-control/template/dialog-import-config.html @@ -0,0 +1,119 @@ +
+
+
+ + + +
+
+
+ + + Ok + Cancel +
+
+ \ No newline at end of file diff --git a/src/tr-web-control/template/dialog-system-config.html b/src/tr-web-control/template/dialog-system-config.html index 356c612..7db0293 100644 --- a/src/tr-web-control/template/dialog-system-config.html +++ b/src/tr-web-control/template/dialog-system-config.html @@ -326,7 +326,10 @@
- + + | + + Ok @@ -843,18 +846,18 @@ } var exportData = JSON.parse(JSON.stringify(data)); - page.saveFileAs("tr-web-contorl-labels.json", JSON.stringify(exportData)); + saveFileAs("tr-web-contorl-labels.json", JSON.stringify(exportData)); }); // 导入 - $("#btn-UserLabel-import").on("click", function () { - page.loadFileContent("json", function (content) { + $("#btn-UserLabel-import", thisDialog).on("click", function () { + loadFileContent("json", function (content) { var item = content; if (typeof (content) === "string") { try { item = JSON.parse(content); } catch (error) { - alert("JSON文件解析失败!"); + alert(system.lang["public"]["text-json-file-parsing-failed"]); return; } } @@ -946,6 +949,14 @@ }; reloadData(); + + $("#btn-export-config", thisDialog).on("click.cus", function() { + page.exportConfig(this); + }); + + $("#btn-import-config", thisDialog).on("click", function() { + page.importConfig(this); + }) }, endEditing: function(){ if (page.editIndex == undefined){return true} @@ -957,91 +968,63 @@ return false; } }, - /** - * 加载指定的文件内容 - */ - loadFileContent: function (fileType, callback) { - $("").on("change", function () { - var fileSelector = this; - if (fileSelector.files.length > 0 && fileSelector.files[0].name.length > 0) { - var files = fileSelector.files; - var count = files.length; - var index = 0; - var r = new FileReader(); - r.onload = function (e) { - callback && callback.call(system, e.target.result); - readFile(); - }; - r.onerror = function () { - alert("文件加载失败"); - console.log("文件加载失败"); - readFile(); - }; - - function readFile(file) { - - if (index == count) { - $(fileSelector).remove(); - fileSelector.value = ""; - return; - } - var file = files[index]; - var lastIndex = file.name.lastIndexOf("."); - var fix = file.name.substr(lastIndex + 1); - - index++; - - if (fileType) { - if (fix != fileType) { - alert("文件类型错误"); - return; - } - } - - r.readAsText(file); - } - readFile(); - } - }).click(); - }, - /** - * 将指定的内容保存为文件 - * @param fileName 文件名 - * @param fileData 文件内容 - */ - saveFileAs: function (fileName, fileData) { - try { - var Blob = window.Blob || window.WebKitBlob; - - // Detect availability of the Blob constructor. - var constructor_supported = false; - if (Blob) { + importConfig: function(button) { + loadFileContent("json", function (content) { + var item = content; + if (typeof (content) === "string") { try { - new Blob([], { - "type": "text/plain" - }); - constructor_supported = true; - } catch (_) {} + item = JSON.parse(content); + } catch (error) { + alert(system.lang["public"]["text-json-file-parsing-failed"]); + return; + } } - var b = null; - if (constructor_supported) { - b = new Blob([fileData], { - "type": "text/plain" + if (item.system || item.user || item.server || item.dictionary) { + system.openDialogFromTemplate({ + id: "dialog-import-config", + options: { + title: system.lang.dialog["import-config"].title, + width: 500, + height: 120 + }, + datas: { + config: item + }, + type: 1, + source: $(button), + onClose: function() { + $(button).on("click", function(){ + page.importConfig(this); + }); + } }); } else { - // Deprecated BlobBuilder API - var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder; - var bb = new BlobBuilder(); - bb.append(fileData); - b = bb.getBlob("text/plain"); + alert(system.lang.dialog["import-config"]["invalid-file"]); + // thisDialog.find("#system-config-nochange").html(system.lang.dialog["import-config"]["invalid-file"]).fadeInAndOut(); } - - saveAs(b, fileName); - } catch (e) { - console.log(e.toString()); - } + }); }, + /** + * 导出当前配置到文件 + */ + exportConfig: function(button) { + system.openDialogFromTemplate({ + id: "dialog-export-config", + options: { + title: system.lang.dialog["export-config"].title, + width: 500, + height: 120 + }, + type: 1, + source: $(button), + onClose: function() { + $(button).on("click", function(){ + page.exportConfig(this); + }); + } + }); + } }; page.init(); })($("#dialog-system-config"));