import { IJSLink , IJSLinkCfg } from "../../@types/helper" ; import { ContextInfo } from "../lib" ; import { SPTypes } from "../sptypes" ; /**
 * JSLink Helper Methods
 */ export const JSLink = { _hideEventFl : false , /**
     * Field to Method Mapper
     * 1 - Display Form
     * 2 - Edit Form
     * 3 - New Form
     * 4 - View
     */ _fieldToMethodMapper : { 'Attachments' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPFieldAttachments_Default" ] , 2 : ContextInfo . window [ "SPFieldAttachments_Default" ] , 3 : ContextInfo . window [ "SPFieldAttachments_Default" ] } , 'Boolean' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPField_FormDisplay_DefaultNoEncode" ] , 2 : ContextInfo . window [ "SPFieldBoolean_Edit" ] , 3 : ContextInfo . window [ "SPFieldBoolean_Edit" ] } , 'Currency' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPField_FormDisplay_Default" ] , 2 : ContextInfo . window [ "SPFieldNumber_Edit" ] , 3 : ContextInfo . window [ "SPFieldNumber_Edit" ] } , 'Calculated' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPField_FormDisplay_Default" ] , 2 : ContextInfo . window [ "SPField_FormDisplay_Empty" ] , 3 : ContextInfo . window [ "SPField_FormDisplay_Empty" ] } , 'Choice' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPField_FormDisplay_Default" ] , 2 : ContextInfo . window [ "SPFieldChoice_Edit" ] , 3 : ContextInfo . window [ "SPFieldChoice_Edit" ] } , 'Computed' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPField_FormDisplay_Default" ] , 2 : ContextInfo . window [ "SPField_FormDisplay_Default" ] , 3 : ContextInfo . window [ "SPField_FormDisplay_Default" ] } , 'DateTime' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPFieldDateTime_Display" ] , 2 : ContextInfo . window [ "SPFieldDateTime_Edit" ] , 3 : ContextInfo . window [ "SPFieldDateTime_Edit" ] } , 'File' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPFieldFile_Display" ] , 2 : ContextInfo . window [ "SPFieldFile_Edit" ] , 3 : ContextInfo . window [ "SPFieldFile_Edit" ] } , 'Integer' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPField_FormDisplay_Default" ] , 2 : ContextInfo . window [ "SPFieldNumber_Edit" ] , 3 : ContextInfo . window [ "SPFieldNumber_Edit" ] } , 'Lookup' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPFieldLookup_Display" ] , 2 : ContextInfo . window [ "SPFieldLookup_Edit" ] , 3 : ContextInfo . window [ "SPFieldLookup_Edit" ] } , 'LookupMulti' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPFieldLookup_Display" ] , 2 : ContextInfo . window [ "SPFieldLookup_Edit" ] , 3 : ContextInfo . window [ "SPFieldLookup_Edit" ] } , 'MultiChoice' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPField_FormDisplay_Default" ] , 2 : ContextInfo . window [ "SPFieldMultiChoice_Edit" ] , 3 : ContextInfo . window [ "SPFieldMultiChoice_Edit" ] } , 'Note' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPFieldNote_Display" ] , 2 : ContextInfo . window [ "SPFieldNote_Edit" ] , 3 : ContextInfo . window [ "SPFieldNote_Edit" ] } , 'Number' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPField_FormDisplay_Default" ] , 2 : ContextInfo . window [ "SPFieldNumber_Edit" ] , 3 : ContextInfo . window [ "SPFieldNumber_Edit" ] } , 'Text' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPField_FormDisplay_Default" ] , 2 : ContextInfo . window [ "SPFieldText_Edit" ] , 3 : ContextInfo . window [ "SPFieldText_Edit" ] } , 'URL' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPFieldUrl_Display" ] , 2 : ContextInfo . window [ "SPFieldUrl_Edit" ] , 3 : ContextInfo . window [ "SPFieldUrl_Edit" ] } , 'User' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPFieldUser_Display" ] , 2 : ContextInfo . window [ "SPClientPeoplePickerCSRTemplate" ] , 3 : ContextInfo . window [ "SPClientPeoplePickerCSRTemplate" ] } , 'UserMulti' : { 4 : ContextInfo . window [ "RenderFieldValueDefault" ] , 1 : ContextInfo . window [ "SPFieldUserMulti_Display" ] , 2 : ContextInfo . window [ "SPClientPeoplePickerCSRTemplate" ] , 3 : ContextInfo . window [ "SPClientPeoplePickerCSRTemplate" ] } } , /**
     * Methods
     */ param ctx param field param requireValueFl disableEdit : ( ctx , field , requireValueFl ? ) : string => { let fieldValue = ctx . CurrentFieldValue ; if ( fieldValue ) { switch ( ctx . CurrentFieldSchema . Type ) { case "MultiChoice" : let regExp = new RegExp ( SPTypes . ClientTemplatesUtility . UserLookupDelimitString , "g" ) ; fieldValue = ctx . CurrentFieldValue . replace ( regExp , "; " ) . replace ( /^; /g , "" ) . replace ( /; $/g , "" ) ; break ; case "Note" : fieldValue = "<div>" + ctx . CurrentFieldValue . replace ( /\n/g , "<br />" ) + "</div>" ; break ; case "User" : case "UserMulti" : for ( let i = 0 ; i < ctx . CurrentFieldValue . length ; i ++ ) { let userValue = ctx . CurrentFieldValue [ i ] ; fieldValue += userValue . EntityData . SPUserID + SPTypes . ClientTemplatesUtility . UserLookupDelimitString + userValue . DisplayText + ( ( i == ctx . CurrentFieldValue . length - 1 ? "" : SPTypes . ClientTemplatesUtility . UserLookupDelimitString ) ) ; } break ; } ; ctx . CurrentFieldValue = fieldValue ; } let controlMode = SPTypes . ControlMode . Display ; if ( requireValueFl && ( fieldValue == null || fieldValue == "" ) ) { controlMode = ctx . ControlMode ; } return JSLink . renderField ( ctx , field , controlMode ) ; } , param ctx param field disableQuickEdit : ( ctx , field ) => { if ( ctx . inGridMode ) { field . AllowGridEditing = false ; return "" ; } return JSLink . renderField ( ctx , field ) ; } , param ctx getListView : ( ctx ) => { let wp = JSLink . getWebPart ( ctx ) ; if ( wp ) { wp = wp . querySelector ( ".ms-formtable" ) ; } return wp ; } , param ctx getListViewItems : ( ctx ) => { return ctx . ListData ? ctx . ListData . Row : [ ] ; } , /**
     * Returns the selected list view items
     */ getListViewSelectedItems : ( ) => { return ContextInfo . window [ "SP" ] . ListOperation . Selection . getSelectedItems ( ) ; } , param ctx getWebPart : ( ctx ) => { return ContextInfo . document . querySelector ( "#WebPart" + ( ctx . FormUniqueId || ctx . wpq ) ) ; } , param ctx param field hideField : ( ctx , field ) => { if ( ! JSLink . _hideEventFl ) { JSLink . _hideEventFl = true ; ContextInfo . window . addEventListener ( "load" , ( ) => { let fieldElements = ContextInfo . document . querySelectorAll ( ".hide-field" ) ; for ( let fieldElement of fieldElements ) { let parentRow = fieldElement . parentNode && fieldElement . parentNode . parentNode ? fieldElement . parentNode . parentNode : null ; if ( parentRow ) { if ( fieldElement . parentNode . getAttribute ( "data-field-name" ) != parentRow . getAttribute ( "data-field-name" ) ) { while ( parentRow && parentRow . nodeName . toLowerCase ( ) != "tr" ) { parentRow = parentRow . parentNode ; } } if ( parentRow ) { parentRow . style . display = "none" ; } } } } ) ; } } , param cfg register : ( cfg ) => { if ( cfg ) { let templateManager = ContextInfo . window . SPClientTemplates ; templateManager = templateManager ? templateManager . TemplateManager : null ; if ( templateManager ) { templateManager . RegisterTemplateOverrides ( cfg ) ; } } } , param ctx param field removeField : ( ctx , field ) => { JSLink . hideField ( ctx , field ) ; return "<div class='hide-field'></div>" ; } , param ctx param field param formType renderField : ( ctx , field , formType ? ) => { var fieldType = field ? field . Type : ( ctx . CurrentFieldSchema ? ctx . CurrentFieldSchema . Type : null ) ; formType = formType ? formType : ctx . ControlMode ; if ( JSLink . _fieldToMethodMapper [ fieldType ] && JSLink . _fieldToMethodMapper [ fieldType ] [ formType ] ) { var defaultHtml = JSLink . _fieldToMethodMapper [ fieldType ] [ formType ] ( ctx ) ; if ( defaultHtml ) { return defaultHtml ; } } var field = ctx . CurrentFieldSchema ; var fieldRenderer = null ; switch ( field . Type ) { case "AllDayEvent" : fieldRenderer = new ContextInfo . window [ "AllDayEventFieldRenderer" ] ( field . Name ) ; break ; case "Attachments" : fieldRenderer = new ContextInfo . window [ "AttachmentFieldRenderer" ] ( field . Name ) ; break ; case "BusinessData" : fieldRenderer = new ContextInfo . window [ "BusinessDataFieldRenderer" ] ( field . Name ) ; break ; case "Computed" : fieldRenderer = new ContextInfo . window [ "ComputedFieldRenderer" ] ( field . Name ) ; break ; case "CrossProjectLink" : fieldRenderer = new ContextInfo . window [ "ProjectLinkFieldRenderer" ] ( field . Name ) ; break ; case "Currency" : fieldRenderer = new ContextInfo . window [ "NumberFieldRenderer" ] ( field . Name ) ; break ; case "DateTime" : fieldRenderer = new ContextInfo . window [ "DateTimeFieldRenderer" ] ( field . Name ) ; break ; case "Lookup" : fieldRenderer = new ContextInfo . window [ "LookupFieldRenderer" ] ( field . Name ) ; break ; case "LookupMulti" : fieldRenderer = new ContextInfo . window [ "LookupFieldRenderer" ] ( field . Name ) ; break ; case "Note" : fieldRenderer = new ContextInfo . window [ "NoteFieldRenderer" ] ( field . Name ) ; break ; case "Number" : fieldRenderer = new ContextInfo . window [ "NumberFieldRenderer" ] ( field . Name ) ; break ; case "Recurrence" : fieldRenderer = new ContextInfo . window [ "RecurrenceFieldRenderer" ] ( field . Name ) ; break ; case "Text" : fieldRenderer = new ContextInfo . window [ "TextFieldRenderer" ] ( field . Name ) ; break ; case "URL" : fieldRenderer = new ContextInfo . window [ "UrlFieldRenderer" ] ( field . Name ) ; break ; case "User" : fieldRenderer = new ContextInfo . window [ "UserFieldRenderer" ] ( field . Name ) ; break ; case "UserMulti" : fieldRenderer = new ContextInfo . window [ "UserFieldRenderer" ] ( field . Name ) ; break ; case "WorkflowStatus" : fieldRenderer = new ContextInfo . window [ "RawFieldRenderer" ] ( field . Name ) ; break ; } ; var currentItem = ctx . CurrentItem || ctx . ListData . Items [ 0 ] ; return fieldRenderer ? fieldRenderer . RenderField ( ctx , field , currentItem , ctx . ListSchema ) : currentItem [ field . Name ] ; } } ;