import * as WebPartTypes from "../../@types/helper/webpart" ; declare var MSOWebPartPageFormName ; /**
 * WebPart Object
 */ interface IWebPartObject { /** The JSOM context object */ Context : any ; /** The webpart properties object */ Properties : any ; /** The webpart id */ WebPartId : string ; /** The webpart html element */ WebPart : HTMLElement ; /** The webpart definition object */ WebPartDefinition : any ; } /**
 * Web Part
 */ class _WebPart { private _props : WebPartTypes . IWebPartProps = null ; private _wp : WebPartTypes . IWebPartInfo = null ; param props constructor ( props ) { this . _props = props || { } as any ; window . addEventListener ( "load" , ( ) => { this . render ( ) ; } ) ; } wpId private addHelpLink = ( ) => { if ( this . _props . helpProps ) { let link = document . querySelector ( "div[webpartid='" + this . _wp . wpId + "'] a[title='Edit Snippet']" ) ; if ( link ) { let helpLink = document . createElement ( "a" ) ; helpLink . href = this . _props . helpProps . url || "#" ; helpLink . style . paddingLeft = "10px" ; helpLink . setAttribute ( "role" , "button" ) ; helpLink . title = this . _props . helpProps . title || "Help" ; helpLink . innerHTML = "<span class='ms-metadata'>" + helpLink . title + "</span>" ; helpLink . target = "_blank" ; link . parentElement . appendChild ( helpLink ) ; } } } ; static create ( props ) { return new _WebPart ( props ) ; } static generateContentEditorXML = ( props ) => { return `<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
    <Assembly>Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
    <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
    <Title>[[Title]]</Title>
    <Description>[[Description]]</Description>
    <FrameType>[[FrameType]]</FrameType>
    <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">[[ContentLink]]</ContentLink>
    <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor"><![CDATA[[[Content]]]]></Content>
</WebPart>` . replace ( /\r?\n/g , '' ) . replace ( /\[\[FrameType\]\]/g , props . frameType || "Default" ) . replace ( /\[\[Content\]\]/g , props . content || "" ) . replace ( /\[\[ContentLink\]\]/g , props . contentLink || "" ) . replace ( /\[\[Description\]\]/g , props . description || "" ) . replace ( /\[\[Title\]\]/g , props . title || "" ) ; } ; static generateScriptEditorXML = ( props ) => { return `<?xml version="1.0" encoding="utf-8"?>
<webParts>
    <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
        <metaData>
            <type name="Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
            <importErrorMessage>$Resources:core,ImportantErrorMessage;</importErrorMessage>
        </metaData>
        <data>
            <properties>
            <property name="Title" type="string">[[Title]]</property>
            <property name="Description" type="string">[[Description]]</property>
            <property name="ChromeType" type="chrometype">[[ChromeType]]</property>
            <property name="Content" type="string">[[Content]]</property>
        </properties>
        </data>
    </webPart>
</webParts>` . replace ( /\r?\n/g , '' ) . replace ( /\[\[ChromeType\]\]/g , props . chromeType || "TitleOnly" ) . replace ( /\[\[Content\]\]/g , props . content . replace ( /\</g , '&lt;' ) . replace ( /\>/g , '&gt;' ) ) . replace ( /\[\[Description\]\]/g , props . description || "" ) . replace ( /\[\[Title\]\]/g , props . title || "" ) ; } ; param el private getWebPartId = ( el ) => { while ( el ) { let wpId = el . getAttribute ( "webpartid" ) ; if ( wpId ) { return wpId ; } el = el . parentElement ; } return "" ; } ; /**
     * Method to get the webpart information
     */ private getWebPartInfo = ( ) : WebPartTypes . IWebPartInfo => { let targetInfo = { cfg : null , el : null , wpId : null } ; if ( this . _props . elementId ) { let elements = document . querySelectorAll ( "#" + this . _props . elementId ) ; for ( let i = 0 ; i < elements . length ; i ++ ) { let elWebPart = elements [ i ] as HTMLElement ; if ( elWebPart . getAttribute ( "data-isConfigured" ) ) { continue ; } let wpId = this . getWebPartId ( elWebPart ) ; if ( wpId ) { let elCfg = this . _props . cfgElementId ? elWebPart . parentElement . querySelector ( "#" + this . _props . cfgElementId ) : null as any ; if ( elCfg ) { try { let cfg = JSON . parse ( elCfg . innerText . trim ( ) ) ; if ( cfg . WebPartId ) { if ( cfg . WebPartId == wpId ) { targetInfo = { cfg , el : elWebPart , wpId : wpId } ; break ; } } else { targetInfo = { cfg : { WebPartId : wpId } , el : elWebPart , wpId : wpId } ; break ; } } catch ( ex ) { targetInfo = { cfg : { WebPartId : wpId } , el : elWebPart , wpId : wpId } ; console . log ( "[sp-webpart] Error parsing the configuration for element '" + this . _props . cfgElementId + "'." ) ; } break ; } else { targetInfo = { cfg : { WebPartId : wpId } , el : elWebPart , wpId : wpId } ; break ; } } } if ( elements . length == 0 ) { console . log ( "[sp-webpart] Error - Unable to find elements with id '" + this . _props . elementId + "'." ) ; } } else { console . log ( "[sp-webpart] The target element id is not defined." ) ; } if ( targetInfo . el ) { targetInfo . el . setAttribute ( "data-isConfigured" , "true" ) ; } return targetInfo ; } ; /**
     * Method to detect if a page is being edited
     */ static isEditMode = ( ) => { let formName = MSOWebPartPageFormName ? MSOWebPartPageFormName : "" ; let form = document . forms [ MSOWebPartPageFormName ] ; if ( form ) { let wikiPageMode = form . _wikiPageMode ? form . _wikiPageMode . value : null ; let wpPageMode = form . MSOLayout_InDesignMode ? form . MSOLayout_InDesignMode . value : null ; return wikiPageMode == "Edit" || wpPageMode == "1" ; } return false ; } ; /**
     * Method to render the webpart
     */ private render = ( ) => { let element = null ; this . _wp = this . getWebPartInfo ( ) ; if ( this . _wp == null || this . _wp . el == null ) { console . log ( "[sp-webpart] The target webpart element '" + this . _props . elementId + "' was not found." ) ; return ; } let returnVal = null ; if ( WebPart . isEditMode ( ) ) { this . addHelpLink ( ) ; if ( this . _props . onRenderEdit ) { returnVal = this . _props . onRenderEdit ( this . _wp ) ; } } else { if ( this . _wp . cfg || ( this . _props . cfgElementId || "" ) . length == 0 ) { returnVal = this . _props . onRenderDisplay ( this . _wp ) ; } else { this . _wp . el . innerHTML = '<h3>Please edit the page and configure the webpart.</h3>' ; } } if ( returnVal && returnVal . then ) { returnVal . then ( ( ... args ) => { this . _props . onPostRender ? this . _props . onPostRender ( this . _wp ) : null ; } ) ; } else { this . _props . onPostRender ? this . _props . onPostRender ( this . _wp ) : null ; } } ; } export const WebPart = _WebPart as any ;