body.js revision 1b08545d7464e74c95a0acc90bc691403b5fb830
View class responsible for rendering the `<tbody>` section of a table. Used as the default `bodyView` for `Y.DataTable.Base` and `Y.DataTable` classes. Translates the provided `modelList` into a rendered `<tbody>` based on the data in the constituent Models, altered or ammended by any special column The `columns` configuration, passed to the constructor, determines which columns will be rendered. The rendering process involves constructing an HTML template for a complete row of data, built by concatenating a customized copy of the instance's `CELL_TEMPLATE` into the `ROW_TEMPLATE` once for each column. This template is then populated with values from each Model in the `modelList`, aggregating a complete HTML string of all row and column data. A `<tbody>` Node is then created from the markup and any column `nodeFormatter`s are applied. Supported properties of the column objects include: * `key` - Used to link a column to an attribute in a Model. * `name` - Used for columns that don't relate to an attribute in the Model (`formatter` or `nodeFormatter` only) if the implementer wants a predictable name to refer to in their CSS. * `cellTemplate` - Overrides the instance's `CELL_TEMPLATE` for cells in this * `formatter` - Used to customize or override the content value from the Model. These do not have access to the cell or row Nodes and should return string (HTML) content. * `nodeFormatter` - Used to provide content for a cell as well as perform any custom modifications on the cell or row Node that could not be performed by `formatter`s. Should be used sparingly for better performance. * `emptyCellValue` - String (HTML) value to use if the Model data for a column, or the content generated by a `formatter`, is the empty string, * `allowHTML` - Set to `true` if a column value, `formatter`, or `emptyCellValue` can contain HTML. This defaults to `false` to protect * `className` - Space delimited CSS classes to add to all `<td>`s in a column. Column `formatter`s are passed an object (`o`) with the following properties: * `value` - The current value of the column's associated attribute, if any. * `data` - An object map of Model keys to their current values. * `record` - The Model instance. * `column` - The column configuration object for the current column. * `className` - Initially empty string to allow `formatter`s to add CSS classes to the cell's `<td>`. * `rowIndex` - The zero-based row number. * `rowClass` - Initially empty string to allow `formatter`s to add CSS classes to the cell's containing row `<tr>`. They may return a value or update `o.value` to assign specific HTML content. A returned value has higher precedence. Column `nodeFormatter`s are passed an object (`o`) with the following * `value` - The current value of the column's associated attribute, if any. * `td` - The `<td>` Node instance. * `cell` - The `<div>` liner Node instance if present, otherwise, the `<td>`. When adding content to the cell, prefer appending into this property. * `data` - An object map of Model keys to their current values. * `record` - The Model instance. * `column` - The column configuration object for the current column. * `rowIndex` - The zero-based row number. They are expected to inject content into the cell's Node directly, including any "empty" cell content. Each `nodeFormatter` will have access through the Node API to all cells and rows in the `<tbody>`, but not to the `<table>`, as it will not be attached yet. If a `nodeFormatter` returns `false`, the `o.td` and `o.cell` Nodes will be `destroy()`ed to remove them from the Node cache and free up memory. The DOM elements will remain as will any content added to them. _It is highly advisable to always return `false` from your `nodeFormatter`s_. // -- Instance properties ------------------------------------------------- HTML template used to create table cells. @default '<td {headers} class="{className}">{content}</td>' CELL_TEMPLATE:
'<td {headers} class="{className}">{content}</td>',
CSS class applied to even rows. This is assigned at instantiation after setting up the `_cssPrefix` for the instance. For DataTable, this will be `yui3-datatable-even`. @default 'yui3-table-even' CSS class applied to odd rows. This is assigned at instantiation after setting up the `_cssPrefix` for the instance. When used by DataTable instances, this will be `yui3-datatable-odd`. @default 'yui3-table-odd' HTML template used to create table rows. @default '<tr id="{rowId}" data-yui3-record="{clientId}" class="{rowClass}">{content}</tr>' ROW_TEMPLATE :
'<tr id="{rowId}" data-yui3-record="{clientId}" class="{rowClass}">{content}</tr>',
The object that serves as the source of truth for column and row data. This property is assigned at instantiation from the `source` property of the configuration object passed to the constructor. @default (initially unset) //TODO: should this be protected? // -- Public methods ------------------------------------------------------ Returns the `<td>` Node from the given row and column index. Alternately, the `seed` can be a Node. If so, the nearest ancestor cell is returned. If the `seed` is a cell, it is returned. If there is no cell at the given coordinates, `null` is returned. Optionally, include an offset array or string to return a cell near the cell identified by the `seed`. The offset can be an array containing the number of rows to shift followed by the number of columns to shift, or one of "above", "below", "next", or "previous". <pre><code>// Previous cell in the previous row var cell = table.getCell(e.target, [-1, -1]); var cell = table.getCell(e.target, 'next'); var cell = table.getCell(e.taregt, [0, 1];</pre></code> @param {Number[]|Node} seed Array of row and column indexes, or a Node that is either the cell itself or a descendant of one. @param {Number[]|String} [shift] Offset by which to identify the returned // TODO this should be a static object map case 'above' :
shift = [-
1,
0];
break;
case 'below' :
shift = [
1,
0];
break;
case 'next' :
shift = [
0,
1];
break;
case 'previous':
shift = [
0, -
1];
break;
Builds a CSS class name from the provided tokens. If the instance is created with `cssPrefix` or `source` in the configuration, it will use this prefix (the `_cssPrefix` of the `source` object) as the base token. This allows class instances to generate markup with class names that correspond to the parent class that is consuming them. @param {String} token* Any number of tokens to include in the class name @return {String} The generated class name Returns the Model associated to the record index (not row index), or to the row Node or id passed. If an id or Node for a child element of the row is passed, that will work, too. If no Model can be found, `null` is returned. @param {Number|String|Node} seed Record index, Node, or identifier for a row Returns the `<tr>` Node from the given row index, Model, or Model's `clientId`. If the rows haven't been rendered yet, or if the row can't be found by the input, `null` is returned. @param {Number|String|Model} id Row index, Model instance, or clientId var tbody =
this.
get(
'container') ||
null;
Creates the table's `<tbody>` content by assembling markup generated by populating the `ROW\_TEMPLATE`, and `CELL\_TEMPLATE` templates with content from the `columns` property and `modelList` attribute. The rendering process happens in three stages: 1. A row template is assembled from the `columns` property (see 2. An HTML string is built up by concatening the application of the data in each Model in the `modelList` to the row template. For cells with `formatter`s, the function is called to generate cell content. Cells with `nodeFormatter`s are ignored. For all other cells, the data value from the Model attribute for the given column key is used. The accumulated row markup is then inserted into the container. 3. If any column is configured with a `nodeFormatter`, the `modelList` is iterated again to apply the `nodeFormatter`s. Supported properties of the column objects include: * `key` - Used to link a column to an attribute in a Model. * `name` - Used for columns that don't relate to an attribute in the Model (`formatter` or `nodeFormatter` only) if the implementer wants a predictable name to refer to in their CSS. * `cellTemplate` - Overrides the instance's `CELL_TEMPLATE` for cells in * `formatter` - Used to customize or override the content value from the Model. These do not have access to the cell or row Nodes and should return string (HTML) content. * `nodeFormatter` - Used to provide content for a cell as well as perform any custom modifications on the cell or row Node that could not be performed by `formatter`s. Should be used sparingly for better * `emptyCellValue` - String (HTML) value to use if the Model data for a column, or the content generated by a `formatter`, is the empty string, * `allowHTML` - Set to `true` if a column value, `formatter`, or `emptyCellValue` can contain HTML. This defaults to `false` to protect * `className` - Space delimited CSS classes to add to all `<td>`s in a Column `formatter`s are passed an object (`o`) with the following * `value` - The current value of the column's associated attribute, if * `data` - An object map of Model keys to their current values. * `record` - The Model instance. * `column` - The column configuration object for the current column. * `className` - Initially empty string to allow `formatter`s to add CSS classes to the cell's `<td>`. * `rowIndex` - The zero-based row number. * `rowClass` - Initially empty string to allow `formatter`s to add CSS classes to the cell's containing row `<tr>`. They may return a value or update `o.value` to assign specific HTML content. A returned value has higher precedence. Column `nodeFormatter`s are passed an object (`o`) with the following * `value` - The current value of the column's associated attribute, if * `td` - The `<td>` Node instance. * `cell` - The `<div>` liner Node instance if present, otherwise, the `<td>`. When adding content to the cell, prefer appending into this * `data` - An object map of Model keys to their current values. * `record` - The Model instance. * `column` - The column configuration object for the current column. * `rowIndex` - The zero-based row number. They are expected to inject content into the cell's Node directly, including any "empty" cell content. Each `nodeFormatter` will have access through the Node API to all cells and rows in the `<tbody>`, but not to the `<table>`, as it will not be attached yet. If a `nodeFormatter` returns `false`, the `o.td` and `o.cell` Nodes will be `destroy()`ed to remove them from the Node cache and free up memory. The DOM elements will remain as will any content added to them. _It is highly advisable to always return `false` from your `nodeFormatter`s_. @return {BodyView} The instance // -- Protected and private methods --------------------------------------- Handles changes in the source's columns attribute. Redraws the table data. @method _afterColumnsChange @param {EventFacade} e The `columnsChange` event object // TODO: Preserve existing DOM // This will involve parsing and comparing the old and new column configs // and reacting to four types of changes: // 1. formatter, nodeFormatter, emptyCellValue changes // 4. column moves (preserve cells) Handles modelList changes, including additions, deletions, and updates. Modifies the existing table DOM accordingly. @param {EventFacade} e The `change` event from the ModelList // Baseline view will just rerender the tbody entirely Reacts to a change in the instance's `modelList` attribute by breaking down the bubbling relationship with the previous `modelList` and setting up that relationship with the new one. @method _afterModelListChange @param {EventFacade} e The `modelListChange` event Iterates the `modelList`, and calls any `nodeFormatter`s found in the `columns` param on the appropriate cell Nodes in the `tbody`. @method _applyNodeFormatters @param {Node} tbody The `<tbody>` Node whose columns to update @param {Object[]} columns The column configurations // Only iterate the ModelList again if there are nodeFormatters // Remove from the Node cache to reduce // memory footprint. This also purges events, // which you shouldn't be scoping to a cell // anyway. You've been warned. Incidentally, // you should always return false. Just sayin. Binds event subscriptions from the UI and the source (if assigned). bind(
'_afterColumnsChange',
this));
[
'modelListChange',
'*:change',
'*:add',
'*:remove',
'*:reset'],
bind(
'_afterDataChange',
this));
The base token for classes created with the `getClassName` method. Iterates the `modelList` and applies each Model to the `_rowTemplate`, allowing any column `formatter` or `emptyCellValue` to override cell content for the appropriate column. The aggregated HTML string is @param {Object[]} columns The column configurations to customize the generated cell content or class names @return {HTML} The markup for all Models in the `modelList`, each applied Applies the data of a given Model, modified by any column formatters and supplemented by other template values to the instance's `_rowTemplate` (see `_createRowTemplate`). The generated string is then returned. The data from Model's attributes is fetched by `toJSON` and this data object is appended with other properties to supply values to {placeholders} in the template. For a template generated from a Model with 'foo' and 'bar' attributes, the data object would end up with the following properties before being used to populate the `_rowTemplate`: * `clientID` - From Model, used the assign the `<tr>`'s 'id' attribute. * `foo` - The value to populate the 'foo' column cell content. This value will be the value stored in the Model's `foo` attribute, or the result of the column's `formatter` if assigned. If the value is '', `null`, or `undefined`, and the column's `emptyCellValue` is assigned, * `bar` - Same for the 'bar' column cell content. * `foo-className` - String of CSS classes to apply to the `<td>`. * `bar-className` - Same. * `rowClass` - String of CSS classes to apply to the `<tr>`. This will be the odd/even class per the specified index plus any additional classes assigned by column formatters (via `o.rowClass`). Because this object is available to formatters, any additional properties can be added to fill in custom {placeholders} in the `_rowTemplate`. @param {Model} model The Model instance to apply to the row template @param {Number} index The index the row will be appearing @return {HTML} The markup for the provided Model, less any `nodeFormatter`s // TODO: look for known formatters by string name // Formatters can either return a value // or update the value property of the data obj passed Creates a custom HTML template string for use in generating the markup for individual table rows with {placeholder}s to capture data from the Models in the `modelList` attribute or from column `formatter`s. Assigns the `_rowTemplate` property. @method _createRowTemplate @param {Object[]} columns Array of column configuration objects // Only include headers if there are more than one ' {' +
token +
'-className}' // Defer all node decoration to the formatter // will unbind the bubble relationship and clear the table if necessary this.
set(
'modelList',
null);
Holds the event subscriptions needing to be detached when the instance is @default undefined (initially unset) Returns the row ID associated with a Model's clientId. @param {String} clientId The Model clientId Map of Model clientIds to row ids. Initializes the instance. Reads the following configuration properties in addition to the instance attributes: * `columns` - (REQUIRED) The initial column information * `cssPrefix` - The base string for classes generated by `getClassName` * `source` - The object to serve as source of truth for column info @param {Object} config Configuration data this.
after(
'modelListChange',
bind(
'_afterModelListChange',
this));
Flattens an array of potentially nested column configurations into a single depth array of data columns. Columns that have children are disregarded in favor of searching their child columns. The resulting array corresponds 1:1 with columns that will contain data in the `<tbody>`. @param {Object[]} data Array of unfiltered column configuration objects @param {Object[]} columns Working array of data columns. Used for recursion. @return {Object[]} Only those columns that will be rendered. if (
typeof col ===
'string') {
The HTML template used to create a full row of markup for a single Model in the `modelList` plus any customizations defined in the column @default (initially unset)