| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
ve.ui.TableLineContext = function VeUiTableLineContext( tableNode, itemGroup, config ) { |
| 21 |
config = config || {}; |
| 22 |
|
| 23 |
|
| 24 |
ve.ui.TableLineContext.super.call( this, tableNode.surface.getSurface(), config ); |
| 25 |
|
| 26 |
|
| 27 |
this.tableNode = tableNode; |
| 28 |
this.itemGroup = itemGroup; |
| 29 |
this.icon = new OO.ui.IconWidget( { |
| 30 |
icon: this.constructor.static.icons[ itemGroup ] |
| 31 |
} ); |
| 32 |
this.popup = new OO.ui.PopupWidget( { |
| 33 |
classes: [ 've-ui-tableLineContext-menu' ], |
| 34 |
$container: this.surface.$element, |
| 35 |
$floatableContainer: this.icon.$element, |
| 36 |
position: this.constructor.static.positions[ this.itemGroup ], |
| 37 |
width: null |
| 38 |
} ); |
| 39 |
|
| 40 |
|
| 41 |
this.icon.$element.on( 'mousedown', this.onIconMouseDown.bind( this ) ); |
| 42 |
this.onDocumentMouseDownHandler = this.onDocumentMouseDown.bind( this ); |
| 43 |
|
| 44 |
|
| 45 |
const labels = { |
| 46 |
col: ve.msg( 'visualeditor-table-context-col' ), |
| 47 |
row: ve.msg( 'visualeditor-table-context-row' ), |
| 48 |
table: ve.msg( 'visualeditor-toolbar-table' ) |
| 49 |
}; |
| 50 |
this.icon.$element |
| 51 |
.attr( 'role', 'button' ) |
| 52 |
.attr( 'aria-label', labels[ itemGroup ] ); |
| 53 |
this.popup.$body.append( this.$group ); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
this.$element |
| 59 |
.addClass( 've-ui-tableLineContext ve-ui-tableLineContext-' + itemGroup ) |
| 60 |
.append( this.icon.$element, this.popup.$element ); |
| 61 |
|
| 62 |
this.toggle( true ); |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
OO.inheritClass( ve.ui.TableLineContext, ve.ui.Context ); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
ve.ui.TableLineContext.static.groups = { |
| 72 |
col: [ 'insertColumnBefore', 'insertColumnAfter', 'moveColumnBefore', 'moveColumnAfter', 'deleteColumn' ], |
| 73 |
row: [ 'insertRowBefore', 'insertRowAfter', 'moveRowBefore', 'moveRowAfter', 'deleteRow' ], |
| 74 |
table: [ 'tableProperties', 'toggleTableEditing', 'deleteTable' ] |
| 75 |
}; |
| 76 |
|
| 77 |
ve.ui.TableLineContext.static.icons = { |
| 78 |
col: 'expand', |
| 79 |
row: 'next', |
| 80 |
table: 'table' |
| 81 |
}; |
| 82 |
|
| 83 |
ve.ui.TableLineContext.static.positions = { |
| 84 |
col: 'below', |
| 85 |
row: 'after', |
| 86 |
table: 'after' |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
ve.ui.TableLineContext.prototype.getRelatedSources = function () { |
| 95 |
const items = this.constructor.static.groups[ this.itemGroup ]; |
| 96 |
|
| 97 |
if ( !this.relatedSources ) { |
| 98 |
this.relatedSources = []; |
| 99 |
|
| 100 |
for ( let i = 0, l = items.length; i < l; i++ ) { |
| 101 |
this.relatedSources.push( { |
| 102 |
type: 'item', |
| 103 |
name: items[ i ] |
| 104 |
} ); |
| 105 |
} |
| 106 |
} |
| 107 |
return this.relatedSources; |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
ve.ui.TableLineContext.prototype.onContextItemCommand = function () { |
| 114 |
this.toggleMenu( false ); |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
ve.ui.TableLineContext.prototype.onIconMouseDown = function ( e ) { |
| 123 |
e.preventDefault(); |
| 124 |
this.toggleMenu( undefined, true ); |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
ve.ui.TableLineContext.prototype.onDocumentMouseDown = function ( e ) { |
| 133 |
if ( !$( e.target ).closest( this.$element ).length ) { |
| 134 |
this.toggleMenu( false, true ); |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
ve.ui.TableLineContext.prototype.onModelSelect = function () { |
| 144 |
this.toggleMenu(); |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
ve.ui.TableLineContext.prototype.toggleMenu = function ( show, restoreEditing ) { |
| 151 |
show = show === undefined ? !this.popup.isVisible() : !!show; |
| 152 |
|
| 153 |
const surfaceModel = this.surface.getModel(); |
| 154 |
const surfaceView = this.surface.getView(); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
this.wasEditing = !!this.tableNode.editingFragment; |
| 160 |
|
| 161 |
|
| 162 |
if ( this.itemGroup !== 'table' ) { |
| 163 |
if ( show ) { |
| 164 |
this.tableNode.setEditing( false ); |
| 165 |
} else if ( restoreEditing && surfaceModel.getSelection() instanceof ve.dm.TableSelection ) { |
| 166 |
this.tableNode.setEditing( this.wasEditing ); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
if ( show ) { |
| 172 |
surfaceModel.connect( this, { select: 'onModelSelect' } ); |
| 173 |
surfaceView.$document.on( 'mousedown', this.onDocumentMouseDownHandler ); |
| 174 |
surfaceView.deactivate(); |
| 175 |
const dir = surfaceView.getSelectionDirectionality(); |
| 176 |
|
| 177 |
this.$element |
| 178 |
.removeClass( 've-ui-dir-block-rtl ve-ui-dir-block-ltr' ) |
| 179 |
.addClass( 've-ui-dir-block-' + dir ); |
| 180 |
} else { |
| 181 |
surfaceModel.disconnect( this ); |
| 182 |
surfaceView.$document.off( 'mousedown', this.onDocumentMouseDownHandler ); |
| 183 |
surfaceView.activate(); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
ve.ui.TableLineContext.super.prototype.toggleMenu.call( this, show ); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
this.popup.toggle( show ); |
| 192 |
}; |