import { IWebPartContext } from '@microsoft/sp-webpart-base' ; import { IPageContext } from '../../utils/IPageContext' ; import * as moment from 'moment' ; declare const _spPageContextInfo ; export default class SearchTokenHelper { private regexVal : RegExp = /\{[^\{]*?\}/gi ; constructor ( ) { } public replaceTokens ( restUrl , context ) { const tokens = restUrl . match ( this . regexVal ) ; if ( tokens !== null && tokens . length > 0 ) { tokens . forEach ( ( token ) => { if ( token . toLowerCase ( ) . indexOf ( 'today' ) !== - 1 ) { const dateValue = this . getDateValue ( token ) ; restUrl = restUrl . replace ( token , dateValue ) ; } else if ( token . toLowerCase ( ) . indexOf ( 'user' ) !== - 1 ) { const userValue = this . getUserValue ( token , context ) ; restUrl = restUrl . replace ( token , userValue ) ; } else { switch ( token . toLowerCase ( ) ) { case "{site}" : restUrl = restUrl . replace ( /{site}/ig , context . pageContext . web . absoluteUrl ) ; break ; case "{sitecollection}" : restUrl = restUrl . replace ( /{sitecollection}/ig , _spPageContextInfo . siteAbsoluteUrl ) ; break ; case "{currentdisplaylanguage}" : restUrl = restUrl . replace ( /{currentdisplaylanguage}/ig , context . pageContext . cultureInfo . currentCultureName ) ; break ; } } } ) ; } return restUrl ; } private getDateValue ( token ) { let dateValue = moment ( ) ; if ( token . toLowerCase ( ) . indexOf ( "{today+" ) !== - 1 ) { const daysVal = this . getDaysVal ( token ) ; dateValue = dateValue . add ( daysVal , 'day' ) ; } if ( token . toLowerCase ( ) . indexOf ( "{today-" ) !== - 1 ) { const daysVal = this . getDaysVal ( token ) ; dateValue = dateValue . subtract ( daysVal , 'day' ) ; } return dateValue . format ( 'YYYY-MM-DD' ) ; } private getDaysVal ( token ) { const tmpDays = token . substring ( 7 , token . length - 1 ) ; return parseInt ( tmpDays ) || 0 ; } private getUserValue ( token , context ) { let userValue = '"' + context . pageContext . user . displayName + '"' ; if ( token . toLowerCase ( ) . indexOf ( "{user." ) !== - 1 ) { const propVal = token . toLowerCase ( ) . substring ( 6 , token . length - 1 ) ; switch ( propVal ) { case "name" : userValue = '"' + context . pageContext . user . displayName + '"' ; break ; case "email" : userValue = context . pageContext . user . email ; break ; } } return userValue ; } }