@ -158,20 +158,25 @@ studio.grid = function (){
} )
var spreadsheet = new ej . spreadsheet . Spreadsheet ( ) ;
// spreadsheet.allowSave = true
// spreadsheet.saveUrl = 'api/io/write'
// spreadsheet.openUrl='/api/io/open'
// spreadsheet.openSettings = {chunkSize:1024}
// spreadsheet.saveUrl = 'https://services.syncfusion.com/js/production/api/spreadsheet/save'
// spreadsheet.created = function (){}
spreadsheet . sheets = [
{ name : _id . replace ( /[#,.]/g , ' ' ) ,
ranges : [ { dataSource : rows } ] ,
}
]
spreadsheet . appendTo ( _id )
$ ( _id ) [ 0 ] . spreadsheet = spreadsheet
// console.log([' **** ',(_id+' .e-input-group')])
// $(_id+' .e-input-group').remove()
spreadsheet . refresh ( )
spreadsheet . hideFileMenuItems ( [ "File" ] , true ) ;
}
}
@ -209,10 +214,105 @@ studio.frame = function (_args){
}
} )
}
this . open = function ( _id , file ) {
var http = HttpClient . instance ( )
_uri = 'api/io/open'
var form = new FormData ( )
form . append ( 'file' , file )
http . setData ( form )
var _index = $ ( _id ) [ 0 ] . spreadsheet . sheets . length
// _name = file.name
// $(_id)[0].spreadsheet.insertSheet([{name:_name,_index:1}],0)
// console.log([_index, $(_id)[0].spreadsheet.sheets.length])
// $(_id)[0].spreadsheet.sheets[_index].name = `sheet {_index}`
http . post ( _uri , function ( x ) {
if ( x . status == 200 && x . readyState == 4 ) {
var _data = JSON . parse ( x . responseText )
_data . Workbook . sheets . forEach ( sheet => {
sheet . name = file . name
sheet . index = _index
$ ( _id ) [ 0 ] . spreadsheet . insertSheet ( [ sheet ] , 0 )
} )
} else {
//@TODO:
// handle the error in a graceful way
}
} )
}
this . export = function ( _label , spreadsheet ) {
var uri = 'api/io/write'
var http = HttpClient . instance ( )
var _data = { }
_rows = { }
spreadsheet . sheets . forEach ( ( sheet ) => {
_rows [ sheet . name ] = { columns : [ ] , values : [ ] }
sheet . rows . forEach ( ( _item , _index ) => {
// cells = _item.cells
if ( _index == 0 ) {
_item . cells . forEach ( _x => {
_rows [ sheet . name ] . columns . push ( _x . value )
} )
} else {
rec = [ ]
_item . cells . forEach ( _x => {
rec . push ( _x . value )
} )
_rows [ sheet . name ] . values . push ( rec )
}
} )
console . log ( _rows [ sheet . name ] . values )
// if (sheet.ranges.length > 0){
// if(sheet.ranges[0].dataSource.length > 0){
// _data.push(sheet) //.ranges[0].dataSource)
// }
// }
} )
if ( _rows ) {
http . setHeader ( 'Content-Type' , 'application/json' )
http . setData ( JSON . stringify ( { rows : _rows , 'label' : _label } ) )
http . post ( uri , ( x ) => {
if ( x . status == 200 && x . readyState == 4 ) {
//
//
// console.log(x)
// var blob = new Blob([x.response], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const byteCharacters = atob ( x . responseText ) ;
const byteNumbers = new Array ( byteCharacters . length ) ;
for ( let i = 0 ; i < byteCharacters . length ; i ++ ) {
byteNumbers [ i ] = byteCharacters . charCodeAt ( i ) ;
}
const byteArray = new Uint8Array ( byteNumbers ) ;
const blob = new Blob ( [ byteArray ] , {
type : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
} ) ;
const link = document . createElement ( "a" ) ;
link . href = URL . createObjectURL ( blob ) ;
link . download = _label + '-export.xlsx' ;
document . body . appendChild ( link ) ;
link . click ( ) ;
// Cleanup
URL . revokeObjectURL ( link . href ) ;
document . body . removeChild ( link ) ;
} else {
//
// something went wront
}
} )
}
}
this . render = function ( ) {
var _args = this . _args
var _importSheet = this . open
var _exportSheet = this . export
var _icon = $ ( '<img>' ) . attr ( 'src' , this . _args . icon )
var _label = $ ( '<div class="bold"></div>' ) . html ( this . _args . label + '<div style="font-weight:lighter; font-size:11px;">' + this . _args . provider + '</div>' )
read = this . read
@ -229,9 +329,23 @@ studio.frame = function (_args){
}
} )
var _imgSrc = "api/disk/read?uri=www/html/_assets/images/python.png"
_imgSrc = studio . _context + _imgSrc
_codeButton = ( '<div class="border-left"><div class="active" title="show in code"><i class="fa-solid fa-code"></i></div></div>' )
_openFile = $ ( '<div class="border-left"><div class="active" title="Open File"><i class="fa-regular fa-folder-open"></i></div></div>' )
$ ( _openFile ) . on ( 'click' , ( ) => {
var _id = '.' + _args . label + ' .open-file'
$ ( _id ) . on ( 'change' , ( event ) => {
var file = event . target . files [ 0 ]
_id = '.' + _args . label + ' .output'
_importSheet ( _id , file )
} )
$ ( _id ) . click ( )
} )
_saveFile = $ ( '<div class="border-left"><div class="active" title="Save File"><i class="fa-solid fa-floppy-disk"></i></div></div>' )
$ ( _saveFile ) . on ( 'click' , ( ) => {
var _id = '.' + _args . label + ' .output'
var spreadsheet = $ ( _id ) [ 0 ] . spreadsheet
_exportSheet ( _args . label , spreadsheet )
} )
_wizButton = $ ( '<div class="border-left"><div class="active" title="New Connection"><i class="fa-solid fa-plug-circle-plus" style="color:#4682B4;"></i></div></div>' )
_wizButton . on ( 'click' , function ( ) {
dialog . show ( { uri : 'www/html/wizard.html' , title : 'Create New Connection' , context : studio . _context } )
@ -253,13 +367,11 @@ studio.frame = function (_args){
}
$ ( _id + ' .expand' ) . empty ( ) . html ( _icon ) . attr ( 'title' , _title )
var spreadsheet = $ ( _id + ' .output' ) [ 0 ] . spreadsheet
if ( spreadsheet != null ) {
spreadsheet . refresh ( )
}
spreadsheet . refresh ( )
} )
var _buttons = $ ( '<div style="display:grid; grid-template-columns: auto repeat( 4 ,64px); gap:4px;" align="center"><div> </div></div>')
_buttons . append ( _ codeButton , _wizButton , _xbutton , _expandButton )
var _buttons = $ ( '<div style="display:grid; grid-template-columns: auto repeat( 5 ,64px); gap:4px;" align="center"><div> </div></div>')
_buttons . append ( _ openFile, _saveFile , _wizButton , _xbutton , _expandButton )
// _frame = $('<div class="studio :label"></div>'.replace(/:label/,this._args.label))
var _frame = $ ( '<div class="studio"></div>' ) . addClass ( this . _args . label )
@ -280,11 +392,7 @@ studio.frame = function (_args){
$ ( _id ) . click ( )
}
} )
// _inputframe = $('<div class="input-frame"><div class="ctrl border-round border">.::</div> <textarea class="code"></textarea></div>')
// _inputframe = $('<div class="input-frame"></div>')
// _inputframe.append(_ctrl,_textarea)
// _outputframe = $('<div class="" style="display:grid; grid-template-rows:auto 32px; gap:4px;"><div class="output border-round" ></div><div class="status border"></div></div>')
_outputframe = $ ( '<div class="output" ></div><div class="status"></div>' )
_outputframe = $ ( '<div class="output" ></div><div class="status"></div><input type="file" class="open-file" style="display:none" accept=".csv, .xls, .xlsx"/>' )
// $(_frame).append(_inputframe, _outputframe)
@ -308,25 +416,7 @@ studio.frame = function (_args){
_gHandler . syncfusion ( _id , _data )
_rows = ( _data . data ) ? _data . data . length : 0
$ ( '.' + _label + ' .status' ) . html ( _rows + ' rows' )
// $(_id).html('<table class="display"></table>')
// _id = _id + ' table'
// var _columns = []
// _args = {}
// if (_data.columns ){
// _data.columns.forEach(_name=>{
// _columns.push({title:_name})
// })
// var _args = {'data':_data.data,'columns':_columns}
// // _args.layout = {topStart:{}}
// _args.buttons = ['excel','pdf']
// }else{
// _data = {'data':[],columns:[]}
// }
// _args.dom = 'rtip'
// _args.scrollCollapse = true
// _args.scrollY = '300px'
// new DataTable(_id,_args)
}
}