55 */
66'use strict' ;
77
8- define ( [ 'utils/dialog-helper' , 'utils/wkt-logger' , 'utils/modelEdit/alias-helper' , 'utils/wdt-archive -helper',
9- 'utils/modelEdit/message-helper' ] ,
10- function ( DialogHelper , WktLogger , AliasHelper , ArchiveHelper , MessageHelper ) {
8+ define ( [ 'utils/dialog-helper' , 'utils/wkt-logger' , 'utils/modelEdit/model-edit -helper' ,
9+ 'utils/modelEdit/alias-helper' , 'utils/wdt-archive-helper' , 'utils/modelEdit/ message-helper'] ,
10+ function ( DialogHelper , WktLogger , ModelEditHelper , AliasHelper , ArchiveHelper , MessageHelper ) {
1111
1212 function FileSelectHelper ( ) {
1313 // support selecting files for attributes
1414
15- this . selectFile = async ( attribute , currentValue ) => {
15+ // matching wdtArchive subtypes
16+ const DIR_TYPES = [ 'dir' , 'either' , 'emptyDir' ] ;
17+ const FILE_TYPES = [ 'file' , 'either' ] ;
18+
19+ this . canChooseDirectory = attribute => {
20+ return canChoose ( DIR_TYPES , attribute ) ;
21+ } ;
22+
23+ this . canChooseFile = attribute => {
24+ return canChoose ( FILE_TYPES , attribute ) ;
25+ } ;
26+
27+ function canChoose ( matchTypes , attribute ) {
28+ const archiveTypeKeys = attribute . archiveTypes || [ ] ;
29+ for ( const archiveTypeKey of archiveTypeKeys ) {
30+ const archiveType = ModelEditHelper . getArchiveType ( archiveTypeKey ) || { } ;
31+ if ( matchTypes . includes ( archiveType . subtype ) ) {
32+ return true ;
33+ }
34+ }
35+ const fileOptions = attribute . fileOptions || [ ] ;
36+ for ( const fileOption of fileOptions ) {
37+ if ( matchTypes . includes ( fileOption . type ) ) {
38+ return true ;
39+ }
40+ }
41+ // if no archive or file options found, allow file or dir
42+ return ! archiveTypeKeys . length && ! fileOptions . length ;
43+ }
44+
45+ this . chooseDirectory = async ( attribute , currentValue ) => {
46+ return choosePath ( attribute , 'dir' , currentValue ) ;
47+ } ;
48+
49+ this . chooseFile = async ( attribute , currentValue ) => {
50+ return choosePath ( attribute , 'file' , currentValue ) ;
51+ } ;
52+
53+ async function choosePath ( attribute , matchType , currentValue ) {
1654 const aliasPath = AliasHelper . getAliasPath ( attribute . path ) ;
1755 const attributeLabel = MessageHelper . getAttributeLabel ( attribute , aliasPath ) ;
1856
19- const archiveTypes = await ArchiveHelper . getEntryTypes ( ) ;
57+ // build a list of select options based on archive and file options
2058 const selectOptions = [ ] ;
2159
2260 // possibly multiple archive types (app, custom?)
2361 const archiveTypeKeys = attribute . archiveTypes || [ ] ;
2462 archiveTypeKeys . forEach ( archiveTypeKey => {
25- if ( ! ( archiveTypeKey in archiveTypes ) ) {
26- WktLogger . error ( 'Invalid archive type: ' + archiveTypeKey ) ;
63+ const archiveType = ModelEditHelper . getArchiveType ( archiveTypeKey ) ;
64+ if ( ! archiveType ) {
2765 return ;
2866 }
2967
30- // file dir either emptyDir
31-
32- const archiveType = archiveTypes [ archiveTypeKey ] ;
3368 const subtype = archiveType . subtype ;
3469 const segregateName = getSegregateName ( attribute ) ;
70+ const segregateLabel = archiveType . segregatedLabel ;
71+ const segregateHelp = archiveType . segregatedHelp ;
3572
36- if ( [ 'file' , 'either' ] . includes ( subtype ) ) {
37- selectOptions . push ( {
38- type : 'file' ,
39- label : archiveType . fileLabel ,
40- extensions : archiveType . extensions ,
41- archiveType : archiveTypeKey ,
42- segregateLabel : archiveType . segregatedLabel ,
43- segregateHelp : archiveType . segregatedHelp ,
44- segregateName
45- } ) ;
46- }
47-
48- if ( [ 'dir' , 'either' ] . includes ( subtype ) ) {
49- selectOptions . push ( {
50- type : 'dir' ,
51- label : archiveType . dirLabel ,
52- archiveType : archiveTypeKey ,
53- segregateLabel : archiveType . segregatedLabel ,
54- segregateHelp : archiveType . segregatedHelp ,
55- segregateName
56- } ) ;
57- }
58-
59- if ( 'emptyDir' === subtype ) {
73+ if ( 'emptyDir' === subtype ) { // add two specific options
6074 // bypass file selection and add to archive
6175 selectOptions . push ( {
6276 type : 'emptyDir' ,
6377 labelKey : 'file-select-archive-empty-dir' ,
6478 archiveType : archiveTypeKey ,
65- segregateLabel : archiveType . segregatedLabel ,
66- segregateHelp : archiveType . segregatedHelp ,
79+ segregateLabel,
80+ segregateHelp,
6781 segregateName
6882 } ) ;
6983
@@ -73,6 +87,23 @@ function (DialogHelper, WktLogger, AliasHelper, ArchiveHelper, MessageHelper) {
7387 labelKey : 'file-select-local-empty-dir' ,
7488 chooserName : attributeLabel
7589 } ) ;
90+
91+ } else { // add one option based on match type
92+ const fileMatch = matchType === 'file' && FILE_TYPES . includes ( subtype ) ;
93+ const dirMatch = matchType === 'dir' && DIR_TYPES . includes ( subtype ) ;
94+ const label = dirMatch ? archiveType . dirLabel : archiveType . fileLabel ;
95+
96+ if ( dirMatch || fileMatch ) {
97+ selectOptions . push ( {
98+ type : matchType ,
99+ label,
100+ extensions : archiveType . extensions ,
101+ archiveType : archiveTypeKey ,
102+ segregateLabel,
103+ segregateHelp,
104+ segregateName
105+ } ) ;
106+ }
76107 }
77108 } ) ;
78109
@@ -83,11 +114,10 @@ function (DialogHelper, WktLogger, AliasHelper, ArchiveHelper, MessageHelper) {
83114 selectOptions . push ( fileOption ) ;
84115 } ) ;
85116
86- // if no options found, default is simple file or directory
117+ // if archive or file options found, default is choose specified type
87118 if ( ! selectOptions . length ) {
88119 selectOptions . push (
89- { type : 'file' , chooserName : attributeLabel } ,
90- { type : 'dir' , chooserName : attributeLabel }
120+ { type : matchType , chooserName : attributeLabel }
91121 ) ;
92122 }
93123
@@ -96,17 +126,14 @@ function (DialogHelper, WktLogger, AliasHelper, ArchiveHelper, MessageHelper) {
96126 const selectType = selectOption . type ;
97127 if ( ! [ 'file' , 'dir' , 'emptyDir' ] . includes ( selectType ) ) {
98128 WktLogger . error ( 'Invalid selection type: ' + selectType ) ;
99- return ; // cancel
100129 }
101-
102130 const defaultLabel = MessageHelper . t ( 'file-select-type-' + selectOption . type ) ;
103131 selectOption . label = MessageHelper . getLabel ( selectOption ) || defaultLabel ;
104132 } ) ;
105133
106134 let selectOption ;
107135
108- // prompt for select option if more than one type is present in metadata
109- // const fileSelectOptions = attribute.fileSelectOptions || [];
136+ // prompt for select option if more than one type is present.
110137 if ( selectOptions . length === 1 ) {
111138 selectOption = selectOptions [ 0 ] ;
112139 } else {
0 commit comments