mirror of
https://github.com/sstent/node.git
synced 2026-01-26 07:02:31 +00:00
added activity blocks
This commit is contained in:
@@ -1,325 +1,325 @@
|
||||
/**
|
||||
* 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: 09.09.2010
|
||||
* Time: 19:02:33
|
||||
*/
|
||||
|
||||
|
||||
var form2js = (function()
|
||||
{
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Returns form values represented as Javascript 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 delimiter {String} structure parts delimiter defaults to '.'
|
||||
* @param skipEmpty {Boolean} should skip empty text values, defaults to true
|
||||
* @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
|
||||
*/
|
||||
function form2js(rootNode, delimiter, skipEmpty, nodeCallback, useIdIfEmptyName)
|
||||
{
|
||||
if (typeof skipEmpty == 'undefined' || skipEmpty == null) skipEmpty = true;
|
||||
if (typeof delimiter == 'undefined' || delimiter == null) delimiter = '.';
|
||||
if (arguments.length < 5) useIdIfEmptyName = false;
|
||||
|
||||
rootNode = typeof rootNode == 'string' ? document.getElementById(rootNode) : rootNode;
|
||||
|
||||
var formValues = [],
|
||||
currNode,
|
||||
i = 0;
|
||||
|
||||
/* If rootNode is array - combine values */
|
||||
if (rootNode.constructor == Array || (typeof NodeList != "undefined" && rootNode.constructor == NodeList))
|
||||
{
|
||||
while(currNode = rootNode[i++])
|
||||
{
|
||||
formValues = formValues.concat(getFormValues(currNode, nodeCallback, useIdIfEmptyName));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
formValues = getFormValues(rootNode, nodeCallback, useIdIfEmptyName);
|
||||
}
|
||||
|
||||
return processNameValues(formValues, skipEmpty, delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes collection of { name: 'name', value: 'value' } objects.
|
||||
* @param nameValues
|
||||
* @param skipEmpty if true skips elements with value == '' or value == null
|
||||
* @param delimiter
|
||||
*/
|
||||
function processNameValues(nameValues, skipEmpty, delimiter)
|
||||
{
|
||||
var result = {},
|
||||
arrays = {},
|
||||
i, j, k, l,
|
||||
value,
|
||||
nameParts,
|
||||
currResult,
|
||||
arrNameFull,
|
||||
arrName,
|
||||
arrIdx,
|
||||
namePart,
|
||||
name,
|
||||
_nameParts;
|
||||
|
||||
for (i = 0; i < nameValues.length; i++)
|
||||
{
|
||||
value = nameValues[i].value;
|
||||
|
||||
if (skipEmpty && (value === '' || value === null)) continue;
|
||||
|
||||
name = nameValues[i].name;
|
||||
_nameParts = name.split(delimiter);
|
||||
nameParts = [];
|
||||
currResult = result;
|
||||
arrNameFull = '';
|
||||
|
||||
for(j = 0; j < _nameParts.length; j++)
|
||||
{
|
||||
namePart = _nameParts[j].split('][');
|
||||
if (namePart.length > 1)
|
||||
{
|
||||
for(k = 0; k < namePart.length; k++)
|
||||
{
|
||||
if (k == 0)
|
||||
{
|
||||
namePart[k] = namePart[k] + ']';
|
||||
}
|
||||
else if (k == namePart.length - 1)
|
||||
{
|
||||
namePart[k] = '[' + namePart[k];
|
||||
}
|
||||
else
|
||||
{
|
||||
namePart[k] = '[' + namePart[k] + ']';
|
||||
}
|
||||
|
||||
arrIdx = namePart[k].match(/([a-z_]+)?\[([a-z_][a-z0-9_]+?)\]/i);
|
||||
if (arrIdx)
|
||||
{
|
||||
for(l = 1; l < arrIdx.length; l++)
|
||||
{
|
||||
if (arrIdx[l]) nameParts.push(arrIdx[l]);
|
||||
}
|
||||
}
|
||||
else{
|
||||
nameParts.push(namePart[k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
nameParts = nameParts.concat(namePart);
|
||||
}
|
||||
|
||||
for (j = 0; j < nameParts.length; j++)
|
||||
{
|
||||
namePart = nameParts[j];
|
||||
|
||||
if (namePart.indexOf('[]') > -1 && j == nameParts.length - 1)
|
||||
{
|
||||
arrName = namePart.substr(0, namePart.indexOf('['));
|
||||
arrNameFull += arrName;
|
||||
|
||||
if (!currResult[arrName]) currResult[arrName] = [];
|
||||
currResult[arrName].push(value);
|
||||
}
|
||||
else if (namePart.indexOf('[') > -1)
|
||||
{
|
||||
arrName = namePart.substr(0, namePart.indexOf('['));
|
||||
arrIdx = namePart.replace(/(^([a-z_]+)?\[)|(\]$)/gi, '');
|
||||
|
||||
/* Unique array name */
|
||||
arrNameFull += '_' + arrName + '_' + arrIdx;
|
||||
|
||||
/*
|
||||
* 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.
|
||||
* Instead we're making a hash where key is arrIdx and value is a reference to
|
||||
* added array element
|
||||
*/
|
||||
|
||||
if (!arrays[arrNameFull]) arrays[arrNameFull] = {};
|
||||
if (arrName != '' && !currResult[arrName]) currResult[arrName] = [];
|
||||
|
||||
if (j == nameParts.length - 1)
|
||||
{
|
||||
if (arrName == '')
|
||||
{
|
||||
currResult.push(value);
|
||||
arrays[arrNameFull][arrIdx] = currResult[currResult.length - 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
currResult[arrName].push(value);
|
||||
arrays[arrNameFull][arrIdx] = currResult[arrName][currResult[arrName].length - 1];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!arrays[arrNameFull][arrIdx])
|
||||
{
|
||||
if ((/^[a-z_]+\[?/i).test(nameParts[j+1])) currResult[arrName].push({});
|
||||
else currResult[arrName].push([]);
|
||||
|
||||
arrays[arrNameFull][arrIdx] = currResult[arrName][currResult[arrName].length - 1];
|
||||
}
|
||||
}
|
||||
|
||||
currResult = arrays[arrNameFull][arrIdx];
|
||||
}
|
||||
else
|
||||
{
|
||||
arrNameFull += namePart;
|
||||
|
||||
if (j < nameParts.length - 1) /* Not the last part of name - means object */
|
||||
{
|
||||
if (!currResult[namePart]) currResult[namePart] = {};
|
||||
currResult = currResult[namePart];
|
||||
}
|
||||
else
|
||||
{
|
||||
currResult[namePart] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function getFormValues(rootNode, nodeCallback, useIdIfEmptyName)
|
||||
{
|
||||
var result = extractNodeValues(rootNode, nodeCallback, useIdIfEmptyName);
|
||||
return result.length > 0 ? result : getSubFormValues(rootNode, nodeCallback, useIdIfEmptyName);
|
||||
}
|
||||
|
||||
function getSubFormValues(rootNode, nodeCallback, useIdIfEmptyName)
|
||||
{
|
||||
var result = [],
|
||||
currentNode = rootNode.firstChild;
|
||||
|
||||
while (currentNode)
|
||||
{
|
||||
result = result.concat(extractNodeValues(currentNode, nodeCallback, useIdIfEmptyName));
|
||||
currentNode = currentNode.nextSibling;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function extractNodeValues(node, nodeCallback, useIdIfEmptyName) {
|
||||
var callbackResult, fieldValue, result, fieldName = getFieldName(node, useIdIfEmptyName);
|
||||
|
||||
callbackResult = nodeCallback && nodeCallback(node);
|
||||
|
||||
if (callbackResult && callbackResult.name) {
|
||||
result = [callbackResult];
|
||||
}
|
||||
else if (fieldName != '' && node.nodeName.match(/INPUT|TEXTAREA/i)) {
|
||||
fieldValue = getFieldValue(node);
|
||||
result = [ { name: fieldName, value: fieldValue} ];
|
||||
}
|
||||
else if (fieldName != '' && node.nodeName.match(/SELECT/i)) {
|
||||
fieldValue = getFieldValue(node);
|
||||
result = [ { name: fieldName.replace(/\[\]$/, ''), value: fieldValue } ];
|
||||
}
|
||||
else {
|
||||
result = getSubFormValues(node, nodeCallback, useIdIfEmptyName);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function getFieldName(node, useIdIfEmptyName)
|
||||
{
|
||||
if (node.name && node.name != '') return node.name;
|
||||
else if (useIdIfEmptyName && node.id && node.id != '') return node.id;
|
||||
else return '';
|
||||
}
|
||||
|
||||
|
||||
function getFieldValue(fieldNode)
|
||||
{
|
||||
if (fieldNode.disabled) return null;
|
||||
|
||||
switch (fieldNode.nodeName) {
|
||||
case 'INPUT':
|
||||
case 'TEXTAREA':
|
||||
switch (fieldNode.type.toLowerCase()) {
|
||||
case 'radio':
|
||||
case 'checkbox':
|
||||
if (fieldNode.checked && fieldNode.value === "true") return true;
|
||||
if (!fieldNode.checked && fieldNode.value === "true") return false;
|
||||
if (fieldNode.checked) return fieldNode.value;
|
||||
break;
|
||||
|
||||
case 'button':
|
||||
case 'reset':
|
||||
case 'submit':
|
||||
case 'image':
|
||||
return '';
|
||||
break;
|
||||
|
||||
default:
|
||||
return fieldNode.value;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'SELECT':
|
||||
return getSelectedOptionValue(fieldNode);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getSelectedOptionValue(selectNode)
|
||||
{
|
||||
var multiple = selectNode.multiple,
|
||||
result = [],
|
||||
options,
|
||||
i, l;
|
||||
|
||||
if (!multiple) return selectNode.value;
|
||||
|
||||
for (options = selectNode.getElementsByTagName("option"), i = 0, l = options.length; i < l; i++)
|
||||
{
|
||||
if (options[i].selected) result.push(options[i].value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return form2js;
|
||||
|
||||
})();
|
||||
/**
|
||||
* 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: 09.09.2010
|
||||
* Time: 19:02:33
|
||||
*/
|
||||
|
||||
|
||||
var form2js = (function()
|
||||
{
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Returns form values represented as Javascript 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 delimiter {String} structure parts delimiter defaults to '.'
|
||||
* @param skipEmpty {Boolean} should skip empty text values, defaults to true
|
||||
* @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
|
||||
*/
|
||||
function form2js(rootNode, delimiter, skipEmpty, nodeCallback, useIdIfEmptyName)
|
||||
{
|
||||
if (typeof skipEmpty == 'undefined' || skipEmpty == null) skipEmpty = true;
|
||||
if (typeof delimiter == 'undefined' || delimiter == null) delimiter = '.';
|
||||
if (arguments.length < 5) useIdIfEmptyName = false;
|
||||
|
||||
rootNode = typeof rootNode == 'string' ? document.getElementById(rootNode) : rootNode;
|
||||
|
||||
var formValues = [],
|
||||
currNode,
|
||||
i = 0;
|
||||
|
||||
/* If rootNode is array - combine values */
|
||||
if (rootNode.constructor == Array || (typeof NodeList != "undefined" && rootNode.constructor == NodeList))
|
||||
{
|
||||
while(currNode = rootNode[i++])
|
||||
{
|
||||
formValues = formValues.concat(getFormValues(currNode, nodeCallback, useIdIfEmptyName));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
formValues = getFormValues(rootNode, nodeCallback, useIdIfEmptyName);
|
||||
}
|
||||
|
||||
return processNameValues(formValues, skipEmpty, delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes collection of { name: 'name', value: 'value' } objects.
|
||||
* @param nameValues
|
||||
* @param skipEmpty if true skips elements with value == '' or value == null
|
||||
* @param delimiter
|
||||
*/
|
||||
function processNameValues(nameValues, skipEmpty, delimiter)
|
||||
{
|
||||
var result = {},
|
||||
arrays = {},
|
||||
i, j, k, l,
|
||||
value,
|
||||
nameParts,
|
||||
currResult,
|
||||
arrNameFull,
|
||||
arrName,
|
||||
arrIdx,
|
||||
namePart,
|
||||
name,
|
||||
_nameParts;
|
||||
|
||||
for (i = 0; i < nameValues.length; i++)
|
||||
{
|
||||
value = nameValues[i].value;
|
||||
|
||||
if (skipEmpty && (value === '' || value === null)) continue;
|
||||
|
||||
name = nameValues[i].name;
|
||||
_nameParts = name.split(delimiter);
|
||||
nameParts = [];
|
||||
currResult = result;
|
||||
arrNameFull = '';
|
||||
|
||||
for(j = 0; j < _nameParts.length; j++)
|
||||
{
|
||||
namePart = _nameParts[j].split('][');
|
||||
if (namePart.length > 1)
|
||||
{
|
||||
for(k = 0; k < namePart.length; k++)
|
||||
{
|
||||
if (k == 0)
|
||||
{
|
||||
namePart[k] = namePart[k] + ']';
|
||||
}
|
||||
else if (k == namePart.length - 1)
|
||||
{
|
||||
namePart[k] = '[' + namePart[k];
|
||||
}
|
||||
else
|
||||
{
|
||||
namePart[k] = '[' + namePart[k] + ']';
|
||||
}
|
||||
|
||||
arrIdx = namePart[k].match(/([a-z_]+)?\[([a-z_][a-z0-9_]+?)\]/i);
|
||||
if (arrIdx)
|
||||
{
|
||||
for(l = 1; l < arrIdx.length; l++)
|
||||
{
|
||||
if (arrIdx[l]) nameParts.push(arrIdx[l]);
|
||||
}
|
||||
}
|
||||
else{
|
||||
nameParts.push(namePart[k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
nameParts = nameParts.concat(namePart);
|
||||
}
|
||||
|
||||
for (j = 0; j < nameParts.length; j++)
|
||||
{
|
||||
namePart = nameParts[j];
|
||||
|
||||
if (namePart.indexOf('[]') > -1 && j == nameParts.length - 1)
|
||||
{
|
||||
arrName = namePart.substr(0, namePart.indexOf('['));
|
||||
arrNameFull += arrName;
|
||||
|
||||
if (!currResult[arrName]) currResult[arrName] = [];
|
||||
currResult[arrName].push(value);
|
||||
}
|
||||
else if (namePart.indexOf('[') > -1)
|
||||
{
|
||||
arrName = namePart.substr(0, namePart.indexOf('['));
|
||||
arrIdx = namePart.replace(/(^([a-z_]+)?\[)|(\]$)/gi, '');
|
||||
|
||||
/* Unique array name */
|
||||
arrNameFull += '_' + arrName + '_' + arrIdx;
|
||||
|
||||
/*
|
||||
* 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.
|
||||
* Instead we're making a hash where key is arrIdx and value is a reference to
|
||||
* added array element
|
||||
*/
|
||||
|
||||
if (!arrays[arrNameFull]) arrays[arrNameFull] = {};
|
||||
if (arrName != '' && !currResult[arrName]) currResult[arrName] = [];
|
||||
|
||||
if (j == nameParts.length - 1)
|
||||
{
|
||||
if (arrName == '')
|
||||
{
|
||||
currResult.push(value);
|
||||
arrays[arrNameFull][arrIdx] = currResult[currResult.length - 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
currResult[arrName].push(value);
|
||||
arrays[arrNameFull][arrIdx] = currResult[arrName][currResult[arrName].length - 1];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!arrays[arrNameFull][arrIdx])
|
||||
{
|
||||
if ((/^[a-z_]+\[?/i).test(nameParts[j+1])) currResult[arrName].push({});
|
||||
else currResult[arrName].push([]);
|
||||
|
||||
arrays[arrNameFull][arrIdx] = currResult[arrName][currResult[arrName].length - 1];
|
||||
}
|
||||
}
|
||||
|
||||
currResult = arrays[arrNameFull][arrIdx];
|
||||
}
|
||||
else
|
||||
{
|
||||
arrNameFull += namePart;
|
||||
|
||||
if (j < nameParts.length - 1) /* Not the last part of name - means object */
|
||||
{
|
||||
if (!currResult[namePart]) currResult[namePart] = {};
|
||||
currResult = currResult[namePart];
|
||||
}
|
||||
else
|
||||
{
|
||||
currResult[namePart] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function getFormValues(rootNode, nodeCallback, useIdIfEmptyName)
|
||||
{
|
||||
var result = extractNodeValues(rootNode, nodeCallback, useIdIfEmptyName);
|
||||
return result.length > 0 ? result : getSubFormValues(rootNode, nodeCallback, useIdIfEmptyName);
|
||||
}
|
||||
|
||||
function getSubFormValues(rootNode, nodeCallback, useIdIfEmptyName)
|
||||
{
|
||||
var result = [],
|
||||
currentNode = rootNode.firstChild;
|
||||
|
||||
while (currentNode)
|
||||
{
|
||||
result = result.concat(extractNodeValues(currentNode, nodeCallback, useIdIfEmptyName));
|
||||
currentNode = currentNode.nextSibling;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function extractNodeValues(node, nodeCallback, useIdIfEmptyName) {
|
||||
var callbackResult, fieldValue, result, fieldName = getFieldName(node, useIdIfEmptyName);
|
||||
|
||||
callbackResult = nodeCallback && nodeCallback(node);
|
||||
|
||||
if (callbackResult && callbackResult.name) {
|
||||
result = [callbackResult];
|
||||
}
|
||||
else if (fieldName != '' && node.nodeName.match(/INPUT|TEXTAREA/i)) {
|
||||
fieldValue = getFieldValue(node);
|
||||
result = [ { name: fieldName, value: fieldValue} ];
|
||||
}
|
||||
else if (fieldName != '' && node.nodeName.match(/SELECT/i)) {
|
||||
fieldValue = getFieldValue(node);
|
||||
result = [ { name: fieldName.replace(/\[\]$/, ''), value: fieldValue } ];
|
||||
}
|
||||
else {
|
||||
result = getSubFormValues(node, nodeCallback, useIdIfEmptyName);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function getFieldName(node, useIdIfEmptyName)
|
||||
{
|
||||
if (node.name && node.name != '') return node.name;
|
||||
else if (useIdIfEmptyName && node.id && node.id != '') return node.id;
|
||||
else return '';
|
||||
}
|
||||
|
||||
|
||||
function getFieldValue(fieldNode)
|
||||
{
|
||||
if (fieldNode.disabled) return null;
|
||||
|
||||
switch (fieldNode.nodeName) {
|
||||
case 'INPUT':
|
||||
case 'TEXTAREA':
|
||||
switch (fieldNode.type.toLowerCase()) {
|
||||
case 'radio':
|
||||
case 'checkbox':
|
||||
if (fieldNode.checked && fieldNode.value === "true") return true;
|
||||
if (!fieldNode.checked && fieldNode.value === "true") return false;
|
||||
if (fieldNode.checked) return fieldNode.value;
|
||||
break;
|
||||
|
||||
case 'button':
|
||||
case 'reset':
|
||||
case 'submit':
|
||||
case 'image':
|
||||
return '';
|
||||
break;
|
||||
|
||||
default:
|
||||
return fieldNode.value;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'SELECT':
|
||||
return getSelectedOptionValue(fieldNode);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getSelectedOptionValue(selectNode)
|
||||
{
|
||||
var multiple = selectNode.multiple,
|
||||
result = [],
|
||||
options,
|
||||
i, l;
|
||||
|
||||
if (!multiple) return selectNode.value;
|
||||
|
||||
for (options = selectNode.getElementsByTagName("option"), i = 0, l = options.length; i < l; i++)
|
||||
{
|
||||
if (options[i].selected) result.push(options[i].value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return form2js;
|
||||
|
||||
})();
|
||||
|
||||
@@ -1,66 +1,66 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
(function($){
|
||||
|
||||
/**
|
||||
* jQuery wrapper for form2object()
|
||||
* Extracts data from child inputs into javascript object
|
||||
*/
|
||||
$.fn.toObject = function(options)
|
||||
{
|
||||
var result = [],
|
||||
settings = {
|
||||
mode: 'first', // what to convert: 'all' or 'first' matched node
|
||||
delimiter: ".",
|
||||
skipEmpty: true,
|
||||
nodeCallback: null,
|
||||
useIdIfEmptyName: false
|
||||
};
|
||||
|
||||
if (options)
|
||||
{
|
||||
$.extend(settings, options);
|
||||
}
|
||||
|
||||
switch(settings.mode)
|
||||
{
|
||||
case 'first':
|
||||
return form2js(this.get(0), settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName);
|
||||
break;
|
||||
case 'all':
|
||||
this.each(function(){
|
||||
result.push(form2js(this, settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName));
|
||||
});
|
||||
return result;
|
||||
break;
|
||||
case 'combine':
|
||||
return form2js(Array.prototype.slice.call(this), settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
(function($){
|
||||
|
||||
/**
|
||||
* jQuery wrapper for form2object()
|
||||
* Extracts data from child inputs into javascript object
|
||||
*/
|
||||
$.fn.toObject = function(options)
|
||||
{
|
||||
var result = [],
|
||||
settings = {
|
||||
mode: 'first', // what to convert: 'all' or 'first' matched node
|
||||
delimiter: ".",
|
||||
skipEmpty: true,
|
||||
nodeCallback: null,
|
||||
useIdIfEmptyName: false
|
||||
};
|
||||
|
||||
if (options)
|
||||
{
|
||||
$.extend(settings, options);
|
||||
}
|
||||
|
||||
switch(settings.mode)
|
||||
{
|
||||
case 'first':
|
||||
return form2js(this.get(0), settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName);
|
||||
break;
|
||||
case 'all':
|
||||
this.each(function(){
|
||||
result.push(form2js(this, settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName));
|
||||
});
|
||||
return result;
|
||||
break;
|
||||
case 'combine':
|
||||
return form2js(Array.prototype.slice.call(this), settings.delimiter, settings.skipEmpty, settings.nodeCallback, settings.useIdIfEmptyName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
})(jQuery);
|
||||
@@ -1,309 +1,309 @@
|
||||
/**
|
||||
* 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_][a-zA-Z_0-9]+/,
|
||||
_arrayItemRegexp = /\[[0-9]+?\]$/,
|
||||
_lastIndexedArrayRegexp = /(.*)(\[)([0-9]*)(\])$/,
|
||||
_arrayOfArraysRegexp = /\[([0-9]+)\]\[([0-9]+)\]/g,
|
||||
_inputOrTextareaRegexp = /INPUT|TEXTAREA/i;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param rootNode
|
||||
* @param data
|
||||
* @param delimiter
|
||||
* @param nodeCallback
|
||||
* @param useIdIfEmptyName
|
||||
*/
|
||||
function js2form(rootNode, data, delimiter, nodeCallback, useIdIfEmptyName)
|
||||
{
|
||||
if (arguments.length < 3) delimiter = '.';
|
||||
if (arguments.length < 4) nodeCallback = null;
|
||||
if (arguments.length < 5) useIdIfEmptyName = false;
|
||||
|
||||
var fieldValues,
|
||||
formFieldsByName;
|
||||
|
||||
fieldValues = object2array(data);
|
||||
formFieldsByName = getFields(rootNode, useIdIfEmptyName, delimiter, {}, true);
|
||||
|
||||
for (var i = 0; i < fieldValues.length; i++)
|
||||
{
|
||||
var fieldName = fieldValues[i].name,
|
||||
fieldValue = fieldValues[i].value;
|
||||
|
||||
if (typeof formFieldsByName[fieldName] != 'undefined')
|
||||
{
|
||||
setValue(formFieldsByName[fieldName], fieldValue);
|
||||
}
|
||||
else if (typeof formFieldsByName[fieldName.replace(_arrayItemRegexp, '[]')] != 'undefined')
|
||||
{
|
||||
setValue(formFieldsByName[fieldName.replace(_arrayItemRegexp, '[]')], fieldValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setValue(field, value)
|
||||
{
|
||||
var children, i, l;
|
||||
|
||||
if (field instanceof Array)
|
||||
{
|
||||
for(i = 0; i < field.length; i++)
|
||||
{
|
||||
if (field[i].value == value) field[i].checked = true;
|
||||
}
|
||||
}
|
||||
else if (_inputOrTextareaRegexp.test(field.nodeName))
|
||||
{
|
||||
field.value = value;
|
||||
}
|
||||
else if (/SELECT/i.test(field.nodeName))
|
||||
{
|
||||
children = field.getElementsByTagName('option');
|
||||
for (i = 0,l = children.length; i < l; i++)
|
||||
{
|
||||
if (children[i].value == value)
|
||||
{
|
||||
children[i].selected = true;
|
||||
if (field.multiple) break;
|
||||
}
|
||||
else if (!field.multiple)
|
||||
{
|
||||
children[i].selected = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getFields(rootNode, useIdIfEmptyName, delimiter, arrayIndexes, shouldClean)
|
||||
{
|
||||
if (arguments.length < 4) arrayIndexes = {};
|
||||
|
||||
var result = {},
|
||||
currNode = rootNode.firstChild,
|
||||
name, nameNormalized,
|
||||
subFieldName,
|
||||
i, j, l,
|
||||
options;
|
||||
|
||||
while (currNode)
|
||||
{
|
||||
name = '';
|
||||
|
||||
if (currNode.name && currNode.name != '')
|
||||
{
|
||||
name = currNode.name;
|
||||
}
|
||||
else if (useIdIfEmptyName && currNode.id && currNode.id != '')
|
||||
{
|
||||
name = currNode.id;
|
||||
}
|
||||
|
||||
if (name == '')
|
||||
{
|
||||
var subFields = getFields(currNode, useIdIfEmptyName, delimiter, arrayIndexes, shouldClean);
|
||||
for (subFieldName in subFields)
|
||||
{
|
||||
if (typeof result[subFieldName] == 'undefined')
|
||||
{
|
||||
result[subFieldName] = subFields[subFieldName];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = 0; i < subFields[subFieldName].length; i++)
|
||||
{
|
||||
result[subFieldName].push(subFields[subFieldName][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (/SELECT/i.test(currNode.nodeName))
|
||||
{
|
||||
for(j = 0, options = currNode.getElementsByTagName('option'), l = options.length; j < l; j++)
|
||||
{
|
||||
if (shouldClean)
|
||||
{
|
||||
options[j].selected = false;
|
||||
}
|
||||
|
||||
nameNormalized = normalizeName(name, delimiter, arrayIndexes);
|
||||
result[nameNormalized] = currNode;
|
||||
}
|
||||
}
|
||||
else if (/INPUT/i.test(currNode.nodeName) && /CHECKBOX|RADIO/i.test(currNode.type))
|
||||
{
|
||||
if(shouldClean)
|
||||
{
|
||||
currNode.checked = false;
|
||||
}
|
||||
|
||||
nameNormalized = normalizeName(name, delimiter, arrayIndexes);
|
||||
nameNormalized = nameNormalized.replace(_arrayItemRegexp, '[]');
|
||||
if (!result[nameNormalized]) result[nameNormalized] = [];
|
||||
result[nameNormalized].push(currNode);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (shouldClean)
|
||||
{
|
||||
currNode.value = '';
|
||||
}
|
||||
|
||||
nameNormalized = normalizeName(name, delimiter, arrayIndexes);
|
||||
result[nameNormalized] = currNode;
|
||||
}
|
||||
}
|
||||
|
||||
currNode = currNode.nextSibling;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes names of arrays, puts correct indexes (consecutive and ordered by element appearance in HTML)
|
||||
* @param name
|
||||
* @param delimiter
|
||||
* @param arrayIndexes
|
||||
*/
|
||||
function normalizeName(name, delimiter, arrayIndexes)
|
||||
{
|
||||
var nameChunksNormalized = [],
|
||||
nameChunks = name.split(delimiter),
|
||||
currChunk,
|
||||
nameMatches,
|
||||
nameNormalized,
|
||||
currIndex,
|
||||
newIndex,
|
||||
i;
|
||||
|
||||
name = name.replace(_arrayOfArraysRegexp, '[$1].[$2]');
|
||||
for (i = 0; i < nameChunks.length; i++)
|
||||
{
|
||||
currChunk = nameChunks[i];
|
||||
nameChunksNormalized.push(currChunk);
|
||||
nameMatches = currChunk.match(_lastIndexedArrayRegexp);
|
||||
if (nameMatches != null)
|
||||
{
|
||||
nameNormalized = nameChunksNormalized.join(delimiter);
|
||||
currIndex = nameNormalized.replace(_lastIndexedArrayRegexp, '$3');
|
||||
nameNormalized = nameNormalized.replace(_lastIndexedArrayRegexp, '$1');
|
||||
|
||||
if (typeof (arrayIndexes[nameNormalized]) == 'undefined')
|
||||
{
|
||||
arrayIndexes[nameNormalized] = {
|
||||
lastIndex: -1,
|
||||
indexes: {}
|
||||
};
|
||||
}
|
||||
|
||||
if (currIndex == '' || typeof arrayIndexes[nameNormalized].indexes[currIndex] == 'undefined')
|
||||
{
|
||||
arrayIndexes[nameNormalized].lastIndex++;
|
||||
arrayIndexes[nameNormalized].indexes[currIndex] = arrayIndexes[nameNormalized].lastIndex;
|
||||
}
|
||||
|
||||
newIndex = arrayIndexes[nameNormalized].indexes[currIndex];
|
||||
nameChunksNormalized[nameChunksNormalized.length - 1] = currChunk.replace(_lastIndexedArrayRegexp, '$1$2' + newIndex + '$4');
|
||||
}
|
||||
}
|
||||
|
||||
nameNormalized = nameChunksNormalized.join(delimiter);
|
||||
nameNormalized = nameNormalized.replace('].[', '][');
|
||||
return nameNormalized;
|
||||
}
|
||||
|
||||
function object2array(obj, lvl)
|
||||
{
|
||||
var result = [], i, name;
|
||||
|
||||
if (arguments.length == 1) lvl = 0;
|
||||
|
||||
if (obj == null)
|
||||
{
|
||||
result = [{ name: "", value: null }];
|
||||
}
|
||||
else if (typeof obj == 'string' || typeof obj == 'number' || typeof obj == 'date' || typeof obj == 'boolean')
|
||||
{
|
||||
result = [
|
||||
{ name: "", value : obj }
|
||||
];
|
||||
}
|
||||
else if (obj instanceof Array)
|
||||
{
|
||||
for (i = 0; i < obj.length; i++)
|
||||
{
|
||||
name = "[" + i + "]";
|
||||
result = result.concat(getSubValues(obj[i], name, lvl + 1));
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
return js2form;
|
||||
|
||||
/**
|
||||
* 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_][a-zA-Z_0-9]+/,
|
||||
_arrayItemRegexp = /\[[0-9]+?\]$/,
|
||||
_lastIndexedArrayRegexp = /(.*)(\[)([0-9]*)(\])$/,
|
||||
_arrayOfArraysRegexp = /\[([0-9]+)\]\[([0-9]+)\]/g,
|
||||
_inputOrTextareaRegexp = /INPUT|TEXTAREA/i;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param rootNode
|
||||
* @param data
|
||||
* @param delimiter
|
||||
* @param nodeCallback
|
||||
* @param useIdIfEmptyName
|
||||
*/
|
||||
function js2form(rootNode, data, delimiter, nodeCallback, useIdIfEmptyName)
|
||||
{
|
||||
if (arguments.length < 3) delimiter = '.';
|
||||
if (arguments.length < 4) nodeCallback = null;
|
||||
if (arguments.length < 5) useIdIfEmptyName = false;
|
||||
|
||||
var fieldValues,
|
||||
formFieldsByName;
|
||||
|
||||
fieldValues = object2array(data);
|
||||
formFieldsByName = getFields(rootNode, useIdIfEmptyName, delimiter, {}, true);
|
||||
|
||||
for (var i = 0; i < fieldValues.length; i++)
|
||||
{
|
||||
var fieldName = fieldValues[i].name,
|
||||
fieldValue = fieldValues[i].value;
|
||||
|
||||
if (typeof formFieldsByName[fieldName] != 'undefined')
|
||||
{
|
||||
setValue(formFieldsByName[fieldName], fieldValue);
|
||||
}
|
||||
else if (typeof formFieldsByName[fieldName.replace(_arrayItemRegexp, '[]')] != 'undefined')
|
||||
{
|
||||
setValue(formFieldsByName[fieldName.replace(_arrayItemRegexp, '[]')], fieldValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setValue(field, value)
|
||||
{
|
||||
var children, i, l;
|
||||
|
||||
if (field instanceof Array)
|
||||
{
|
||||
for(i = 0; i < field.length; i++)
|
||||
{
|
||||
if (field[i].value == value) field[i].checked = true;
|
||||
}
|
||||
}
|
||||
else if (_inputOrTextareaRegexp.test(field.nodeName))
|
||||
{
|
||||
field.value = value;
|
||||
}
|
||||
else if (/SELECT/i.test(field.nodeName))
|
||||
{
|
||||
children = field.getElementsByTagName('option');
|
||||
for (i = 0,l = children.length; i < l; i++)
|
||||
{
|
||||
if (children[i].value == value)
|
||||
{
|
||||
children[i].selected = true;
|
||||
if (field.multiple) break;
|
||||
}
|
||||
else if (!field.multiple)
|
||||
{
|
||||
children[i].selected = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getFields(rootNode, useIdIfEmptyName, delimiter, arrayIndexes, shouldClean)
|
||||
{
|
||||
if (arguments.length < 4) arrayIndexes = {};
|
||||
|
||||
var result = {},
|
||||
currNode = rootNode.firstChild,
|
||||
name, nameNormalized,
|
||||
subFieldName,
|
||||
i, j, l,
|
||||
options;
|
||||
|
||||
while (currNode)
|
||||
{
|
||||
name = '';
|
||||
|
||||
if (currNode.name && currNode.name != '')
|
||||
{
|
||||
name = currNode.name;
|
||||
}
|
||||
else if (useIdIfEmptyName && currNode.id && currNode.id != '')
|
||||
{
|
||||
name = currNode.id;
|
||||
}
|
||||
|
||||
if (name == '')
|
||||
{
|
||||
var subFields = getFields(currNode, useIdIfEmptyName, delimiter, arrayIndexes, shouldClean);
|
||||
for (subFieldName in subFields)
|
||||
{
|
||||
if (typeof result[subFieldName] == 'undefined')
|
||||
{
|
||||
result[subFieldName] = subFields[subFieldName];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = 0; i < subFields[subFieldName].length; i++)
|
||||
{
|
||||
result[subFieldName].push(subFields[subFieldName][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (/SELECT/i.test(currNode.nodeName))
|
||||
{
|
||||
for(j = 0, options = currNode.getElementsByTagName('option'), l = options.length; j < l; j++)
|
||||
{
|
||||
if (shouldClean)
|
||||
{
|
||||
options[j].selected = false;
|
||||
}
|
||||
|
||||
nameNormalized = normalizeName(name, delimiter, arrayIndexes);
|
||||
result[nameNormalized] = currNode;
|
||||
}
|
||||
}
|
||||
else if (/INPUT/i.test(currNode.nodeName) && /CHECKBOX|RADIO/i.test(currNode.type))
|
||||
{
|
||||
if(shouldClean)
|
||||
{
|
||||
currNode.checked = false;
|
||||
}
|
||||
|
||||
nameNormalized = normalizeName(name, delimiter, arrayIndexes);
|
||||
nameNormalized = nameNormalized.replace(_arrayItemRegexp, '[]');
|
||||
if (!result[nameNormalized]) result[nameNormalized] = [];
|
||||
result[nameNormalized].push(currNode);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (shouldClean)
|
||||
{
|
||||
currNode.value = '';
|
||||
}
|
||||
|
||||
nameNormalized = normalizeName(name, delimiter, arrayIndexes);
|
||||
result[nameNormalized] = currNode;
|
||||
}
|
||||
}
|
||||
|
||||
currNode = currNode.nextSibling;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes names of arrays, puts correct indexes (consecutive and ordered by element appearance in HTML)
|
||||
* @param name
|
||||
* @param delimiter
|
||||
* @param arrayIndexes
|
||||
*/
|
||||
function normalizeName(name, delimiter, arrayIndexes)
|
||||
{
|
||||
var nameChunksNormalized = [],
|
||||
nameChunks = name.split(delimiter),
|
||||
currChunk,
|
||||
nameMatches,
|
||||
nameNormalized,
|
||||
currIndex,
|
||||
newIndex,
|
||||
i;
|
||||
|
||||
name = name.replace(_arrayOfArraysRegexp, '[$1].[$2]');
|
||||
for (i = 0; i < nameChunks.length; i++)
|
||||
{
|
||||
currChunk = nameChunks[i];
|
||||
nameChunksNormalized.push(currChunk);
|
||||
nameMatches = currChunk.match(_lastIndexedArrayRegexp);
|
||||
if (nameMatches != null)
|
||||
{
|
||||
nameNormalized = nameChunksNormalized.join(delimiter);
|
||||
currIndex = nameNormalized.replace(_lastIndexedArrayRegexp, '$3');
|
||||
nameNormalized = nameNormalized.replace(_lastIndexedArrayRegexp, '$1');
|
||||
|
||||
if (typeof (arrayIndexes[nameNormalized]) == 'undefined')
|
||||
{
|
||||
arrayIndexes[nameNormalized] = {
|
||||
lastIndex: -1,
|
||||
indexes: {}
|
||||
};
|
||||
}
|
||||
|
||||
if (currIndex == '' || typeof arrayIndexes[nameNormalized].indexes[currIndex] == 'undefined')
|
||||
{
|
||||
arrayIndexes[nameNormalized].lastIndex++;
|
||||
arrayIndexes[nameNormalized].indexes[currIndex] = arrayIndexes[nameNormalized].lastIndex;
|
||||
}
|
||||
|
||||
newIndex = arrayIndexes[nameNormalized].indexes[currIndex];
|
||||
nameChunksNormalized[nameChunksNormalized.length - 1] = currChunk.replace(_lastIndexedArrayRegexp, '$1$2' + newIndex + '$4');
|
||||
}
|
||||
}
|
||||
|
||||
nameNormalized = nameChunksNormalized.join(delimiter);
|
||||
nameNormalized = nameNormalized.replace('].[', '][');
|
||||
return nameNormalized;
|
||||
}
|
||||
|
||||
function object2array(obj, lvl)
|
||||
{
|
||||
var result = [], i, name;
|
||||
|
||||
if (arguments.length == 1) lvl = 0;
|
||||
|
||||
if (obj == null)
|
||||
{
|
||||
result = [{ name: "", value: null }];
|
||||
}
|
||||
else if (typeof obj == 'string' || typeof obj == 'number' || typeof obj == 'date' || typeof obj == 'boolean')
|
||||
{
|
||||
result = [
|
||||
{ name: "", value : obj }
|
||||
];
|
||||
}
|
||||
else if (obj instanceof Array)
|
||||
{
|
||||
for (i = 0; i < obj.length; i++)
|
||||
{
|
||||
name = "[" + i + "]";
|
||||
result = result.concat(getSubValues(obj[i], name, lvl + 1));
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
return js2form;
|
||||
|
||||
})();
|
||||
Reference in New Issue
Block a user