📄 xmlstore.js
字号:
console.log("XmlStore._fetchItems(): length=" + (items ? items.length : 0)); if (items && items.length > 0) { fetchHandler(items, request); } else { fetchHandler([], request); } }); getHandler.addErrback(function(data){ errorHandler(data, request); }); }, _getFetchUrl: function(request){ // summary: // Generate a URL for fetch // description: // This default implementation generates a query string in the form of // "?name1=value1&name2=value2..." off properties of 'query' object // specified in 'request' and appends it to 'url', if 'sendQuery' // is set to false. // Otherwise, 'url' is returned as is. // Sub-classes may override this method for the custom URL generation. // request: // A request object // returns: // A fetch URL if(!this.sendQuery){ return this.url; } var query = request.query; if(!query){ return this.url; } if(dojo.isString(query)){ return this.url + query; } var queryString = ""; for(var name in query){ var value = query[name]; if(value){ if(queryString){ queryString += "&"; } queryString += (name + "=" + value); } } if(!queryString){ return this.url; } //Check to see if the URL already has query params or not. var fullUrl = this.url; if(fullUrl.indexOf("?") < 0){ fullUrl += "?"; }else{ fullUrl += "&"; } return fullUrl + queryString; }, _getItems: function(document, request) { // summary: // Fetch items (XML elements) in an XML document based on a request // description: // This default implementation walks through child elements of // the document element to see if all properties of 'query' object // match corresponding attributes of the element (item). // If 'request' is not specified, all child elements are returned. // Sub-classes may override this method for the custom search in // an XML document. // document: // An XML document // request: // A request object // returns: // An array of items var query = null; if(request){ query = request.query; } var items = []; var nodes = null; console.log("Looking up root item: " + this.rootItem); if(this.rootItem !== ""){ nodes = document.getElementsByTagName(this.rootItem); } else{ nodes = document.documentElement.childNodes; } for(var i = 0; i < nodes.length; i++){ var node = nodes[i]; if(node.nodeType != 1 /*ELEMENT_NODE*/){ continue; } var item = this._getItem(node); if(query){ var found = true; var ignoreCase = request.queryOptions ? request.queryOptions.ignoreCase : false; //See if there are any string values that can be regexp parsed first to avoid multiple regexp gens on the //same value for each item examined. Much more efficient. var regexpList = {}; for(var key in query){ var value = query[key]; if(typeof value === "string"){ regexpList[key] = dojo.data.util.filter.patternToRegExp(value, ignoreCase); } } for(var attribute in query){ var value = this.getValue(item, attribute); if(value){ var queryValue = query[attribute]; if ((typeof value) === "string" && (regexpList[attribute])){ if((value.match(regexpList[attribute])) !== null){ continue; } }else if((typeof value) === "object"){ if( value.toString && (regexpList[attribute])){ var stringValue = value.toString(); if((stringValue.match(regexpList[attribute])) !== null){ continue; } }else{ if(queryValue === "*" || queryValue === value){ continue; } } } } found = false; break; } if(!found){ continue; } } items.push(item); } dojo.forEach(items,function(item){ item.element.parentNode.removeChild(item.element); // make it root },this); return items; }, close: function(/*dojo.data.api.Request || keywordArgs || null */ request){ // summary: // See dojo.data.api.Read.close() },/* dojo.data.api.Write */ newItem: function(/* object? */ keywordArgs){ // summary: // Return a new dojox.data.XmlItem // description: // At least, 'keywordArgs' must contain "tagName" to be used for // the new element. // Other attributes in 'keywordArgs' are set to the new element, // including "text()", but excluding "childNodes". // keywordArgs: // An object containing initial attributes // returns: // An XML element console.log("XmlStore.newItem()"); keywordArgs = (keywordArgs || {}); var tagName = keywordArgs.tagName; if(!tagName){ tagName = this.rootItem; if(tagName === ""){ return null; } } var document = this._getDocument(); var element = document.createElement(tagName); for(var attribute in keywordArgs){ if(attribute === "tagName"){ continue; }else if(attribute === "text()"){ var text = document.createTextNode(keywordArgs[attribute]); element.appendChild(text); }else{ attribute = this._getAttribute(tagName, attribute); if(attribute.charAt(0) === '@'){ var name = attribute.substring(1); element.setAttribute(name, keywordArgs[attribute]); }else{ var child = document.createElement(attribute); var text = document.createTextNode(keywordArgs[attribute]); child.appendChild(text); element.appendChild(child); } } } var item = this._getItem(element); this._newItems.push(item); return item; //object }, deleteItem: function(/* item */ item){ // summary: // Delete an dojox.data.XmlItem (wrapper to a XML element). // item: // An XML element to delete // returns: // True console.log("XmlStore.deleteItem()"); var element = item.element; if(element.parentNode){ this._backupItem(item); element.parentNode.removeChild(element); return true; } this._forgetItem(item); this._deletedItems.push(item); return true; //boolean }, setValue: function(/* item */ item, /* attribute || string */ attribute, /* almost anything */ value){ // summary: // Set an attribute value // description: // 'item' must be an instance of a dojox.data.XmlItem from the store instance. // If 'attribute' specifies "tagName", nothing is set and false is // returned. // If 'attribute' specifies "childNodes", the value (XML element) is // added to the element. // If 'attribute' specifies "text()", a text node is created with // the value and set it to the element as a child. // 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 is set to the XML // attribute. // Otherwise, a text node is created with the value and set it to // the first child element of the tag name specified with 'attribute'. // If the child element does not exist, it is created. // 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 // value: // A attribute value to set // returns: // False for "tagName", otherwise true if(attribute === "tagName"){ return false; //boolean } this._backupItem(item); var element = item.element; if(attribute === "childNodes"){ var child = value.element; element.appendChild(child); }else if(attribute === "text()"){ while (element.firstChild){ element.removeChild(element.firstChild); } var text = this._getDocument(element).createTextNode(value); element.appendChild(text); }else{ attribute = this._getAttribute(element.nodeName, attribute); if(attribute.charAt(0) === '@'){ var name = attribute.substring(1); element.setAttribute(name, value); }else{ var child = null; for(var i = 0; i < element.childNodes.length; i++){ var node = element.childNodes[i]; if( node.nodeType === 1 /*ELEMENT_NODE*/&& node.nodeName === attribute){ child = node; break; } } var document = this._getDocument(element); if(child){ while(child.firstChild){ child.removeChild(child.firstChild); } }else{ child = document.createElement(attribute); element.appendChild(child); } var text = document.createTextNode(value); child.appendChild(text); } } return true; //boolean }, setValues: function(/* item */ item, /* attribute || string */ attribute, /* array */ values){ // summary: // Set attribute values // description: // 'item' must be an instance of a dojox.data.XmlItem from the store instance. // If 'attribute' specifies "tagName", nothing is set and false is // returned. // If 'attribute' specifies "childNodes", the value (array of XML // elements) is set to the element's childNodes. // If 'attribute' specifies "text()", a text node is created with // the values and set it to the element as a child. // 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 first value is set to // the XML attribute. // Otherwise, child elements of the tag name specified with // 'attribute' are replaced with new child elements and their // child text nodes of values. // item: // An XML element that holds the attribute // attribute: // A tag name of child elements, an XML attribute name or one of // special names // value: // A attribute value to set // returns: // False for "tagName", otherwise true if(attribute === "tagName"){ return false; //boolean } this._backupItem(item); var element = item.element; if(attribute === "childNodes"){ while(element.firstChild){ element.removeChild(element.firstChild); } for(var i = 0; i < values.length; i++){ var child = values[i].element; element.appendChild(child); } }else if(attribute === "text()"){ while (element.firstChild){ element.removeChild(element.firstChild); } var value = ""; for(var i = 0; i < values.length; i++){ value += values[i]; } var text = this._getDocument(element).createTextNode(value); element.appendChild(text); }else{ attribute = this._getAttribute(element.nodeName, attribute); if(attribute.charAt(0) === '@'){ var name = attribute.substring(1); element.setAttribute(name, values[0]); }else{ for(var i = element.childNodes.length - 1; i >= 0; i--){ var node = element.childNodes[i]; if( node.nodeType === 1 /*ELEMENT_NODE*/ && node.nodeName === attribute){ element.removeChild(node); } } var document = this._getDocument(element); for(var i = 0; i < values.length; i++){ var child = document.createElement(attribute); var text = document.createTextNode(values[i]); child.appendChild(text); element.appendChild(child); } } } return true; //boolean }, unsetAttribute: function(/* item */ item, /* attribute || string */ attribute){ // summary: // Remove an attribute // description:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -