import { Component } from '../../lib/vues6/vues6.js' import { action_service } from '../service/Action.service.js' import { message_service } from '../service/Message.service.js' const template = `

{{ resource_class.plural }}

# {{ col.name }}
{{ index + 1 }} {{ col.renderer(row[col.field]) }} {{ row[col.field] ? 'Yes' : 'No' }} {{ col.field in row ? row[col.field] : '-' }}
` export default class ListingComponent extends Component { static get selector() { return 'cobalt-listing' } static get template() { return template } static get props() { return ['resource'] } definition = {} data = [] resource_class = {} async vue_on_create() { // Load the resource const resource_mod = await import(`../resource/${this.resource}.resource.js`) if ( !resource_mod ) throw new Error('Unable to load Cobalt listing resource.') const rsc_name = this.resource.toLowerCase().replace(/\//g, '_') if ( !resource_mod[rsc_name] ) throw new Error('Unable to extract resource object from module.') this.resource_class = resource_mod[rsc_name] await this.load() } async load() { this.definition = this.resource_class.listing_definition this.data = await this.resource_class.list() } async perform($event, action, row = undefined) { if ( action.confirm ) { message_service.modal({ title: 'Are you sure?', message: `You are about to ${action.action}${row ? ' this '+this.resource_class.item : ''}. Do you want to continue?`, buttons: [ { text: 'Cancel', type: 'close', }, { text: 'Continue', class: ['btn', 'btn-primary'], type: 'close', on_click: async () => { await action_service.perform({...action, resource: this.resource, ...(row ? {id: row.id} : {})}) await this.load() }, }, ], }) } else { await action_service.perform({...action, resource: this.resource, ...(row ? {id: row.id} : {})}) await this.load() } } }