| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
ve.ui.CompletionWidget = function VeUiCompletionWidget( surface, config ) { |
| 20 |
this.surface = surface; |
| 21 |
this.surfaceModel = surface.getModel(); |
| 22 |
|
| 23 |
|
| 24 |
config = config || { |
| 25 |
anchor: false |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
ve.ui.CompletionWidget.super.call( this, config ); |
| 30 |
|
| 31 |
this.$tabIndexed = this.$element; |
| 32 |
|
| 33 |
const $doc = surface.getView().getDocument().getDocumentNode().$element; |
| 34 |
this.popup = new OO.ui.PopupWidget( { |
| 35 |
anchor: false, |
| 36 |
align: 'forwards', |
| 37 |
hideWhenOutOfView: false, |
| 38 |
autoFlip: false, |
| 39 |
width: null, |
| 40 |
$container: config.$popupContainer || this.surface.$element, |
| 41 |
containerPadding: config.popupPadding |
| 42 |
} ); |
| 43 |
this.input = new OO.ui.TextInputWidget(); |
| 44 |
this.menu = new OO.ui.MenuSelectWidget( { |
| 45 |
widget: this, |
| 46 |
$input: $doc.add( this.input.$input ) |
| 47 |
} ); |
| 48 |
|
| 49 |
|
| 50 |
this.header = new OO.ui.MenuOptionWidget( { |
| 51 |
classes: [ 've-ui-completionWidget-header' ], |
| 52 |
disabled: true |
| 53 |
} ); |
| 54 |
this.noResults = new OO.ui.MenuOptionWidget( { |
| 55 |
label: ve.msg( 'visualeditor-completionwidget-noresults' ), |
| 56 |
classes: [ 've-ui-completionWidget-noresults' ], |
| 57 |
disabled: true |
| 58 |
} ); |
| 59 |
|
| 60 |
|
| 61 |
this.menu.connect( this, { |
| 62 |
choose: 'onMenuChoose', |
| 63 |
toggle: 'onMenuToggle' |
| 64 |
} ); |
| 65 |
this.input.connect( this, { change: 'update' } ); |
| 66 |
|
| 67 |
this.popup.$element.prepend( this.input.$element ); |
| 68 |
this.popup.$body.append( |
| 69 |
this.menu.$element |
| 70 |
); |
| 71 |
|
| 72 |
|
| 73 |
this.$element.addClass( 've-ui-completionWidget' ) |
| 74 |
.append( |
| 75 |
this.popup.$element |
| 76 |
); |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
OO.inheritClass( ve.ui.CompletionWidget, OO.ui.Widget ); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
ve.ui.CompletionWidget.prototype.setup = function ( action, isolateInput ) { |
| 90 |
const range = this.surfaceModel.getSelection().getCoveringRange(); |
| 91 |
this.action = action; |
| 92 |
this.isolateInput = !!isolateInput; |
| 93 |
this.sequenceLength = this.action.getSequenceLength(); |
| 94 |
this.initialOffset = range.end - this.sequenceLength; |
| 95 |
|
| 96 |
this.input.toggle( this.isolateInput ); |
| 97 |
if ( this.isolateInput ) { |
| 98 |
this.wasActive = !this.surface.getView().isDeactivated(); |
| 99 |
this.surface.getView().deactivate(); |
| 100 |
this.input.setValue( '' ); |
| 101 |
setTimeout( () => { |
| 102 |
this.input.focus(); |
| 103 |
}, 1 ); |
| 104 |
} else { |
| 105 |
this.wasActive = false; |
| 106 |
} |
| 107 |
|
| 108 |
this.update(); |
| 109 |
|
| 110 |
this.surfaceModel.connect( this, { select: 'onModelSelect' } ); |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
ve.ui.CompletionWidget.prototype.teardown = function () { |
| 117 |
this.tearingDown = true; |
| 118 |
this.popup.toggle( false ); |
| 119 |
this.surfaceModel.disconnect( this ); |
| 120 |
if ( this.wasActive ) { |
| 121 |
this.surface.getView().activate(); |
| 122 |
} |
| 123 |
this.action = undefined; |
| 124 |
this.tearingDown = false; |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
ve.ui.CompletionWidget.prototype.update = function () { |
| 131 |
const direction = this.surface.getDir(), |
| 132 |
range = this.getCompletionRange(), |
| 133 |
boundingRect = this.surface.getView().getSelection( new ve.dm.LinearSelection( range ) ).getSelectionBoundingRect(), |
| 134 |
style = { |
| 135 |
top: boundingRect.bottom |
| 136 |
}; |
| 137 |
|
| 138 |
let input; |
| 139 |
if ( this.isolateInput ) { |
| 140 |
input = this.input.getValue(); |
| 141 |
} else { |
| 142 |
const data = this.surfaceModel.getDocument().data; |
| 143 |
input = data.getText( false, range ); |
| 144 |
} |
| 145 |
|
| 146 |
if ( direction === 'rtl' ) { |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
style.left = boundingRect.right; |
| 151 |
} else { |
| 152 |
style.left = boundingRect.left; |
| 153 |
} |
| 154 |
this.$element.css( style ); |
| 155 |
|
| 156 |
this.updateMenu( input ); |
| 157 |
this.action.getSuggestions( input ).then( ( suggestions ) => { |
| 158 |
if ( !this.action ) { |
| 159 |
|
| 160 |
return; |
| 161 |
} |
| 162 |
this.menu.clearItems(); |
| 163 |
let menuItems = suggestions.map( this.action.getMenuItemForSuggestion.bind( this.action ) ); |
| 164 |
menuItems = this.action.updateMenuItems( menuItems ); |
| 165 |
this.menu.addItems( menuItems ); |
| 166 |
this.menu.highlightItem( this.menu.findFirstSelectableItem() ); |
| 167 |
this.updateMenu( input, suggestions ); |
| 168 |
} ); |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
ve.ui.CompletionWidget.prototype.updateMenu = function ( input, suggestions ) { |
| 178 |
|
| 179 |
const label = this.action.getHeaderLabel( input, suggestions ); |
| 180 |
if ( label !== undefined ) { |
| 181 |
this.header.setLabel( label ); |
| 182 |
} |
| 183 |
if ( this.header.getLabel() !== null ) { |
| 184 |
this.menu.addItems( [ this.header ], 0 ); |
| 185 |
} else { |
| 186 |
this.menu.removeItems( [ this.header ] ); |
| 187 |
} |
| 188 |
if ( !this.isolateInput ) { |
| 189 |
|
| 190 |
if ( this.menu.items.length ) { |
| 191 |
this.menu.toggle( true ); |
| 192 |
this.popup.toggle( true ); |
| 193 |
|
| 194 |
this.popup.updateDimensions(); |
| 195 |
} else { |
| 196 |
this.popup.toggle( false ); |
| 197 |
} |
| 198 |
} else { |
| 199 |
if ( !this.menu.items.length ) { |
| 200 |
this.menu.addItems( [ this.noResults ], 0 ); |
| 201 |
} |
| 202 |
this.menu.toggle( true ); |
| 203 |
this.popup.toggle( true ); |
| 204 |
this.popup.updateDimensions(); |
| 205 |
} |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
ve.ui.CompletionWidget.prototype.onMenuChoose = function ( item ) { |
| 214 |
this.action.chooseItem( item, this.getCompletionRange( true ) ); |
| 215 |
|
| 216 |
this.teardown(); |
| 217 |
}; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
ve.ui.CompletionWidget.prototype.onMenuToggle = function ( visible ) { |
| 225 |
if ( !visible && !this.tearingDown ) { |
| 226 |
|
| 227 |
this.teardown(); |
| 228 |
} |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
ve.ui.CompletionWidget.prototype.onModelSelect = function () { |
| 237 |
const range = this.getCompletionRange(); |
| 238 |
|
| 239 |
const countMatches = () => { |
| 240 |
let matches = this.menu.getItems().length; |
| 241 |
if ( this.header.getLabel() !== null ) { |
| 242 |
matches--; |
| 243 |
} |
| 244 |
if ( this.action.constructor.static.alwaysIncludeInput ) { |
| 245 |
matches--; |
| 246 |
} |
| 247 |
return matches; |
| 248 |
}; |
| 249 |
|
| 250 |
if ( !range || range.isBackwards() || this.action.shouldAbandon( this.surfaceModel.getDocument().data.getText( false, range ), countMatches() ) ) { |
| 251 |
this.teardown(); |
| 252 |
} else { |
| 253 |
this.update(); |
| 254 |
} |
| 255 |
}; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
ve.ui.CompletionWidget.prototype.getCompletionRange = function ( withSequence ) { |
| 264 |
const range = this.surfaceModel.getSelection().getCoveringRange(); |
| 265 |
if ( !range || !this.action ) { |
| 266 |
return null; |
| 267 |
} |
| 268 |
return new ve.Range( this.initialOffset + ( withSequence ? 0 : this.sequenceLength ), range.end ); |
| 269 |
}; |