import { Selector } from "../extend/Selector" ; import { ComponentConfiguration } from "../model/ComponentConfiguration" ; import { IUkulele } from "../core/IUkulele" ; import { ControllerModel } from "../model/ControllerModel" ; class ValueAndParent { value : any ; parent : Object ; constructor ( _value , _parant ) { this . value = _value ; this . parent = _parant ; } } export class UkuleleUtil { static searchHtmlTag ( htmlString , tagName ) { let reTemp = "^<" + tagName + "[\\s\\S]*>" + "[\\s\\S]*</" + tagName + ">$" ; let re = new RegExp ( reTemp ) ; let index = htmlString . search ( re ) ; return index ; } static getInnerHtml ( htmlString , tagName ) { let reTemp = "<" + tagName + "[\\s\\S]*>" + "[\\s\\S]*</" + tagName + ">" ; let re = new RegExp ( reTemp ) ; let match = htmlString . match ( re ) ; if ( match . index > - 1 ) { let matchString = match [ 0 ] ; let index = matchString . search ( ">" ) ; let tempString = matchString . substr ( index + 1 ) ; let index2 = tempString . lastIndexOf ( "</" ) ; tempString = tempString . substring ( 0 , index2 ) ; tempString = tempString . replace ( /(^\s*)|(\s*$)/g , "" ) ; return tempString ; } else { return null ; } } static getComponentConfiguration ( htmlString ) { let tempDom = document . createElement ( "div" ) ; tempDom . innerHTML = htmlString ; let tpl = Selector . querySelectorAll ( tempDom , "template" ) ; let scripts = Selector . querySelectorAll ( tempDom , "script" ) ; let stylesheet = Selector . querySelectorAll ( tempDom , "style" ) ; let deps = [ ] ; let ccs = null ; for ( let i = 0 ; i < scripts . length ; i ++ ) { let script = scripts [ i ] as HTMLScriptElement ; if ( script . src !== "" ) { deps . push ( script . src ) ; } else { ccs = script . innerHTML ; } } if ( stylesheet && stylesheet [ 0 ] ) { return new ComponentConfiguration ( ( tpl [ 0 ] as HTMLElement ) . innerHTML , deps , ccs , ( stylesheet [ 0 ] as HTMLElement ) . innerHTML ) ; } else { return new ComponentConfiguration ( ( tpl [ 0 ] as HTMLElement ) . innerHTML , deps , ccs ) ; } } static searchUkuAttrTag ( htmlString ) { let re = /^uku\-.*/ ; let index = htmlString . search ( re ) ; return index ; } static isUkuAttrTag ( tagName ) { if ( this . searchUkuAttrTag ( tagName ) > - 1 ) { return true ; } return false ; } static getAttrFromUkuTag ( ukuTag , camelCase = false ) { if ( UkuleleUtil . searchUkuAttrTag ( ukuTag ) === 0 ) { ukuTag = ukuTag . replace ( 'uku-' , '' ) ; } if ( camelCase ) { let names = ukuTag . split ( '-' ) ; ukuTag = names [ 0 ] ; for ( let i = 1 ; i < names . length ; i ++ ) { let firstLetter = names [ i ] . charAt ( 0 ) . toUpperCase ( ) ; ukuTag = ukuTag + firstLetter + names [ i ] . substr ( 1 ) ; } } return ukuTag ; } static searchUkuExpTag ( expression ) { let re = /^\{\{.*\}\}$/ ; let index = expression . search ( re ) ; return index ; } static searchUkuFuncArg ( htmlString ) { let re1 = /[\+\-\*\/\%\?\:\>\<]/ ; let index = htmlString . search ( re1 ) ; if ( index === - 1 ) { let re2 = /\(.*\)$/ ; index = htmlString . search ( re2 ) ; return index ; } else { return - 1 ; } } static isRepeat ( element ) { if ( element . getAttribute ( "uku-repeat" ) ) { return true ; } return false ; } static isInRepeat ( element ) { let parents = Selector . parents ( element ) ; for ( let i = 0 ; i < parents . length ; i ++ ) { let parent = parents [ i ] as HTMLElement ; if ( parent . nodeType !== 9 ) { let b = parent . getAttribute ( "uku-repeat" ) ; if ( b ) { return true ; } } } return false ; } static getBoundModelInstantName ( expression ) { let controlInstName = expression . split ( '.' ) [ 0 ] ; if ( controlInstName ) { return controlInstName ; } return undefined ; } static getBoundModelInstantNames ( controller_alias_list , expression ) { let arr = [ ] ; controller_alias_list . forEach ( alias => { let pattern = new RegExp ( "\\b" + alias , "gm" ) ; console . count ( 'getBoundModelInstantNames执行的次数' ) ; if ( expression . search ( pattern ) > - 1 ) { arr . push ( alias ) ; } } ) ; return arr ; } static getFinalValue ( objects , attrName ) { return ( function ( ) { var tempScope = { } ; objects . forEach ( object => { tempScope [ object [ '_alias' ] ] = object ; let alias = object [ '_alias' ] ; let pattern = new RegExp ( "\\b" + alias + "\\b" , "gm" ) ; attrName = attrName . replace ( pattern , "tempScope." + alias ) ; let pattern2 = new RegExp ( "\\.tempScope\\." + alias , "gm" ) ; attrName = attrName . replace ( pattern2 , "." + alias ) ; } ) ; var result ; try { result = eval ( attrName ) ; } catch ( err ) { result = '' ; } tempScope = null ; return result ; } ) ( ) ; } static setFinalValue ( object , attrName , value ) { return ( function ( ) { var tempScope = { } ; tempScope [ object [ '_alias' ] ] = object ; let valueString ; if ( typeof value === "string" ) { valueString = '"' + value + '"' ; } else if ( typeof value === "object" ) { valueString = "JSON.parse('" + JSON . stringify ( value ) + "')" ; } else { valueString = value ; } let evalString = "tempScope." + attrName + "=" + valueString ; eval ( evalString ) ; } ) ( ) ; } static wrapScriptInComponent ( originalScript ) { let trimScript = originalScript . replace ( new RegExp ( '\\s' , 'gm' ) , '' ) ; if ( trimScript . search ( new RegExp ( '\\(function\\(' ) ) === 0 ) { return originalScript ; } else { let selfExcutingFrame = `	(function(){
                        return function(uku){                
                            Object.defineProperty(this, 'currentState', {
                                set: function(value){
                                    if(value){
                                        this._currentState = value;
                                        uku.refresh(this._alias);
                                    }
                                }
                            });

                            this.setState = function(state){
                                this._currentState = state;
                                uku.refresh(this._alias);
                            };

                            ${ originalScript }
                        };
                    })();
                ` ; return selfExcutingFrame ; } } }