Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Vasiliev committed Sep 19, 2011
1 parent b611d38 commit e3a5af1
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
79 changes: 79 additions & 0 deletions example/js2form.example.html
@@ -0,0 +1,79 @@
<!doctype html>

<!--[if lt IE 7 ]> <html lang="ru" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]> <html lang="ru" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]> <html lang="ru" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]> <html lang="ru" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->
<html lang="ru" class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
</head>

<body>
<form id="testForm">
<dl>
<dt><label>First name</label></dt>
<dd><input type="text" name="person.name.first" value="John"></dd>
</dl>
<dl>
<dt><label>Last name</label></dt>
<dd><input type="text" name="person.name.last" value="Doe"></dd>
</dl>
<dl>
<dt><label>First name</label></dt>
<dd><input type="text" name="neo.name.first" value="Thomas"></dd>
</dl>
<dl>
<dt><label>Last name</label></dt>
<dd><input type="text" name="neo.name.last" value="Anderson"></dd>
</dl>
<dl>
<dt><label>Emails</label></dt>
<dd><input type="text" name="neo.Emails[]" value="email1@example.com"></dd>
<dd><input type="text" name="neo.Emails[]" value="email2@example.com"></dd>
<dd><input type="text" name="neo.Emails[]" value="email3@example.com"></dd>
<dd><input type="text" name="neo.Emails[]" value="email4@example.com"></dd>
<dd><input type="text" name="neo.Emails[]" value="email5@example.com"></dd>
<dd><input type="text" name="neo.Emails[]" value="email6@example.com"></dd>
</dl>
<dl>
<dt><label>Addresses[0]</label></dt>
<dd><input type="text" name="neo.Addresses[0].Zip" value="12345"></dd>
<dd><input type="text" name="neo.Addresses[0].City" value="NY"></dd>
<dd><input type="text" name="neo.Addresses[0].Street" value="Boardwalk"></dd>
</dl>
<dl>
<dt><label>Addresses[0]</label></dt>
<dd><input type="text" name="neo.Addresses[1].Zip" value="65432"></dd>
<dd><input type="text" name="neo.Addresses[1].City" value="LA"></dd>
<dd><input type="text" name="neo.Addresses[1].Street" value="Under the bridge"></dd>
</dl>
<button type="button" onclick="getJson()">Get JSON</button>
<button type="reset">Reset form</button>
</form>

<div>
<textarea id="src" cols="100" rows="20"></textarea>
</div>
<button type="button" onclick="populateForm()">Populate form</button>

<script type="text/javascript" src="../src/form2js.js"></script>
<script type="text/javascript" src="../src/js2form.js"></script>
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript">
function getJson()
{
document.getElementById('src').value = JSON.stringify(form2js(document.getElementById('testForm')));
}

function populateForm()
{
var data = document.getElementById('src').value;
data = JSON.parse(data);
js2form(document.getElementById('testForm'), data);
}
</script>
</body>
</html>
20 changes: 20 additions & 0 deletions src/jquery.toObject.js
@@ -1,4 +1,24 @@
/**
* Copyright (c) 2010 Maxim Vasiliev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Maxim Vasiliev
* Date: 29.06.11
* Time: 20:09
Expand Down
112 changes: 112 additions & 0 deletions src/js2form.js
@@ -0,0 +1,112 @@
/**
* Copyright (c) 2010 Maxim Vasiliev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Maxim Vasiliev
* Date: 19.09.11
* Time: 23:40
*/

var js2form = (function()
{
"use strict";

var _subArrayRegexp = /^\[\d+?\]/,
_subObjectRegexp = /^[a-zA-Z_]+/;

/**
*
* @param rootNode
* @param data
* @param delimiter
* @param nodeCallback
* @param useIdIfEmptyName
*/
function js2form(rootNode, data, delimiter, nodeCallback, useIdIfEmptyName)
{
console.log(JSON.stringify(object2array(data)));
}

function object2array(obj, lvl)
{
var result = [], i, name;

if (arguments.length == 1) lvl = 0;

if (obj instanceof Array)
{
for(i = 0; i < obj.length; i++)
{
name = "[" + i + "]";
result = result.concat(getSubValues(obj[i], name, lvl+1));
}
}
else if (typeof obj == 'string' || typeof obj == 'number' || typeof obj == 'date')
{
result = [{ name: "", value : obj }];
}
else
{
for(i in obj)
{
name = i;
result = result.concat(getSubValues(obj[i], name, lvl+1));
}
}

return result;
}

function getSubValues(subObj, name, lvl)
{
var itemName;
var result = [], tempResult = object2array(subObj, lvl+1), i, tempItem;

for(i = 0; i < tempResult.length; i++)
{
itemName = name;
if (_subArrayRegexp.test(tempResult[i].name))
{
itemName += tempResult[i].name;
}
else if (_subObjectRegexp.test(tempResult[i].name))
{
itemName += '.' + tempResult[i].name;
}

tempItem = { name: itemName, value: tempResult[i].value };
result.push(tempItem);
}
return result;
}

function tabs(lvl)
{
var result = '';
for (var i = 0; i < lvl; i++){
result += '\t';
}

return result;
}

return js2form;

})();

0 comments on commit e3a5af1

Please sign in to comment.