/**
 * DOM操作的工具方法模块
 */ namespace drunk . dom { import config = drunk . config ; import Promise = drunk . Promise ; param html return export function create ( htmlString ) { var div = document . createElement ( "div" ) ; var str = htmlString . trim ( ) ; console . assert ( str . length > 0 , `HTML不能为空` ) ; html ( div , str ) ; return div . childNodes . length === 1 ? div . firstChild : util . toArray ( div . childNodes ) ; } param container param value export function html ( container , value ) { if ( typeof MSApp !== 'undefined' && MSApp [ 'execUnsafeLocalFunction' ] ) { MSApp [ 'execUnsafeLocalFunction' ] ( ( ) => { container . innerHTML = value ; } ) ; } else { container . innerHTML = value ; } } /**
     * 创建标记节点，如果开启debug模式，则创建comment节点，发布版本则用textNode
     */ export function createFlagNode ( content ) { let node = config . debug ? document . createComment ( content ) : document . createTextNode ( ' ' ) ; node [ 'flag' ] = content ; return node ; } param newNode param oldNode export function before ( newNode , oldNode ) { let parent = oldNode . parentNode ; if ( ! parent ) { return ; } if ( Array . isArray ( newNode ) ) { newNode . forEach ( ( node ) => parent . insertBefore ( node , oldNode ) ) ; } else { parent . insertBefore ( newNode , oldNode ) ; } } param newNode param oldNode export function after ( newNode , oldNode ) { if ( ! oldNode . parentNode ) { return ; } if ( ! Array . isArray ( newNode ) ) { newNode = [ newNode ] ; } if ( oldNode . nextSibling ) { before ( newNode , oldNode . nextSibling ) ; } else { let parent = oldNode . parentNode ; newNode . forEach ( ( node ) => { parent . appendChild ( node ) ; } ) ; } } param target export function remove ( target ) { if ( Array . isArray ( target ) ) { return Promise . all ( target . map ( node => removeAfterActionEnd ( node ) ) ) ; } return Promise . resolve ( removeAfterActionEnd ( target ) ) ; } function removeAfterActionEnd ( node ) { if ( node . parentNode ) { let currentAction = Action . getCurrentAction ( node ) ; if ( currentAction && currentAction . promise ) { return currentAction . promise . then ( ( ) => { node . parentNode . removeChild ( node ) ; } ) ; } else { node . parentNode . removeChild ( node ) ; } } } param newNode param oldNode export function replace ( newNode , oldNode ) { if ( ! oldNode . parentNode ) { return ; } if ( ! Array . isArray ( newNode ) ) { newNode = [ newNode ] ; } let parent = oldNode . parentNode ; newNode . forEach ( ( node ) => { parent . insertBefore ( node , oldNode ) ; } ) ; parent . removeChild ( oldNode ) ; } param element param type param listener export function on ( element , type , listener ) { element . addEventListener ( type , listener , false ) ; } param element param type param listener export function off ( element , type , listener ) { element . removeEventListener ( type , listener , false ) ; } param element param token export function addClass ( element , token ) { var list = token . trim ( ) . split ( /\s+/ ) ; element . classList . add . apply ( element . classList , list ) ; } param element param token export function removeClass ( element , token ) { var list = token . trim ( ) . split ( /\s+/ ) ; element . classList . remove . apply ( element . classList , list ) ; } var styleSheet ; export function addCSSRule ( rules ) { if ( ! styleSheet ) { var styleElement = document . createElement ( 'style' ) ; document . head . appendChild ( styleElement ) ; styleSheet = < CSSStyleSheet > styleElement . sheet ; } Object . keys ( rules ) . forEach ( selector => { var rule = rules [ selector ] ; var content = Object . keys ( rule ) . map ( property => `${ property }:${ rule [ property ] }` ) . join ( ';' ) ; styleSheet . insertRule ( `${ selector } {${ content }}` , styleSheet . cssRules . length ) ; } ) ; } }