import { commands , Uri , window , workspace } from 'vscode' ; import { Item } from './Item' ; import * as utils from '../Utils/Utils' ; import * as _ from 'lodash' ; import axios , { AxiosRequestConfig } from 'axios' ; import { ConfigManager } from '../Utils/ConfigManager' ; import { OH_CONFIG_PARAMETERS } from '../Utils/types' ; author Kuba Wolanin - Initial contribution
  export class ItemsModel { constructor ( ) { } /**
     * Returns Items that don't belong to any Group
     */ public get roots ( ) : Thenable < Item [ ] > { return this . sendRequest ( null , ( items ) => { let itemsMap = items . map ( item => new Item ( item ) ) ; let rootItems = _ . filter ( itemsMap , ( item ) => item . isRootItem ) ; return this . sort ( rootItems ) ; } ) ; } param item public getChildren ( item ) { return this . sendRequest ( utils . getHost ( ) + '/rest/items/' + item . name , ( item ) => { let itemsMap = item . members . map ( item => new Item ( item ) ) ; return this . sort ( itemsMap ) ; } ) ; } /**
     * List of items used in ItemsCompletion
     */ public get completions ( ) : Thenable < Item [ ] > { return this . sendRequest ( null , ( items ) => { return items ; } ) ; } param uri param transform private sendRequest ( uri , transform ) { let config = { url : uri || utils . getHost ( ) + '/rest/items' , headers : { } } ; if ( ConfigManager . tokenAuthAvailable ( ) ) { config . headers = { 'X-OPENHAB-TOKEN' : ConfigManager . get ( OH_CONFIG_PARAMETERS . connection . authToken ) } ; } return new Promise ( ( resolve , _reject ) => { axios ( config ) . then ( function ( response ) { resolve ( transform ( response . data as Item [ ] | Item ) ) ; } . bind ( this ) ) . catch ( err => { utils . appendToOutput ( `Could not reload items for Items Explorer` ) ; utils . handleRequestError ( err ) . then ( err => resolve ( [ ] ) ) ; } ) ; } ) ; } /**
     * Sorts items in alphabetical order. Only needed for items explorer, code completion is sorted by vscode
     */ protected sort ( nodes ) { return nodes . sort ( ( n1 , n2 ) => { if ( n1 . isGroup && ! n2 . isGroup ) { return - 1 ; } if ( ! n1 . isGroup && n2 . isGroup ) { return 1 ; } return n1 . name . localeCompare ( n2 . name ) ; } ) ; } }