From ecd4e695cdf8c4eaf724727718b13d0b0ddeb47b Mon Sep 17 00:00:00 2001 From: Sanj Date: Thu, 26 May 2011 00:31:22 +0530 Subject: [PATCH] json indent, change json format, implement load json file --- chrome/content/speedtag.css | 2 +- chrome/content/speedtag.html | 3 ++- chrome/content/speedtag.js | 29 ++++++++++++++++++++++++++--- chrome/content/staticfuncs.js | 18 ++++++++++++++++++ 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/chrome/content/speedtag.css b/chrome/content/speedtag.css index 12ba503..be6438b 100644 --- a/chrome/content/speedtag.css +++ b/chrome/content/speedtag.css @@ -22,7 +22,7 @@ body { float: left; } -#notesArea { +#notes { width: 90%; height: 600px; } diff --git a/chrome/content/speedtag.html b/chrome/content/speedtag.html index 01d5514..04f93df 100644 --- a/chrome/content/speedtag.html +++ b/chrome/content/speedtag.html @@ -13,6 +13,7 @@ +
@@ -70,7 +71,7 @@
- +
diff --git a/chrome/content/speedtag.js b/chrome/content/speedtag.js index 1e3fb8f..68620f6 100644 --- a/chrome/content/speedtag.js +++ b/chrome/content/speedtag.js @@ -1,3 +1,5 @@ +// vim: et:ts=2:sw=2:sts=2:ft=js + var startTime = new Date(); $(function() { @@ -10,21 +12,42 @@ $(function() { if (savePath === '') { alert("Please select a save location first."); return false; } fil = selectFile(); // alert(savePath); - var destName = getDateString(startTime) + ".mp3"; + var destName = getDateString(startTime) + "." + getFileNameExt(fil.file.path); mozillaCopyFile(fil.file.path, savePath, destName); }); + $('#saveFormData').click(function() { if (savePath === '') { alert("Please select a save location first."); return false; } - var arr = $('#metadataForm').serializeArray(); - var s = JSON.stringify(arr); + var arr = $('#metadataForm').serializeObject(); + var s = JSON.stringify(arr, null, 2); // alert(s); var destFilePath = savePath + "/" + getDateString(startTime) + ".json"; // alert(destFilePath); mozillaSaveFile(destFilePath, s); alert("saved " + destFilePath); }); + + $('#loadData').click(function() { + var txtFile = selectFile().file.path; + var txt = mozillaLoadFile(txtFile); + var data = JSON.parse(txt); + loadFormData(data); + }); }); function getDateString(dateObj) { return dateObj.toUTCString().replace(",", "").replace(":", "."); } + +function loadFormData(data) { + for (var key in data) { + if (data.hasOwnProperty(key)) { + var value = data[key]; + if (value === "on") { + $('#' + key).attr("checked", "checked"); + } else { + $('#' + key).val(data[key]); + } + } + } +} diff --git a/chrome/content/staticfuncs.js b/chrome/content/staticfuncs.js index a5c0cbd..718aa7a 100755 --- a/chrome/content/staticfuncs.js +++ b/chrome/content/staticfuncs.js @@ -260,3 +260,21 @@ function getFileNameSansExt(filename) { return filenameSansExt; } + +$.fn.serializeObject = function() +{ + var o = {}; + var a = this.serializeArray(); + $.each(a, function() { + if (o[this.name] !== undefined) { + if (!o[this.name].push) { + o[this.name] = [o[this.name]]; + } + o[this.name].push(this.value || ''); + } else { + o[this.name] = this.value || ''; + } + }); + return o; +} +