Skip to content

Commit

Permalink
Merge pull request #5 from sasjs/download-logs
Browse files Browse the repository at this point in the history
Logs component, download functionality
  • Loading branch information
allanbowe authored Oct 16, 2023
2 parents dce7f14 + 314355f commit 2842967
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
16 changes: 16 additions & 0 deletions projects/ngx-sasjs/src/lib/ngx-sasjs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,20 @@ import { Injectable } from '@angular/core'
})
export class NgxSasjsService {
constructor() {}

downloadTextFile(filename: string, text: string) {
const element = document.createElement('a')
element.setAttribute(
'href',
'data:text/plain;charset=utf-8,' + encodeURIComponent(text)
)
element.setAttribute('download', filename + '.txt')

element.style.display = 'none'
document.body.appendChild(element)

element.click()

document.body.removeChild(element)
}
}
14 changes: 13 additions & 1 deletion projects/ngx-sasjs/src/lib/sasjs-logs/sasjs-logs.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
import { SASjsConfig } from '@sasjs/adapter'
import moment from 'moment'
import { SASjsRequestExtended } from './models/sasjs-request.model'
import { NgxSasjsService } from '../ngx-sasjs.service'

@Component({
selector: 'sasjs-logs',
Expand All @@ -12,6 +13,8 @@ export class SasjsLogsComponent implements OnInit {
private _show: boolean = false
private _sasjsRequests: SASjsRequestExtended[] = []

@Input() defaultDownloadBehaviour: boolean = true

@Input()
set show(value: boolean) {
this._show = value
Expand Down Expand Up @@ -43,7 +46,7 @@ export class SasjsLogsComponent implements OnInit {
public tablesActive: boolean = false
public workTables: any

constructor() {}
constructor(private ngxSasjsService: NgxSasjsService) {}

ngOnInit(): void {}

Expand Down Expand Up @@ -149,13 +152,22 @@ export class SasjsLogsComponent implements OnInit {

downloadLog(logFile: string) {
this.onDownloadLog.emit(logFile)
const timestamp = new Date().valueOf()
this.ngxSasjsService.downloadTextFile(`logFile-${timestamp}`, logFile)
}

downloadSourceCode(sourceCode: string) {
this.onDownloadSourceCode.emit(sourceCode)
const timestamp = new Date().valueOf()
this.ngxSasjsService.downloadTextFile(`sourceCode-${timestamp}`, sourceCode)
}

downloadGeneratedCode(generatedCode: string) {
this.onDownloadGeneratedCode.emit(generatedCode)
const timestamp = new Date().valueOf()
this.ngxSasjsService.downloadTextFile(
`generatedCode-${timestamp}`,
generatedCode
)
}
}

0 comments on commit 2842967

Please sign in to comment.