44 */
55
66import Component from '@glimmer/component' ;
7- import { tracked } from '@glimmer/tracking' ;
87import { action } from '@ember/object' ;
8+ import { service } from '@ember/service' ;
9+ import { tracked } from '@glimmer/tracking' ;
10+ import {
11+ autocompletion ,
12+ completionKeymap ,
13+ keymap ,
14+ } from '@hashicorp/design-system-components/codemirror' ;
15+
16+ import { createGrantCompletionSource } from 'admin/utils/grant-completions' ;
17+
18+ export default class FormRoleEditGrantsComponent extends Component {
19+ @service intl ;
920
10- export default class FormRoleEditGrants extends Component {
1121 // =attributes
1222
1323 exportOptionsMap = { terraform : 'terraform' , nativeHCL : 'native-hcl' } ;
1424 exportOptions = Object . values ( this . exportOptionsMap ) ;
25+ completionTranslatedStrings = {
26+ noSuggestions : this . intl . t ( 'resources.role.edit-grants.no-suggestions' ) ,
27+ wildcardTypes : this . intl . t (
28+ 'resources.role.edit-grants.completion-info.wildcard-types' ,
29+ ) ,
30+ wildcardIds : this . intl . t (
31+ 'resources.role.edit-grants.completion-info.wildcard-ids' ,
32+ ) ,
33+ templateValue : this . intl . t (
34+ 'resources.role.edit-grants.completion-info.template-value' ,
35+ ) ,
36+ wildcardActions : this . intl . t (
37+ 'resources.role.edit-grants.completion-info.wildcard-actions' ,
38+ ) ,
39+ allFields : this . intl . t (
40+ 'resources.role.edit-grants.completion-info.all-fields' ,
41+ ) ,
42+ } ;
1543
16- // TODO: Replace with actual grant lines from code editor once implemented.
17- grantStringLines =
18- 'ids=hc_123;type=host-catalog;actions=read,create,list\nids=ttcp_123;type=target;actions=list\ntype=credential;actions=create' ;
44+ completionSource = createGrantCompletionSource (
45+ this . args . grantsSchema ,
46+ this . completionTranslatedStrings ,
47+ ) ;
1948
49+ @tracked grantStringsText = ( this . args . model ?. grant_strings ?? [ ] ) . join ( '\n' ) ;
50+ @tracked currentLineText = this . args . model ?. grant_strings ?. [ 0 ] ?? '' ;
2051 @tracked showExportOptionsFlyout = false ;
2152 @tracked selectedExportOption = this . exportOptions [ 0 ] ;
2253
54+ customExtensions = [
55+ autocompletion ( {
56+ override : [ this . completionSource ] ,
57+ // Trigger autocompletion when the user completes a grant field (which we labeled as keywords)
58+ activateOnCompletion : ( completion ) => completion . type === 'keyword' ,
59+ } ) ,
60+ keymap . of ( completionKeymap ) ,
61+ ] ;
62+
63+ get grantStrings ( ) {
64+ return this . grantStringsText
65+ . split ( '\n' )
66+ . map ( ( grantString ) => grantString . trim ( ) )
67+ . filter ( Boolean ) ;
68+ }
69+
2370 /**
2471 * Returns the formatted export based on the selected export option.
2572 * @type {string }
@@ -37,7 +84,7 @@ export default class FormRoleEditGrants extends Component {
3784 */
3885 get terraformFormattedExport ( ) {
3986 let formatted = `grant_strings = [ \n` ;
40- this . grantStringLines . split ( '\n' ) . forEach ( ( line ) => {
87+ this . grantStringsText . split ( '\n' ) . forEach ( ( line ) => {
4188 formatted += ` "${ line } ",\n` ;
4289 } ) ;
4390 formatted += `]\n` ;
@@ -50,7 +97,7 @@ export default class FormRoleEditGrants extends Component {
5097 */
5198 get nativeHclFormattedExport ( ) {
5299 let formatted = `[ \n` ;
53- this . grantStringLines . split ( '\n' ) . forEach ( ( line ) => {
100+ this . grantStringsText . split ( '\n' ) . forEach ( ( line ) => {
54101 formatted += ` "${ line } ",\n` ;
55102 } ) ;
56103 formatted += `]\n` ;
@@ -59,6 +106,14 @@ export default class FormRoleEditGrants extends Component {
59106
60107 // =actions
61108
109+ @action
110+ onInput ( value , view ) {
111+ this . grantStringsText = value ;
112+
113+ const line = view . state . doc . lineAt ( view . state . selection . main . head ) ;
114+ this . currentLineText = line . text ;
115+ }
116+
62117 /**
63118 * Toggles the export options flyout open and closed.
64119 */
0 commit comments