mutable.js revision c25f05e8aa5c555f79e3e8fd68fb2d2850b0d57b
0N/Avar toArray = Y.Array,
726N/A YLang = Y.Lang,
0N/A isString = YLang.isString,
0N/A isArray = YLang.isArray,
0N/A isObject = YLang.isObject,
0N/A isNumber = YLang.isNumber,
0N/A arrayIndex = Y.Array.indexOf,
0N/A Mutable;
0N/A
0N/A/**
0N/AAdds mutation convenience methods to `Y.DataTable` (or other built class).
0N/A
0N/AColumn mutation methods are paired with new custom events:
0N/A
0N/A * addColumn
0N/A * removeColumn
0N/A * modifyColumn
0N/A * moveColumn
0N/A
0N/ARow mutation events are bubbled from the DataTable's `data` ModelList through
0N/Athe DataTable instance.
0N/A
0N/A@module datatable-mutable
0N/A@class DataTable.Mutable
0N/A@for DataTable
0N/A**/
0N/A
0N/AY.namespace('DataTable').Mutable = Mutable = function () {};
0N/A
0N/AY.mix(Mutable.prototype, {
0N/A /**
0N/A Adds the column configuration to the DataTable's `columns` configuration.
0N/A If the `index` parameter is supplied, it is injected at that index. If the
0N/A table has nested headers, inject a subcolumn by passing an array of indexes
0N/A to identify the new column's final location.
0N/A
0N/A The `index` parameter is required if adding a nested column.
0N/A
0N/A This method is a convienience method for fetching the DataTable's `columns`
0N/A attribute, updating it, and calling
0N/A `table.set('columns', _updatedColumnsDefs_)`
617N/A
0N/A @example
0N/A // Becomes last column
0N/A table.addColumn('name');
0N/A
0N/A // Inserted after the current second column, moving the current third column
0N/A // to index 4
0N/A table.addColumn({ key: 'price', formatter: currencyFormatter }, 2 );
0N/A
0N/A // Insert a new column in a set of headers three rows deep. The index array
0N/A // translates to
0N/A // [ 2, -- in the third column's children
0N/A // 1, -- in the second child's children
0N/A // 3 ] -- as the fourth child column
0N/A table.addColumn({ key: 'age', sortable: true }, [ 2, 1, 3 ]);
0N/A
0N/A @method addColumn
0N/A @param {Object|String} config The new column configuration object
0N/A @param {Number|Number[]} [index] the insertion index
0N/A @return {DataTable}
0N/A @chainable
0N/A **/
0N/A addColumn: function (config, index) {
0N/A if (isString(config)) {
0N/A config = { key: config };
0N/A }
0N/A
0N/A if (config) {
0N/A if (arguments.length < 2 || (!isNumber(index) && !isArray(index))) {
0N/A index = this.get('columns').length;
0N/A }
0N/A
0N/A this.fire('addColumn', {
0N/A column: config,
0N/A index: index
0N/A });
0N/A }
0N/A return this;
0N/A },
0N/A
0N/A /**
0N/A Updates an existing column definition. Fires the `modifyColumn` event.
0N/A
0N/A @example
0N/A // Add a formatter to the existing 'price' column definition
0N/A table.modifyColumn('price', { formatter: currencyFormatter });
0N/A
0N/A // Change the label on a header cell in a set of nested headers three rows
0N/A // deep. The index array translates to
0N/A // [ 2, -- in the third column's children
0N/A // 1, -- the second child
0N/A // 3 ] -- the fourth child column
0N/A table.modifyColumn([2, 1, 3], { label: 'Experience' });
0N/A
0N/A @method modifyColumn
0N/A @param {String|Number|Number[]|Object} name The column key, name, index, or
0N/A current configuration object
0N/A @param {Object} config The new column configuration properties
0N/A @return {DataTable}
0N/A @chainable
0N/A **/
0N/A modifyColumn: function (name, config) {
0N/A if (isString(config)) {
0N/A config = { key: config };
0N/A }
0N/A
0N/A if (isObject(config)) {
0N/A this.fire('modifyColumn', {
0N/A column: name,
0N/A newColumnDef: config
0N/A });
0N/A }
0N/A
0N/A return this;
0N/A },
0N/A
0N/A /**
0N/A Moves an existing column to a new location. Fires the `moveColumn` event.
0N/A
0N/A The destination index can be a number or array of numbers to place a column
0N/A header in a nested header row.
0N/A
0N/A @method moveColumn
0N/A @param {String|Number|Number[]|Object} name The column key, name, index, or
0N/A current configuration object
0N/A @param {Number|Number[]} index The destination index of the column
0N/A @return {DataTable}
0N/A @chainable
0N/A **/
0N/A moveColumn: function (name, index) {
0N/A if (name && (isNumber(index) || isArray(index))) {
0N/A this.fire('moveColumn', {
0N/A column: name,
0N/A index: index
0N/A });
0N/A }
0N/A
0N/A return this;
0N/A },
0N/A
0N/A /**
0N/A Removes an existing column. Fires the `removeColumn` event.
0N/A
0N/A @method removeColumn
0N/A @param {String|Number|Number[]|Object} name The column key, name, index, or
0N/A current configuration object
0N/A @return {DataTable}
0N/A @chainable
0N/A **/
0N/A removeColumn: function (name) {
0N/A if (name) {
0N/A this.fire('removeColumn', {
0N/A column: name
0N/A });
0N/A }
0N/A
0N/A return this;
0N/A },
0N/A
0N/A /**
0N/A Adds a new record to the DataTable's `data` ModelList. Record data can be
0N/A an object of field values or an instance of the DataTable's configured
0N/A `recordType` class.
0N/A
0N/A This relays all parameters to the `data` ModelList's `add` method.
0N/A
0N/A @method addRow
0N/A @param {Object} data The data or Model instance for the new record
0N/A @return {DataTable}
0N/A @chainable
0N/A **/
0N/A addRow: function () {
0N/A if (this.data) {
0N/A this.data.add.apply(this.data, arguments);
0N/A }
0N/A
0N/A return this;
0N/A },
0N/A
0N/A /**
0N/A Removes a record from the DataTable's `data` ModelList. The record can be
0N/A provided explicitly or targeted by it's `id` (see ModelList's `getById`
0N/A method), `clientId`, or index in the ModelList.
0N/A
0N/A After locating the target Model, this relays the Model and all other passed
0N/A arguments to the `data` ModelList's `remove` method.
0N/A
0N/A @method removeRow
0N/A @param {Object|String|Number} id The Model instance or identifier
0N/A @return {DataTable}
0N/A @chainable
0N/A **/
0N/A removeRow: function (id) {
0N/A var modelList = this.data,
0N/A model;
0N/A
0N/A // TODO: support removing via DOM element. This should be relayed to View
0N/A if (isObject(id) && id instanceof this.get('recordType')) {
0N/A model = id;
0N/A } else if (modelList && id !== undefined) {
0N/A model = modelList.getById(id) ||
0N/A modelList.getByClientId(id) ||
0N/A modelList.item(id);
0N/A }
0N/A
0N/A if (model) {
0N/A modelList.remove.apply(modelList,
0N/A [model].concat(toArray(arguments, 1, true)));
0N/A }
0N/A
0N/A return this;
0N/A },
0N/A
0N/A /**
0N/A Updates an existing record in the DataTable's `data` ModelList. The record
0N/A can be provided explicitly or targeted by it's `id` (see ModelList's
0N/A `getById` method), `clientId`, or index in the ModelList.
0N/A
0N/A After locating the target Model, this relays the all other passed
0N/A arguments to the Model's `setAttrs` method.
0N/A
0N/A @method modifyRow
0N/A @param {Object|String|Number} id The Model instance or identifier
0N/A @return {DataTable}
0N/A @chainable
0N/A **/
0N/A modifyRow: function (id, data) {
0N/A var modelList = this.data,
0N/A model;
0N/A
0N/A if (isObject(id) && id instanceof this.get('recordType')) {
0N/A model = id;
0N/A } else if (modelList && id !== undefined) {
0N/A model = modelList.getById(id) ||
0N/A modelList.getByClientId(id) ||
0N/A modelList.item(id);
0N/A }
0N/A
0N/A if (model && isObject(data)) {
0N/A model.setAttrs.apply(model, toArray(arguments, 1, true));
0N/A }
0N/A
0N/A return this;
0N/A },
0N/A
617N/A // --------------------------------------------------------------------------
617N/A // Protected properties and methods
0N/A // --------------------------------------------------------------------------
617N/A
617N/A /**
617N/A Default function for the `addColumn` event.
617N/A
617N/A Inserts the specified column at the provided index.
617N/A
617N/A @method _defAddColumnFn
0N/A @param {EventFacade} e The `addColumn` event
0N/A @param {Object} e.column The new column definition object
0N/A @param {Number|Number[]} e.index The array index to insert the new column
0N/A @protected
0N/A **/
0N/A _defAddColumnFn: function (e) {
0N/A var index = toArray(e.index),
0N/A columns = this.get('columns'),
0N/A cols = columns,
0N/A i, len;
0N/A
0N/A for (i = 0, len = index.length - 1; cols && i < len; ++i) {
0N/A cols = cols[index[i]] && cols[index[i]].children;
0N/A }
0N/A
0N/A if (cols) {
0N/A cols.splice(index[i], 0, e.column);
0N/A
0N/A this.set('columns', columns, { originEvent: e });
0N/A } else { Y.log('addColumn index not findable', 'warn', 'datatable');
0N/A }
0N/A },
0N/A
0N/A /**
0N/A Default function for the `modifyColumn` event.
617N/A
617N/A Mixes the new column properties into the specified column definition.
617N/A
617N/A @method _defModifyColumnFn
0N/A @param {EventFacade} e The `modifyColumn` event
617N/A @param {Object|String|Number|Number[]} e.column The column definition object or identifier
617N/A @param {Object} e.newColumnDef The properties to assign to the column
617N/A @protected
0N/A **/
0N/A _defModifyColumnFn: function (e) {
0N/A var columns = this.get('columns'),
0N/A column = this.getColumn(e.column);
0N/A
0N/A if (column) {
0N/A Y.mix(column, e.newColumnDef, true);
0N/A
0N/A this.set('columns', columns, { originEvent: e });
0N/A } else { Y.log('Could not locate column index to modify column', 'warn', 'datatable');
0N/A }
0N/A },
0N/A
0N/A /**
0N/A Default function for the `moveColumn` event.
0N/A
0N/A Removes the specified column from its current location and inserts it at the
0N/A specified array index (may be an array of indexes for nested headers).
0N/A
0N/A @method _defMoveColumnFn
0N/A @param {EventFacade} e The `moveColumn` event
0N/A @param {Object|String|Number|Number[]} e.column The column definition object or identifier
0N/A @param {Object} e.index The destination index to move to
0N/A @protected
0N/A **/
0N/A _defMoveColumnFn: function (e) {
0N/A var columns = this.get('columns'),
0N/A column = this.getColumn(e.column),
0N/A toIndex = toArray(e.index),
0N/A fromCols, fromIndex, toCols, i, len;
0N/A
0N/A if (column) {
0N/A fromCols = column.parent ? column.parent.children : columns;
0N/A fromIndex = arrayIndex(fromCols, column);
0N/A
0N/A if (fromIndex > -1) {
0N/A toCols = columns;
0N/A
0N/A for (i = 0, len = toIndex.length - 1; toCols && i < len; ++i) {
0N/A toCols = toCols[i] && toCols[i].children;
0N/A }
0N/A
0N/A if (toCols) {
0N/A fromCols.splice(fromIndex, 1);
0N/A toCols.splice(toIndex[i], 1, column);
0N/A
0N/A this.set('columns', columns, { originEvent: e });
0N/A } else { Y.log('Column [' + e.column + '] could not be moved. Destination index invalid for moveColumn', 'warn', 'datatable');
0N/A }
0N/A }
0N/A } else { Y.log('Column [' + e.column + '] not found for moveColumn', 'warn', 'datatable');
0N/A }
0N/A },
0N/A
0N/A /**
0N/A Default function for the `removeColumn` event.
0N/A
0N/A Splices the specified column from its containing columns array.
0N/A
0N/A @method _defRemoveColumnFn
0N/A @param {EventFacade} e The `removeColumn` event
0N/A @param {Object|String|Number|Number[]} e.column The column definition object or identifier
0N/A @protected
0N/A **/
0N/A _defRemoveColumnFn: function (e) {
0N/A var columns = this.get('columns'),
0N/A column = this.getColumn(e.column),
0N/A cols, index;
0N/A
0N/A if (column) {
0N/A cols = column.parent ? column.parent.children : columns;
0N/A index = Y.Array.indexOf(cols, column);
0N/A
0N/A if (index > -1) {
0N/A cols.splice(index, 1);
0N/A
0N/A this.set('columns', columns, { originEvent: e });
0N/A }
0N/A } else { Y.log('Could not locate column [' + e.column + '] for removeColumn', 'warn', 'datatable');
0N/A }
0N/A },
0N/A
0N/A /**
0N/A Publishes the events used by the mutation methods:
0N/A
0N/A * addColumn
0N/A * removeColumn
0N/A * modifyColumn
0N/A * moveColumn
0N/A
0N/A @method initializer
0N/A @protected
0N/A **/
0N/A initializer: function () {
0N/A this.publish({
0N/A addColumn: { defaultFn: Y.bind('_defAddColumnFn', this) },
0N/A removeColumn: { defaultFn: Y.bind('_defRemoveColumnFn', this) },
0N/A moveColumn: { defaultFn: Y.bind('_defMoveColumnFn', this) },
0N/A modifyColumn: { defaultFn: Y.bind('_defModifyColumnFn', this) }
0N/A });
0N/A }
0N/A});
0N/A
0N/A/**
0N/AAdds an array of new records to the DataTable's `data` ModelList. Record data
0N/Acan be an array of objects containing field values or an array of instance of
0N/Athe DataTable's configured `recordType` class.
0N/A
0N/AThis relays all parameters to the `data` ModelList's `add` method.
0N/A
0N/ATechnically, this is an alias to `addRow`, but please use the appropriately
0N/Anamed method for readability.
0N/A
0N/A@method addRows
0N/A@param {Object[]} data The data or Model instances to add
0N/A@return {DataTable}
0N/A@chainable
0N/A**/
0N/AMutable.prototype.addRows = Mutable.prototype.addRow;
0N/A
0N/A// Add feature APIs to public Y.DataTable class
0N/Aif (YLang.isFunction(Y.DataTable)) {
0N/A Y.Base.mix(Y.DataTable, [Mutable]);
0N/A}
0N/A
0N/A/**
0N/AFired by the `addColumn` method.
0N/A
0N/A@event addColumn
0N/A@preventable _defAddColumnFn
0N/A@param {Object} column The new column definition object
0N/A@param {Number|Number[]} index The array index to insert the new column
0N/A**/
0N/A
0N/A/**
0N/AFired by the `removeColumn` method.
0N/A
0N/A@event removeColumn
0N/A@preventable _defRemoveColumnFn
0N/A@param {Object|String|Number|Number[]} column The column definition object or identifier
0N/A**/
0N/A
0N/A/**
0N/AFired by the `modifyColumn` method.
0N/A
0N/A@event modifyColumn
0N/A@preventable _defModifyColumnFn
0N/A@param {Object|String|Number|Number[]} column The column definition object or identifier
0N/A@param {Object} newColumnDef The properties to assign to the column
0N/A**/
0N/A
0N/A/**
0N/AFired by the `moveColumn` method.
0N/A
0N/A@event moveColumn
0N/A@preventable _defMoveColumnFn
0N/A@param {Object|String|Number|Number[]} column The column definition object or identifier
0N/A@param {Object} index The destination index to move to
0N/A**/
0N/A
0N/A