2016-09-19 08:24:34 +00:00
var ref = function ( a , b ) { for ( var e = null ; null == e || a . hasOwnProperty ( e ) ; ) e = "$" + ( 1048576 + Math . floor ( 15728639 * Math . random ( ) ) ) . toString ( 16 ) + "$" ; a [ e ] = b ; return e } , FormBuilder = function ( a ) { this . form _object = a } ; FormBuilder . prototype = { form _object : this . form _object , defs _object : { } , parent _element : null , built _form : null , root _element : null , ref _table : { } , ref _assoc : { } } ;
2016-09-19 06:37:35 +00:00
FormBuilder . regex = { reg _in _key : /^\/\^(.+)\$\/$/ , reg _out _val : /\{(\$[1-9])\}/ , pri _out _val : /\{([a-z-]+)\}/g , pri _in _key : /^\$([a-z-]+)$/ , arr _out _set : /^\{\{([a-z-]+)\}\}$/ , arr _out _val : /\{([a-z-]+)\.([a-z-]+)\}/g , arr _in _key : /^\$\$([a-z-]+)$/ , ref _pri : /^\$[a-f0-9]{6}\$$/ } ; FormBuilder . spread _attr = [ "children" , "next_nodes" , "prev_nodes" , "attributes" , "node_link" ] ; FormBuilder . allowed _attr = "node node_type next_nodes prev_nodes attributes children text repeat" . split ( " " ) ;
2016-09-19 08:24:34 +00:00
FormBuilder . prototype . add _definition = function ( a ) { for ( var b in a ) this . defs _object [ b ] = a [ b ] } ; FormBuilder . prototype . build = function ( a ) { a = null == a ? { } : a ; for ( var b in a ) a [ b ] = ref ( this . ref _table , a [ b ] ) , this . ref _assoc [ b ] = a [ b ] ; this . built _form = JSON . parse ( JSON . stringify ( this . form _object ) ) ; this . built _form = FormBuilder . formatFormObject ( this . built _form , this . defs _object ) ; this . built _form = FormBuilder . replaceStatements ( this . built _form , a , this . defs _object , this . ref _table ) } ;
FormBuilder . prototype . update = function ( a ) { a = null == a ? { } : a ; for ( var b in a ) this . ref _assoc . hasOwnProperty ( b ) && ( this . ref _table [ this . ref _assoc [ b ] ] = a [ b ] ) } ; FormBuilder . prototype . attach = function ( a ) { if ( ! ( a instanceof Element ) && null == this . parent _element ) return ! 1 ; this . parent _element = a instanceof Element ? a : this . parent _element } ; FormBuilder . formatFormObject = function ( a , b ) { if ( a . hasOwnProperty ( "children" ) ) for ( var e in a . children ) a . children [ e ] . parent = a , FormBuilder . formatFormObject ( a . children [ e ] , b ) ; return a } ;
FormBuilder . fetchNodeDefinition = function ( a , b ) { var e , d ; r = FormBuilder . regex . reg _in _key ; if ( "undefined" != typeof b && b . hasOwnProperty ( a ) ) return { def : b [ a ] } ; for ( e in b ) if ( r . test ( e ) && ( d = new RegExp ( e . slice ( 1 , - 1 ) ) , d . test ( a ) ) ) { matches = { } ; for ( d = 1 ; d < RegExp . length && 10 > d ; d ++ ) matches [ "$" + d ] = RegExp [ "$" + d ] ; return { def : b [ e ] , scope : matches } } return { } } ;
FormBuilder . replaceStatements = function ( a , b , e , d ) { a = a instanceof Object ? a : { } ; b = b instanceof Object ? JSON . parse ( JSON . stringify ( b ) ) : { } ; var c , f ; if ( a . hasOwnProperty ( "scope" ) && a . scope instanceof Object ) for ( c in a . scope ) b [ c ] = a . scope [ c ] ; if ( a . hasOwnProperty ( "node" ) && "string" == typeof a . node && ( f = FormBuilder . fetchNodeDefinition ( a . node , e ) , f . hasOwnProperty ( "def" ) && ( a . node _link = JSON . parse ( JSON . stringify ( f . def ) ) , f . hasOwnProperty ( "scope" ) ) ) ) for ( c in f . scope ) b [ c ] = f . scope [ c ] ; for ( c in a ) "string" == typeof a [ c ] && ( a [ c ] = [ a [ c ] ] ,
f = FormBuilder . replaceStatementsArray ( a [ c ] [ 0 ] , b ) , ! 1 !== f ? a [ c ] = f : ( a [ c ] = FormBuilder . replaceStatementsRegex ( a [ c ] , b ) , a [ c ] = FormBuilder . replaceStatementsPrimary ( a [ c ] , b ) , a [ c ] = FormBuilder . replaceStatementsArrayValue ( a [ c ] , b ) ) ) ; for ( c in a ) FormBuilder . regex . pri _in _key . test ( c ) ? b [ c . substring ( 1 ) ] = ref ( d , a [ c ] ) : FormBuilder . regex . arr _in _key . test ( c ) && ( b [ c . substring ( 2 ) ] = ref ( d , a [ c ] ) ) ; b = JSON . parse ( JSON . stringify ( b ) ) ; for ( c in a ) if ( - 1 < FormBuilder . spread _attr . indexOf ( c ) ) if ( a [ c ] instanceof Array ) for ( var h in a [ c ] ) FormBuilder . replaceStatements ( a [ c ] [ h ] ,
b , e , d ) ; else a [ c ] instanceof Object && FormBuilder . replaceStatements ( a [ c ] , b , e , d ) ; return a } ; FormBuilder . replaceStatementsArray = function ( a , b ) { var e = null , e = FormBuilder . regex . arr _out _set . exec ( a ) ; if ( null === e ) return ! 1 ; b . hasOwnProperty ( e [ 1 ] ) || ( b [ e [ 1 ] ] = [ ] ) ; return b [ e [ 1 ] ] } ;
FormBuilder . replaceStatementsRegex = function ( a , b ) { var e = FormBuilder . regex . reg _out _val , d = null , c = [ ] , f , h , g , k ; for ( g = 0 ; g < a . length ; g ++ ) { m = null ; c = [ ] ; f = - 1 ; for ( h = [ ] ; null !== ( d = e . exec ( a [ g ] ) ) && ! ( f >= e . lastIndex ) ; ) f = e . lastIndex , c . push ( d ) ; for ( d = f = 0 ; d < c . length ; d ++ ) k = c [ d ] [ 1 ] , ( 0 < f || 0 < c [ d ] . index ) && h . push ( a [ g ] . substr ( f , c [ d ] . index - f ) ) , b . hasOwnProperty ( k ) || ( b [ k ] = "" ) , h . push ( b [ k ] ) , f = c [ d ] . index + c [ d ] [ 0 ] . length ; f < a [ g ] . length && h . push ( a [ g ] . substr ( f , a [ g ] . length ) ) ; a = a . slice ( 0 , g ) . concat ( h ) . concat ( a . slice ( g + 1 ) ) } return a } ;
FormBuilder . replaceStatementsPrimary = function ( a , b ) { var e = FormBuilder . regex . pri _out _val , d = null , c = [ ] , f , h , g , k ; for ( g = 0 ; g < a . length ; g ++ ) { m = null ; c = [ ] ; f = 0 ; for ( h = [ ] ; null !== ( d = e . exec ( a [ g ] ) ) ; ) c . push ( d ) ; for ( d = 0 ; d < c . length ; d ++ ) k = c [ d ] [ 1 ] , ( 0 < f || 0 < c [ d ] . index ) && h . push ( a [ g ] . substr ( f , c [ d ] . index - f ) ) , b . hasOwnProperty ( k ) || ( b [ k ] = "" ) , h . push ( b [ k ] ) , f = c [ d ] . index + c [ d ] [ 0 ] . length ; f < a [ g ] . length && h . push ( a [ g ] . substr ( f , a [ g ] . length ) ) ; a = a . slice ( 0 , g ) . concat ( h ) . concat ( a . slice ( g + 1 ) ) } return a } ;
FormBuilder . replaceStatementsArrayValue = function ( a , b ) { var e = FormBuilder . regex . arr _out _val , d = null , c = [ ] , f , h , g , k ; for ( g = 0 ; g < a . length ; g ++ ) { m = null ; c = [ ] ; f = 0 ; for ( h = [ ] ; null !== ( d = e . exec ( a [ g ] ) ) ; ) c . push ( d ) ; for ( d = 0 ; d < c . length ; d ++ ) k = c [ d ] [ 1 ] , ( 0 < f || 0 < c [ d ] . index ) && h . push ( a [ g ] . substr ( f , c [ d ] . index - f ) ) , b . hasOwnProperty ( k ) || ( b [ k ] = "" ) , h . push ( b [ k ] ) , f = c [ d ] . index + c [ d ] [ 0 ] . length ; f < a [ g ] . length && h . push ( a [ g ] . substr ( f , a [ g ] . length ) ) ; a = a . slice ( 0 , g ) . concat ( h ) . concat ( a . slice ( g + 1 ) ) } return a } ;
FormBuilder . readRef = function ( a , b ) { var e , d = "" ; if ( "string" == typeof b && FormBuilder . regex . ref _pri . test ( b ) ) { for ( ; "string" == typeof b && FormBuilder . regex . ref _pri . test ( b ) ; ) b = a [ b ] ; return b } for ( e in b ) d = FormBuilder . regex . ref _pri . test ( b [ e ] ) && a . hasOwnProperty ( b [ e ] ) ? a [ b [ e ] ] instanceof Array ? d + ( "" + FormBuilder . readRef ( a , a [ b [ e ] ] ) ) : d + ( "" + a [ b [ e ] ] ) : d + ( "" + b [ e ] . toString ( ) ) ; return d } ;