/src/column.js
JavaScript | 200 lines | 43 code | 15 blank | 142 comment | 5 complexity | 986f396c470b8c3ed7ad132b0b826571 MD5 | raw file
Possible License(s): MIT
- /*
- backgrid
- http://github.com/wyuenho/backgrid
- Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
- Licensed under the MIT license.
- */
- /**
- A Column is a placeholder for column metadata.
- You usually don't need to create an instance of this class yourself as a
- collection of column instances will be created for you from a list of column
- attributes in the Backgrid.js view class constructors.
- @class Backgrid.Column
- @extends Backbone.Model
- */
- var Column = Backgrid.Column = Backbone.Model.extend({
- /**
- @cfg {Object} defaults Column defaults. To override any of these default
- values, you can either change the prototype directly to override
- Column.defaults globally or extend Column and supply the custom class to
- Backgrid.Grid:
- // Override Column defaults globally
- Column.prototype.defaults.sortable = false;
- // Override Column defaults locally
- var MyColumn = Column.extend({
- defaults: _.defaults({
- editable: false
- }, Column.prototype.defaults)
- });
- var grid = new Backgrid.Grid(columns: new Columns([{...}, {...}], {
- model: MyColumn
- }));
- @cfg {string} [defaults.name] The default name of the model attribute.
- @cfg {string} [defaults.label] The default label to show in the header.
- @cfg {string|Backgrid.Cell} [defaults.cell] The default cell type. If this
- is a string, the capitalized form will be used to look up a cell class in
- Backbone, i.e.: string => StringCell. If a Cell subclass is supplied, it is
- initialized with a hash of parameters. If a Cell instance is supplied, it
- is used directly.
- @cfg {string|Backgrid.HeaderCell} [defaults.headerCell] The default header
- cell type.
- @cfg {boolean|string} [defaults.sortable=true] Whether this column is
- sortable. If the value is a string, a method will the same name will be
- looked up from the column instance to determine whether the column should
- be sortable. The method's signature must be `function (Backgrid.Column,
- Backbone.Model): boolean`.
- @cfg {boolean|string} [defaults.editable=true] Whether this column is
- editable. If the value is a string, a method will the same name will be
- looked up from the column instance to determine whether the column should
- be editable. The method's signature must be `function (Backgrid.Column,
- Backbone.Model): boolean`.
- @cfg {boolean|string} [defaults.renderable=true] Whether this column is
- renderable. If the value is a string, a method will the same name will be
- looked up from the column instance to determine whether the column should
- be renderable. The method's signature must be `function (Backrid.Column,
- Backbone.Model): boolean`.
- @cfg {Backgrid.CellFormatter | Object | string} [defaults.formatter] The
- formatter to use to convert between raw model values and user input.
- @cfg {(function(Backbone.Model, string): *) | string} [defaults.sortValue]
- The function to use to extract a value from the model for comparison during
- sorting. If this value is a string, a method with the same name will be
- looked up from the column instance.
- */
- defaults: {
- name: undefined,
- label: undefined,
- sortable: true,
- editable: true,
- renderable: true,
- formatter: undefined,
- sortValue: undefined,
- cell: undefined,
- headerCell: undefined
- },
- /**
- Initializes this Column instance.
- @param {Object} attrs
- @param {string} attrs.name The model attribute this column is responsible
- for.
- @param {string|Backgrid.Cell} attrs.cell The cell type to use to render
- this column.
- @param {string} [attrs.label]
- @param {string|Backgrid.HeaderCell} [attrs.headerCell]
- @param {boolean|string} [attrs.sortable=true]
- @param {boolean|string} [attrs.editable=true]
- @param {boolean|string} [attrs.renderable=true]
- @param {Backgrid.CellFormatter | Object | string} [attrs.formatter]
- @param {(function(Backbone.Model, string): *) | string} [attrs.sortValue]
- @throws {TypeError} If attrs.cell or attrs.options are not supplied.
- @throws {ReferenceError} If formatter is a string but a formatter class of
- said name cannot be found in the Backgrid module.
- See:
- - Backgrid.Column.defaults
- - Backgrid.Cell
- - Backgrid.CellFormatter
- */
- initialize: function (attrs) {
- Backgrid.requireOptions(attrs, ["cell", "name"]);
- if (!this.has("label")) {
- this.set({ label: this.get("name") }, { silent: true });
- }
- var headerCell = Backgrid.resolveNameToClass(this.get("headerCell"), "HeaderCell");
- var cell = Backgrid.resolveNameToClass(this.get("cell"), "Cell");
- this.set({
- cell: cell,
- headerCell: headerCell
- }, { silent: true });
- },
- /**
- @return {function(Backbone.Model, string): *}
- */
- sortValue: function () {
- var sortValue = this.get("sortValue");
- if (_.isString(sortValue)) return this[sortValue];
- else if (_.isFunction(sortValue)) return sortValue;
- return function (model, colName) {
- return model.get(colName);
- };
- }
- /**
- @member Backgrid.Column
- @protected
- @method sortable
- @return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
- */
- /**
- @member Backgrid.Column
- @protected
- @method editable
- @return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
- */
- /**
- @member Backgrid.Column
- @protected
- @method renderable
- @return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
- */
- });
- _.each(["sortable", "renderable", "editable"], function (key) {
- Column.prototype[key] = function () {
- var value = this.get(key);
- if (_.isString(value)) return this[value];
- return !!value;
- };
- });
- /**
- A Backbone collection of Column instances.
- @class Backgrid.Columns
- @extends Backbone.Collection
- */
- var Columns = Backgrid.Columns = Backbone.Collection.extend({
- /**
- @property {Backgrid.Column} model
- */
- model: Column
- });