Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Added some settings to it. By default, it returns the xml without
- * any formatting, but if you need to look it over easily, you can
- * turn it on. The rest should be self-explanatory, but if you have
- * any questions, let me know.
- *
- * Example Usages:
- *
- * var xmlDoc = apex.widget.tree.serialize();
- * $.post("http://example.com", xmlDoc, function(data) {
- * stuff();
- * });
- *
- * $("textarea").val(
- * apex.widget.tree.serialize(
- * {
- * outputFormat: true,
- * txtFormat:true
- * }
- * )
- * );
- *
- */
- (function($tree) {
- $tree.serialize = function(settings) {
- var _sets = {};
- var _defaults = {
- outputFormat: false, // for debugging purposes
- txtFormat: false, // ^
- sIndent: "\t", // ^
- domRoot: 'categories',
- skip: [],
- replace: [],
- nodes: [],
- errors: []
- };
- var getIndent = function(depth) {
- var output = '';
- for (var i = 0; i < depth; i++) {
- output += _sets.sIndent;
- }
- return output;
- };
- var getPropertyName = function(ptPar, name) {
- var _i = _sets.replace.length;
- var searchName = ptPar + name;
- while (_i--) {
- if (_sets.replace[_i].hasOwnProperty(searchName)) {
- return _sets.replace[_i][searchName];
- }
- }
- return name;
- };
- var arrayToNode = function(src, name, path, depth, suff) {
- var _node = '';
- if (src.length > 0) {
- for (var _i in src) {
- if (typeof(src[_i]) !== 'object') {
- if (src[_i] === "") {
- _node += getIndent(depth) + '<' + name + '/>' + suff;
- }
- else {
- var textPrefix = (_sets.txtFormat)
- ? suff + getIndent(depth + 1) : '';
- var textsuff = (_sets.txtFormat)
- ? suff + getIndent(depth) : '';
- _node += getIndent(depth) + '<' + name + '>'
- + textPrefix + src[_i] + textsuff
- + '</' + name + '>' + suff;
- }
- }
- else {
- _node += toDOM(src[_i], name, path, depth);
- }
- }
- }
- else {
- _node += getIndent(depth) + '<' + name + '/>' + suff;
- }
- return _node;
- };
- var txtNode = function(name, text, parentDepth, suff) {
- var textPrefix = (_sets.txtFormat)
- ? suff + getIndent(parentDepth + 2) : '';
- var textsuff = (_sets.txtFormat)
- ? suff + getIndent(parentDepth + 1) : '';
- var _node = getIndent(parentDepth + 1) + '<' + name + '>'
- + textPrefix + text + textsuff
- + '</' + name + '>' + suff;
- return _node;
- };
- var toDOM = function(json, domRoot, ptPar, depth) {
- var suff = (_sets.outputFormat) ? '\r\n' : '';
- var indent = (_sets.outputFormat) ? getIndent(depth) : '';
- var xmlChar = indent + '<' + domRoot;
- var children = '';
- for (var key in json) {
- if (json.hasOwnProperty(key)) {
- var propPath = ptPar + key;
- var propertyName = getPropertyName(ptPar, key);
- if ($.inArray(propPath, _sets.skip) == -1) {
- if ($.isArray(json[key])) {
- children += arrayToNode(json[key], propertyName,
- propPath + '.', depth + 1, suff);
- }
- else if (typeof(json[key]) === 'object') {
- children += toDOM(json[key], propertyName,propPath + '.', depth + 1);
- }
- else {
- // shouldnt be a node
- if ($.inArray(propPath, _sets.nodes) != -1) {
- children += txtNode(propertyName, json[key],depth, suff);
- }
- else {
- xmlChar += ' ' + propertyName + '="' + json[key] + '"';
- }
- }
- }
- }
- }
- if (children !== '') {
- xmlChar += '>' + suff + children + indent + '</' + domRoot + '>' + suff;
- }
- else {
- xmlChar += '/>' + suff;
- }
- return xmlChar;
- };
- var childTraversal = function() {
- var _domCollection = {"category" : []};
- $(".tree li").each(function(_i,_e) {
- var newCategory = {
- "title" : $(this).children("a").text().trim(),
- "id" : $(this).attr("id")
- };
- if (newCategory.id != "0") {
- newCategory.parent = $(this).parent().parent().attr("id");
- $(this).parent().children().each(function(ci,ce) {
- if ($(ce).attr("id") == newCategory.id) {
- newCategory.seq = ci;
- }
- });
- }
- _domCollection.category.push(newCategory);
- });
- return _domCollection;
- };
- _sets = $.extend(true, _sets, _defaults, settings || { });
- // In case you need a valid xml header, remove the last line and uncomment the line below.
- //return '<?xml version="1.0" encoding="utf-8" ?>\r\n' + toDOM(childTraversal(), _sets.domRoot, '', 0);
- return toDOM(childTraversal(), _sets.domRoot, '', 0);
- };
- })(apex.widget.tree);
- /* First attempt resulted in way too much recursion.
- (function() {
- //var rgXp = /var\s([^\s]+)\s?=\s?\[/i;
- //var match = rgXp.exec($('div#body td.tbl-main > script:eq(0)').text());
- //var _varname = match[1];
- function clickCallback(e) {
- var objDOM = $.tree.reference($('td.tbl-main > div[id*="tree"]').get()).get();
- var _domCollection = {
- "categories" : {
- "category" : []
- }
- };
- function childTraversal(_i,_el) {
- function createDOM(_ci,_cel) {
- var _title;
- if(_cel.data.title.length == 0) {
- if($("#" + _cel.attributes.id).length == 0) {
- _title = $(_el.attributes.id).children("li:eq("+_ci+")").children("a").text().trim();
- } else {
- _title = $("#" + _cel.attributes.id).children("a").text().trim();
- }
- } else {
- _title = _cel.data.title;
- }
- var newCategory = {
- "id" : _cel.attributes.id,
- "parent" : _el.attributes.id,
- "title" : _title,
- };
- _domCollection.categories.push(newCategory);
- if(typeof(this.children)!= "undefined") {
- $(this.children).map(createDOM).map(childTraversal);
- }
- }
- if(typeof(this.children)!= "undefined") {
- objDOM.children(_el).map(childTraversal);//createDOM);
- }
- }
- $(objDOM.children).unwrap().map(childTraversal);
- return false;
- }
- $('button[value="Save"]').css("margin-top", "50px").click(clickCallback);
- })(this);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement