Mercurial > eldonilo > lightstring
changeset 58:bcb5b7c2c3d3
Add a dataforms plugin and make disco#info use this one.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 01 Feb 2012 17:23:08 +0100 |
parents | 91f18fdc0e2c |
children | bbcc6bbdac73 |
files | plugins/dataforms.js plugins/disco.js |
diffstat | 2 files changed, 153 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/plugins/dataforms.js @@ -0,0 +1,140 @@ +'use strict'; + +/** + Copyright (c) 2012, Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +///////// +//Disco// +///////// + +(function() { + var form_types = ['cancel', 'form', 'result', 'submit']; + var field_types = ['boolean', 'fixed', 'hidden', 'jid-multi', 'jid-single', 'list-multi', 'list-single', 'text-multi', 'text-private', 'text-single']; + + var parseFields = function(fieldsList) { + var fields = []; + + for (var i = 0; i < fieldsList.length; i++) { + var field = {}; + var child = fieldsList[i]; + + var type = child.getAttributeNS(null, 'type'); + if (field_types.indexOf(type) === -1) + field.type = type; + else + field.type = 'text-single'; + + var label = child.getAttributeNS(null, 'label'); + if (label) + field.label = label; + + var _var = child.getAttributeNS(null, 'var'); + if (_var) + field.var = _var; + + var desc = child.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'desc'); + if (desc.length > 1) + ; //TODO: emit a warning if there is more than one. + if (0 in desc) + field.desc = desc[0]; + + var required = child.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'required'); + if (required.length > 1) + ; //TODO: emit a warning if there is more than one. + field.required = (0 in required) + + var values = child.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'value'); + if (values.length) { + field.values = []; + for (var j = 0; j < values.length; j++) + field.values.push(values[j].textContent); + } + + var options = child.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'option'); + if (options.length) { + field.options = []; + for (var j = 0; j < options.length; j++) { + var option = {}; + + var opt = options[j]; + + var val = opt.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'value'); + if (val.length > 1) + ; //TODO: emit a warning if there is more than one. + if (0 in val) + option.value = val[0]; + + var optionLabel = opt.getAttributeNS(null, 'label'); + if (optionLabel) + option.label = optionLabel; + + field.options.push(option); + } + } + + fields.push(field); + } + + return fields; + }; + + Lightstring.plugins['disco'] = { + namespaces: { + dataforms: 'jabber:x:data' + }, + methods: { + parse: function(x) { + if (x.namespaceURI !== Lightstring.namespaces['dataforms'] || x.localName !== 'x') + return null; + + var form = {}; + + var type = x.getAttributeNS(null, 'type'); + if (form_types.indexOf(type) === -1) + return; //TODO: emit a warning too? + form.type = type; + + var title = x.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'title'); + if (title.length > 1) + ; //TODO: emit a warning if there is more than one. + else if (0 in title) + form.title = title[0]; + + var fields = parseFields(x.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'fields')); + if (fields) + form.fields = fields + + var reported = x.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'reported'); + if (reported.length > 1) + ; //TODO: emit a warning if there is more than one. + else if (0 in reported) { + var fields = parseFields(reported[0].getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'fields')); + if (fields) + form.reported = fields + } + + var itemsList = x.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'item'); + if (itemsList) { + var fields = parseFields(itemsList[0].getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'fields')); + if (fields) + form.items = fields; + } + + return form; + } + } + }; +})();
--- a/plugins/disco.js +++ b/plugins/disco.js @@ -119,46 +119,29 @@ Lightstring.plugins['disco'] = { var length = children.length; for (var i = 0; i < length; i++) { + var child = children[i]; + var ns = child.namespaceURI; + var name = child.localName; - if (children[i].localName === 'feature') - features.push(children[i].getAttributeNS(null, 'var')); + if (ns === Lightstring.namespaces['disco#info'] && name === 'feature') + features.push(child.getAttributeNS(null, 'var')); - else if (children[i].localName === 'identity') { + else if (ns === Lightstring.namespaces['disco#info'] && name === 'identity') { var identity = { - category: children[i].getAttributeNS(null, 'category'), - type: children[i].getAttributeNS(null, 'type') + category: child.getAttributeNS(null, 'category'), + type: child.getAttributeNS(null, 'type') }; - var name = children[i].getAttributeNS(null, 'name'); + var name = child.getAttributeNS(null, 'name'); if (name) identity.name = name; identities.push(identity); } - else if (children[i].localName === 'x') { - for (var j = 0; j < children[i].childNodes.length; j++) { - var child = children[i].childNodes[j]; - var field = { - type: child.getAttribute('type') - }; - - var _var = child.getAttribute('var'); - - var label = child.getAttribute('label'); - if (label) - field.label = label; + else if (ns === Lightstring.namespaces['dataforms'] && name === 'x') + this.disco.parse(child); //TODO: check if that plugin is enabled. - for (var y = 0; y < child.childNodes.length; y++) { - if(child.childNodes[y].localName === 'desc') - field.desc = child.childNodes[y].textContent; - else if(child.childNodes[y].localName === 'required') - field.required = true; - else if(child.childNodes[y].localName === 'value') - field.value = child.childNodes[y].textContent; - } - - fields[_var] = field; - } - } + else + ; //TODO: emit a warning. } stanza.identities = identities;