| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
ve.ui.PreviewElement = function VeUiPreviewElement( model, config ) { |
| 22 |
config = config || {}; |
| 23 |
|
| 24 |
|
| 25 |
ve.ui.PreviewElement.super.call( this, config ); |
| 26 |
|
| 27 |
|
| 28 |
OO.EventEmitter.call( this ); |
| 29 |
|
| 30 |
this.useView = !!config.useView; |
| 31 |
|
| 32 |
if ( model ) { |
| 33 |
this.setModel( model ); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
this.$element.addClass( 've-ui-previewElement' ); |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
OO.inheritClass( ve.ui.PreviewElement, OO.ui.Element ); |
| 49 |
|
| 50 |
OO.mixinClass( ve.ui.PreviewElement, OO.EventEmitter ); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
ve.ui.PreviewElement.prototype.destroy = function () { |
| 56 |
if ( this.view ) { |
| 57 |
this.view.destroy(); |
| 58 |
this.view = null; |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
ve.ui.PreviewElement.prototype.setModel = function ( model ) { |
| 68 |
this.model = model; |
| 69 |
this.updatePreview(); |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
ve.ui.PreviewElement.prototype.beforeAppend = function ( element ) { |
| 78 |
|
| 79 |
|
| 80 |
Array.prototype.forEach.call( element.querySelectorAll( '.ve-ce-nail, .ve-ce-branchNode-slug' ), ( el ) => { |
| 81 |
el.parentNode.removeChild( el ); |
| 82 |
} ); |
| 83 |
ve.targetLinksToNewWindow( element ); |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
ve.ui.PreviewElement.prototype.replaceWithModelDom = function () { |
| 92 |
const htmlDocument = ve.dm.converter.getDomFromNode( this.model, ve.dm.Converter.static.PREVIEW_MODE ), |
| 93 |
body = htmlDocument.body, |
| 94 |
element = this.$element[ 0 ]; |
| 95 |
|
| 96 |
|
| 97 |
ve.resolveAttributes( |
| 98 |
body, |
| 99 |
this.model.getDocument().getHtmlDocument(), |
| 100 |
ve.dm.Converter.static.computedAttributes |
| 101 |
); |
| 102 |
|
| 103 |
this.beforeAppend( body ); |
| 104 |
|
| 105 |
|
| 106 |
element.innerHTML = ''; |
| 107 |
while ( body.childNodes.length ) { |
| 108 |
element.appendChild( |
| 109 |
element.ownerDocument.adoptNode( body.childNodes[ 0 ] ) |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
this.afterRender(); |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
ve.ui.PreviewElement.prototype.updatePreview = function () { |
| 120 |
|
| 121 |
this.view = ve.ce.nodeFactory.createFromModel( this.model ); |
| 122 |
this.beforeAppend( this.view.$element[ 0 ] ); |
| 123 |
this.$element.append( this.view.$element ); |
| 124 |
this.view.setLive( true ); |
| 125 |
|
| 126 |
ve.ce.GeneratedContentNode.static.awaitGeneratedContent( this.view ) |
| 127 |
.then( () => { |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
if ( !this.useView ) { |
| 132 |
|
| 133 |
if ( this.view ) { |
| 134 |
this.replaceWithModelDom(); |
| 135 |
} |
| 136 |
} else { |
| 137 |
this.afterRender(); |
| 138 |
} |
| 139 |
} ); |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
ve.ui.PreviewElement.prototype.afterRender = function () { |
| 148 |
|
| 149 |
this.view.destroy(); |
| 150 |
this.view = null; |
| 151 |
|
| 152 |
|
| 153 |
this.emit( 'render' ); |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
ve.ui.PreviewElement.prototype.isGenerating = function () { |
| 162 |
return !!this.view; |
| 163 |
}; |