📄 xmlstore.js
字号:
if(!dojo._hasResource["dojox.data.XmlStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.dojo._hasResource["dojox.data.XmlStore"] = true;dojo.provide("dojox.data.XmlStore");dojo.provide("dojox.data.XmlItem");dojo.require("dojo.data.util.simpleFetch");dojo.require("dojo.data.util.filter");dojo.require("dojox.data.dom");dojo.declare("dojox.data.XmlStore", null, { // summary: // A data store for XML based services or documents // description: // A data store for XML based services or documents constructor: function(/* object */ args) { // summary: // Constructor for the XML store. // args: // An anonymous object to initialize properties. It expects the following values: // url: The url to a service or an XML document that represents the store // rootItem: A tag name for root items // keyAttribute: An attribute name for a key or an indentify // attributeMap: An anonymous object contains properties for attribute mapping, // {"tag_name.item_attribute_name": "@xml_attribute_name", ...} // sendQuery: A boolean indicate to add a query string to the service URL console.log("XmlStore()"); if(args){ this.url = args.url; this.rootItem = (args.rootItem || args.rootitem || this.rootItem); this.keyAttribute = (args.keyAttribute || args.keyattribute || this.keyAttribute); this._attributeMap = (args.attributeMap || args.attributemap); this.label = args.label || this.label; this.sendQuery = (args.sendQuery || args.sendquery || this.sendQuery); } this._newItems = []; this._deletedItems = []; this._modifiedItems = []; }, //Values that may be set by the parser. //Ergo, have to be instantiated to something //So the parser knows how to set them. url: "", rootItem: "", keyAttribute: "", label: "", sendQuery: false,/* dojo.data.api.Read */ getValue: function(/* item */ item, /* attribute || attribute-name-string */ attribute, /* value? */ defaultValue){ // summary: // Return an attribute value // description: // 'item' must be an instance of a dojox.data.XmlItem from the store instance. // If 'attribute' specifies "tagName", the tag name of the element is // returned. // If 'attribute' specifies "childNodes", the first element child is // returned. // If 'attribute' specifies "text()", the value of the first text // child is returned. // For generic attributes, if '_attributeMap' is specified, // an actual attribute name is looked up with the tag name of // the element and 'attribute' (concatenated with '.'). // Then, if 'attribute' starts with "@", the value of the XML // attribute is returned. // Otherwise, the first child element of the tag name specified with // 'attribute' is returned. // item: // An XML element that holds the attribute // attribute: // A tag name of a child element, An XML attribute name or one of // special names // defaultValue: // A default value // returns: // An attribute value found, otherwise 'defaultValue' var element = item.element; if(attribute === "tagName"){ return element.nodeName; }else if (attribute === "childNodes"){ for (var i = 0; i < element.childNodes.length; i++) { var node = element.childNodes[i]; if (node.nodeType === 1 /*ELEMENT_NODE*/) { return this._getItem(node); //object } } return defaultValue; }else if(attribute === "text()"){ for(var i = 0; i < element.childNodes.length; i++){ var node = element.childNodes[i]; if(node.nodeType === 3 /*TEXT_NODE*/ || node.nodeType === 4 /*CDATA_SECTION_NODE*/){ return node.nodeValue; //string } } return defaultValue; }else{ attribute = this._getAttribute(element.nodeName, attribute); if(attribute.charAt(0) === '@'){ var name = attribute.substring(1); var value = element.getAttribute(name); return (value !== undefined) ? value : defaultValue; //object }else{ for(var i = 0; i < element.childNodes.length; i++){ var node = element.childNodes[i]; if( node.nodeType === 1 /*ELEMENT_NODE*/ && node.nodeName === attribute){ return this._getItem(node); //object } } return defaultValue; //object } } }, getValues: function(/* item */ item, /* attribute || attribute-name-string */ attribute){ // summary: // Return an array of attribute values // description: // 'item' must be an instance of a dojox.data.XmlItem from the store instance. // If 'attribute' specifies "tagName", the tag name of the element is // returned. // If 'attribute' specifies "childNodes", child elements are returned. // If 'attribute' specifies "text()", the values of child text nodes // are returned. // For generic attributes, if '_attributeMap' is specified, // an actual attribute name is looked up with the tag name of // the element and 'attribute' (concatenated with '.'). // Then, if 'attribute' starts with "@", the value of the XML // attribute is returned. // Otherwise, child elements of the tag name specified with // 'attribute' are returned. // item: // An XML element that holds the attribute // attribute: // A tag name of child elements, An XML attribute name or one of // special names // returns: // An array of attribute values found, otherwise an empty array var element = item.element; if(attribute === "tagName"){ return [element.nodeName]; }else if(attribute === "childNodes"){ var values = []; for(var i = 0; i < element.childNodes.length; i++){ var node = element.childNodes[i]; if(node.nodeType === 1 /*ELEMENT_NODE*/){ values.push(this._getItem(node)); } } return values; //array }else if(attribute === "text()"){ var values = []; for(var i = 0; i < element.childNodes.length; i++){ var node = childNodes[i]; if(node.nodeType === 3){ values.push(node.nodeValue); } } return values; //array }else{ attribute = this._getAttribute(element.nodeName, attribute); if(attribute.charAt(0) === '@'){ var name = attribute.substring(1); var value = element.getAttribute(name); return (value !== undefined) ? [value] : []; //array }else{ var values = []; for(var i = 0; i < element.childNodes.length; i++){ var node = element.childNodes[i]; if( node.nodeType === 1 /*ELEMENT_NODE*/ && node.nodeName === attribute){ values.push(this._getItem(node)); } } return values; //array } } }, getAttributes: function(/* item */ item) { // summary: // Return an array of attribute names // description: // 'item' must be an instance of a dojox.data.XmlItem from the store instance. // tag names of child elements and XML attribute names of attributes // specified to the element are returned along with special attribute // names applicable to the element including "tagName", "childNodes" // if the element has child elements, "text()" if the element has // child text nodes, and attribute names in '_attributeMap' that match // the tag name of the element. // item: // An XML element // returns: // An array of attributes found var element = item.element; var attributes = []; attributes.push("tagName"); if(element.childNodes.length > 0){ var names = {}; var childNodes = true; var text = false; for(var i = 0; i < element.childNodes.length; i++){ var node = element.childNodes[i]; if (node.nodeType === 1 /*ELEMENT_NODE*/) { var name = node.nodeName; if(!names[name]){ attributes.push(name); names[name] = name; } childNodes = true; }else if(node.nodeType === 3){ text = true; } } if(childNodes){ attributes.push("childNodes"); } if(text){ attributes.push("text()"); } } for(var i = 0; i < element.attributes.length; i++){ attributes.push("@" + element.attributes[i].nodeName); } if(this._attributeMap){ for (var key in this._attributeMap){ var i = key.indexOf('.'); if(i > 0){ var tagName = key.substring(0, i); if (tagName === element.nodeName){ attributes.push(key.substring(i + 1)); } }else{ // global attribute attributes.push(key); } } } return attributes; //array }, hasAttribute: function(/* item */ item, /* attribute || attribute-name-string */ attribute){ // summary: // Check whether an element has the attribute // item: // 'item' must be an instance of a dojox.data.XmlItem from the store instance. // attribute: // A tag name of a child element, An XML attribute name or one of // special names // returns: // True if the element has the attribute, otherwise false return (this.getValue(item, attribute) !== undefined); //boolean }, containsValue: function(/* item */ item, /* attribute || attribute-name-string */ attribute, /* anything */ value){ // summary: // Check whether the attribute values contain the value // item: // 'item' must be an instance of a dojox.data.XmlItem from the store instance. // attribute: // A tag name of a child element, An XML attribute name or one of // special names // returns: // True if the attribute values contain the value, otherwise false var values = this.getValues(item, attribute); for(var i = 0; i < values.length; i++){ if((typeof value === "string")){ if(values[i].toString && values[i].toString() === value){ return true; } }else if (values[i] === value){ return true; //boolean } } return false;//boolean }, isItem: function(/* anything */ something){ // summary: // Check whether the object is an item (XML element) // item: // An object to check // returns: // True if the object is an XML element, otherwise false if(something && something.element && something.store && something.store === this){ return true; //boolean } return false; //boolran }, isItemLoaded: function(/* anything */ something){ // summary: // Check whether the object is an item (XML element) and loaded // item: // An object to check // returns: // True if the object is an XML element, otherwise false return this.isItem(something); //boolean }, loadItem: function(/* object */ keywordArgs){ // summary: // Load an item (XML element) // keywordArgs: // object containing the args for loadItem. See dojo.data.api.Read.loadItem() }, getFeatures: function() { // summary: // Return supported data APIs // returns: // "dojo.data.api.Read" and "dojo.data.api.Write" var features = { "dojo.data.api.Read": true, "dojo.data.api.Write": true }; return features; //array }, getLabel: function(/* item */ item){ // summary: // See dojo.data.api.Read.getLabel() if((this.label !== "") && this.isItem(item)){ var label = this.getValue(item,this.label); if(label){ return label.toString(); } } return undefined; //undefined }, getLabelAttributes: function(/* item */ item){ // summary: // See dojo.data.api.Read.getLabelAttributes() if(this.label !== ""){ return [this.label]; //array } return null; //null }, _fetchItems: function(request, fetchHandler, errorHandler) { // summary: // Fetch items (XML elements) that match to a query // description: // If 'sendQuery' is true, an XML document is loaded from // 'url' with a query string. // Otherwise, an XML document is loaded and list XML elements that // match to a query (set of element names and their text attribute // values that the items to contain). // A wildcard, "*" can be used to query values to match all // occurrences. // If 'rootItem' is specified, it is used to fetch items. // request: // A request object // fetchHandler: // A function to call for fetched items // errorHandler: // A function to call on error var url = this._getFetchUrl(request); console.log("XmlStore._fetchItems(): url=" + url); if(!url){ errorHandler(new Error("No URL specified.")); return; } var localRequest = (!this.sendQuery ? request : null); // use request for _getItems() var self = this; var getArgs = { url: url, handleAs: "xml", preventCache: true }; var getHandler = dojo.xhrGet(getArgs); getHandler.addCallback(function(data){ var items = self._getItems(data, localRequest);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -