| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.SumCellsContextItem = function VeUiSumCellsContextItem( context, model, config ) { |
| 18 |
|
| 19 |
ve.ui.SumCellsContextItem.super.call( this, context, model, config ); |
| 20 |
|
| 21 |
|
| 22 |
this.$element.addClass( 've-ui-sumCellsContextItem' ); |
| 23 |
}; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
OO.inheritClass( ve.ui.SumCellsContextItem, ve.ui.LinearContextItem ); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
ve.ui.SumCellsContextItem.static.name = 'sumCells'; |
| 32 |
|
| 33 |
ve.ui.SumCellsContextItem.static.icon = 'mathematics'; |
| 34 |
|
| 35 |
ve.ui.SumCellsContextItem.static.editable = false; |
| 36 |
|
| 37 |
ve.ui.SumCellsContextItem.static.embeddable = false; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
ve.ui.SumCellsContextItem.static.isCompatibleWith = function ( model ) { |
| 45 |
return model instanceof ve.dm.Node && model.isCellable(); |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
ve.ui.SumCellsContextItem.prototype.setup = function () { |
| 52 |
|
| 53 |
ve.ui.SumCellsContextItem.super.prototype.setup.apply( this, arguments ); |
| 54 |
|
| 55 |
|
| 56 |
const selection = this.getFragment().getSurface().getSelection(), |
| 57 |
documentModel = this.getFragment().getDocument(), |
| 58 |
documentView = this.context.getSurface().getView().getDocument(); |
| 59 |
|
| 60 |
let count = 0; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
let sum; |
| 65 |
if ( selection instanceof ve.dm.TableSelection ) { |
| 66 |
const cells = selection.getMatrixCells( documentModel, true ); |
| 67 |
if ( cells.length > 1 ) { |
| 68 |
sum = cells.reduce( ( s, cell ) => { |
| 69 |
let number; |
| 70 |
if ( !cell.isPlaceholder() ) { |
| 71 |
|
| 72 |
const viewCell = documentView.getBranchNodeFromOffset( cell.node.getRange().start ); |
| 73 |
number = ve.init.platform.parseNumber( |
| 74 |
viewCell.$element.text() |
| 75 |
); |
| 76 |
if ( !isNaN( number ) ) { |
| 77 |
count++; |
| 78 |
return s + number; |
| 79 |
} |
| 80 |
} |
| 81 |
return s; |
| 82 |
}, 0 ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
if ( count > 1 ) { |
| 88 |
this.setLabel( |
| 89 |
ve.msg( 'visualeditor-table-sum', |
| 90 |
ve.init.platform.formatNumber( sum ), |
| 91 |
ve.init.platform.formatNumber( sum / count ) |
| 92 |
) |
| 93 |
); |
| 94 |
} else { |
| 95 |
this.$element.detach(); |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
ve.ui.contextItemFactory.register( ve.ui.SumCellsContextItem ); |