/*! * VisualEditor ContentEditable MWTableNode class. * * @copyright 2011-2016 VisualEditor Team and others; see AUTHORS.txt * @license The MIT License (MIT); see LICENSE.txt */ /** * ContentEditable MW table node. * * @class * @extends ve.ce.TableNode * @mixins ve.ce.ClassAttributeNode * * @constructor * @param {ve.dm.MWTableNode} model Model to observe * @param {Object} [config] Configuration options */ ve.ce.MWTableNode = function VeCeMWTableNode() { // Parent constructor ve.ce.MWTableNode.super.apply( this, arguments ); // Mixin constructors ve.ce.ClassAttributeNode.call( this ); // Events // TODO Also have to hook to any child change (cell merges or style changes), how? this.connect( this, { setup: 'updateSortableHeaders' } ); this.model.connect( this, { attributeChange: 'updateSortableHeaders' } ); // DOM changes this.$element.addClass( 've-ce-mwTableNode' ); }; /* Inheritance */ OO.inheritClass( ve.ce.MWTableNode, ve.ce.TableNode ); OO.mixinClass( ve.ce.MWTableNode, ve.ce.ClassAttributeNode ); /* Static Properties */ ve.ce.MWTableNode.static.name = 'mwTable'; /** * Update sortable headers from attributes. */ ve.ce.MWTableNode.prototype.updateSortableHeaders = function () { if ( this.model.element.attributes.sortable ) { var view, matrix, i, row, longestRowLength, rowLength, headerRows, longestRow, modelNodes, viewNodes; view = this; matrix = this.model.getMatrix(); i = 0; longestRowLength = -1; headerRows = []; while ( i < matrix.getRowCount() ) { /*jshint loopfunc:true */ row = matrix.getRow( i ); if ( row.some( function ( cell ) { return cell.node.getAttribute( 'style' ) !== 'header'; } ) ) { break; } rowLength = OO.unique( row.map( function ( cell ) { return cell.getOwner(); } ) ).length; if ( rowLength >= longestRowLength ) { longestRowLength = rowLength; longestRow = row; } i++; } modelNodes = longestRow.map( function ( cell ) { return cell.getOwner().node; } ); viewNodes = modelNodes.map( function ( cellModel ) { return view.getNodeFromOffset( cellModel.getOffset() - view.model.getOffset() ); } ); $( viewNodes.map( function ( cell ) { return cell.$element[ 0 ]; } ) ) .not( '.unsortable' ) // TODO Do something smarter here .css( 'background', 'red' ); } else { // TODO Remove the styles when table was made not sortable } }; /* Registration */ ve.ce.nodeFactory.register( ve.ce.MWTableNode );