123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- define("dojox/wire/ml/util", ["dijit","dojo","dojox","dojo/require!dojox/xml/parser,dojox/wire/Wire"], function(dijit,dojo,dojox){
- dojo.provide("dojox.wire.ml.util");
- dojo.require("dojox.xml.parser");
- dojo.require("dojox.wire.Wire");
- dojox.wire.ml._getValue = function(/*String*/source, /*Array*/args){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if(!source){
- return undefined;
- }
- var property = undefined;
- if(args && source.length >= 9 && source.substring(0, 9) == "arguments"){
- property = source.substring(9);
- return new dojox.wire.Wire({property: property}).getValue(args);
- }
- var i = source.indexOf('.');
- if(i >= 0){
- property = source.substring(i + 1);
- source = source.substring(0, i);
- }
- var object = (dijit.byId(source) || dojo.byId(source) || dojo.getObject(source));
- if(!object){
- return undefined;
- }
- if(!property){
- return object;
- }else{
- return new dojox.wire.Wire({object: object, property: property}).getValue();
- }
- };
- dojox.wire.ml._setValue = function(/*String*/target, /*anything*/value){
-
-
-
-
-
-
-
-
-
- if(!target){
- return;
- }
- var i = target.indexOf('.');
- if(i < 0){
- return;
- }
- var object = this._getValue(target.substring(0, i));
- if(!object){
- return;
- }
- var property = target.substring(i + 1);
- var wire = new dojox.wire.Wire({object: object, property: property}).setValue(value);
- };
- dojo.declare("dojox.wire.ml.XmlElement", null, {
-
-
-
-
- constructor: function(/*Element||String*/element){
-
-
-
-
- if(dojo.isString(element)){
- element = this._getDocument().createElement(element);
- }
- this.element = element;
- },
- getPropertyValue: function(/*String*/property){
-
-
-
-
-
-
-
-
-
-
-
-
- var value = undefined;
- if(!this.element){
- return value;
- }
- if(!property){
- return value;
- }
- if(property.charAt(0) == '@'){
- var attribute = property.substring(1);
- value = this.element.getAttribute(attribute);
- }else if(property == "text()"){
- var text = this.element.firstChild;
- if(text){
- value = text.nodeValue;
- }
- }else{
- var elements = [];
- for(var i = 0; i < this.element.childNodes.length; i++){
- var child = this.element.childNodes[i];
- if(child.nodeType === 1 && child.nodeName == property){
- elements.push(new dojox.wire.ml.XmlElement(child));
- }
- }
- if(elements.length > 0){
- if(elements.length === 1){
- value = elements[0];
- }else{
- value = elements;
- }
- }
- }
- return value;
- },
- setPropertyValue: function(/*String*/property, /*String||Array||XmlElement*/value){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var i;
- var text;
- if(!this.element){
- return;
- }
- if(!property){
- return;
- }
- if(property.charAt(0) == '@'){
- var attribute = property.substring(1);
- if(value){
- this.element.setAttribute(attribute, value);
- }else{
- this.element.removeAttribute(attribute);
- }
- }else if(property == "text()"){
- while(this.element.firstChild){
- this.element.removeChild(this.element.firstChild);
- }
- if(value){
- text = this._getDocument().createTextNode(value);
- this.element.appendChild(text);
- }
- }else{
- var nextChild = null;
- var child;
- for(i = this.element.childNodes.length - 1; i >= 0; i--){
- child = this.element.childNodes[i];
- if(child.nodeType === 1 && child.nodeName == property){
- if(!nextChild){
- nextChild = child.nextSibling;
- }
- this.element.removeChild(child);
- }
- }
- if(value){
- if(dojo.isArray(value)){
- for(i in value){
- var e = value[i];
- if(e.element){
- this.element.insertBefore(e.element, nextChild);
- }
- }
- }else if(value instanceof dojox.wire.ml.XmlElement){
- if(value.element){
- this.element.insertBefore(value.element, nextChild);
- }
- }else{
- child = this._getDocument().createElement(property);
- text = this._getDocument().createTextNode(value);
- child.appendChild(text);
- this.element.insertBefore(child, nextChild);
- }
- }
- }
- },
- toString: function(){
-
-
-
-
-
-
- var s = "";
- if(this.element){
- var text = this.element.firstChild;
- if(text){
- s = text.nodeValue;
- }
- }
- return s;
- },
- toObject: function(){
-
-
-
-
-
-
-
- if(!this.element){
- return null;
- }
- var text = "";
- var obj = {};
- var elements = 0;
- var i;
- for(i = 0; i < this.element.childNodes.length; i++){
- var child = this.element.childNodes[i];
- if(child.nodeType === 1 ){
- elements++;
- var o = new dojox.wire.ml.XmlElement(child).toObject();
- var name = child.nodeName;
- var p = obj[name];
- if(!p){
- obj[name] = o;
- }else if(dojo.isArray(p)){
- p.push(o);
- }else{
- obj[name] = [p, o];
- }
- }else if(child.nodeType === 3 ||
- child.nodeType === 4 ){
- text += child.nodeValue;
- }
- }
- var attributes = 0;
- if(this.element.nodeType === 1 ){
- attributes = this.element.attributes.length;
- for(i = 0; i < attributes; i++){
- var attr = this.element.attributes[i];
- obj["@" + attr.nodeName] = attr.nodeValue;
- }
- }
- if(elements === 0){
- if(attributes === 0){
-
- return text;
- }
-
- obj["text()"] = text;
- }
-
- return obj;
- },
- _getDocument: function(){
-
-
-
-
-
-
-
-
- if(this.element){
- return (this.element.nodeType == 9 ?
- this.element : this.element.ownerDocument);
- }else{
- return dojox.xml.parser.parse();
- }
- }
- });
- });
|