Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/nodes/ve.ce.TableRowNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable TableRowNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * ContentEditable table row node.
9 *
10 * @class
11 * @extends ve.ce.BranchNode
12 * @constructor
13 * @param {ve.dm.TableRowNode} model Model to observe
14 * @param {Object} [config] Configuration options
15 */
16 ve.ce.TableRowNode = function VeCeTableRowNode() {
17 // Parent constructor
18 ve.ce.TableRowNode.super.apply( this, arguments );
19
20 this.$missingCell = null;
21 };
22
23 /* Inheritance */
24
25 OO.inheritClass( ve.ce.TableRowNode, ve.ce.BranchNode );
26
27 /* Static Properties */
28
29 ve.ce.TableRowNode.static.name = 'tableRow';
30
31 ve.ce.TableRowNode.static.tagName = 'tr';
32
33 /* Methods */
34
35 /**
36 * @inheritdoc
37 */
38 ve.ce.TableRowNode.prototype.onSetup = function () {
39 // Parent method
40 ve.ce.TableRowNode.super.prototype.onSetup.apply( this, arguments );
41
42 this.setupMissingCell();
43 };
44
45 /**
46 * @inheritdoc
47 */
48 ve.ce.TableRowNode.prototype.onSplice = function () {
49 // Parent method
50 ve.ce.TableRowNode.super.prototype.onSplice.apply( this, arguments );
51
52 // Defer call until after other changes in this cycle have been made
53 setTimeout( () => {
54 if ( this.getRoot() ) {
55 // It's possible for this to have been removed from the model in the last tick
56 // This mostly seems to happen during cell merges
57 this.setupMissingCell();
58 }
59 } );
60 };
61
62 /**
63 * Setup a slug for a missing cell, if this row contains fewer cells than the table
64 */
65 ve.ce.TableRowNode.prototype.setupMissingCell = function () {
66 const matrix = this.findParent( ve.ce.TableNode ).getModel().getMatrix(),
67 maxColCount = matrix.getMaxColCount();
68
69 const row = matrix.getRowNodes().indexOf( this.model );
70 if ( maxColCount > matrix.getColCount( row ) ) {
71 if ( !this.$missingCell ) {
72 this.$missingCell = $( '<td>' )
73 .prop( 'contentEditable', 'false' )
74 .addClass( 've-ce-branchNode-slug ve-ce-branchNode-blockSlug ve-ce-tableNode-missingCell' );
75 const slugButton = new ve.ui.NoFocusButtonWidget( {
76 icon: 'add',
77 framed: false
78 } ).on( 'click', this.onMissingCellClick.bind( this ) );
79 this.$missingCell.append( slugButton.$element );
80 }
81 this.$element.append( this.$missingCell );
82 } else {
83 this.removeSlugs();
84 }
85 };
86
87 /**
88 * @inheritdoc
89 */
90 ve.ce.TableRowNode.prototype.removeSlugs = function () {
91 if ( this.$missingCell ) {
92 this.$missingCell.detach();
93 }
94 };
95
96 /**
97 * Handle click events on the missing cell slug
98 *
99 * @param {jQuery.Event} e Click event
100 */
101 ve.ce.TableRowNode.prototype.onMissingCellClick = function () {
102 const surfaceModel = this.getRoot().getSurface().getModel(),
103 documentModel = surfaceModel.getDocument(),
104 tableModel = this.findParent( ve.ce.TableNode ).getModel(),
105 matrix = tableModel.getMatrix();
106
107 // Add a cell onto the end of the row
108 surfaceModel.change(
109 ve.dm.TransactionBuilder.static.newFromInsertion(
110 documentModel, this.getModel().getRange().end,
111 ve.dm.TableCellNode.static.createData()
112 )
113 );
114
115 // Select the newly-inserted cell
116 const row = matrix.getRowNodes().indexOf( this.model );
117 const col = matrix.getColCount( row ) - 1;
118 surfaceModel.setSelection(
119 new ve.dm.TableSelection( tableModel.getOuterRange(), col, row )
120 );
121 };
122
123 /* Registration */
124
125 ve.ce.nodeFactory.register( ve.ce.TableRowNode );