| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
ve.ui.TargetWidget = function VeUiTargetWidget( config ) { |
| 33 |
|
| 34 |
config = config || {}; |
| 35 |
|
| 36 |
|
| 37 |
ve.ui.TargetWidget.super.call( this, config ); |
| 38 |
|
| 39 |
|
| 40 |
OO.ui.mixin.PendingElement.call( this, config ); |
| 41 |
|
| 42 |
|
| 43 |
this.toolbarGroups = config.toolbarGroups; |
| 44 |
|
| 45 |
this.includeCommands = config.includeCommands; |
| 46 |
this.excludeCommands = config.excludeCommands; |
| 47 |
this.multiline = config.multiline !== false; |
| 48 |
this.placeholder = config.placeholder; |
| 49 |
this.readOnly = config.readOnly; |
| 50 |
this.importRules = config.importRules; |
| 51 |
this.inDialog = config.inDialog; |
| 52 |
this.modes = config.modes; |
| 53 |
this.defaultMode = config.defaultMode; |
| 54 |
|
| 55 |
this.target = this.createTarget(); |
| 56 |
|
| 57 |
if ( config.doc ) { |
| 58 |
this.setDocument( config.doc ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
this.$element.addClass( 've-ui-targetWidget' ) |
| 63 |
.append( this.target.$element ); |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
OO.inheritClass( ve.ui.TargetWidget, OO.ui.Widget ); |
| 69 |
OO.mixinClass( ve.ui.TargetWidget, OO.ui.mixin.PendingElement ); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
ve.ui.TargetWidget.prototype.createTarget = function () { |
| 103 |
return new ve.init.Target( { |
| 104 |
register: false, |
| 105 |
toolbarGroups: this.toolbarGroups, |
| 106 |
modes: this.modes, |
| 107 |
defaultMode: this.defaultMode |
| 108 |
} ); |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
ve.ui.TargetWidget.prototype.setDocument = function ( doc ) { |
| 122 |
|
| 123 |
this.clear(); |
| 124 |
const surface = this.target.addSurface( doc, { |
| 125 |
inTargetWidget: true, |
| 126 |
includeCommands: this.includeCommands, |
| 127 |
excludeCommands: this.excludeCommands, |
| 128 |
importRules: this.importRules, |
| 129 |
multiline: this.multiline, |
| 130 |
placeholder: this.placeholder, |
| 131 |
readOnly: this.readOnly, |
| 132 |
|
| 133 |
overlayPadding: 5, |
| 134 |
inDialog: this.inDialog |
| 135 |
} ); |
| 136 |
this.target.setSurface( surface ); |
| 137 |
|
| 138 |
|
| 139 |
surface.getView().connect( this, { |
| 140 |
activation: 'onFocusChange', |
| 141 |
focus: 'onFocusChange', |
| 142 |
blur: 'onFocusChange' |
| 143 |
} ); |
| 144 |
|
| 145 |
surface.getModel().connect( this, { history: [ 'emit', 'change' ] } ); |
| 146 |
surface.connect( this, { |
| 147 |
submit: 'onSurfaceSubmit', |
| 148 |
cancel: 'onSurfaceCancel' |
| 149 |
} ); |
| 150 |
|
| 151 |
surface.getView().once( 'focus', () => { |
| 152 |
surface.getView().emit( 'position' ); |
| 153 |
} ); |
| 154 |
|
| 155 |
this.emit( 'setup' ); |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
ve.ui.TargetWidget.prototype.onSurfaceSubmit = function () { |
| 164 |
const handled = this.emit( 'submit' ); |
| 165 |
if ( !handled && this.inDialog ) { |
| 166 |
|
| 167 |
this.$element.parent().trigger( $.Event( 'keydown', { |
| 168 |
which: OO.ui.Keys.ENTER, |
| 169 |
ctrlKey: true |
| 170 |
} ) ); |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
ve.ui.TargetWidget.prototype.onSurfaceCancel = function () { |
| 180 |
const handled = this.emit( 'cancel' ); |
| 181 |
if ( !handled && this.inDialog ) { |
| 182 |
|
| 183 |
this.$element.parent().trigger( $.Event( 'keydown', { |
| 184 |
which: OO.ui.Keys.ESCAPE |
| 185 |
} ) ); |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
ve.ui.TargetWidget.prototype.hasBeenModified = function () { |
| 195 |
return !!this.getSurface() && this.getSurface().getModel().hasBeenModified(); |
| 196 |
}; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
ve.ui.TargetWidget.prototype.setReadOnly = function ( readOnly ) { |
| 204 |
this.readOnly = !!readOnly; |
| 205 |
if ( this.getSurface() ) { |
| 206 |
this.getSurface().setReadOnly( this.readOnly ); |
| 207 |
} |
| 208 |
this.$element.toggleClass( 've-ui-targetWidget-readOnly', this.readOnly ); |
| 209 |
}; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
ve.ui.TargetWidget.prototype.isReadOnly = function () { |
| 217 |
return this.readOnly; |
| 218 |
}; |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
ve.ui.TargetWidget.prototype.getSurface = function () { |
| 226 |
return this.target.getSurface(); |
| 227 |
}; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
ve.ui.TargetWidget.prototype.getToolbar = function () { |
| 235 |
return this.target.getToolbar(); |
| 236 |
}; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
ve.ui.TargetWidget.prototype.getContent = function () { |
| 244 |
return this.getSurface().getModel().getDocument().getData(); |
| 245 |
}; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
ve.ui.TargetWidget.prototype.initialize = function () { |
| 255 |
OO.ui.warnDeprecation( 've.ui.TargetWidget#initialize is deprecated and no longer needed.' ); |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
ve.ui.TargetWidget.prototype.clear = function () { |
| 262 |
this.target.clearSurfaces(); |
| 263 |
|
| 264 |
}; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
ve.ui.TargetWidget.prototype.onFocusChange = function () { |
| 270 |
|
| 271 |
const surface = this.getSurface(); |
| 272 |
|
| 273 |
|
| 274 |
this.$element.toggleClass( |
| 275 |
've-ui-targetWidget-focused', |
| 276 |
surface && surface.getView().isFocused() && !surface.getView().isDeactivated() |
| 277 |
); |
| 278 |
}; |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
ve.ui.TargetWidget.prototype.focus = function () { |
| 284 |
const surface = this.getSurface(); |
| 285 |
if ( surface ) { |
| 286 |
if ( !surface.getView().attachedRoot.isLive() ) { |
| 287 |
surface.once( 'ready', () => { |
| 288 |
surface.getView().focus(); |
| 289 |
} ); |
| 290 |
} else { |
| 291 |
surface.getView().focus(); |
| 292 |
} |
| 293 |
} |
| 294 |
}; |