json indent, change json format, implement load json file

This commit is contained in:
Sanj 2011-05-26 00:31:22 +05:30
parent 589694e376
commit ecd4e695cd
4 changed files with 47 additions and 5 deletions

View File

@ -22,7 +22,7 @@ body {
float: left;
}
#notesArea {
#notes {
width: 90%;
height: 600px;
}

View File

@ -13,6 +13,7 @@
<button id="saveLocationBtn">Select Output Folder</button>
<button id="selectAudioFile">Select Audio File</button>
<button id="saveFormData">Save Metadata</button>
<button id="loadData">Load Data</button>
</div>
<div style="clear:both;"></div>
<form id="metadataForm" action="" method="GET">
@ -70,7 +71,7 @@
</div>
<div id="rightForm">
<textarea id="notesArea"></textarea>
<textarea name="notes" id="notes"></textarea>
</div>
</form>
</div>

View File

@ -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]);
}
}
}
}

View File

@ -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;
}