PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/column.js

https://github.com/rhinoman/backgrid
JavaScript | 200 lines | 43 code | 15 blank | 142 comment | 5 complexity | 986f396c470b8c3ed7ad132b0b826571 MD5 | raw file
Possible License(s): MIT
  1. /*
  2. backgrid
  3. http://github.com/wyuenho/backgrid
  4. Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
  5. Licensed under the MIT license.
  6. */
  7. /**
  8. A Column is a placeholder for column metadata.
  9. You usually don't need to create an instance of this class yourself as a
  10. collection of column instances will be created for you from a list of column
  11. attributes in the Backgrid.js view class constructors.
  12. @class Backgrid.Column
  13. @extends Backbone.Model
  14. */
  15. var Column = Backgrid.Column = Backbone.Model.extend({
  16. /**
  17. @cfg {Object} defaults Column defaults. To override any of these default
  18. values, you can either change the prototype directly to override
  19. Column.defaults globally or extend Column and supply the custom class to
  20. Backgrid.Grid:
  21. // Override Column defaults globally
  22. Column.prototype.defaults.sortable = false;
  23. // Override Column defaults locally
  24. var MyColumn = Column.extend({
  25. defaults: _.defaults({
  26. editable: false
  27. }, Column.prototype.defaults)
  28. });
  29. var grid = new Backgrid.Grid(columns: new Columns([{...}, {...}], {
  30. model: MyColumn
  31. }));
  32. @cfg {string} [defaults.name] The default name of the model attribute.
  33. @cfg {string} [defaults.label] The default label to show in the header.
  34. @cfg {string|Backgrid.Cell} [defaults.cell] The default cell type. If this
  35. is a string, the capitalized form will be used to look up a cell class in
  36. Backbone, i.e.: string => StringCell. If a Cell subclass is supplied, it is
  37. initialized with a hash of parameters. If a Cell instance is supplied, it
  38. is used directly.
  39. @cfg {string|Backgrid.HeaderCell} [defaults.headerCell] The default header
  40. cell type.
  41. @cfg {boolean|string} [defaults.sortable=true] Whether this column is
  42. sortable. If the value is a string, a method will the same name will be
  43. looked up from the column instance to determine whether the column should
  44. be sortable. The method's signature must be `function (Backgrid.Column,
  45. Backbone.Model): boolean`.
  46. @cfg {boolean|string} [defaults.editable=true] Whether this column is
  47. editable. If the value is a string, a method will the same name will be
  48. looked up from the column instance to determine whether the column should
  49. be editable. The method's signature must be `function (Backgrid.Column,
  50. Backbone.Model): boolean`.
  51. @cfg {boolean|string} [defaults.renderable=true] Whether this column is
  52. renderable. If the value is a string, a method will the same name will be
  53. looked up from the column instance to determine whether the column should
  54. be renderable. The method's signature must be `function (Backrid.Column,
  55. Backbone.Model): boolean`.
  56. @cfg {Backgrid.CellFormatter | Object | string} [defaults.formatter] The
  57. formatter to use to convert between raw model values and user input.
  58. @cfg {(function(Backbone.Model, string): *) | string} [defaults.sortValue]
  59. The function to use to extract a value from the model for comparison during
  60. sorting. If this value is a string, a method with the same name will be
  61. looked up from the column instance.
  62. */
  63. defaults: {
  64. name: undefined,
  65. label: undefined,
  66. sortable: true,
  67. editable: true,
  68. renderable: true,
  69. formatter: undefined,
  70. sortValue: undefined,
  71. cell: undefined,
  72. headerCell: undefined
  73. },
  74. /**
  75. Initializes this Column instance.
  76. @param {Object} attrs
  77. @param {string} attrs.name The model attribute this column is responsible
  78. for.
  79. @param {string|Backgrid.Cell} attrs.cell The cell type to use to render
  80. this column.
  81. @param {string} [attrs.label]
  82. @param {string|Backgrid.HeaderCell} [attrs.headerCell]
  83. @param {boolean|string} [attrs.sortable=true]
  84. @param {boolean|string} [attrs.editable=true]
  85. @param {boolean|string} [attrs.renderable=true]
  86. @param {Backgrid.CellFormatter | Object | string} [attrs.formatter]
  87. @param {(function(Backbone.Model, string): *) | string} [attrs.sortValue]
  88. @throws {TypeError} If attrs.cell or attrs.options are not supplied.
  89. @throws {ReferenceError} If formatter is a string but a formatter class of
  90. said name cannot be found in the Backgrid module.
  91. See:
  92. - Backgrid.Column.defaults
  93. - Backgrid.Cell
  94. - Backgrid.CellFormatter
  95. */
  96. initialize: function (attrs) {
  97. Backgrid.requireOptions(attrs, ["cell", "name"]);
  98. if (!this.has("label")) {
  99. this.set({ label: this.get("name") }, { silent: true });
  100. }
  101. var headerCell = Backgrid.resolveNameToClass(this.get("headerCell"), "HeaderCell");
  102. var cell = Backgrid.resolveNameToClass(this.get("cell"), "Cell");
  103. this.set({
  104. cell: cell,
  105. headerCell: headerCell
  106. }, { silent: true });
  107. },
  108. /**
  109. @return {function(Backbone.Model, string): *}
  110. */
  111. sortValue: function () {
  112. var sortValue = this.get("sortValue");
  113. if (_.isString(sortValue)) return this[sortValue];
  114. else if (_.isFunction(sortValue)) return sortValue;
  115. return function (model, colName) {
  116. return model.get(colName);
  117. };
  118. }
  119. /**
  120. @member Backgrid.Column
  121. @protected
  122. @method sortable
  123. @return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
  124. */
  125. /**
  126. @member Backgrid.Column
  127. @protected
  128. @method editable
  129. @return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
  130. */
  131. /**
  132. @member Backgrid.Column
  133. @protected
  134. @method renderable
  135. @return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
  136. */
  137. });
  138. _.each(["sortable", "renderable", "editable"], function (key) {
  139. Column.prototype[key] = function () {
  140. var value = this.get(key);
  141. if (_.isString(value)) return this[value];
  142. return !!value;
  143. };
  144. });
  145. /**
  146. A Backbone collection of Column instances.
  147. @class Backgrid.Columns
  148. @extends Backbone.Collection
  149. */
  150. var Columns = Backgrid.Columns = Backbone.Collection.extend({
  151. /**
  152. @property {Backgrid.Column} model
  153. */
  154. model: Column
  155. });