PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/backgrid-0.3.5/src/column.js

https://github.com/kuzbida/backgrid_test
JavaScript | 219 lines | 42 code | 15 blank | 162 comment | 7 complexity | 07399b5f7e3fa7acbeb42f672726736d 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|function(): boolean} [defaults.sortable=true] Whether
  42. this column is sortable. If the value is a string, a method will the same
  43. name will be looked up from the column instance to determine whether the
  44. column should be sortable. The method's signature must be `function
  45. (Backgrid.Column, Backbone.Model): boolean`.
  46. @cfg {boolean|string|function(): boolean} [defaults.editable=true] Whether
  47. this column is editable. If the value is a string, a method will the same
  48. name will be looked up from the column instance to determine whether the
  49. column should be editable. The method's signature must be `function
  50. (Backgrid.Column, Backbone.Model): boolean`.
  51. @cfg {boolean|string|function(): boolean} [defaults.renderable=true]
  52. Whether this column is renderable. If the value is a string, a method will
  53. the same name will be looked up from the column instance to determine
  54. whether the column should be renderable. The method's signature must be
  55. `function (Backrid.Column, 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 {"toggle"|"cycle"} [defaults.sortType="cycle"] Whether sorting will
  59. toggle between ascending and descending order, or cycle between insertion
  60. order, ascending and descending order.
  61. @cfg {(function(Backbone.Model, string): *) | string} [defaults.sortValue]
  62. The function to use to extract a value from the model for comparison during
  63. sorting. If this value is a string, a method with the same name will be
  64. looked up from the column instance.
  65. @cfg {"ascending"|"descending"|null} [defaults.direction=null] The initial
  66. sorting direction for this column. The default is ordered by
  67. Backbone.Model.cid, which usually means the collection is ordered by
  68. insertion order.
  69. */
  70. defaults: {
  71. name: undefined,
  72. label: undefined,
  73. sortable: true,
  74. editable: true,
  75. renderable: true,
  76. formatter: undefined,
  77. sortType: "cycle",
  78. sortValue: undefined,
  79. direction: null,
  80. cell: undefined,
  81. headerCell: undefined
  82. },
  83. /**
  84. Initializes this Column instance.
  85. @param {Object} attrs
  86. @param {string} attrs.name The model attribute this column is responsible
  87. for.
  88. @param {string|Backgrid.Cell} attrs.cell The cell type to use to render
  89. this column.
  90. @param {string} [attrs.label]
  91. @param {string|Backgrid.HeaderCell} [attrs.headerCell]
  92. @param {boolean|string|function(): boolean} [attrs.sortable=true]
  93. @param {boolean|string|function(): boolean} [attrs.editable=true]
  94. @param {boolean|string|function(): boolean} [attrs.renderable=true]
  95. @param {Backgrid.CellFormatter | Object | string} [attrs.formatter]
  96. @param {"toggle"|"cycle"} [attrs.sortType="cycle"]
  97. @param {(function(Backbone.Model, string): *) | string} [attrs.sortValue]
  98. @throws {TypeError} If attrs.cell or attrs.options are not supplied.
  99. @throws {ReferenceError} If formatter is a string but a formatter class of
  100. said name cannot be found in the Backgrid module.
  101. See:
  102. - Backgrid.Column.defaults
  103. - Backgrid.Cell
  104. - Backgrid.CellFormatter
  105. */
  106. initialize: function () {
  107. if (!this.has("label")) {
  108. this.set({ label: this.get("name") }, { silent: true });
  109. }
  110. var headerCell = Backgrid.resolveNameToClass(this.get("headerCell"), "HeaderCell");
  111. var cell = Backgrid.resolveNameToClass(this.get("cell"), "Cell");
  112. this.set({cell: cell, headerCell: headerCell}, { silent: true });
  113. },
  114. /**
  115. Returns an appropriate value extraction function from a model for sorting.
  116. If the column model contains an attribute `sortValue`, if it is a string, a
  117. method from the column instance identifified by the `sortValue` string is
  118. returned. If it is a function, it it returned as is. If `sortValue` isn't
  119. found from the column model's attributes, a default value extraction
  120. function is returned which will compare according to the natural order of
  121. the value's type.
  122. @return {function(Backbone.Model, string): *}
  123. */
  124. sortValue: function () {
  125. var sortValue = this.get("sortValue");
  126. if (_.isString(sortValue)) return this[sortValue];
  127. else if (_.isFunction(sortValue)) return sortValue;
  128. return function (model, colName) {
  129. return model.get(colName);
  130. };
  131. }
  132. /**
  133. @member Backgrid.Column
  134. @protected
  135. @method sortable
  136. @return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
  137. */
  138. /**
  139. @member Backgrid.Column
  140. @protected
  141. @method editable
  142. @return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
  143. */
  144. /**
  145. @member Backgrid.Column
  146. @protected
  147. @method renderable
  148. @return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
  149. */
  150. });
  151. _.each(["sortable", "renderable", "editable"], function (key) {
  152. Column.prototype[key] = function () {
  153. var value = this.get(key);
  154. if (_.isString(value)) return this[value];
  155. else if (_.isFunction(value)) return value;
  156. return !!value;
  157. };
  158. });
  159. /**
  160. A Backbone collection of Column instances.
  161. @class Backgrid.Columns
  162. @extends Backbone.Collection
  163. */
  164. var Columns = Backgrid.Columns = Backbone.Collection.extend({
  165. /**
  166. @property {Backgrid.Column} model
  167. */
  168. model: Column
  169. });