PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/row.js

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