Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F7426133
ve.ui.SumCellsContextItem.js
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
ve.ui.SumCellsContextItem.js
View Options
/*!
* VisualEditor SumCellsContextItem class.
*
* @copyright See AUTHORS.txt
*/
/**
* Show the sum and average of numeric values across a table selection.
*
* @class
* @extends ve.ui.LinearContextItem
*
* @param {ve.ui.LinearContext} context Context the item is in
* @param {ve.dm.Model} model Model the item is related to
* @param {Object} [config] Configuration options
*/
ve
.
ui
.
SumCellsContextItem
=
function
VeUiSumCellsContextItem
(
context
,
model
,
config
=
{}
)
{
// Parent constructor
ve
.
ui
.
SumCellsContextItem
.
super
.
call
(
this
,
context
,
model
,
config
);
// Initialization
this
.
$element
.
addClass
(
've-ui-sumCellsContextItem'
);
};
/* Inheritance */
OO
.
inheritClass
(
ve
.
ui
.
SumCellsContextItem
,
ve
.
ui
.
LinearContextItem
);
/* Static Properties */
ve
.
ui
.
SumCellsContextItem
.
static
.
name
=
'sumCells'
;
ve
.
ui
.
SumCellsContextItem
.
static
.
icon
=
'mathematics'
;
ve
.
ui
.
SumCellsContextItem
.
static
.
editable
=
false
;
ve
.
ui
.
SumCellsContextItem
.
static
.
embeddable
=
false
;
/* Methods */
/**
* @inheritdoc
*/
ve
.
ui
.
SumCellsContextItem
.
static
.
isCompatibleWith
=
function
(
model
)
{
return
model
instanceof
ve
.
dm
.
Node
&&
model
.
isCellable
();
};
/**
* @inheritdoc
*/
ve
.
ui
.
SumCellsContextItem
.
prototype
.
setup
=
function
()
{
// Parent method
ve
.
ui
.
SumCellsContextItem
.
super
.
prototype
.
setup
.
apply
(
this
,
arguments
);
// If not disabled, selection must be table and spanning multiple matrix cells
const
selection
=
this
.
getFragment
().
getSurface
().
getSelection
(),
documentModel
=
this
.
getFragment
().
getDocument
(),
documentView
=
this
.
context
.
getSurface
().
getView
().
getDocument
();
let
count
=
0
;
// There's some situations involving transclusion table cells which
// can make us have a LinearSelection here, so make sure this will
// work:
let
sum
;
if
(
selection
instanceof
ve
.
dm
.
TableSelection
)
{
const
cells
=
selection
.
getMatrixCells
(
documentModel
,
true
);
if
(
cells
.
length
>
1
)
{
sum
=
cells
.
reduce
(
(
s
,
cell
)
=>
{
let
number
;
if
(
!
cell
.
isPlaceholder
()
)
{
// Get text from view rendering to catch numbers in alien nodes, etc.
const
viewCell
=
documentView
.
getBranchNodeFromOffset
(
cell
.
node
.
getRange
().
start
);
number
=
ve
.
init
.
platform
.
parseNumber
(
viewCell
.
$element
.
text
()
);
if
(
!
isNaN
(
number
)
)
{
count
++
;
return
s
+
number
;
}
}
return
s
;
},
0
);
}
}
// Only show if more than one numeric value was selected
if
(
count
>
1
)
{
this
.
setLabel
(
ve
.
msg
(
'visualeditor-table-sum'
,
ve
.
init
.
platform
.
formatNumber
(
sum
),
ve
.
init
.
platform
.
formatNumber
(
sum
/
count
)
)
);
}
else
{
this
.
$element
.
detach
();
}
};
/* Registration */
ve
.
ui
.
contextItemFactory
.
register
(
ve
.
ui
.
SumCellsContextItem
);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 22, 08:17 (19 h, 11 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
c8/93/3c43b2cacfa4cc82a8912fcfb806
Default Alt Text
ve.ui.SumCellsContextItem.js (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment