head.js revision e0a1afd287c016be5722249b10ba81684f33886b
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav GlassView class responsible for rendering the `<thead>` section of a table. Used as
ad598bab533c3f4f0bdb0f536bcf5e0ac0769d15Dav Glassthe default `headerView` for `Y.DataTable.Base` and `Y.DataTable` classes.
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav GlassTranslates the provided array of column configuration objects into a rendered
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glass`<thead>` based on the data in those objects.
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav GlassThe structure of the column data is expected to be a single array of objects,
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glasswhere each object corresponds to a `<th>`. Those objects may contain a
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glass`children` property containing a similarly structured array to indicate the
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glassnested cells should be grouped under the parent column's colspan in a separate
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glassrow of header cells. E.g.
482da2d388e0f999f372383039af944e21bc717bDav Glassnew Y.DataTable.HeaderView({
482da2d388e0f999f372383039af944e21bc717bDav Glass container: tableNode,
482da2d388e0f999f372383039af944e21bc717bDav Glass { key: 'id' }, // no nesting
713a27b6ab5ab057e9b4ab551617b54730e8906eDav Glass { key: 'name', children: [
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glass { key: 'firstName', label: 'First' },
6cd85fc002ad88c48179730400dc0bc36a7a9f5bDav Glass { key: 'lastName', label: 'Last' } ] }
f05c0eb9de3e3021b733c24fb7d17ba9ae093272Dav Glass</code></pre>
f05c0eb9de3e3021b733c24fb7d17ba9ae093272Dav GlassThis would translate to the following visualization:
f05c0eb9de3e3021b733c24fb7d17ba9ae093272Dav Glass---------------------
3b3bd34996b581ab171be1465201869cb009dbadDav Glass| |---------------
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glass| id | First | Last |
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glass---------------------
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav GlassSupported properties of the column objects include:
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glass * `label` - The HTML content of the header cell.
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glass * `key` - If `label` is not specified, the `key` is used for content.
6cd85fc002ad88c48179730400dc0bc36a7a9f5bDav Glass * `children` - Array of columns to appear below this column in the next
3b3bd34996b581ab171be1465201869cb009dbadDav Glass * `abbr` - The content of the 'abbr' attribute of the `<th>`
3b3bd34996b581ab171be1465201869cb009dbadDav GlassThrough the life of instantiation and rendering, the column objects will have
3b3bd34996b581ab171be1465201869cb009dbadDav Glassthe following properties added to them:
482da2d388e0f999f372383039af944e21bc717bDav Glass * `colspan` - To supply the `<th>` attribute
3b3bd34996b581ab171be1465201869cb009dbadDav Glass * `rowspan` - To supply the `<th>` attribute
3b3bd34996b581ab171be1465201869cb009dbadDav Glass * `parent` - If the column is a child of another column, this points to
3b3bd34996b581ab171be1465201869cb009dbadDav Glass its parent column
3b3bd34996b581ab171be1465201869cb009dbadDav Glass * `_yuid` - A unique YUI generated id used as the `<th>`'s 'id' for
482da2d388e0f999f372383039af944e21bc717bDav Glass reference in the data `<td>`'s 'headers' attribute.
3b3bd34996b581ab171be1465201869cb009dbadDav GlassThe column object is also used to provide values for {placeholder} tokens in the
3b3bd34996b581ab171be1465201869cb009dbadDav Glassinstance's `CELL_TEMPLATE`, so you can modify the template and include other
482da2d388e0f999f372383039af944e21bc717bDav Glasscolumn object properties to populate them.
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass@module datatable-head
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass@class HeaderView
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass@namespace DataTable
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass@extends View
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav GlassY.namespace('DataTable').HeaderView = Y.Base.create('tableHeader', Y.View, [], {
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass // -- Instance properties -------------------------------------------------
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass Template used to create the table's header cell markup. Override this to
713a27b6ab5ab057e9b4ab551617b54730e8906eDav Glass customize how these cells' markup is created.
713a27b6ab5ab057e9b4ab551617b54730e8906eDav Glass @property CELL_TEMPLATE
a201dfca71f32a095917fac03611f2f999cb3b97Dav Glass @type {HTML}
713a27b6ab5ab057e9b4ab551617b54730e8906eDav Glass @default '<th id="{_yuid}" abbr="{abbr} colspan="{colspan}" rowspan="{rowspan}"><div class="{linerClass}">{content}</div></th>'
482da2d388e0f999f372383039af944e21bc717bDav Glass '<th id="{_yuid}" abbr="{abbr}" ' +
3b3bd34996b581ab171be1465201869cb009dbadDav Glass 'colspan="{colspan}" rowspan="{rowspan}">' +
3b3bd34996b581ab171be1465201869cb009dbadDav Glass '<div class="{linerClass}">' +
3b3bd34996b581ab171be1465201869cb009dbadDav Glass '{content}' +
3b3bd34996b581ab171be1465201869cb009dbadDav Glass The data representation of the header rows to render. This is assigned by
482da2d388e0f999f372383039af944e21bc717bDav Glass parsing the `columns` configuration array, and is used by the render()
3b3bd34996b581ab171be1465201869cb009dbadDav Glass @property columns
3b3bd34996b581ab171be1465201869cb009dbadDav Glass @type {Array[]}
3b3bd34996b581ab171be1465201869cb009dbadDav Glass @default (initially unset)
3b3bd34996b581ab171be1465201869cb009dbadDav Glass //TODO: should this be protected?
3b3bd34996b581ab171be1465201869cb009dbadDav Glass //columns: null,
3b3bd34996b581ab171be1465201869cb009dbadDav Glass The object that serves as the source of truth for column and row data.
3b3bd34996b581ab171be1465201869cb009dbadDav Glass This property is assigned at instantiation from the `source` property of
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass the configuration object passed to the constructor.
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass @property source
6cd85fc002ad88c48179730400dc0bc36a7a9f5bDav Glass @type {Object}
713a27b6ab5ab057e9b4ab551617b54730e8906eDav Glass @default (initially unset)
713a27b6ab5ab057e9b4ab551617b54730e8906eDav Glass //TODO: should this be protected?
3b3bd34996b581ab171be1465201869cb009dbadDav Glass //source: null,
482da2d388e0f999f372383039af944e21bc717bDav Glass Template used to create the table's header row markup. Override this to
482da2d388e0f999f372383039af944e21bc717bDav Glass customize the row markup.
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glass @property ROW_TEMPLATE
482da2d388e0f999f372383039af944e21bc717bDav Glass @type {HTML}
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glass @default '<tr>{content}</tr>'
482da2d388e0f999f372383039af944e21bc717bDav Glass '<tr>{content}</tr>',
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass Template used to create the table's thead markup.
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass @property THEAD_TEMPLATE
6cd85fc002ad88c48179730400dc0bc36a7a9f5bDav Glass @type {HTML}
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass @default '<thead class="{classes}">{content}</thead>'
482da2d388e0f999f372383039af944e21bc717bDav Glass '<thead class="{classes}">{content}</thead>',
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glass // -- Public methods ------------------------------------------------------
4d589fe0c1bf5d088a90cdf29f3bc777942006cbDav Glass Destroys the instance.
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass @method destructor
6cd85fc002ad88c48179730400dc0bc36a7a9f5bDav Glass destructor: function () {
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass Builds a CSS class name from the provided tokens. If the instance is
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass created with `cssPrefix` or `source` in the configuration, it will use this
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass prefix (the `\_cssPrefix` of the `source` object) as the base token. This
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass allows class instances to generate markup with class names that correspond
f05c0eb9de3e3021b733c24fb7d17ba9ae093272Dav Glass to the parent class that is consuming them.
3b3bd34996b581ab171be1465201869cb009dbadDav Glass @method getClassName
3b3bd34996b581ab171be1465201869cb009dbadDav Glass @param {String} token* Any number of tokens to include in the class name
6cd85fc002ad88c48179730400dc0bc36a7a9f5bDav Glass @return {String} The generated class name
eba99cf5219728ad7993f0b1ed3b75f7fdb78193Dav Glass getClassName: function () {
3b3bd34996b581ab171be1465201869cb009dbadDav Glass return _getClassName.apply(ClassNameManager, args);
3b3bd34996b581ab171be1465201869cb009dbadDav Glass Creates the `<thead>` Node by assembling markup generated by populating the
3b3bd34996b581ab171be1465201869cb009dbadDav Glass `THEAD\_TEMPLATE`, `ROW\_TEMPLATE`, and `CELL\_TEMPLATE` templates with
3b3bd34996b581ab171be1465201869cb009dbadDav Glass content from the `columns` property.
3b3bd34996b581ab171be1465201869cb009dbadDav Glass @method render
482da2d388e0f999f372383039af944e21bc717bDav Glass @return {HeaderView} The instance
3b3bd34996b581ab171be1465201869cb009dbadDav Glass render: function () {
3b3bd34996b581ab171be1465201869cb009dbadDav Glass // TODO: remove dependence on this.source
482da2d388e0f999f372383039af944e21bc717bDav Glass Y.log('Could not render thead. Table not provided', 'warn');
482da2d388e0f999f372383039af944e21bc717bDav Glass return this;
482da2d388e0f999f372383039af944e21bc717bDav Glass existing = table.one('> .' + this.getClassName('columns'));
482da2d388e0f999f372383039af944e21bc717bDav Glass replace = existing && (!thead || !thead.compareTo(existing));
482da2d388e0f999f372383039af944e21bc717bDav Glass for (j = 0, jlen = columns[i].length; j < jlen; ++j) {
482da2d388e0f999f372383039af944e21bc717bDav Glass table.insertBefore(thead, table.one('> tfoot, > tbody'));
482da2d388e0f999f372383039af944e21bc717bDav Glass return this;
482da2d388e0f999f372383039af944e21bc717bDav Glass // -- Protected and private properties and methods ------------------------
482da2d388e0f999f372383039af944e21bc717bDav Glass The base token for classes created with the `getClassName` method.
482da2d388e0f999f372383039af944e21bc717bDav Glass @property _cssPrefix
482da2d388e0f999f372383039af944e21bc717bDav Glass @type {String}
482da2d388e0f999f372383039af944e21bc717bDav Glass @default 'yui3-table'
482da2d388e0f999f372383039af944e21bc717bDav Glass _cssPrefix: ClassNameManager.getClassName('table'),
482da2d388e0f999f372383039af944e21bc717bDav Glass Handles changes in the source's columns attribute. Redraws the headers.
482da2d388e0f999f372383039af944e21bc717bDav Glass @method _afterColumnChange
482da2d388e0f999f372383039af944e21bc717bDav Glass @param {EventFacade} e The `columnsChange` event object
482da2d388e0f999f372383039af944e21bc717bDav Glass _afterColumnChange: function (e) {
482da2d388e0f999f372383039af944e21bc717bDav Glass Binds event subscriptions from the UI and the source (if assigned).
482da2d388e0f999f372383039af944e21bc717bDav Glass @method bindUI
482da2d388e0f999f372383039af944e21bc717bDav Glass bindUI: function () {
6cd85fc002ad88c48179730400dc0bc36a7a9f5bDav Glass // TODO: How best to decouple this?
6cd85fc002ad88c48179730400dc0bc36a7a9f5bDav Glass this.source.after('columnsChange', this._afterColumnsChange));
3b3bd34996b581ab171be1465201869cb009dbadDav Glass Initializes the instance. Reads the following configuration properties:
3b3bd34996b581ab171be1465201869cb009dbadDav Glass * `columns` - (REQUIRED) The initial column information
3b3bd34996b581ab171be1465201869cb009dbadDav Glass * `cssPrefix` - The base string for classes generated by `getClassName`
3b3bd34996b581ab171be1465201869cb009dbadDav Glass * `source` - The object to serve as source of truth for column info
3b3bd34996b581ab171be1465201869cb009dbadDav Glass @method initializer
3b3bd34996b581ab171be1465201869cb009dbadDav Glass @param {Object} config Configuration data
3b3bd34996b581ab171be1465201869cb009dbadDav Glass var cssPrefix = config.cssPrefix || (config.source || {}).cssPrefix;
3b3bd34996b581ab171be1465201869cb009dbadDav Glass this.columns = this._parseColumns(config.columns);
3b3bd34996b581ab171be1465201869cb009dbadDav Glass Translate the input column format into a structure useful for rendering a
3b3bd34996b581ab171be1465201869cb009dbadDav Glass `<thead>`, rows, and cells. The structure of the input is expected to be a
3b3bd34996b581ab171be1465201869cb009dbadDav Glass single array of objects, where each object corresponds to a `<th>`. Those
3b3bd34996b581ab171be1465201869cb009dbadDav Glass objects may contain a `children` property containing a similarly structured
3b3bd34996b581ab171be1465201869cb009dbadDav Glass array to indicate the nested cells should be grouped under the parent
6cd85fc002ad88c48179730400dc0bc36a7a9f5bDav Glass column's colspan in a separate row of header cells. E.g.
var columns = [],
stack = [],
if (i >= len) {
if (i >= len) {
return columns;