@@ -9,6 +9,7 @@ import "@/components/util/json_button"
99import "@/components/util/korp-error"
1010import "@/components/wordpicture/word-picture"
1111import { StoreService } from "@/services/store"
12+ import { CsvType , downloadCsvFile } from "@/csv"
1213
1314type ResultsWordPictureController = IController & {
1415 isActive : boolean
@@ -19,6 +20,7 @@ type ResultsWordPictureController = IController & {
1920type ResultsWordPictureScope = IScope & {
2021 activated : boolean
2122 data ?: WordPicture
23+ downloadOption : CsvType | ""
2224 error ?: string
2325 limit : string // Number as string to work with <select ng-model>
2426 limitOptions : number [ ]
@@ -75,6 +77,12 @@ angular.module("korpApp").component("resultsWordPicture", {
7577 </ help-box >
7678
7779 < div class ="mt-4 flex gap-4 justify-end ">
80+ < select ng-show ="!$ctrl.loading && data && !warning && !error " ng-model ="downloadOption ">
81+ < option value =""> {{ "download" | loc:$root.lang }}</ option >
82+ < option value ="comma "> {{ "csv_comma" | loc:$root.lang }}</ option >
83+ < option value ="tab "> {{ "csv_tab" | loc:$root.lang }}</ option >
84+ </ select >
85+
7886 < json-button
7987 ng-if ="!$ctrl.loading && data && !warning && !error "
8088 endpoint ="relations "
@@ -94,6 +102,7 @@ angular.module("korpApp").component("resultsWordPicture", {
94102 function ( $scope : ResultsWordPictureScope , $timeout : ITimeoutService , store : StoreService ) {
95103 const $ctrl = this as ResultsWordPictureController
96104 $scope . activated = false
105+ $scope . downloadOption = ""
97106 $scope . limit = String ( LIMITS [ 0 ] )
98107 $scope . limitOptions = [ ...LIMITS ]
99108 $scope . proxy = new RelationsProxy ( )
@@ -185,6 +194,19 @@ angular.module("korpApp").component("resultsWordPicture", {
185194 } )
186195 . finally ( ( ) => $timeout ( ( ) => $ctrl . setProgress ( false , 0 ) ) )
187196 }
197+
198+ // Create and download CSV file when the download selector is used
199+ $scope . $watch ( "downloadOption" , ( ) => {
200+ // Skip if empty (at init or if the label option is selected)
201+ if ( ! $scope . downloadOption ) return
202+
203+ if ( ! $scope . data ) throw new Error ( "Word picture data missing" )
204+ const rows = $scope . data . generateCsv ( )
205+ downloadCsvFile ( "word-picture" , rows , $scope . downloadOption )
206+
207+ // Reset to the label option
208+ $scope . downloadOption = ""
209+ } )
188210 } ,
189211 ] ,
190212} )
0 commit comments