import React, { Component } from "react";
import { render } from "react-dom";
import { AgGridReact } from "ag-grid-react";
import "ag-grid-enterprise";
import 'bootstrap/dist/js/bootstrap.js';
import $ from 'jquery'
function cloneObject (obj) {
return JSON.parse(JSON.stringify(obj))
}
class InvoiceTextFields extends Component {
constructor (props) {
super(props)
this.state = {
columnDefs: [
{headerName: 'Field Name', field: 'fieldName', width: 70},
{headerName: 'Extracted Taxonomy', field: 'extractedTaxonomy', width: 70},
{headerName: 'Taxonomy Data Field', field: 'taxonomyDataField', width: 70},
{
headerName: 'Corrected Texonomy',
field: 'value',
width: 70,
editable: true,
cellEditorSelector:function (params) {
if (params.data.type === 'age') {
return {
component: 'numericCellEditor'
}
}
if (params.data.type === 'examination') {
return {
component: 'agRichSelectCellEditor',
params: { values: extractValues(carMappings) },
valueFormatter: function (params) {
return lookupValue(carMappings, params.value)
},
valueParser: function (params) {
return lookupKey(carMappings, params.newValue)
}
}
}
if (params.data.type === 'tenor') {
return {
component: 'agSelectCellEditor',
params: { values: extractValues(tenorMappings) },
valueFormatter: function (params) {
return lookupValue(carMappings, params.value)
},
valueParser: function (params) {
return lookupKey(carMappings, params.newValue)
}
}
}
if (params.data.type === 'dateVal') {
return {
cellEditor: 'datePicker'
}
}
return null
}
}
],
// components: { datePicker: getDatePicker() },
rowData: [
{value: '28/02/2019', type: 'dateVal', fieldName:'Expiry Date'},
{value: 'YES', type: 'tenor', fieldName:'Tenor Type'},
{value: 'Vessel 1', type: 'text', fieldName:'Vessel Name'},
{value: 'Examine Document', type: 'examination', fieldName:'Examination Type'},
],
gridOptions:{
onRowEditingStarted: function (event) {
console.log('never called - not doing row editing')
},
onRowEditingStopped: function (event) {
console.log('never called - not doing row editing')
},
onCellEditingStarted: function (event) {
console.log('cellEditingStarted')
},
onCellEditingStopped: function (event) {
console.log('cellEditingStopped')
}
}
}
}
onGridReady = params => {
this.gridApi = params.api
this.gridColumnApi = params.columnApi
params.api.sizeColumnsToFit()
};
onCellClicked = (event) => {
console.log('hi: ' + event.colDef.field)
console.log('hi type: ' + event.colDef.type)
if (this.props.correctedTaxonomy === null) {
console.log('null')
}
if (this.props.correctedTaxonomy === '') {
console.log('empty')
}
if (event.colDef.field === 'value' && this.props.correctedTaxonomy !== null) {
var rowModel = this.gridApi.getModel()
var rowNode = rowModel.rowsToDisplay[event.rowIndex]
event.data.correctedText = this.props.correctedTaxonomy
rowNode.setDataValue('value', event.data.correctedText)
}
};
render () {
return (
<div style={{ width: '100%', height: '100%' }}>
<div
id='myGrid'
style={{
height: '100%',
width: '103%'
}}
className='ag-theme-balham'
>
<AgGridReact
columnDefs={this.state.columnDefs}
// components={this.state.components}
rowData={this.state.rowData}
gridOptions={this.state.gridOptions}
onGridReady={this.onGridReady}
// onCellClicked={this.onCellClicked.bind(this)}
/>
</div>
</div>
)
}
}
var carMappings = {
'Examine Document': 'Examine Document',
'Do not Examine': 'Do not Examine',
'Send on approval with Doc Exam': 'Send on approval with Doc Exam',
'Send on Approval Without Doc' : 'Send on Approval Without Doc ',
'Exam' : 'Exam'
}
var tenorMappings = {
'YES': 'YES',
'NO': 'NO'
}
function extractValues (mappings) {
return Object.keys(mappings)
}
function lookupValue (mappings, key) {
return mappings[key]
}
function lookupKey (mappings, name) {
for (var key in mappings) {
if (mappings.hasOwnProperty(key)) {
if (name === mappings[key]) {
return key
}
}
}
}
function getCharCodeFromEvent (event) {
event = event || window.event
return (typeof event.which === 'undefined') ? event.keyCode : event.which
}
function isCharNumeric (charStr) {
return !!/\d/.test(charStr)
}
function isKeyPressedNumeric (event) {
var charCode = getCharCodeFromEvent(event)
var charStr = String.fromCharCode(charCode)
return isCharNumeric(charStr)
}
function getDatePicker () {
console.log('in gerDatePicker...')
function Datepicker () {}
Datepicker.prototype.init = function (params) {
this.eInput = document.createElement('input')
this.eInput.value = params.value
$(this.eInput).datepicker({ dateFormat: 'dd/mm/yy' })
}
Datepicker.prototype.getGui = function () {
return this.eInput
}
Datepicker.prototype.afterGuiAttached = function () {
this.eInput.focus()
this.eInput.select()
}
Datepicker.prototype.getValue = function () {
return this.eInput.value
}
Datepicker.prototype.destroy = function () {}
Datepicker.prototype.isPopup = function () {
return false
}
return Datepicker
}
export default InvoiceTextFields