Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ce/nodes/ve.ce.MWTableNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable MWTableNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * ContentEditable MW table node.
10 *
11 * @class
12 * @extends ve.ce.TableNode
13 * @mixes ve.ce.ClassAttributeNode
14 *
15 * @constructor
16 * @param {ve.dm.MWTableNode} model Model to observe
17 * @param {Object} [config] Configuration options
18 */
19 ve.ce.MWTableNode = function VeCeMWTableNode() {
20 // Parent constructor
21 ve.ce.MWTableNode.super.apply( this, arguments );
22
23 // Mixin constructors
24 ve.ce.ClassAttributeNode.call( this );
25
26 // Properties
27 this.updateSortableHeadersHandler = ve.debounce( this.updateSortableHeaders );
28 this.$sortableHeaders = $( [] );
29
30 // Events
31 this.connect( this, { setup: 'updateSortableHeadersHandler' } );
32 this.model.connect( this, {
33 // Update when the table is made sortable or not sortable
34 attributeChange: 'updateSortableHeadersHandler',
35 // Update when a cell style changes between content cell and header cell
36 cellAttributeChange: 'updateSortableHeadersHandler'
37 } );
38 this.model.getMatrix().connect( this, {
39 // Update when cells are added, removed, merged, split
40 structureChange: 'updateSortableHeadersHandler'
41 } );
42
43 // DOM changes
44 this.$element.addClass( 've-ce-mwTableNode' );
45 };
46
47 /* Inheritance */
48
49 OO.inheritClass( ve.ce.MWTableNode, ve.ce.TableNode );
50
51 OO.mixinClass( ve.ce.MWTableNode, ve.ce.ClassAttributeNode );
52
53 /* Static Properties */
54
55 ve.ce.MWTableNode.static.name = 'mwTable';
56
57 /* Methods */
58
59 /**
60 * @inheritdoc
61 */
62 ve.ce.MWTableNode.prototype.destroy = function () {
63 this.model.getMatrix().disconnect( this );
64
65 // Parent method
66 ve.ce.MWTableNode.super.prototype.destroy.apply( this, arguments );
67 };
68
69 /**
70 * Update sortable headers (if the table is sortable).
71 *
72 * @private
73 */
74 ve.ce.MWTableNode.prototype.updateSortableHeaders = function () {
75 if ( !this.model ) {
76 // Fired after teardown due to debounce
77 return;
78 }
79
80 if ( this.model.getAttribute( 'collapsible' ) ) {
81 mw.loader.load( 'jquery.makeCollapsible.styles' );
82 }
83
84 this.$element.toggleClass( 'jquery-tablesorter', this.model.getAttribute( 'sortable' ) );
85
86 this.$sortableHeaders.removeClass( 'headerSort' );
87
88 if ( this.model.getAttribute( 'sortable' ) ) {
89 // We only really want the styles. But it's harmless to load the entire module, and if the user
90 // ends up saving this change, it will be loaded anyway to render the real sortable table.
91 mw.loader.load( 'jquery.tablesorter' );
92
93 const cellModels = this.getTablesorterHeaderCells();
94 const cellViews = cellModels.map( ( cellModel ) => this.getNodeFromOffset( cellModel.getOffset() - this.model.getOffset() ) );
95
96 this.$sortableHeaders = $( cellViews.map( ( cell ) => cell.$element[ 0 ] ) ).not( '.unsortable' );
97 } else {
98 this.$sortableHeaders = $( [] );
99 }
100
101 this.$sortableHeaders.addClass( 'headerSort' );
102
103 // .headerSort class adds some padding, causing the overlays to become misaligned
104 this.updateOverlay();
105 };
106
107 /**
108 * Find the last of header rows with maximum number of cells (minimum number of colspans) and return
109 * all of its cells. These are the cells that serve as sortable headers in jQuery Tablesorter.
110 * This algorithm is exactly the same, see the buildHeaders() function in jquery.tablesorter.js.
111 *
112 * @private
113 * @return {ve.dm.TableCellNode[]}
114 */
115 ve.ce.MWTableNode.prototype.getTablesorterHeaderCells = function () {
116 const matrix = this.model.getMatrix();
117
118 let longestRow = [];
119 let longestRowLength = 0;
120 for ( let i = 0, l = matrix.getRowCount(); i < l; i++ ) {
121 const matrixCells = matrix.getRow( i );
122 const cellModels = OO.unique( matrixCells.map( ( matrixCell ) => matrixCell && matrixCell.getOwner().node ) );
123 const isAllHeaders = cellModels.every( ( cellModel ) => cellModel && cellModel.getAttribute( 'style' ) === 'header' );
124 if ( !isAllHeaders ) {
125 // This is the end of table head (thead), stop looking further
126 break;
127 }
128 const rowLength = cellModels.length;
129 if ( rowLength >= longestRowLength ) {
130 longestRowLength = rowLength;
131 longestRow = cellModels;
132 }
133 }
134
135 return longestRow;
136 };
137
138 /* Registration */
139
140 ve.ce.nodeFactory.register( ve.ce.MWTableNode );