| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
ve.ui.MWCitationNeededContextItem = function VeUiMWCitationNeededContextItem() { |
| 20 |
|
| 21 |
ve.ui.MWCitationNeededContextItem.super.apply( this, arguments ); |
| 22 |
|
| 23 |
this.addButton = new OO.ui.ButtonWidget( { |
| 24 |
label: ve.msg( 'cite-ve-citationneeded-button' ), |
| 25 |
flags: [ 'progressive' ] |
| 26 |
} ).on( 'click', this.onAddClick.bind( this ) ); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
this.editButton.setFlags( { progressive: false } ); |
| 31 |
|
| 32 |
|
| 33 |
this.$element.addClass( 've-ui-mwCitationNeededContextItem' ); |
| 34 |
}; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
OO.inheritClass( ve.ui.MWCitationNeededContextItem, ve.ui.MWDefinedTransclusionContextItem ); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
ve.ui.MWCitationNeededContextItem.static.name = 'citationNeeded'; |
| 43 |
|
| 44 |
ve.ui.MWCitationNeededContextItem.static.icon = 'quotes'; |
| 45 |
|
| 46 |
ve.ui.MWCitationNeededContextItem.static.label = OO.ui.deferMsg( 'cite-ve-citationneeded-title' ); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
ve.ui.MWCitationNeededContextItem.prototype.onAddClick = function () { |
| 51 |
const surface = this.context.getSurface(); |
| 52 |
const encapsulatedWikitext = this.getCanonicalParam( 'encapsulate' ); |
| 53 |
|
| 54 |
let promise; |
| 55 |
if ( encapsulatedWikitext ) { |
| 56 |
this.addButton.setDisabled( true ); |
| 57 |
promise = ve.init.target |
| 58 |
.parseWikitextFragment( encapsulatedWikitext, false, this.model.getDocument() ) |
| 59 |
.then( ( response ) => { |
| 60 |
|
| 61 |
if ( ve.getProp( response, 'visualeditor', 'result' ) !== 'success' ) { |
| 62 |
return ve.createDeferred().reject().promise(); |
| 63 |
} |
| 64 |
|
| 65 |
const dmDoc = ve.ui.MWWikitextStringTransferHandler.static |
| 66 |
.createDocumentFromParsoidHtml( |
| 67 |
response.visualeditor.content, |
| 68 |
surface.getModel().getDocument() |
| 69 |
); |
| 70 |
const nodes = dmDoc.getDocumentNode().children.filter( ( node ) => !node.isInternal() ); |
| 71 |
let range; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
if ( |
| 76 |
nodes.length === 1 && |
| 77 |
nodes[ 0 ].canContainContent() |
| 78 |
) { |
| 79 |
range = nodes[ 0 ].getRange(); |
| 80 |
} |
| 81 |
|
| 82 |
surface.getModel().pushStaging(); |
| 83 |
surface.getModel().getFragment() |
| 84 |
.insertDocument( dmDoc, range ).collapseToEnd().select(); |
| 85 |
return true; |
| 86 |
} ); |
| 87 |
promise.always( () => { |
| 88 |
this.addButton.setDisabled( false ); |
| 89 |
} ); |
| 90 |
} else { |
| 91 |
promise = ve.createDeferred().resolve( false ).promise(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
const action = ve.ui.actionFactory.create( 'citoid', surface ); |
| 96 |
promise.then( ( inStaging ) => { |
| 97 |
action.open( true, undefined, inStaging ); |
| 98 |
} ); |
| 99 |
ve.track( 'activity.' + this.constructor.static.name, { action: 'context-add-citation' } ); |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
ve.ui.MWCitationNeededContextItem.prototype.renderBody = function () { |
| 106 |
const date = this.getCanonicalParam( 'date' ); |
| 107 |
let description = ve.msg( 'cite-ve-citationneeded-description' ); |
| 108 |
|
| 109 |
if ( date ) { |
| 110 |
description += ve.msg( 'word-separator' ) + ve.msg( 'parentheses', date ); |
| 111 |
} |
| 112 |
|
| 113 |
this.$body.empty(); |
| 114 |
this.$body.append( $( '<p>' ).addClass( 've-ui-mwCitationNeededContextItem-description' ).text( description ) ); |
| 115 |
|
| 116 |
const reason = this.getCanonicalParam( 'reason' ); |
| 117 |
if ( reason ) { |
| 118 |
this.$body.append( |
| 119 |
$( '<p>' ).addClass( 've-ui-mwCitationNeededContextItem-reason' ).append( |
| 120 |
document.createTextNode( ve.msg( 'cite-ve-citationneeded-reason' ) + ve.msg( 'word-separator' ) ), |
| 121 |
|
| 122 |
$( '<em>' ).text( reason ) |
| 123 |
) |
| 124 |
); |
| 125 |
} |
| 126 |
this.$body.append( this.addButton.$element ); |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
ve.ui.contextItemFactory.register( ve.ui.MWCitationNeededContextItem ); |