📄 csvstore.js
字号:
} } }else{ // We want a copy to pass back in case the parent wishes to sort the array. We shouldn't allow resort // of the internal list so that multiple callers can get lists and sort without affecting each other. if(arrayOfAllItems.length> 0){ items = arrayOfAllItems.slice(0,arrayOfAllItems.length); } } findCallback(items, requestArgs); }; if(this._loadFinished){ filter(keywordArgs, this._arrayOfAllItems); }else{ if(this.url !== ""){ //If fetches come in before the loading has finished, but while //a load is in progress, we have to defer the fetching to be //invoked in the callback. if(this._loadInProgress){ this._queuedFetches.push({args: keywordArgs, filter: filter}); }else{ this._loadInProgress = true; var getArgs = { url: self.url, handleAs: "text" }; var getHandler = dojo.xhrGet(getArgs); getHandler.addCallback(function(data){ self._processData(data); filter(keywordArgs, self._arrayOfAllItems); self._handleQueuedFetches(); }); getHandler.addErrback(function(error){ self._loadInProgress = false; if(errorCallback){ errorCallback(error, keywordArgs); }else{ throw error; } }); } }else if(this._csvData){ this._processData(this._csvData); this._csvData = null; filter(keywordArgs, this._arrayOfAllItems); }else{ var error = new Error("dojox.data.CsvStore: No CSV source data was provided as either URL or String data input."); if(errorCallback){ errorCallback(error, keywordArgs); }else{ throw error; } } } }, close: function(/*dojo.data.api.Request || keywordArgs || null */ request){ // summary: // See dojo.data.api.Read.close() }, // ------------------------------------------------------------------- // Private methods _getArrayOfArraysFromCsvFileContents: function(/* string */ csvFileContents){ /* summary: * Parses a string of CSV records into a nested array structure. * description: * Given a string containing CSV records, this method parses * the string and returns a data structure containing the parsed * content. The data structure we return is an array of length * R, where R is the number of rows (lines) in the CSV data. The * return array contains one sub-array for each CSV line, and each * sub-array contains C string values, where C is the number of * columns in the CSV data. */ /* example: * For example, given this CSV string as input: * "Title, Year, Producer \n Alien, 1979, Ridley Scott \n Blade Runner, 1982, Ridley Scott" * this._dataArray will be set to: * [["Alien", "1979", "Ridley Scott"], * ["Blade Runner", "1982", "Ridley Scott"]] * And this._attributes will be set to: * ["Title", "Year", "Producer"] * And this._attributeIndexes will be set to: * { "Title":0, "Year":1, "Producer":2 } */ if(dojo.isString(csvFileContents)){ var lineEndingCharacters = new RegExp("\r\n|\n|\r"); var leadingWhiteSpaceCharacters = new RegExp("^\\s+",'g'); var trailingWhiteSpaceCharacters = new RegExp("\\s+$",'g'); var doubleQuotes = new RegExp('""','g'); var arrayOfOutputRecords = []; var arrayOfInputLines = csvFileContents.split(lineEndingCharacters); for(var i = 0; i < arrayOfInputLines.length; ++i){ var singleLine = arrayOfInputLines[i]; if(singleLine.length > 0){ var listOfFields = singleLine.split(','); var j = 0; while(j < listOfFields.length){ var space_field_space = listOfFields[j]; var field_space = space_field_space.replace(leadingWhiteSpaceCharacters, ''); // trim leading whitespace var field = field_space.replace(trailingWhiteSpaceCharacters, ''); // trim trailing whitespace var firstChar = field.charAt(0); var lastChar = field.charAt(field.length - 1); var secondToLastChar = field.charAt(field.length - 2); var thirdToLastChar = field.charAt(field.length - 3); if(field.length === 2 && field == "\"\""){ listOfFields[j] = ""; //Special case empty string field. }else if((firstChar == '"') && ((lastChar != '"') || ((lastChar == '"') && (secondToLastChar == '"') && (thirdToLastChar != '"')))){ if(j+1 === listOfFields.length){ // alert("The last field in record " + i + " is corrupted:\n" + field); return null; //null } var nextField = listOfFields[j+1]; listOfFields[j] = field_space + ',' + nextField; listOfFields.splice(j+1, 1); // delete element [j+1] from the list }else{ if((firstChar == '"') && (lastChar == '"')){ field = field.slice(1, (field.length - 1)); // trim the " characters off the ends field = field.replace(doubleQuotes, '"'); // replace "" with " } listOfFields[j] = field; j += 1; } } arrayOfOutputRecords.push(listOfFields); } } // The first item of the array must be the header row with attribute names. this._attributes = arrayOfOutputRecords.shift(); for(var i=0; i<this._attributes.length; i++){ // Store the index of each attribute this._attributeIndexes[this._attributes[i]] = i; } this._dataArray = arrayOfOutputRecords; //Array } }, _processData: function(/* String */ data){ this._getArrayOfArraysFromCsvFileContents(data); this._arrayOfAllItems = []; for(var i=0; i<this._dataArray.length; i++){ this._arrayOfAllItems.push(this._createItemFromIdentity(i)); } this._loadFinished = true; this._loadInProgress = false; }, _createItemFromIdentity: function(/* String */ identity){ var item = {}; item[this._storeProp] = this; item[this._idProp] = identity; return item; //Object }, /*************************************** dojo.data.api.Identity API***************************************/ getIdentity: function(/* item */ item){ // summary: // See dojo.data.api.Identity.getIdentity() if(this.isItem(item)){ return item[this._idProp]; //String } return null; //null }, fetchItemByIdentity: function(/* Object */ keywordArgs){ // summary: // See dojo.data.api.Identity.fetchItemByIdentity() //Hasn't loaded yet, we have to trigger the load. if(!this._loadFinished){ var self = this; if(this.url !== ""){ //If fetches come in before the loading has finished, but while //a load is in progress, we have to defer the fetching to be //invoked in the callback. if(this._loadInProgress){ this._queuedFetches.push({args: keywordArgs}); }else{ this._loadInProgress = true; var getArgs = { url: self.url, handleAs: "text" }; var getHandler = dojo.xhrGet(getArgs); getHandler.addCallback(function(data){ var scope = keywordArgs.scope?keywordArgs.scope:dojo.global; try{ self._processData(data); var item = self._createItemFromIdentity(keywordArgs.identity); if(!self.isItem(item)){ item = null; } if(keywordArgs.onItem){ keywordArgs.onItem.call(scope, item); } self._handleQueuedFetches(); }catch(error){ if(keywordArgs.onError){ keywordArgs.onError.call(scope, error); } } }); getHandler.addErrback(function(error){ this._loadInProgress = false; if(keywordArgs.onError){ var scope = keywordArgs.scope?keywordArgs.scope:dojo.global; keywordArgs.onError.call(scope, error); } }); } }else if(this._csvData){ self._processData(self._csvData); self._csvData = null; var item = self._createItemFromIdentity(keywordArgs.identity); if(!self.isItem(item)){ item = null; } if(keywordArgs.onItem){ var scope = keywordArgs.scope?keywordArgs.scope:dojo.global; keywordArgs.onItem.call(scope, item); } } }else{ //Already loaded. We can just look it up and call back. var item = this._createItemFromIdentity(keywordArgs.identity); if(!this.isItem(item)){ item = null; } if(keywordArgs.onItem){ var scope = keywordArgs.scope?keywordArgs.scope:dojo.global; keywordArgs.onItem.call(scope, item); } } }, getIdentityAttributes: function(/* item */ item){ // summary: // See dojo.data.api.Identity.getIdentifierAttributes() //Identity isn't a public attribute in the item, it's the row position index. //So, return null. return null; }, _handleQueuedFetches: function(){ // summary: // Internal function to execute delayed request in the store. //Execute any deferred fetches now. if (this._queuedFetches.length > 0) { for(var i = 0; i < this._queuedFetches.length; i++){ var fData = this._queuedFetches[i]; var delayedFilter = fData.filter; var delayedQuery = fData.args; if(delayedFilter){ delayedFilter(delayedQuery, this._arrayOfAllItems); }else{ this.fetchItemByIdentity(fData.args); } } this._queuedFetches = []; } }});//Mix in the simple fetch implementation to this class.dojo.extend(dojox.data.CsvStore,dojo.data.util.simpleFetch);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -