mirror of
https://github.com/sstent/node.git
synced 2026-01-25 14:42:00 +00:00
added activity blocks
This commit is contained in:
@@ -53,12 +53,21 @@ app.post('/input', store.input_post_handler);
|
|||||||
|
|
||||||
|
|
||||||
app.get('/items', function(req, res) {
|
app.get('/items', function(req, res) {
|
||||||
testcollection.find({}, function(err, result) {
|
// testcollection.find({}, function(err, result) {
|
||||||
result.each(function(err, data) {
|
// if (err) throw err;
|
||||||
console.log(data);
|
// result.each(function(err, data) {
|
||||||
emitdata('populate',data);
|
// if (err) throw err;
|
||||||
});
|
// console.log('This Is What I Got' + JSON.stringify(data));
|
||||||
|
// emitdata('populate',data);
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
|
||||||
|
testcollection.find().toArray(function(err, result) {
|
||||||
|
if (err) throw err;
|
||||||
|
console.log('This Is What I Got ' +JSON.stringify(result));
|
||||||
|
emitdata('populate', result);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (typeof req.session.username == 'undefined') res.redirect('/');
|
if (typeof req.session.username == 'undefined') res.redirect('/');
|
||||||
else res.render('items', { title: 'Ninja Store - Items'});
|
else res.render('items', { title: 'Ninja Store - Items'});
|
||||||
// else res.render('items', { title: 'Ninja Store - Items', username: req.session.username, items:items, keys:keys, fieldkeys:fieldkeys });
|
// else res.render('items', { title: 'Ninja Store - Items', username: req.session.username, items:items, keys:keys, fieldkeys:fieldkeys });
|
||||||
@@ -94,28 +103,28 @@ io.sockets.on('connection', function (socket) {
|
|||||||
var io = require('socket.io');
|
var io = require('socket.io');
|
||||||
io = io.listen(app);
|
io = io.listen(app);
|
||||||
io.configure('development', function(){
|
io.configure('development', function(){
|
||||||
io.set('close timeout', '50');
|
io.set("transports", ["xhr-polling"]);
|
||||||
|
io.set("polling duration", 10);
|
||||||
});
|
});
|
||||||
|
|
||||||
io.sockets.on('connection', function(client) {
|
io.sockets.on('connection', function(socket) {
|
||||||
|
|
||||||
console.log('Client connected');
|
console.log('Client connected');
|
||||||
|
|
||||||
// client add new employee
|
socket.on('data', function(data) {
|
||||||
client.on('add employee', function(data) {
|
|
||||||
console.log("addemployee ")
|
|
||||||
addemployee(data);
|
|
||||||
});
|
|
||||||
client.on('data', function(data) {
|
|
||||||
console.log("data" + JSON.stringify(data))
|
console.log("data" + JSON.stringify(data))
|
||||||
testcollection.insert(data, function(err, result) {
|
testcollection.insert(data, function(err, result) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
if (result) console.log('Added!');
|
if (result) console.log('Added!' + result);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// client add new employee
|
||||||
|
io.sockets.on('add employee', function(data) {
|
||||||
|
console.log("addemployee ")
|
||||||
|
addemployee(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,325 +1,325 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2010 Maxim Vasiliev
|
* Copyright (c) 2010 Maxim Vasiliev
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
* 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
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* @author Maxim Vasiliev
|
* @author Maxim Vasiliev
|
||||||
* Date: 09.09.2010
|
* Date: 09.09.2010
|
||||||
* Time: 19:02:33
|
* Time: 19:02:33
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
var form2js = (function()
|
var form2js = (function()
|
||||||
{
|
{
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns form values represented as Javascript object
|
* Returns form values represented as Javascript object
|
||||||
* "name" attribute defines structure of resulting object
|
* "name" attribute defines structure of resulting object
|
||||||
*
|
*
|
||||||
* @param rootNode {Element|String} root form element (or it's id) or array of root elements
|
* @param rootNode {Element|String} root form element (or it's id) or array of root elements
|
||||||
* @param delimiter {String} structure parts delimiter defaults to '.'
|
* @param delimiter {String} structure parts delimiter defaults to '.'
|
||||||
* @param skipEmpty {Boolean} should skip empty text values, defaults to true
|
* @param skipEmpty {Boolean} should skip empty text values, defaults to true
|
||||||
* @param nodeCallback {Function} custom function to get node value
|
* @param nodeCallback {Function} custom function to get node value
|
||||||
* @param useIdIfEmptyName {Boolean} if true value of id attribute of field will be used if name of field is empty
|
* @param useIdIfEmptyName {Boolean} if true value of id attribute of field will be used if name of field is empty
|
||||||
*/
|
*/
|
||||||
function form2js(rootNode, delimiter, skipEmpty, nodeCallback, useIdIfEmptyName)
|
function form2js(rootNode, delimiter, skipEmpty, nodeCallback, useIdIfEmptyName)
|
||||||
{
|
{
|
||||||
if (typeof skipEmpty == 'undefined' || skipEmpty == null) skipEmpty = true;
|
if (typeof skipEmpty == 'undefined' || skipEmpty == null) skipEmpty = true;
|
||||||
if (typeof delimiter == 'undefined' || delimiter == null) delimiter = '.';
|
if (typeof delimiter == 'undefined' || delimiter == null) delimiter = '.';
|
||||||
if (arguments.length < 5) useIdIfEmptyName = false;
|
if (arguments.length < 5) useIdIfEmptyName = false;
|
||||||
|
|
||||||
rootNode = typeof rootNode == 'string' ? document.getElementById(rootNode) : rootNode;
|
rootNode = typeof rootNode == 'string' ? document.getElementById(rootNode) : rootNode;
|
||||||
|
|
||||||
var formValues = [],
|
var formValues = [],
|
||||||
currNode,
|
currNode,
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
/* If rootNode is array - combine values */
|
/* If rootNode is array - combine values */
|
||||||
if (rootNode.constructor == Array || (typeof NodeList != "undefined" && rootNode.constructor == NodeList))
|
if (rootNode.constructor == Array || (typeof NodeList != "undefined" && rootNode.constructor == NodeList))
|
||||||
{
|
{
|
||||||
while(currNode = rootNode[i++])
|
while(currNode = rootNode[i++])
|
||||||
{
|
{
|
||||||
formValues = formValues.concat(getFormValues(currNode, nodeCallback, useIdIfEmptyName));
|
formValues = formValues.concat(getFormValues(currNode, nodeCallback, useIdIfEmptyName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
formValues = getFormValues(rootNode, nodeCallback, useIdIfEmptyName);
|
formValues = getFormValues(rootNode, nodeCallback, useIdIfEmptyName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return processNameValues(formValues, skipEmpty, delimiter);
|
return processNameValues(formValues, skipEmpty, delimiter);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processes collection of { name: 'name', value: 'value' } objects.
|
* Processes collection of { name: 'name', value: 'value' } objects.
|
||||||
* @param nameValues
|
* @param nameValues
|
||||||
* @param skipEmpty if true skips elements with value == '' or value == null
|
* @param skipEmpty if true skips elements with value == '' or value == null
|
||||||
* @param delimiter
|
* @param delimiter
|
||||||
*/
|
*/
|
||||||
function processNameValues(nameValues, skipEmpty, delimiter)
|
function processNameValues(nameValues, skipEmpty, delimiter)
|
||||||
{
|
{
|
||||||
var result = {},
|
var result = {},
|
||||||
arrays = {},
|
arrays = {},
|
||||||
i, j, k, l,
|
i, j, k, l,
|
||||||
value,
|
value,
|
||||||
nameParts,
|
nameParts,
|
||||||
currResult,
|
currResult,
|
||||||
arrNameFull,
|
arrNameFull,
|
||||||
arrName,
|
arrName,
|
||||||
arrIdx,
|
arrIdx,
|
||||||
namePart,
|
namePart,
|
||||||
name,
|
name,
|
||||||
_nameParts;
|
_nameParts;
|
||||||
|
|
||||||
for (i = 0; i < nameValues.length; i++)
|
for (i = 0; i < nameValues.length; i++)
|
||||||
{
|
{
|
||||||
value = nameValues[i].value;
|
value = nameValues[i].value;
|
||||||
|
|
||||||
if (skipEmpty && (value === '' || value === null)) continue;
|
if (skipEmpty && (value === '' || value === null)) continue;
|
||||||
|
|
||||||
name = nameValues[i].name;
|
name = nameValues[i].name;
|
||||||
_nameParts = name.split(delimiter);
|
_nameParts = name.split(delimiter);
|
||||||
nameParts = [];
|
nameParts = [];
|
||||||
currResult = result;
|
currResult = result;
|
||||||
arrNameFull = '';
|
arrNameFull = '';
|
||||||
|
|
||||||
for(j = 0; j < _nameParts.length; j++)
|
for(j = 0; j < _nameParts.length; j++)
|
||||||
{
|
{
|
||||||
namePart = _nameParts[j].split('][');
|
namePart = _nameParts[j].split('][');
|
||||||
if (namePart.length > 1)
|
if (namePart.length > 1)
|
||||||
{
|
{
|
||||||
for(k = 0; k < namePart.length; k++)
|
for(k = 0; k < namePart.length; k++)
|
||||||
{
|
{
|
||||||
if (k == 0)
|
if (k == 0)
|
||||||
{
|
{
|
||||||
namePart[k] = namePart[k] + ']';
|
namePart[k] = namePart[k] + ']';
|
||||||
}
|
}
|
||||||
else if (k == namePart.length - 1)
|
else if (k == namePart.length - 1)
|
||||||
{
|
{
|
||||||
namePart[k] = '[' + namePart[k];
|
namePart[k] = '[' + namePart[k];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
namePart[k] = '[' + namePart[k] + ']';
|
namePart[k] = '[' + namePart[k] + ']';
|
||||||
}
|
}
|
||||||
|
|
||||||
arrIdx = namePart[k].match(/([a-z_]+)?\[([a-z_][a-z0-9_]+?)\]/i);
|
arrIdx = namePart[k].match(/([a-z_]+)?\[([a-z_][a-z0-9_]+?)\]/i);
|
||||||
if (arrIdx)
|
if (arrIdx)
|
||||||
{
|
{
|
||||||
for(l = 1; l < arrIdx.length; l++)
|
for(l = 1; l < arrIdx.length; l++)
|
||||||
{
|
{
|
||||||
if (arrIdx[l]) nameParts.push(arrIdx[l]);
|
if (arrIdx[l]) nameParts.push(arrIdx[l]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
nameParts.push(namePart[k]);
|
nameParts.push(namePart[k]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
nameParts = nameParts.concat(namePart);
|
nameParts = nameParts.concat(namePart);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j = 0; j < nameParts.length; j++)
|
for (j = 0; j < nameParts.length; j++)
|
||||||
{
|
{
|
||||||
namePart = nameParts[j];
|
namePart = nameParts[j];
|
||||||
|
|
||||||
if (namePart.indexOf('[]') > -1 && j == nameParts.length - 1)
|
if (namePart.indexOf('[]') > -1 && j == nameParts.length - 1)
|
||||||
{
|
{
|
||||||
arrName = namePart.substr(0, namePart.indexOf('['));
|
arrName = namePart.substr(0, namePart.indexOf('['));
|
||||||
arrNameFull += arrName;
|
arrNameFull += arrName;
|
||||||
|
|
||||||
if (!currResult[arrName]) currResult[arrName] = [];
|
if (!currResult[arrName]) currResult[arrName] = [];
|
||||||
currResult[arrName].push(value);
|
currResult[arrName].push(value);
|
||||||
}
|
}
|
||||||
else if (namePart.indexOf('[') > -1)
|
else if (namePart.indexOf('[') > -1)
|
||||||
{
|
{
|
||||||
arrName = namePart.substr(0, namePart.indexOf('['));
|
arrName = namePart.substr(0, namePart.indexOf('['));
|
||||||
arrIdx = namePart.replace(/(^([a-z_]+)?\[)|(\]$)/gi, '');
|
arrIdx = namePart.replace(/(^([a-z_]+)?\[)|(\]$)/gi, '');
|
||||||
|
|
||||||
/* Unique array name */
|
/* Unique array name */
|
||||||
arrNameFull += '_' + arrName + '_' + arrIdx;
|
arrNameFull += '_' + arrName + '_' + arrIdx;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Because arrIdx in field name can be not zero-based and step can be
|
* Because arrIdx in field name can be not zero-based and step can be
|
||||||
* other than 1, we can't use them in target array directly.
|
* other than 1, we can't use them in target array directly.
|
||||||
* Instead we're making a hash where key is arrIdx and value is a reference to
|
* Instead we're making a hash where key is arrIdx and value is a reference to
|
||||||
* added array element
|
* added array element
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (!arrays[arrNameFull]) arrays[arrNameFull] = {};
|
if (!arrays[arrNameFull]) arrays[arrNameFull] = {};
|
||||||
if (arrName != '' && !currResult[arrName]) currResult[arrName] = [];
|
if (arrName != '' && !currResult[arrName]) currResult[arrName] = [];
|
||||||
|
|
||||||
if (j == nameParts.length - 1)
|
if (j == nameParts.length - 1)
|
||||||
{
|
{
|
||||||
if (arrName == '')
|
if (arrName == '')
|
||||||
{
|
{
|
||||||
currResult.push(value);
|
currResult.push(value);
|
||||||
arrays[arrNameFull][arrIdx] = currResult[currResult.length - 1];
|
arrays[arrNameFull][arrIdx] = currResult[currResult.length - 1];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
currResult[arrName].push(value);
|
currResult[arrName].push(value);
|
||||||
arrays[arrNameFull][arrIdx] = currResult[arrName][currResult[arrName].length - 1];
|
arrays[arrNameFull][arrIdx] = currResult[arrName][currResult[arrName].length - 1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!arrays[arrNameFull][arrIdx])
|
if (!arrays[arrNameFull][arrIdx])
|
||||||
{
|
{
|
||||||
if ((/^[a-z_]+\[?/i).test(nameParts[j+1])) currResult[arrName].push({});
|
if ((/^[a-z_]+\[?/i).test(nameParts[j+1])) currResult[arrName].push({});
|
||||||
else currResult[arrName].push([]);
|
else currResult[arrName].push([]);
|
||||||
|
|
||||||
arrays[arrNameFull][arrIdx] = currResult[arrName][currResult[arrName].length - 1];
|
arrays[arrNameFull][arrIdx] = currResult[arrName][currResult[arrName].length - 1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
currResult = arrays[arrNameFull][arrIdx];
|
currResult = arrays[arrNameFull][arrIdx];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
arrNameFull += namePart;
|
arrNameFull += namePart;
|
||||||
|
|
||||||
if (j < nameParts.length - 1) /* Not the last part of name - means object */
|
if (j < nameParts.length - 1) /* Not the last part of name - means object */
|
||||||
{
|
{
|
||||||
if (!currResult[namePart]) currResult[namePart] = {};
|
if (!currResult[namePart]) currResult[namePart] = {};
|
||||||
currResult = currResult[namePart];
|
currResult = currResult[namePart];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
currResult[namePart] = value;
|
currResult[namePart] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFormValues(rootNode, nodeCallback, useIdIfEmptyName)
|
function getFormValues(rootNode, nodeCallback, useIdIfEmptyName)
|
||||||
{
|
{
|
||||||
var result = extractNodeValues(rootNode, nodeCallback, useIdIfEmptyName);
|
var result = extractNodeValues(rootNode, nodeCallback, useIdIfEmptyName);
|
||||||
return result.length > 0 ? result : getSubFormValues(rootNode, nodeCallback, useIdIfEmptyName);
|
return result.length > 0 ? result : getSubFormValues(rootNode, nodeCallback, useIdIfEmptyName);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSubFormValues(rootNode, nodeCallback, useIdIfEmptyName)
|
function getSubFormValues(rootNode, nodeCallback, useIdIfEmptyName)
|
||||||
{
|
{
|
||||||
var result = [],
|
var result = [],
|
||||||
currentNode = rootNode.firstChild;
|
currentNode = rootNode.firstChild;
|
||||||
|
|
||||||
while (currentNode)
|
while (currentNode)
|
||||||
{
|
{
|
||||||
result = result.concat(extractNodeValues(currentNode, nodeCallback, useIdIfEmptyName));
|
result = result.concat(extractNodeValues(currentNode, nodeCallback, useIdIfEmptyName));
|
||||||
currentNode = currentNode.nextSibling;
|
currentNode = currentNode.nextSibling;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractNodeValues(node, nodeCallback, useIdIfEmptyName) {
|
function extractNodeValues(node, nodeCallback, useIdIfEmptyName) {
|
||||||
var callbackResult, fieldValue, result, fieldName = getFieldName(node, useIdIfEmptyName);
|
var callbackResult, fieldValue, result, fieldName = getFieldName(node, useIdIfEmptyName);
|
||||||
|
|
||||||
callbackResult = nodeCallback && nodeCallback(node);
|
callbackResult = nodeCallback && nodeCallback(node);
|
||||||
|
|
||||||
if (callbackResult && callbackResult.name) {
|
if (callbackResult && callbackResult.name) {
|
||||||
result = [callbackResult];
|
result = [callbackResult];
|
||||||
}
|
}
|
||||||
else if (fieldName != '' && node.nodeName.match(/INPUT|TEXTAREA/i)) {
|
else if (fieldName != '' && node.nodeName.match(/INPUT|TEXTAREA/i)) {
|
||||||
fieldValue = getFieldValue(node);
|
fieldValue = getFieldValue(node);
|
||||||
result = [ { name: fieldName, value: fieldValue} ];
|
result = [ { name: fieldName, value: fieldValue} ];
|
||||||
}
|
}
|
||||||
else if (fieldName != '' && node.nodeName.match(/SELECT/i)) {
|
else if (fieldName != '' && node.nodeName.match(/SELECT/i)) {
|
||||||
fieldValue = getFieldValue(node);
|
fieldValue = getFieldValue(node);
|
||||||
result = [ { name: fieldName.replace(/\[\]$/, ''), value: fieldValue } ];
|
result = [ { name: fieldName.replace(/\[\]$/, ''), value: fieldValue } ];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result = getSubFormValues(node, nodeCallback, useIdIfEmptyName);
|
result = getSubFormValues(node, nodeCallback, useIdIfEmptyName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFieldName(node, useIdIfEmptyName)
|
function getFieldName(node, useIdIfEmptyName)
|
||||||
{
|
{
|
||||||
if (node.name && node.name != '') return node.name;
|
if (node.name && node.name != '') return node.name;
|
||||||
else if (useIdIfEmptyName && node.id && node.id != '') return node.id;
|
else if (useIdIfEmptyName && node.id && node.id != '') return node.id;
|
||||||
else return '';
|
else return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function getFieldValue(fieldNode)
|
function getFieldValue(fieldNode)
|
||||||
{
|
{
|
||||||
if (fieldNode.disabled) return null;
|
if (fieldNode.disabled) return null;
|
||||||
|
|
||||||
switch (fieldNode.nodeName) {
|
switch (fieldNode.nodeName) {
|
||||||
case 'INPUT':
|
case 'INPUT':
|
||||||
case 'TEXTAREA':
|
case 'TEXTAREA':
|
||||||
switch (fieldNode.type.toLowerCase()) {
|
switch (fieldNode.type.toLowerCase()) {
|
||||||
case 'radio':
|
case 'radio':
|
||||||
case 'checkbox':
|
case 'checkbox':
|
||||||
if (fieldNode.checked && fieldNode.value === "true") return true;
|
if (fieldNode.checked && fieldNode.value === "true") return true;
|
||||||
if (!fieldNode.checked && fieldNode.value === "true") return false;
|
if (!fieldNode.checked && fieldNode.value === "true") return false;
|
||||||
if (fieldNode.checked) return fieldNode.value;
|
if (fieldNode.checked) return fieldNode.value;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'button':
|
case 'button':
|
||||||
case 'reset':
|
case 'reset':
|
||||||
case 'submit':
|
case 'submit':
|
||||||
case 'image':
|
case 'image':
|
||||||
return '';
|
return '';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return fieldNode.value;
|
return fieldNode.value;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'SELECT':
|
case 'SELECT':
|
||||||
return getSelectedOptionValue(fieldNode);
|
return getSelectedOptionValue(fieldNode);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSelectedOptionValue(selectNode)
|
function getSelectedOptionValue(selectNode)
|
||||||
{
|
{
|
||||||
var multiple = selectNode.multiple,
|
var multiple = selectNode.multiple,
|
||||||
result = [],
|
result = [],
|
||||||
options,
|
options,
|
||||||
i, l;
|
i, l;
|
||||||
|
|
||||||
if (!multiple) return selectNode.value;
|
if (!multiple) return selectNode.value;
|
||||||
|
|
||||||
for (options = selectNode.getElementsByTagName("option"), i = 0, l = options.length; i < l; i++)
|
for (options = selectNode.getElementsByTagName("option"), i = 0, l = options.length; i < l; i++)
|
||||||
{
|
{
|
||||||
if (options[i].selected) result.push(options[i].value);
|
if (options[i].selected) result.push(options[i].value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return form2js;
|
return form2js;
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -1,66 +1,66 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2010 Maxim Vasiliev
|
* Copyright (c) 2010 Maxim Vasiliev
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
* 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
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* @author Maxim Vasiliev
|
* @author Maxim Vasiliev
|
||||||
* Date: 29.06.11
|
* Date: 29.06.11
|
||||||
* Time: 20:09
|
* Time: 20:09
|
||||||
*/
|
*/
|
||||||
|
|
||||||
(function($){
|
(function($){
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* jQuery wrapper for form2object()
|
* jQuery wrapper for form2object()
|
||||||
* Extracts data from child inputs into javascript object
|
* Extracts data from child inputs into javascript object
|
||||||
*/
|
*/
|
||||||
$.fn.toObject = function(options)
|
$.fn.toObject = function(options)
|
||||||
{
|
{
|
||||||
var result = [],
|
var result = [],
|
||||||
settings = {
|
settings = {
|
||||||
mode: 'first', // what to convert: 'all' or 'first' matched node
|
mode: 'first', // what to convert: 'all' or 'first' matched node
|
||||||
delimiter: ".",
|
delimiter: ".",
|
||||||
skipEmpty: true,
|
skipEmpty: true,
|
||||||
nodeCallback: null,
|
nodeCallback: null,
|
||||||
useIdIfEmptyName: false
|
useIdIfEmptyName: false
|
||||||
};
|
};
|
||||||
|
|
||||||
if (options)
|
if (options)
|
||||||
{
|
{
|
||||||
$.extend(settings, options);
|
$.extend(settings, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(settings.mode)
|
switch(settings.mode)
|
||||||
{
|
{
|
||||||
case 'first':
|
case 'first':
|
||||||
return form2js(this.get(0), settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName);
|
return form2js(this.get(0), settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName);
|
||||||
break;
|
break;
|
||||||
case 'all':
|
case 'all':
|
||||||
this.each(function(){
|
this.each(function(){
|
||||||
result.push(form2js(this, settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName));
|
result.push(form2js(this, settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName));
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
break;
|
break;
|
||||||
case 'combine':
|
case 'combine':
|
||||||
return form2js(Array.prototype.slice.call(this), settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName);
|
return form2js(Array.prototype.slice.call(this), settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
})(jQuery);
|
})(jQuery);
|
||||||
@@ -1,309 +1,309 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2010 Maxim Vasiliev
|
* Copyright (c) 2010 Maxim Vasiliev
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
* furnished to do so, subject to the following conditions:
|
* furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
* 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
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*
|
*
|
||||||
* @author Maxim Vasiliev
|
* @author Maxim Vasiliev
|
||||||
* Date: 19.09.11
|
* Date: 19.09.11
|
||||||
* Time: 23:40
|
* Time: 23:40
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var js2form = (function()
|
var js2form = (function()
|
||||||
{
|
{
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var _subArrayRegexp = /^\[\d+?\]/,
|
var _subArrayRegexp = /^\[\d+?\]/,
|
||||||
_subObjectRegexp = /^[a-zA-Z_][a-zA-Z_0-9]+/,
|
_subObjectRegexp = /^[a-zA-Z_][a-zA-Z_0-9]+/,
|
||||||
_arrayItemRegexp = /\[[0-9]+?\]$/,
|
_arrayItemRegexp = /\[[0-9]+?\]$/,
|
||||||
_lastIndexedArrayRegexp = /(.*)(\[)([0-9]*)(\])$/,
|
_lastIndexedArrayRegexp = /(.*)(\[)([0-9]*)(\])$/,
|
||||||
_arrayOfArraysRegexp = /\[([0-9]+)\]\[([0-9]+)\]/g,
|
_arrayOfArraysRegexp = /\[([0-9]+)\]\[([0-9]+)\]/g,
|
||||||
_inputOrTextareaRegexp = /INPUT|TEXTAREA/i;
|
_inputOrTextareaRegexp = /INPUT|TEXTAREA/i;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param rootNode
|
* @param rootNode
|
||||||
* @param data
|
* @param data
|
||||||
* @param delimiter
|
* @param delimiter
|
||||||
* @param nodeCallback
|
* @param nodeCallback
|
||||||
* @param useIdIfEmptyName
|
* @param useIdIfEmptyName
|
||||||
*/
|
*/
|
||||||
function js2form(rootNode, data, delimiter, nodeCallback, useIdIfEmptyName)
|
function js2form(rootNode, data, delimiter, nodeCallback, useIdIfEmptyName)
|
||||||
{
|
{
|
||||||
if (arguments.length < 3) delimiter = '.';
|
if (arguments.length < 3) delimiter = '.';
|
||||||
if (arguments.length < 4) nodeCallback = null;
|
if (arguments.length < 4) nodeCallback = null;
|
||||||
if (arguments.length < 5) useIdIfEmptyName = false;
|
if (arguments.length < 5) useIdIfEmptyName = false;
|
||||||
|
|
||||||
var fieldValues,
|
var fieldValues,
|
||||||
formFieldsByName;
|
formFieldsByName;
|
||||||
|
|
||||||
fieldValues = object2array(data);
|
fieldValues = object2array(data);
|
||||||
formFieldsByName = getFields(rootNode, useIdIfEmptyName, delimiter, {}, true);
|
formFieldsByName = getFields(rootNode, useIdIfEmptyName, delimiter, {}, true);
|
||||||
|
|
||||||
for (var i = 0; i < fieldValues.length; i++)
|
for (var i = 0; i < fieldValues.length; i++)
|
||||||
{
|
{
|
||||||
var fieldName = fieldValues[i].name,
|
var fieldName = fieldValues[i].name,
|
||||||
fieldValue = fieldValues[i].value;
|
fieldValue = fieldValues[i].value;
|
||||||
|
|
||||||
if (typeof formFieldsByName[fieldName] != 'undefined')
|
if (typeof formFieldsByName[fieldName] != 'undefined')
|
||||||
{
|
{
|
||||||
setValue(formFieldsByName[fieldName], fieldValue);
|
setValue(formFieldsByName[fieldName], fieldValue);
|
||||||
}
|
}
|
||||||
else if (typeof formFieldsByName[fieldName.replace(_arrayItemRegexp, '[]')] != 'undefined')
|
else if (typeof formFieldsByName[fieldName.replace(_arrayItemRegexp, '[]')] != 'undefined')
|
||||||
{
|
{
|
||||||
setValue(formFieldsByName[fieldName.replace(_arrayItemRegexp, '[]')], fieldValue);
|
setValue(formFieldsByName[fieldName.replace(_arrayItemRegexp, '[]')], fieldValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setValue(field, value)
|
function setValue(field, value)
|
||||||
{
|
{
|
||||||
var children, i, l;
|
var children, i, l;
|
||||||
|
|
||||||
if (field instanceof Array)
|
if (field instanceof Array)
|
||||||
{
|
{
|
||||||
for(i = 0; i < field.length; i++)
|
for(i = 0; i < field.length; i++)
|
||||||
{
|
{
|
||||||
if (field[i].value == value) field[i].checked = true;
|
if (field[i].value == value) field[i].checked = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (_inputOrTextareaRegexp.test(field.nodeName))
|
else if (_inputOrTextareaRegexp.test(field.nodeName))
|
||||||
{
|
{
|
||||||
field.value = value;
|
field.value = value;
|
||||||
}
|
}
|
||||||
else if (/SELECT/i.test(field.nodeName))
|
else if (/SELECT/i.test(field.nodeName))
|
||||||
{
|
{
|
||||||
children = field.getElementsByTagName('option');
|
children = field.getElementsByTagName('option');
|
||||||
for (i = 0,l = children.length; i < l; i++)
|
for (i = 0,l = children.length; i < l; i++)
|
||||||
{
|
{
|
||||||
if (children[i].value == value)
|
if (children[i].value == value)
|
||||||
{
|
{
|
||||||
children[i].selected = true;
|
children[i].selected = true;
|
||||||
if (field.multiple) break;
|
if (field.multiple) break;
|
||||||
}
|
}
|
||||||
else if (!field.multiple)
|
else if (!field.multiple)
|
||||||
{
|
{
|
||||||
children[i].selected = false;
|
children[i].selected = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFields(rootNode, useIdIfEmptyName, delimiter, arrayIndexes, shouldClean)
|
function getFields(rootNode, useIdIfEmptyName, delimiter, arrayIndexes, shouldClean)
|
||||||
{
|
{
|
||||||
if (arguments.length < 4) arrayIndexes = {};
|
if (arguments.length < 4) arrayIndexes = {};
|
||||||
|
|
||||||
var result = {},
|
var result = {},
|
||||||
currNode = rootNode.firstChild,
|
currNode = rootNode.firstChild,
|
||||||
name, nameNormalized,
|
name, nameNormalized,
|
||||||
subFieldName,
|
subFieldName,
|
||||||
i, j, l,
|
i, j, l,
|
||||||
options;
|
options;
|
||||||
|
|
||||||
while (currNode)
|
while (currNode)
|
||||||
{
|
{
|
||||||
name = '';
|
name = '';
|
||||||
|
|
||||||
if (currNode.name && currNode.name != '')
|
if (currNode.name && currNode.name != '')
|
||||||
{
|
{
|
||||||
name = currNode.name;
|
name = currNode.name;
|
||||||
}
|
}
|
||||||
else if (useIdIfEmptyName && currNode.id && currNode.id != '')
|
else if (useIdIfEmptyName && currNode.id && currNode.id != '')
|
||||||
{
|
{
|
||||||
name = currNode.id;
|
name = currNode.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name == '')
|
if (name == '')
|
||||||
{
|
{
|
||||||
var subFields = getFields(currNode, useIdIfEmptyName, delimiter, arrayIndexes, shouldClean);
|
var subFields = getFields(currNode, useIdIfEmptyName, delimiter, arrayIndexes, shouldClean);
|
||||||
for (subFieldName in subFields)
|
for (subFieldName in subFields)
|
||||||
{
|
{
|
||||||
if (typeof result[subFieldName] == 'undefined')
|
if (typeof result[subFieldName] == 'undefined')
|
||||||
{
|
{
|
||||||
result[subFieldName] = subFields[subFieldName];
|
result[subFieldName] = subFields[subFieldName];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (i = 0; i < subFields[subFieldName].length; i++)
|
for (i = 0; i < subFields[subFieldName].length; i++)
|
||||||
{
|
{
|
||||||
result[subFieldName].push(subFields[subFieldName][i]);
|
result[subFieldName].push(subFields[subFieldName][i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (/SELECT/i.test(currNode.nodeName))
|
if (/SELECT/i.test(currNode.nodeName))
|
||||||
{
|
{
|
||||||
for(j = 0, options = currNode.getElementsByTagName('option'), l = options.length; j < l; j++)
|
for(j = 0, options = currNode.getElementsByTagName('option'), l = options.length; j < l; j++)
|
||||||
{
|
{
|
||||||
if (shouldClean)
|
if (shouldClean)
|
||||||
{
|
{
|
||||||
options[j].selected = false;
|
options[j].selected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
nameNormalized = normalizeName(name, delimiter, arrayIndexes);
|
nameNormalized = normalizeName(name, delimiter, arrayIndexes);
|
||||||
result[nameNormalized] = currNode;
|
result[nameNormalized] = currNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (/INPUT/i.test(currNode.nodeName) && /CHECKBOX|RADIO/i.test(currNode.type))
|
else if (/INPUT/i.test(currNode.nodeName) && /CHECKBOX|RADIO/i.test(currNode.type))
|
||||||
{
|
{
|
||||||
if(shouldClean)
|
if(shouldClean)
|
||||||
{
|
{
|
||||||
currNode.checked = false;
|
currNode.checked = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
nameNormalized = normalizeName(name, delimiter, arrayIndexes);
|
nameNormalized = normalizeName(name, delimiter, arrayIndexes);
|
||||||
nameNormalized = nameNormalized.replace(_arrayItemRegexp, '[]');
|
nameNormalized = nameNormalized.replace(_arrayItemRegexp, '[]');
|
||||||
if (!result[nameNormalized]) result[nameNormalized] = [];
|
if (!result[nameNormalized]) result[nameNormalized] = [];
|
||||||
result[nameNormalized].push(currNode);
|
result[nameNormalized].push(currNode);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (shouldClean)
|
if (shouldClean)
|
||||||
{
|
{
|
||||||
currNode.value = '';
|
currNode.value = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
nameNormalized = normalizeName(name, delimiter, arrayIndexes);
|
nameNormalized = normalizeName(name, delimiter, arrayIndexes);
|
||||||
result[nameNormalized] = currNode;
|
result[nameNormalized] = currNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
currNode = currNode.nextSibling;
|
currNode = currNode.nextSibling;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalizes names of arrays, puts correct indexes (consecutive and ordered by element appearance in HTML)
|
* Normalizes names of arrays, puts correct indexes (consecutive and ordered by element appearance in HTML)
|
||||||
* @param name
|
* @param name
|
||||||
* @param delimiter
|
* @param delimiter
|
||||||
* @param arrayIndexes
|
* @param arrayIndexes
|
||||||
*/
|
*/
|
||||||
function normalizeName(name, delimiter, arrayIndexes)
|
function normalizeName(name, delimiter, arrayIndexes)
|
||||||
{
|
{
|
||||||
var nameChunksNormalized = [],
|
var nameChunksNormalized = [],
|
||||||
nameChunks = name.split(delimiter),
|
nameChunks = name.split(delimiter),
|
||||||
currChunk,
|
currChunk,
|
||||||
nameMatches,
|
nameMatches,
|
||||||
nameNormalized,
|
nameNormalized,
|
||||||
currIndex,
|
currIndex,
|
||||||
newIndex,
|
newIndex,
|
||||||
i;
|
i;
|
||||||
|
|
||||||
name = name.replace(_arrayOfArraysRegexp, '[$1].[$2]');
|
name = name.replace(_arrayOfArraysRegexp, '[$1].[$2]');
|
||||||
for (i = 0; i < nameChunks.length; i++)
|
for (i = 0; i < nameChunks.length; i++)
|
||||||
{
|
{
|
||||||
currChunk = nameChunks[i];
|
currChunk = nameChunks[i];
|
||||||
nameChunksNormalized.push(currChunk);
|
nameChunksNormalized.push(currChunk);
|
||||||
nameMatches = currChunk.match(_lastIndexedArrayRegexp);
|
nameMatches = currChunk.match(_lastIndexedArrayRegexp);
|
||||||
if (nameMatches != null)
|
if (nameMatches != null)
|
||||||
{
|
{
|
||||||
nameNormalized = nameChunksNormalized.join(delimiter);
|
nameNormalized = nameChunksNormalized.join(delimiter);
|
||||||
currIndex = nameNormalized.replace(_lastIndexedArrayRegexp, '$3');
|
currIndex = nameNormalized.replace(_lastIndexedArrayRegexp, '$3');
|
||||||
nameNormalized = nameNormalized.replace(_lastIndexedArrayRegexp, '$1');
|
nameNormalized = nameNormalized.replace(_lastIndexedArrayRegexp, '$1');
|
||||||
|
|
||||||
if (typeof (arrayIndexes[nameNormalized]) == 'undefined')
|
if (typeof (arrayIndexes[nameNormalized]) == 'undefined')
|
||||||
{
|
{
|
||||||
arrayIndexes[nameNormalized] = {
|
arrayIndexes[nameNormalized] = {
|
||||||
lastIndex: -1,
|
lastIndex: -1,
|
||||||
indexes: {}
|
indexes: {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currIndex == '' || typeof arrayIndexes[nameNormalized].indexes[currIndex] == 'undefined')
|
if (currIndex == '' || typeof arrayIndexes[nameNormalized].indexes[currIndex] == 'undefined')
|
||||||
{
|
{
|
||||||
arrayIndexes[nameNormalized].lastIndex++;
|
arrayIndexes[nameNormalized].lastIndex++;
|
||||||
arrayIndexes[nameNormalized].indexes[currIndex] = arrayIndexes[nameNormalized].lastIndex;
|
arrayIndexes[nameNormalized].indexes[currIndex] = arrayIndexes[nameNormalized].lastIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
newIndex = arrayIndexes[nameNormalized].indexes[currIndex];
|
newIndex = arrayIndexes[nameNormalized].indexes[currIndex];
|
||||||
nameChunksNormalized[nameChunksNormalized.length - 1] = currChunk.replace(_lastIndexedArrayRegexp, '$1$2' + newIndex + '$4');
|
nameChunksNormalized[nameChunksNormalized.length - 1] = currChunk.replace(_lastIndexedArrayRegexp, '$1$2' + newIndex + '$4');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nameNormalized = nameChunksNormalized.join(delimiter);
|
nameNormalized = nameChunksNormalized.join(delimiter);
|
||||||
nameNormalized = nameNormalized.replace('].[', '][');
|
nameNormalized = nameNormalized.replace('].[', '][');
|
||||||
return nameNormalized;
|
return nameNormalized;
|
||||||
}
|
}
|
||||||
|
|
||||||
function object2array(obj, lvl)
|
function object2array(obj, lvl)
|
||||||
{
|
{
|
||||||
var result = [], i, name;
|
var result = [], i, name;
|
||||||
|
|
||||||
if (arguments.length == 1) lvl = 0;
|
if (arguments.length == 1) lvl = 0;
|
||||||
|
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
{
|
{
|
||||||
result = [{ name: "", value: null }];
|
result = [{ name: "", value: null }];
|
||||||
}
|
}
|
||||||
else if (typeof obj == 'string' || typeof obj == 'number' || typeof obj == 'date' || typeof obj == 'boolean')
|
else if (typeof obj == 'string' || typeof obj == 'number' || typeof obj == 'date' || typeof obj == 'boolean')
|
||||||
{
|
{
|
||||||
result = [
|
result = [
|
||||||
{ name: "", value : obj }
|
{ name: "", value : obj }
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
else if (obj instanceof Array)
|
else if (obj instanceof Array)
|
||||||
{
|
{
|
||||||
for (i = 0; i < obj.length; i++)
|
for (i = 0; i < obj.length; i++)
|
||||||
{
|
{
|
||||||
name = "[" + i + "]";
|
name = "[" + i + "]";
|
||||||
result = result.concat(getSubValues(obj[i], name, lvl + 1));
|
result = result.concat(getSubValues(obj[i], name, lvl + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (i in obj)
|
for (i in obj)
|
||||||
{
|
{
|
||||||
name = i;
|
name = i;
|
||||||
result = result.concat(getSubValues(obj[i], name, lvl + 1));
|
result = result.concat(getSubValues(obj[i], name, lvl + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSubValues(subObj, name, lvl)
|
function getSubValues(subObj, name, lvl)
|
||||||
{
|
{
|
||||||
var itemName;
|
var itemName;
|
||||||
var result = [], tempResult = object2array(subObj, lvl + 1), i, tempItem;
|
var result = [], tempResult = object2array(subObj, lvl + 1), i, tempItem;
|
||||||
|
|
||||||
for (i = 0; i < tempResult.length; i++)
|
for (i = 0; i < tempResult.length; i++)
|
||||||
{
|
{
|
||||||
itemName = name;
|
itemName = name;
|
||||||
if (_subArrayRegexp.test(tempResult[i].name))
|
if (_subArrayRegexp.test(tempResult[i].name))
|
||||||
{
|
{
|
||||||
itemName += tempResult[i].name;
|
itemName += tempResult[i].name;
|
||||||
}
|
}
|
||||||
else if (_subObjectRegexp.test(tempResult[i].name))
|
else if (_subObjectRegexp.test(tempResult[i].name))
|
||||||
{
|
{
|
||||||
itemName += '.' + tempResult[i].name;
|
itemName += '.' + tempResult[i].name;
|
||||||
}
|
}
|
||||||
|
|
||||||
tempItem = { name: itemName, value: tempResult[i].value };
|
tempItem = { name: itemName, value: tempResult[i].value };
|
||||||
result.push(tempItem);
|
result.push(tempItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return js2form;
|
return js2form;
|
||||||
|
|
||||||
})();
|
})();
|
||||||
@@ -5,25 +5,33 @@
|
|||||||
include userbar
|
include userbar
|
||||||
|
|
||||||
div
|
div
|
||||||
input(type='button', class='AddNeut', value='Add note Field', data-field='note_area', data-area='note_', data-limit='0', data-jsontag='activity.note')
|
input(type='Button', id='AddAct', value='Add Activity')
|
||||||
input(type='button', class='AddNeut', value='Add exercise Field', data-field='exercise_area', data-area='exercise_', data-limit='0',data-jsontag='activity.exercise')
|
|
||||||
input(type='submit', id='save', value='Save')
|
|
||||||
|
div(id='Activity',style='display: none')
|
||||||
|
form#myForm
|
||||||
form#myForm
|
input(type='submit', id='save', value='Save')
|
||||||
ol(id='note_area')
|
div(Class='ActivityBlock_T', style='display: none')
|
||||||
li(class='note_T', style='display: none')
|
input(type='button', class='AddNeut', value='Add note Field', data-field='note_area', data-area='note_', data-limit='0', data-jsontag='activity.note')
|
||||||
label Note
|
input(type='button', class='AddNeut', value='Add exercise Field', data-field='exercise_area', data-area='exercise_', data-limit='0',data-jsontag='activity.exercise')
|
||||||
input(type='text', name='activity.note[]', value='Note', disabled='disabled')
|
input(type='button', class='RemNeut', value='Remove note Field', data-field='note_area', data-area='note_', data-limit='0', data-jsontag='activity.note', disabled='disabled')
|
||||||
a(style='cursor:pointer;color:blue;', onclick='this.parentNode.parentNode.removeChild(this.parentNode);') Remove Field
|
input(type='button', class='RemNeut', value='Remove exercise Field', data-field='exercise_area', data-area='exercise_', data-limit='0',data-jsontag='activity.exercise', disabled='disabled' )
|
||||||
ol(id='exercise_area')
|
ul(class='Activity_area')
|
||||||
li(class='exercise_T', style='display: none')
|
li(class='exercise_T', style='display: none')
|
||||||
label Exercise
|
label Activity
|
||||||
input(type='text', name='activity.exercise[].name', value='Name', disabled='disabled')
|
input(type='text', name='activity.name', value='Name', disabled='disabled')
|
||||||
input(type='text', name='activity.exercise[].sets', value='Sets', disabled='disabled', class='numericonly')
|
ul(class='note_area')
|
||||||
input(type='text', name='activity.exercise[].reps', value='Reps', disabled='disabled')
|
li(class='note_T', style='display: none')
|
||||||
input(type='text', name='activity.exercise[x].weight', value='Weight', disabled='disabled')
|
label Note
|
||||||
a(style='cursor:pointer;color:blue;', onclick='this.parentNode.parentNode.removeChild(this.parentNode);') Remove Field
|
input(type='text', name='activity.note[]', value='Note', disabled='disabled')
|
||||||
|
ul(class='exercise_area')
|
||||||
|
li(class='exercise_T', style='display: none')
|
||||||
|
label Exercise
|
||||||
|
input(type='text', name='activity.exercise[].name', value='Name', disabled='disabled')
|
||||||
|
input(type='text', name='activity.exercise[].sets', value='Sets', disabled='disabled', class='numericonly')
|
||||||
|
input(type='text', name='activity.exercise[].reps', value='Reps', disabled='disabled')
|
||||||
|
input(type='text', name='activity.exercise[].weight', value='Weight', disabled='disabled')
|
||||||
|
|
||||||
ul#employees
|
ul#employees
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,36 +9,58 @@ html
|
|||||||
script(src='/form2js/jquery.toObject.js')
|
script(src='/form2js/jquery.toObject.js')
|
||||||
script(src='/form2js/json2.js')
|
script(src='/form2js/json2.js')
|
||||||
script
|
script
|
||||||
//$(document).ready(function() {
|
$(function() {
|
||||||
|
|
||||||
var socket = io.connect('http://localhost:3000');
|
var socket = io.connect('http://localhost:3000');
|
||||||
|
|
||||||
socket.on('populate', function(json) {
|
socket.on('populate', function(json) {
|
||||||
var out = "";
|
var content = "";
|
||||||
for (var n in json) { // Each top-level entry
|
$('#employees').empty();
|
||||||
out += '<li>' + n + '<ul>';
|
//iterate activities
|
||||||
for (var i = 0; i < json[n].length; i++) { // Each sub-entry
|
$.each (json, function (bb) {
|
||||||
out += '<li>' + json[n][i] + '</li>';
|
var activity = json[bb].activity;
|
||||||
};
|
content += '<p>Activity - '+ bb + '</p>';
|
||||||
out += '</ul></li>';
|
$.each (activity.note, function (cc) {
|
||||||
};
|
content += '<p>Note - '+ cc + '</p>';
|
||||||
console.log('out ' + out);
|
});
|
||||||
$('#employees').html(out);
|
$.each (activity.exercise, function (cc) {
|
||||||
|
content += '<p>Exercise '+ cc +' - name:' + activity.exercise[cc].name +'</p>';
|
||||||
|
content += '<p>Exercise '+ cc +' - sets:' + activity.exercise[cc].sets +'</p>';
|
||||||
|
content += '<p>Exercise '+ cc +' - reps:' + activity.exercise[cc].reps +'</p>';
|
||||||
|
content += '<p>Exercise '+ cc +' - weight:' + activity.exercise[cc].weight +'</p>';
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
$(content).appendTo("#employees");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
$(function() {
|
|
||||||
|
$('#AddAct').click(function() {
|
||||||
|
$('#Activity').attr('style', 'display: block');
|
||||||
|
var last_item = $('.ActivityBlock').length;
|
||||||
|
//if last_item = 0
|
||||||
|
var newElem = $('.ActivityBlock_T').clone().attr('style', 'display: block');
|
||||||
|
$(newElem).attr('class', 'ActivityBlock');
|
||||||
|
$(newElem).attr('id', 'ActivityBlock' + (last_item + 1) );
|
||||||
|
$(newElem).children('.AddNeut').attr('data-activity','ActivityBlock' + (last_item + 1));
|
||||||
|
$(newElem).children('.RemNeut').attr('data-activity','ActivityBlock' + (last_item + 1));
|
||||||
|
$(newElem).children('.AddNeut').attr('data-activity','ActivityBlock' + (last_item + 1));
|
||||||
|
$(newElem).appendTo('form#myForm');
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
//Add more fields dynamically.
|
//Add more fields dynamically.
|
||||||
$('.AddNeut').click(function() {
|
$('.AddNeut').click(function() {
|
||||||
var field = $(this).attr('data-field');
|
var field = $(this).attr('data-field');
|
||||||
var area = $(this).attr('data-area');
|
var area = $(this).attr('data-area');
|
||||||
var limit = $(this).attr('data-limit');
|
var limit = $(this).attr('data-limit');
|
||||||
var jsontag = $(this).attr('data-jsontag');
|
var jsontag = $(this).attr('data-jsontag');
|
||||||
|
var actblock = $(this).attr('data-activity');
|
||||||
var last_item = $('.' + area ).length;
|
var last_item = $('.' + area ).length;
|
||||||
console.log('.' + area + ' li')
|
console.log('div#' + actblock + ' ul#' + field + ' li:first')
|
||||||
console.log('LastItem - ',last_item, 'next_Item - ', (last_item + 1) );
|
console.log('LastItem - ',last_item, 'next_Item - ', (last_item + 1) );
|
||||||
// create the new element via clone(), and manipulate it's ID using newNum value
|
// create the new element via clone(), and manipulate it's ID using newNum value
|
||||||
var newElem = $('ol#' + field + ' li:first').clone().attr('style', 'display: block');
|
var newElem = $('div#' + actblock + ' ul.' + field + ' li:first').clone().attr('style', 'display: block');
|
||||||
$(newElem).attr('class', area);
|
$(newElem).attr('class', area);
|
||||||
$(newElem).children('input').attr('disabled',false);
|
$(newElem).children('input').attr('disabled',false);
|
||||||
$(newElem).children('input').each(function(){
|
$(newElem).children('input').each(function(){
|
||||||
@@ -46,10 +68,34 @@ html
|
|||||||
$(this).attr('name', newName);
|
$(this).attr('name', newName);
|
||||||
console.log('name ' + newName);
|
console.log('name ' + newName);
|
||||||
});
|
});
|
||||||
$(newElem).appendTo('ol#' +field);
|
$(newElem).appendTo('ul#' +field);
|
||||||
|
$('#'+ area + 'rem').attr('disabled',false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$('.RemNeut').click(function() {
|
||||||
|
var field = $(this).attr('data-field');
|
||||||
|
var area = $(this).attr('data-area');
|
||||||
|
var limit = $(this).attr('data-limit');
|
||||||
|
var last_item = $('.' + area ).length;
|
||||||
|
var actblock = $(this).attr('data-activity');
|
||||||
|
console.log('.' + area + ' li')
|
||||||
|
console.log('LastItem - ',last_item, 'next_Item - ', (last_item - 1) );
|
||||||
|
if (last_item != 0)
|
||||||
|
$('div#' + actblock + ' ul.' + field + ' li:last').remove();
|
||||||
|
|
||||||
|
// enable the "add" button
|
||||||
|
$('.AddNeut#' + area).attr('disabled',false);
|
||||||
|
|
||||||
|
// if only one element remains, disable the "remove" button
|
||||||
|
if ((last_item - 1) == 0)
|
||||||
|
$('#'+ area + 'rem').attr('disabled','disabled');
|
||||||
|
|
||||||
|
console.log('#'+ area + ' .RemNeut')
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$('#save').click(function() {
|
$('#save').click(function() {
|
||||||
var selector= "#myForm"
|
var selector= "#myForm"
|
||||||
//var formDataFirst = $(selector).toObject({mode: 'first'});
|
//var formDataFirst = $(selector).toObject({mode: 'first'});
|
||||||
@@ -58,7 +104,7 @@ html
|
|||||||
|
|
||||||
console.log('All ', JSON.stringify(formDataAll, null, '\t'));
|
console.log('All ', JSON.stringify(formDataAll, null, '\t'));
|
||||||
// to prevent the page from changing
|
// to prevent the page from changing
|
||||||
return false;
|
//return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user