PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/row.js

https://github.com/antonyraj/backgrid
JavaScript | 179 lines | 88 code | 28 blank | 63 comment | 15 complexity | 079e954ea730f4b335fadedf3b810840 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. Row is a simple container view that takes a model instance and a list of
  9. column metadata describing how each of the model's attribute is to be
  10. rendered, and apply the appropriate cell to each attribute.
  11. @class Backgrid.Row
  12. @extends Backbone.View
  13. */
  14. var Row = Backgrid.Row = Backbone.View.extend({
  15. /** @property */
  16. tagName: "tr",
  17. requiredOptions: ["columns", "model"],
  18. /**
  19. Initializes a row view instance.
  20. @param {Object} options
  21. @param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns Column metadata.
  22. @param {Backbone.Model} options.model The model instance to render.
  23. @throws {TypeError} If options.columns or options.model is undefined.
  24. */
  25. initialize: function (options) {
  26. Backgrid.requireOptions(options, this.requiredOptions);
  27. var columns = this.columns = options.columns;
  28. if (!(columns instanceof Backbone.Collection)) {
  29. columns = this.columns = new Columns(columns);
  30. }
  31. var cells = this.cells = [];
  32. for (var i = 0; i < columns.length; i++) {
  33. cells.push(this.makeCell(columns.at(i), options));
  34. }
  35. this.listenTo(columns, "change:renderable", function (column, renderable) {
  36. for (var i = 0; i < cells.length; i++) {
  37. var cell = cells[i];
  38. if (cell.column.get("name") == column.get("name")) {
  39. if (renderable) cell.$el.show(); else cell.$el.hide();
  40. }
  41. }
  42. });
  43. this.listenTo(columns, "add", function (column, columns) {
  44. var i = columns.indexOf(column);
  45. var cell = this.makeCell(column, options);
  46. cells.splice(i, 0, cell);
  47. var renderable = Backgrid.callByNeed(column.get("renderable"), column, cell.model);
  48. if (!renderable) cell.$el.hide();
  49. var $el = this.$el;
  50. if (i === 0) {
  51. $el.prepend(cell.render().$el);
  52. }
  53. else if (i === columns.length - 1) {
  54. $el.append(cell.render().$el);
  55. }
  56. else {
  57. $el.children().eq(i).before(cell.render().$el);
  58. }
  59. });
  60. this.listenTo(columns, "remove", function (column, columns, opts) {
  61. cells[opts.index].remove();
  62. cells.splice(opts.index, 1);
  63. });
  64. },
  65. /**
  66. Factory method for making a cell. Used by #initialize internally. Override
  67. this to provide an appropriate cell instance for a custom Row subclass.
  68. @protected
  69. @param {Backgrid.Column} column
  70. @param {Object} options The options passed to #initialize.
  71. @return {Backgrid.Cell}
  72. */
  73. makeCell: function (column) {
  74. return new (column.get("cell"))({
  75. column: column,
  76. model: this.model
  77. });
  78. },
  79. /**
  80. Renders a row of cells for this row's model.
  81. */
  82. render: function () {
  83. this.$el.empty();
  84. var fragment = document.createDocumentFragment();
  85. for (var i = 0; i < this.cells.length; i++) {
  86. var cell = this.cells[i], column = cell.column;
  87. fragment.appendChild(cell.render().el);
  88. var renderable = Backgrid.callByNeed(column.get("renderable"), column, cell.model);
  89. if (!renderable) cell.$el.hide();
  90. }
  91. this.el.appendChild(fragment);
  92. this.delegateEvents();
  93. return this;
  94. },
  95. /**
  96. Clean up this row and its cells.
  97. @chainable
  98. */
  99. remove: function () {
  100. for (var i = 0; i < this.cells.length; i++) {
  101. var cell = this.cells[i];
  102. cell.remove.apply(cell, arguments);
  103. }
  104. return Backbone.View.prototype.remove.apply(this, arguments);
  105. }
  106. });
  107. /**
  108. EmptyRow is a simple container view that takes a list of column and render a
  109. row with a single column.
  110. @class Backgrid.EmptyRow
  111. @extends Backbone.View
  112. */
  113. var EmptyRow = Backgrid.EmptyRow = Backbone.View.extend({
  114. /** @property */
  115. tagName: "tr",
  116. /** @property */
  117. emptyText: null,
  118. /**
  119. Initializer.
  120. @param {Object} options
  121. @param {string} options.emptyText
  122. @param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns Column metadata.
  123. */
  124. initialize: function (options) {
  125. Backgrid.requireOptions(options, ["emptyText", "columns"]);
  126. this.emptyText = options.emptyText;
  127. this.columns = options.columns;
  128. },
  129. /**
  130. Renders an empty row.
  131. */
  132. render: function () {
  133. this.$el.empty();
  134. var td = document.createElement("td");
  135. td.setAttribute("colspan", this.columns.length);
  136. td.textContent = this.emptyText;
  137. this.el.setAttribute("class", "empty");
  138. this.el.appendChild(td);
  139. return this;
  140. }
  141. });