📄 csvolapdatamodel.js
字号:
function Csv( s ){ this._data = []; if ( s ) this.parse(s);}_p = Csv.prototype;_p._width = 0;_p._height = 0;_p.parse = function ( s ){ s = String(s); var inString = false; var hasText = false; var tmp = ""; var i = 0; var l = s.length; var rv = []; var newRow = true; var x = 0, y = 0; var maxX = 0, maxY = 0; while ( i < l ) { if ( newRow ) { rv[y] = []; maxX = Math.max( x, maxX ); x = 0; newRow = false; hasText = false; } c = s.charAt(i); switch ( c ) { case '"': if ( s.charAt( i + 1 ) == '"' ) { hasText = true; tmp += '"'; i++; } else { inString = !inString; } break; case "\n": if ( hasText ) rv[y][x++] = tmp; tmp = ""; hasText = false; newRow = true; y++; maxY = Math.max( y, maxY ); break; case ",": if ( inString ) { tmp += ","; } else { rv[y][x++] = tmp; tmp = ""; } hasText = true; break; case "\r": break; default: hasText = true; tmp += c; } i++; } if ( tmp != "" ) rv[y][x++] = tmp; this._data = rv; this._width = maxX; this._height = maxY;};_p.getDataAt = function ( x, y ){ var rv = this._data[y]; if ( rv == undefined ) return ""; return rv[x] || "";};_p.setDataAt = function ( x, y, v ){ // we should either expand the grid or raise an error if we are outside // the grid this._data[y][x] = v;};_p.getWidth = function (){ return this._width;};_p.getHeight = function (){ return this._height;};function CsvOlapDataModel(){ BiOlapGridDataModel.call(this); this._csv = new Csv; this._textLoader = new BiTextLoader; this._textLoader.setAsync( true ); this._textLoader.addEventListener( "load", this._onTextLoaderLoad, this );}_p = CsvOlapDataModel.prototype = new BiOlapGridDataModel;_p.getAxisDimensionCount = function ( nAxis ){ if ( !this.getLoaded() ) return 0; return this._dimensionCount[nAxis];};_p.getAxisPositionWidth = function ( nAxis ){ if ( !this.getLoaded() ) return 0; return this._positionWidth[nAxis];};_p.getAxisCellWidth = function ( nAxis, nDim, nPos ){ return this._axisCellWidth[nAxis + "-" + nDim + "-" + nPos];};_p.getCellText = function ( x, y ){ x += this.getAxisDimensionCount( 0 ); y += this.getAxisDimensionCount( 1 ); return this._csv.getDataAt( x, y );};_p.getCellStyle = function ( x, y ){ return "text-align:right;";};_p.getAxisCellText = function ( nAxis, nDim, nPos ){ if ( nAxis == 0 ) return this._csv.getDataAt( this._dimensionCount[1] + nPos, nDim ); else return this._csv.getDataAt( nDim, this._dimensionCount[0] + nPos );};// not part of BiOlapGridDataModel interface but used in this demo for the// inline edit model_p.setCellText = function ( x, y, v ){ x += this.getAxisDimensionCount( 0 ); y += this.getAxisDimensionCount( 1 ); this._csv.setDataAt( x, y, v );};_p.setUri = function ( oUri ){ if ( String(this._uri) != String(oUri) ) { if ( this._textLoader.getLoading() ) this._textLoader.abort(); this._textLoader.setUri( oUri ); this._textLoader.load(); }};_p.getUri = function (){ return this._textLoader.getUri();};_p._onTextLoaderLoad = function ( e ){ this.dispatchEvent( new BiEvent("beforedatastructurechanged") ); this._interpretCsv( this._textLoader.getText() ); this.dispatchEvent( new BiEvent("load") ); this.dispatchEvent( new BiEvent("datastructurechanged") );};_p._interpretCsv = function ( s ){ this._csv.parse( s ); var csv = this._csv; var i, x, y, last; /* var data = s.split("\n"); this._data = []; for ( i = 0; i < data.length; i++ ) { if ( data[i] != "" ) this._data.push( data[i].split(",") ); } */ // find dimension counts this._dimensionCount = [0, 0]; for ( i = 0; csv.getDataAt( i, 0 ) == ""; i++ ) {} this._dimensionCount[0] = i; for ( i = 0; csv.getDataAt( 0, i ) == ""; i++ ) {} this._dimensionCount[1] = i; this._positionWidth = [ csv.getWidth() - this._dimensionCount[0], csv.getHeight() - this._dimensionCount[1] ]; this._axisCellWidth = {}; for ( y = 0; y < this._dimensionCount[0]; y++ ) { last = 0; for ( x = 0; x < this._positionWidth[0]; x++ ) { //if ( !/^\s*$/.test( this._data[y][x + this._dimensionCount[1]] ) ) if ( csv.getDataAt( x + this._dimensionCount[1], y ) != "" ) { last = x; this._axisCellWidth["0-" + y + "-" + x] = 1; } else { this._axisCellWidth["0-" + y + "-" + last]++; } } } for ( x = 0; x < this._dimensionCount[1]; x++ ) { last = 0; for ( y = 0; y < this._positionWidth[1]; y++ ) { //if ( !/^\s*$/.test( this._data[y + this._dimensionCount[0]][x] ) ) if ( csv.getDataAt( x, y + this._dimensionCount[0] ) != "" ) { last = y; this._axisCellWidth["1-" + x + "-" + y] = 1; } else { this._axisCellWidth["1-" + x + "-" + last]++; } } }};_p.getLoaded = function (){ return this._textLoader.getLoaded();};_p.dispose = function (){ if ( this.getDisposed() ) return; BiOlapGridDataModel.prototype.dispose.call( this ); this._textLoader.dispose(); this._textLoader = null;};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -