forked from eclipsesource/theia-tree-editor-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move common utils used by clients into extension
- Loading branch information
1 parent
1c6ad3a
commit 7e94cb0
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
theia-tree-editor-extension/src/browser/tree-editor-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { JsonSchema7 } from '@jsonforms/core'; | ||
import { Property } from '@jsonforms/material-tree-renderer'; | ||
|
||
export interface LabelDefinition { | ||
/** A constant label value displayed for every object for which this label definition applies. */ | ||
constant?: string; | ||
/** The property name that is used to get a variable part of an object's label. */ | ||
property?: string; | ||
} | ||
|
||
export const calculateLabel = (labels) => | ||
(schema: JsonSchema7) => (element: Object): string => { | ||
|
||
if (labels !== undefined && labels[schema.$id] !== undefined) { | ||
|
||
if (typeof labels[schema.$id] === 'string') { | ||
// To be backwards compatible: a simple string is assumed to be a property name | ||
console.log('element', element); | ||
return element[labels[schema.$id]]; | ||
} | ||
if (typeof labels[schema.$id] === 'object') { | ||
const info = labels[schema.$id] as LabelDefinition; | ||
let label; | ||
if (info.constant !== undefined) { | ||
label = info.constant; | ||
} | ||
if (info.property !== undefined && element[info.property] !== undefined) { | ||
label = label === undefined ? | ||
element[info.property] : | ||
`${label} ${element[info.property]}`; | ||
} | ||
if (label !== undefined) { | ||
return label; | ||
} | ||
} | ||
} | ||
|
||
const namingKeys = Object | ||
.keys(schema.properties) | ||
.filter(key => key === '$id' || key === 'name'); | ||
if (namingKeys.length !== 0) { | ||
return element[namingKeys[0]]; | ||
} | ||
|
||
return JSON.stringify(element); | ||
}; | ||
|
||
export const filterPredicate = (modelMapping) => (data: Object) => { | ||
return (property: Property): boolean => { | ||
if (modelMapping !== undefined && modelMapping.mapping !== undefined) { | ||
if (data[modelMapping.attribute]) { | ||
return property.schema.$id === modelMapping.mapping[data[modelMapping.attribute]]; | ||
} | ||
return true; | ||
} | ||
|
||
return false; | ||
}; | ||
}; |