namespace BrickyEditor { export namespace Fields { export abstract class BaseField { private static _fields : any = { } ; public static get type ( ) : string { var name = ( < any > this ) . name ; name = name . replace ( "Field" , "" ) ; name = name . substring ( 0 , 1 ) . toLowerCase ( ) + name . substring ( 1 ) ; return name ; } public $field : JQuery ; public name : string ; public data : any ; private onSelect : ( field : BaseField ) => void ; private onUpdate : ( property , oldValue , newValue ) => void ; protected onUpload : ( file : any , callback : ( url : string ) => void ) => void ; protected settings : ( field : BaseField ) => void ; protected getSettingsEl ( ) { return null ; } constructor ( $field , data , onSelect , onUpdate , onUpload ? ) { this . $field = $field ; this . data = data ; this . onSelect = onSelect ; this . onUpdate = onUpdate ; this . onUpload = onUpload ; this . bind ( ) ; } /**
             * Register Field Type
             */ public static registerCommonFields ( ) { HtmlField . registerField ( ) ; ImageField . registerField ( ) ; EmbedField . registerField ( ) ; ContainerField . registerField ( ) ; } private static registerField ( ) { if ( this . _fields . hasOwnProperty ( this . type ) ) { delete this . _fields [ this . type ] ; } this . _fields [ this . type ] = this ; } public static createField ( $field , data , onSelect , onUpdate , onUpload ? ) { let fieldData = $field . data ( ) . breField ; if ( ! fieldData ) { throw `There is no any data in field ${ $field . html ( ) }` ; } if ( typeof fieldData === "string" ) { fieldData = JSON . parse ( fieldData . replace ( /'/g , '"' ) ) ; } if ( ! fieldData . name ) { throw `There is no name in data of field ${ $field . html ( ) }` ; } if ( data ) { let addFieldData = { } ; for ( var idx = 0 ; idx < data . length ; idx ++ ) { let field = data [ idx ] ; if ( field . name . toLowerCase ( ) === fieldData . name . toLowerCase ( ) ) { addFieldData = field ; break ; } } if ( addFieldData ) { fieldData = $ . extend ( fieldData , addFieldData ) ; } } let type = fieldData . type ; if ( type != null ) { if ( this . _fields . hasOwnProperty ( type ) ) { const field = this . _fields [ type ] ; return new field ( $field , fieldData , onSelect , onUpdate , onUpload ) ; } else { throw `${ type } field not found` ; } } else { throw `Field type not defined in data-bre-field attribute` ; } } protected bind ( ) { } protected select ( ) { this . $field . addClass ( Selectors . selectorFieldSelected ) ; this . onSelect ( this ) ; } public deselect ( ) { this . $field . removeClass ( Selectors . selectorFieldSelected ) ; } public getEl ( ) { let $el = this . $field . clone ( false ) ; $el . removeAttr ( Selectors . attrField ) ; return $el ; } protected updateProperty ( prop , value , fireUpdate = true ) { const oldValue = this . data [ prop ] ; if ( oldValue === value ) return ; this . data [ prop ] = value ; if ( fireUpdate ) { this . onUpdate ( prop , oldValue , value ) ; } } } } }