1+ import { getOwner } from '@ember/application' ;
12import { inject as service } from '@ember/service' ;
3+ import { waitFor } from '@ember/test-waiters' ;
24import Model , { AsyncHasMany , attr , hasMany } from '@ember-data/model' ;
5+ import { dropTask } from 'ember-concurrency' ;
36import IntlService from 'ember-intl/services/intl' ;
47
58import GetLocalizedPropertyHelper from 'ember-osf-web/helpers/get-localized-property' ;
6- import { getOwner } from '@ember/application' ;
9+ import config from 'ember-osf-web/config/environment' ;
10+ import OsfModel from 'ember-osf-web/models/osf-model' ;
11+ import { tracked } from 'tracked-built-ins' ;
12+ const osfUrl = config . OSF . url ;
713
814export interface LanguageText {
915 '@language' : string ;
@@ -23,10 +29,28 @@ export default class IndexCardModel extends Model {
2329
2430 getLocalizedString = new GetLocalizedPropertyHelper ( getOwner ( this ) ) ;
2531
32+ @tracked osfModel ?: OsfModel ;
33+
2634 get resourceId ( ) {
2735 return this . resourceIdentifier [ 0 ] ;
2836 }
2937
38+ get osfModelType ( ) {
39+ const types = this . resourceMetadata . resourceType . map ( ( item : any ) => item [ '@id' ] ) ;
40+ if ( types . includes ( 'Project' ) || types . includes ( 'ProjectComponent' ) ) {
41+ return 'node' ;
42+ } else if ( types . includes ( 'Registration' ) || types . includes ( 'RegistrationComponent' ) ) {
43+ return 'registration' ;
44+ } else if ( types . includes ( 'Preprint' ) ) {
45+ return 'preprint' ;
46+ } else if ( types . includes ( 'Person' ) || types . includes ( 'Agent' ) ) {
47+ return 'user' ;
48+ } else if ( types . includes ( 'File' ) ) {
49+ return 'file' ;
50+ }
51+ return null ;
52+ }
53+
3054 get label ( ) {
3155 const possibleLabelKeys = [ 'displayLabel' , 'name' , 'title' ] ;
3256 for ( const key of possibleLabelKeys ) {
@@ -44,6 +68,31 @@ export default class IndexCardModel extends Model {
4468 }
4569 return '' ;
4670 }
71+
72+ @dropTask
73+ @waitFor
74+ async getOsfModel ( options ?: object ) {
75+ const identifier = this . resourceIdentifier ;
76+ if ( identifier && this . osfModelType ) {
77+ const guid = this . guidFromIdentifierList ( identifier ) ;
78+ if ( guid ) {
79+ const osfModel = await this . store . findRecord ( this . osfModelType , guid , options ) ;
80+ this . osfModel = osfModel ;
81+ }
82+ }
83+ }
84+
85+ guidFromIdentifierList ( ) {
86+ for ( const iri of this . resourceIdentifier ) {
87+ if ( iri && iri . startsWith ( osfUrl ) ) {
88+ const pathSegments = iri . slice ( osfUrl . length ) . split ( '/' ) . filter ( Boolean ) ;
89+ if ( pathSegments . length === 1 ) {
90+ return pathSegments [ 0 ] ; // one path segment; looks like osf-id
91+ }
92+ }
93+ }
94+ return null ;
95+ }
4796}
4897
4998declare module 'ember-data/types/registries/model' {
0 commit comments