import { IListFormField , IListFormFieldInfo , IListFormLookupFieldInfo , IListFormMMSFieldInfo , IListFormDateFieldInfo , IListFormTextFieldInfo , IListFormUserFieldInfo , IListFormChoiceFieldInfo , IListFormNumberFieldInfo , ITermInfo } from "../../@types/helper" ; import { SP } from "gd-sprest-def" ; import { Helper , Site , SPTypes , Web } from ".." ; /**
 * List Form Field
 */ export const ListFormField = { create : ( props ) : PromiseLike < IListFormFieldInfo > => { let _fieldInfo = props || { } as any ; let _reject = null ; let _resolve = null ; let load = ( ) => { if ( _fieldInfo . field ) { processField ( ) ; } else { Web ( _fieldInfo . webUrl ) . Lists ( _fieldInfo . listName ) . Fields ( ) . getByInternalNameOrTitle ( _fieldInfo . name ) . execute ( ( field ) => { _fieldInfo . field = field ; processField ( ) ; } , _reject ) ; } } ; let processField = ( ) => { _fieldInfo . defaultValue = _fieldInfo . field . DefaultValue ; _fieldInfo . readOnly = _fieldInfo . field . ReadOnlyField ; _fieldInfo . required = _fieldInfo . field . Required ? true : false ; _fieldInfo . title = _fieldInfo . field . Title ; _fieldInfo . type = _fieldInfo . field . FieldTypeKind as number ; _fieldInfo . typeAsString = _fieldInfo . field . TypeAsString ; switch ( _fieldInfo . type ) { case SPTypes . FieldType . Choice : case SPTypes . FieldType . MultiChoice : let choices = ( _fieldInfo . field as SP . FieldChoice ) . Choices ; ( _fieldInfo as IListFormChoiceFieldInfo ) . choices = ( choices ? choices [ "results" ] : null ) || [ ] ; ( _fieldInfo as IListFormChoiceFieldInfo ) . multi = _fieldInfo . type == SPTypes . FieldType . MultiChoice ; break ; case SPTypes . FieldType . DateTime : let fldDate = _fieldInfo . field as SP . FieldDateTime ; ( _fieldInfo as IListFormDateFieldInfo ) . showTime = fldDate . DisplayFormat == SPTypes . DateFormat . DateTime ; break ; case SPTypes . FieldType . Lookup : let fldLookup = _fieldInfo . field as SP . FieldLookup ; ( _fieldInfo as IListFormLookupFieldInfo ) . lookupField = fldLookup . LookupField ; ( _fieldInfo as IListFormLookupFieldInfo ) . lookupListId = fldLookup . LookupList ; ( _fieldInfo as IListFormLookupFieldInfo ) . lookupWebId = fldLookup . LookupWebId ; ( _fieldInfo as IListFormLookupFieldInfo ) . multi = fldLookup . AllowMultipleValues ; break ; case SPTypes . FieldType . Number : let fldNumber = _fieldInfo . field as SP . FieldNumber ; let startIdx = fldNumber . SchemaXml . indexOf ( 'Decimals="' ) + 10 ; ( _fieldInfo as IListFormNumberFieldInfo ) . decimals = startIdx > 10 ? parseInt ( fldNumber . SchemaXml . substr ( startIdx , fldNumber . SchemaXml . substr ( startIdx ) . indexOf ( '"' ) ) ) : 0 ; ( _fieldInfo as IListFormNumberFieldInfo ) . maxValue = fldNumber . MaximumValue ; ( _fieldInfo as IListFormNumberFieldInfo ) . minValue = fldNumber . MinimumValue ; ( _fieldInfo as IListFormNumberFieldInfo ) . showAsPercentage = fldNumber . SchemaXml . indexOf ( 'Percentage="TRUE"' ) > 0 ; break ; case SPTypes . FieldType . Note : let fldNote = _fieldInfo . field as SP . FieldMultiLineText ; ( _fieldInfo as IListFormTextFieldInfo ) . multiline = true ; ( _fieldInfo as IListFormTextFieldInfo ) . richText = fldNote . RichText ; ( _fieldInfo as IListFormTextFieldInfo ) . rows = fldNote . NumberOfLines ; break ; case SPTypes . FieldType . Text : ( _fieldInfo as IListFormTextFieldInfo ) . multiline = false ; ( _fieldInfo as IListFormTextFieldInfo ) . richText = false ; ( _fieldInfo as IListFormTextFieldInfo ) . rows = 1 ; break ; case SPTypes . FieldType . User : let fldUser = _fieldInfo . field as SP . FieldUser ; ( _fieldInfo as IListFormUserFieldInfo ) . allowGroups = fldUser . SelectionMode == SPTypes . FieldUserSelectionType . PeopleAndGroups ; ( _fieldInfo as IListFormUserFieldInfo ) . multi = fldUser . AllowMultipleValues ; break ; default : if ( _fieldInfo . typeAsString . indexOf ( "TaxonomyFieldType" ) == 0 ) { let fldMMS = _fieldInfo . field as SP . Taxonomy . TaxonomyField ; ( _fieldInfo as IListFormMMSFieldInfo ) . multi = fldMMS . AllowMultipleValues ; ( _fieldInfo as IListFormMMSFieldInfo ) . termId = fldMMS . IsAnchorValid ? fldMMS . AnchorId : fldMMS . TermSetId ; ( _fieldInfo as IListFormMMSFieldInfo ) . termSetId = fldMMS . TermSetId ; ( _fieldInfo as IListFormMMSFieldInfo ) . termStoreId = fldMMS . SspId ; } break ; } _resolve ( _fieldInfo ) ; } ; return new Promise ( ( resolve , reject ) => { _resolve = resolve ; _reject = reject ; if ( _fieldInfo . field ) { processField ( ) ; } else { load ( ) ; } } ) ; } , loadLookupData : ( info , queryTop ? ) : PromiseLike < Array < SP . ListItemOData > > => { return new Promise ( ( resolve , reject ) => { Site ( ) . openWebById ( info . lookupWebId ) . execute ( ( web ) => { let query = info . lookupFilter || { } ; if ( typeof ( query ) == "string" ) { query = { Filter : query } ; } if ( query . GetAllItems == null ) { query . GetAllItems = true ; } if ( query . OrderBy == null ) { query . OrderBy = [ "Title" ] ; } if ( query . Select == null ) { query . Select = [ "ID" , info . lookupField ] ; } if ( query . Top == null ) { query . Top = queryTop > 0 && queryTop <= 5000 ? queryTop : 500 ; } web . Lists ( ) . getById ( info . lookupListId ) . Items ( ) . query ( query ) . execute ( ( items ) => { resolve ( items . results ) ; } , reject ) ; } , reject ) ; } ) ; } , loadMMSData : ( info ) : PromiseLike < Array < ITermInfo > > => { return new Promise ( ( resolve , reject ) => { Helper . Taxonomy . getTermSetById ( info . termStoreId , info . termSetId ) . then ( termSet => { let root = Helper . Taxonomy . findById ( termSet , info . termId ) ; if ( root == null ) { root = Helper . Taxonomy . findById ( termSet , info . termSetId ) ; } resolve ( Helper . Taxonomy . toArray ( root ) ) ; } , reject ) ; } ) ; } , loadMMSValueField : ( info ) : PromiseLike < SP . FieldMultiLineText > => { return new Promise ( ( resolve , reject ) => { Web ( info . webUrl ) . Lists ( info . listName ) . Fields ( ) . getByInternalNameOrTitle ( info . name + "_0" ) . execute ( field => { resolve ( field as any ) ; } , ( ) => { reject ( "Unable to find the hidden value field for '" + info . name + "'." ) ; } ) ; } ) ; } } ;