PageRenderTime 64ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/extensions/select-all/backgrid-select-all.js

https://github.com/antonyraj/backgrid
JavaScript | 217 lines | 108 code | 29 blank | 80 comment | 21 complexity | dffb2998d7caf686a2507a4812d5b284 MD5 | raw file
Possible License(s): MIT
  1. /*
  2. backgrid-select-all
  3. http://github.com/wyuenho/backgrid
  4. Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
  5. Licensed under the MIT @license.
  6. */
  7. (function (window, $, _, Backbone, Backgrid) {
  8. /**
  9. Renders a checkbox for row selection.
  10. @class Backgrid.Extension.SelectRowCell
  11. @extends Backbone.View
  12. */
  13. var SelectRowCell = Backgrid.Extension.SelectRowCell = Backbone.View.extend({
  14. /** @property */
  15. className: "select-row-cell",
  16. /** @property */
  17. tagName: "td",
  18. /** @property */
  19. events: {
  20. "keydown :checkbox": "onKeydown",
  21. "change :checkbox": "onChange",
  22. "click :checkbox": "enterEditMode"
  23. },
  24. /**
  25. Initializer. If the underlying model triggers a `select` event, this cell
  26. will change its checked value according to the event's `selected` value.
  27. @param {Object} options
  28. @param {Backgrid.Column} options.column
  29. @param {Backbone.Model} options.model
  30. */
  31. initialize: function (options) {
  32. Backgrid.requireOptions(options, ["model", "column"]);
  33. this.column = options.column;
  34. if (!(this.column instanceof Backgrid.Column)) {
  35. this.column = new Backgrid.Column(this.column);
  36. }
  37. this.listenTo(this.model, "backgrid:select", function (model, selected) {
  38. this.$el.find(":checkbox").prop("checked", selected).change();
  39. });
  40. },
  41. /**
  42. Focuses the checkbox.
  43. */
  44. enterEditMode: function () {
  45. this.$el.find(":checkbox").focus();
  46. },
  47. /**
  48. Unfocuses the checkbox.
  49. */
  50. exitEditMode: function () {
  51. this.$el.find(":checkbox").blur();
  52. },
  53. /**
  54. Process keyboard navigation.
  55. */
  56. onKeydown: function (e) {
  57. var command = new Backgrid.Command(e);
  58. if (command.passThru()) return true; // skip ahead to `change`
  59. if (command.cancel()) {
  60. e.stopPropagation();
  61. this.$el.find(":checkbox").blur();
  62. }
  63. else if (command.save() || command.moveLeft() || command.moveRight() ||
  64. command.moveUp() || command.moveDown()) {
  65. e.preventDefault();
  66. e.stopPropagation();
  67. this.model.trigger("backgrid:edited", this.model, this.column, command);
  68. }
  69. },
  70. /**
  71. When the checkbox's value changes, this method will trigger a Backbone
  72. `backgrid:selected` event with a reference of the model and the
  73. checkbox's `checked` value.
  74. */
  75. onChange: function (e) {
  76. var checked = $(e.target).prop('checked');
  77. this.$el.parent().toggleClass('selected', checked);
  78. this.model.trigger("backgrid:selected", this.model, checked);
  79. },
  80. /**
  81. Renders a checkbox in a table cell.
  82. */
  83. render: function () {
  84. this.$el.empty().append('<input tabindex="-1" type="checkbox" />');
  85. this.delegateEvents();
  86. return this;
  87. }
  88. });
  89. /**
  90. Renders a checkbox to select all rows on the current page.
  91. @class Backgrid.Extension.SelectAllHeaderCell
  92. @extends Backgrid.Extension.SelectRowCell
  93. */
  94. var SelectAllHeaderCell = Backgrid.Extension.SelectAllHeaderCell = SelectRowCell.extend({
  95. /** @property */
  96. className: "select-all-header-cell",
  97. /** @property */
  98. tagName: "th",
  99. /**
  100. Initializer. When this cell's checkbox is checked, a Backbone
  101. `backgrid:select` event will be triggered for each model for the current
  102. page in the underlying collection. If a `SelectRowCell` instance exists
  103. for the rows representing the models, they will check themselves. If any
  104. of the SelectRowCell instances trigger a Backbone `backgrid:selected`
  105. event with a `false` value, this cell will uncheck its checkbox. In the
  106. event of a Backbone `backgrid:refresh` event, which is triggered when the
  107. body refreshes its rows, which can happen under a number of conditions
  108. such as paging or the columns were reset, this cell will still remember
  109. the previously selected models and trigger a Backbone `backgrid:select`
  110. event on them such that the SelectRowCells can recheck themselves upon
  111. refreshing.
  112. @param {Object} options
  113. @param {Backgrid.Column} options.column
  114. @param {Backbone.Collection} options.collection
  115. */
  116. initialize: function (options) {
  117. Backgrid.requireOptions(options, ["column", "collection"]);
  118. this.column = options.column;
  119. if (!(this.column instanceof Backgrid.Column)) {
  120. this.column = new Backgrid.Column(this.column);
  121. }
  122. var collection = this.collection;
  123. var selectedModels = this.selectedModels = {};
  124. this.listenTo(collection, "backgrid:selected", function (model, selected) {
  125. if (selected) selectedModels[model.id || model.cid] = model;
  126. else {
  127. delete selectedModels[model.id || model.cid];
  128. this.$el.find(":checkbox").prop("checked", false);
  129. }
  130. });
  131. this.listenTo(collection, "remove", function (model) {
  132. delete selectedModels[model.id || model.cid];
  133. });
  134. this.listenTo(collection, "backgrid:refresh", function () {
  135. this.$el.find(":checkbox").prop("checked", false);
  136. for (var i = 0; i < collection.length; i++) {
  137. var model = collection.at(i);
  138. if (selectedModels[model.id || model.cid]) {
  139. model.trigger('backgrid:select', model, true);
  140. }
  141. }
  142. });
  143. },
  144. /**
  145. Progagates the checked value of this checkbox to all the models of the
  146. underlying collection by triggering a Backbone `backgrid:select` event on
  147. the models themselves, passing each model and the current `checked` value
  148. of the checkbox in each event.
  149. */
  150. onChange: function (e) {
  151. var checked = $(e.target).prop("checked");
  152. var collection = this.collection;
  153. collection.each(function (model) {
  154. model.trigger("backgrid:select", model, checked);
  155. });
  156. }
  157. });
  158. /**
  159. Convenient method to retrieve a list of selected models. This method only
  160. exists when the `SelectAll` extension has been included.
  161. @member Backgrid.Grid
  162. @return {Array.<Backbone.Model>}
  163. */
  164. Backgrid.Grid.prototype.getSelectedModels = function () {
  165. var selectAllHeaderCell;
  166. var headerCells = this.header.row.cells;
  167. for (var i = 0, l = headerCells.length; i < l; i++) {
  168. var headerCell = headerCells[i];
  169. if (headerCell instanceof SelectAllHeaderCell) {
  170. selectAllHeaderCell = headerCell;
  171. break;
  172. }
  173. }
  174. var result = [];
  175. if (selectAllHeaderCell) {
  176. for (var modelId in selectAllHeaderCell.selectedModels) {
  177. result.push(this.collection.get(modelId));
  178. }
  179. }
  180. return result;
  181. };
  182. }(window, jQuery, _, Backbone, Backgrid));