import * as TaxonomyTypes from "../../@types/helper/taxonomy" ; import { ContextInfo } from "../lib/contextInfo" ; declare var SP ; /**
 * Taxonomy Helper Class
 */ export const Taxonomy = { /**
     * Method to find a term by id
     */ findById : ( term , termId ) : TaxonomyTypes . ITerm => { if ( term . info && term . info . id == termId ) { return term ; } for ( let prop in term ) { if ( prop == "info" || prop == "parent" ) { continue ; } let childTerm = Taxonomy . findById ( term [ prop ] , termId ) ; if ( childTerm ) { return childTerm ; } } } , /**
     * Method to find a term by name
     */ findByName : ( term , termName ) : TaxonomyTypes . ITerm => { if ( term . info && term . info . name == termName ) { return term ; } for ( let prop in term ) { if ( prop == "info" || prop == "parent" ) { continue ; } let childTerm = Taxonomy . findByName ( term [ prop ] , termName ) ; if ( childTerm ) { return childTerm ; } } } , /**
     * Method to get the terms
     */ getTerms : ( termSet , termSetTerms ) : Array < TaxonomyTypes . ITermInfo > => { let terms = [ ] ; terms . push ( { description : termSet . get_description ( ) , id : termSet . get_id ( ) . toString ( ) , name : termSet . get_name ( ) , path : [ ] , pathAsString : "" , props : termSet . get_customProperties ( ) } ) ; let enumerator = termSetTerms . getEnumerator ( ) ; while ( enumerator . moveNext ( ) ) { let term = enumerator . get_current ( ) ; terms . push ( { description : term . get_description ( ) , id : term . get_id ( ) . toString ( ) , name : term . get_name ( ) , path : term . get_pathOfTerm ( ) . split ( ";" ) , pathAsString : term . get_pathOfTerm ( ) , props : term . get_customProperties ( ) } ) ; } terms = terms . sort ( ( a , b ) => { if ( a . pathAsString < b . pathAsString ) { return - 1 ; } if ( a . pathAsString > b . pathAsString ) { return 1 ; } return 0 ; } ) ; return terms ; } , /**
     * Method to get the term group
     */ getTermGroup : ( groupName ? ) : PromiseLike < { context : any ; termGroup : any ; } > => { return new Promise ( ( resolve , reject ) => { Taxonomy . loadScripts ( ) . then ( ( ) => { let context = SP . ClientContext . get_current ( ) ; let session = SP . Taxonomy . TaxonomySession . getTaxonomySession ( context ) ; if ( groupName ) { let termStores = session . get_termStores ( ) ; context . load ( termStores , "Include(Groups)" ) ; context . executeQueryAsync ( ( ) => { let enumerator = termStores . getEnumerator ( ) ; let termStore = enumerator . moveNext ( ) ? enumerator . get_current ( ) : null ; if ( termStore ) { let termGroup = termStore . get_groups ( ) . getByName ( groupName ) ; context . load ( termGroup ) ; resolve ( { context , termGroup } ) ; } else { reject ( "Unable to find the taxonomy store." ) ; } } , ( ... args ) => { reject ( args [ 1 ] . get_message ( ) ) ; } ) ; } else { let termStore = session . getDefaultSiteCollectionTermStore ( ) ; let termGroup = termStore . getSiteCollectionGroup ( context . get_site ( ) ) ; context . load ( termGroup ) ; resolve ( { context , termGroup } ) ; } } ) ; } ) ; } , /**
     * Method to get the term groups
     */ getTermGroups : ( ) => { return new Promise ( ( resolve , reject ) => { Taxonomy . loadScripts ( ) . then ( ( ) => { let context = SP . ClientContext . get_current ( ) ; let session = SP . Taxonomy . TaxonomySession . getTaxonomySession ( context ) ; let termStores = session . get_termStores ( ) ; context . load ( termStores , "Include(Groups)" ) ; context . executeQueryAsync ( ( ) => { let enumerator = termStores . getEnumerator ( ) ; let termStore = enumerator . moveNext ( ) ? enumerator . get_current ( ) : null ; if ( termStore ) { let termGroups = termStore . get_groups ( ) ; context . load ( termGroups , "Include(Description, Id, Name)" ) ; context . executeQueryAsync ( ( ) => { let groups = [ ] ; let enumerator = termGroups . getEnumerator ( ) ; while ( enumerator . moveNext ( ) ) { let group = enumerator . get_current ( ) ; groups . push ( { description : group . get_description ( ) , id : group . get_id ( ) . toString ( ) , name : group . get_name ( ) } ) ; } resolve ( groups ) ; } , ( ... args ) => { reject ( args [ 1 ] . get_message ( ) ) ; } ) ; } else { reject ( "Unable to find the taxonomy store." ) ; } } , ( ... args ) => { reject ( args [ 1 ] . get_message ( ) ) ; } ) ; } ) ; } ) ; } , /**
     * Method to get the term sets for a group
     */ getTermSets : ( groupName ) => { return new Promise ( ( resolve , reject ) => { Taxonomy . getTermGroup ( groupName ) . then ( ( { context , termGroup } ) => { let termGroupInfo = termGroup . get_termSets ( ) ; context . load ( termGroupInfo , "Include(CustomProperties, Description, Id, Name)" ) ; context . executeQueryAsync ( ( ) => { let termSets = [ ] ; let enumerator = termGroupInfo . getEnumerator ( ) ; while ( enumerator . moveNext ( ) ) { let termSet = enumerator . get_current ( ) ; termSets . push ( { description : termSet . get_description ( ) , id : termSet . get_id ( ) . toString ( ) , name : termSet . get_name ( ) , props : termSet . get_customProperties ( ) } ) ; } resolve ( termSets ) ; } , ( ... args ) => { reject ( args [ 1 ] . get_message ( ) ) ; } ) ; } , reason => { reject ( reason ) ; } ) ; } ) ; } , /**
     * Method to get the term sets from the default site collection.
     */ getTermSetsFromDefaultSC : ( ) => { return new Promise ( ( resolve , reject ) => { Taxonomy . loadScripts ( ) . then ( ( ) => { let context = SP . ClientContext . get_current ( ) ; let session = SP . Taxonomy . TaxonomySession . getTaxonomySession ( context ) ; let termStore = session . getDefaultSiteCollectionTermStore ( ) ; let termGroup = termStore . getSiteCollectionGroup ( context . get_site ( ) ) ; let termGroupInfo = termGroup . get_termSets ( ) ; context . load ( termGroupInfo , "Include(CustomProperties, Description, Id, Name)" ) ; context . executeQueryAsync ( ( ) => { let termSets = [ ] ; let enumerator = termGroupInfo . getEnumerator ( ) ; while ( enumerator . moveNext ( ) ) { let termSet = enumerator . get_current ( ) ; termSets . push ( { description : termSet . get_description ( ) , id : termSet . get_id ( ) . toString ( ) , name : termSet . get_name ( ) , props : termSet . get_customProperties ( ) } ) ; } resolve ( termSets ) ; } , ( ... args ) => { reject ( args [ 1 ] . get_message ( ) ) ; } ) ; } ) ; } ) ; } , /**
     * Method to get the terms by id
     */ getTermsById : ( termStoreId , termSetId ) : PromiseLike < any > => { return new Promise ( ( resolve , reject ) => { Taxonomy . loadScripts ( ) . then ( ( ) => { let context = SP . ClientContext . get_current ( ) ; let session = SP . Taxonomy . TaxonomySession . getTaxonomySession ( context ) ; let termStore = session . get_termStores ( ) . getById ( termStoreId ) ; let termSet = termStore . getTermSet ( termSetId ) ; let terms = termSet . getAllTerms ( ) ; context . load ( termSet ) ; context . load ( terms , "Include(CustomProperties, Description, Id, Name, PathOfTerm)" ) ; context . executeQueryAsync ( ( ) => { resolve ( Taxonomy . getTerms ( termSet , terms ) ) ; } , ( ... args ) => { reject ( args [ 1 ] . get_message ( ) ) ; } ) ; } ) ; } ) ; } , /**
     * Method to get the term set by id
     */ getTermSetById : ( termStoreId , termSetId ) : PromiseLike < TaxonomyTypes . ITerm > => { return new Promise ( ( resolve , reject ) => { Taxonomy . getTermsById ( termStoreId , termSetId ) . then ( terms => { resolve ( Taxonomy . toObject ( terms ) ) ; } , reason => { reject ( reason ) ; } ) ; } ) ; } , /**
     * Method to get the terms from the default site collection
     */ getTermsFromDefaultSC : ( termSetName ) : PromiseLike < any > => { return new Promise ( ( resolve , reject ) => { Taxonomy . getTermGroup ( ) . then ( ( { context , termGroup } ) => { let termSet = termGroup . get_termSets ( ) . getByName ( termSetName ) ; let terms = termSet . getAllTerms ( ) ; context . load ( termSet ) ; context . load ( terms , "Include(CustomProperties, Description, Id, Name, PathOfTerm)" ) ; context . executeQueryAsync ( ( ) => { resolve ( Taxonomy . getTerms ( termSet , terms ) ) ; } , ( ... args ) => { reject ( args [ 1 ] . get_message ( ) ) ; } ) ; } , reason => { reject ( reason ) ; } ) ; } ) ; } , /**
     * Method to get the term set from the default site collection
     */ getTermSetFromDefaultSC : ( termSetName ) : PromiseLike < any > => { return new Promise ( ( resolve , reject ) => { Taxonomy . getTermsFromDefaultSC ( termSetName ) . then ( terms => { resolve ( Taxonomy . toObject ( terms ) ) ; } , reason => { reject ( reason ) ; } ) ; } ) ; } , /**
     * Method to get a terms from a specified group
     */ getTermsByGroupName : ( termSetName , groupName ) : PromiseLike < any > => { return new Promise ( ( resolve , reject ) => { Taxonomy . getTermGroup ( groupName ) . then ( ( { context , termGroup } ) => { let termSet = termGroup . get_termSets ( ) . getByName ( termSetName ) ; let terms = termSet . getAllTerms ( ) ; context . load ( termSet ) ; context . load ( terms , "Include(CustomProperties, Description, Id, Name, PathOfTerm)" ) ; context . executeQueryAsync ( ( ) => { resolve ( Taxonomy . getTerms ( termSet , terms ) ) ; } , ( ... args ) => { reject ( args [ 1 ] . get_message ( ) ) ; } ) ; } ) ; } ) ; } , /**
     * Method to get the term set from the default site collection
     */ getTermSetByGroupName : ( termSetName , groupName ) : PromiseLike < any > => { return new Promise ( ( resolve , reject ) => { Taxonomy . getTermsByGroupName ( termSetName , groupName ) . then ( terms => { resolve ( Taxonomy . toObject ( terms ) ) ; } , reason => { reject ( reason ) ; } ) ; } ) ; } , /**
     * Method to ensure the taxonomy script references are loaded.
     */ loadScripts : ( ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { SP . SOD . executeFunc ( "sp.js" , "SP.Utilities.Utility" , ( ) => { SP . SOD . registerSod ( "sp.taxonomy.js" , ContextInfo . webServerRelativeUrl + "/_layouts/15/sp.taxonomy.js" ) ; SP . SOD . executeFunc ( "sp.taxonomy.js" , "SP.Taxonomy.TaxonomySession" , ( ) => { resolve ( ) ; } ) ; } , "sp.js" ) ; } ) ; } , /**
     * Method to convert a term to an array of term information
     */ toArray : ( term ) : Array < TaxonomyTypes . ITermInfo > => { let terms = [ ] ; let getChildTerms = ( term , terms ) => { for ( let prop in term ) { if ( prop == "info" || prop == "parent" ) { continue ; } let childTerm = term [ prop ] ; terms . push ( childTerm . info ) ; getChildTerms ( childTerm , terms ) ; } } ; if ( term ) { if ( term . info ) { terms . push ( term . info ) ; } getChildTerms ( term as any , terms ) ; } return terms ; } , /**
     * Method to convert a term to a field value
     */ toFieldValue : ( term ) => { let termInfo = term ? term [ "info" ] || term : null ; if ( termInfo ) { return { __metadata : { "type" : "SP.Taxonomy.TaxonomyFieldValue" } , Label : termInfo . name , TermGuid : termInfo . id , WssId : - 1 } ; } return null ; } , /**
     * Method to convert a collection of terms to a field value
     */ toFieldMultiValue : ( terms ) => { let results = [ ] ; if ( terms && terms . length > 0 ) { for ( let i = 0 ; i < terms . length ; i ++ ) { let termInfo = terms [ i ] [ "info" ] || terms [ i ] ; results . push ( ";#" + termInfo . name + "|" + termInfo . id ) ; } } return { __metadata : { type : "Collection(SP.Taxonomy.TaxonomyFieldValue)" } , results } ; } , /**
     * Method to convert the terms to an object
     */ toObject : ( terms ) : TaxonomyTypes . ITerm => { let root = { } as any ; let addTerm = ( node , info , path ) => { let term = node ; let termName = "" ; while ( path . length > 0 ) { termName = path [ 0 ] ; if ( term [ termName ] == null ) { term [ termName ] = { } ; } let parent = term ; term = term [ termName ] ; term . parent = parent ; path . splice ( 0 , 1 ) ; } term . info = info ; } ; if ( terms && terms . length > 0 ) { for ( let i = 0 ; i < terms . length ; i ++ ) { let term = terms [ i ] ; if ( term . pathAsString == "" ) { root . info = term ; } else { addTerm ( root , term , term . pathAsString . split ( ";" ) ) ; } } return Taxonomy . findById ( root , terms [ 0 ] . id ) ; } return null ; } } ;