PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/backgrid-0.3.5/src/header.js

https://github.com/kuzbida/backgrid_test
JavaScript | 227 lines | 109 code | 30 blank | 88 comment | 23 complexity | 412adcf33b7002fed5d02eb78c560860 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. HeaderCell is a special cell class that renders a column header cell. If the
  9. column is sortable, a sorter is also rendered and will trigger a table
  10. refresh after sorting.
  11. @class Backgrid.HeaderCell
  12. @extends Backbone.View
  13. */
  14. var HeaderCell = Backgrid.HeaderCell = Backbone.View.extend({
  15. /** @property */
  16. tagName: "th",
  17. /** @property */
  18. events: {
  19. "click a": "onClick"
  20. },
  21. /**
  22. Initializer.
  23. @param {Object} options
  24. @param {Backgrid.Column|Object} options.column
  25. @throws {TypeError} If options.column or options.collection is undefined.
  26. */
  27. initialize: function (options) {
  28. this.column = options.column;
  29. if (!(this.column instanceof Column)) {
  30. this.column = new Column(this.column);
  31. }
  32. var column = this.column, collection = this.collection, $el = this.$el;
  33. this.listenTo(column, "change:editable change:sortable change:renderable",
  34. function (column) {
  35. var changed = column.changedAttributes();
  36. for (var key in changed) {
  37. if (changed.hasOwnProperty(key)) {
  38. $el.toggleClass(key, changed[key]);
  39. }
  40. }
  41. });
  42. this.listenTo(column, "change:direction", this.setCellDirection);
  43. this.listenTo(column, "change:name change:label", this.render);
  44. if (Backgrid.callByNeed(column.editable(), column, collection)) $el.addClass("editable");
  45. if (Backgrid.callByNeed(column.sortable(), column, collection)) $el.addClass("sortable");
  46. if (Backgrid.callByNeed(column.renderable(), column, collection)) $el.addClass("renderable");
  47. this.listenTo(collection.fullCollection || collection, "sort", this.removeCellDirection);
  48. },
  49. /**
  50. Event handler for the collection's `sort` event. Removes all the CSS
  51. direction classes.
  52. */
  53. removeCellDirection: function () {
  54. this.$el.removeClass("ascending").removeClass("descending");
  55. this.column.set("direction", null);
  56. },
  57. /**
  58. Event handler for the column's `change:direction` event. If this
  59. HeaderCell's column is being sorted on, it applies the direction given as a
  60. CSS class to the header cell. Removes all the CSS direction classes
  61. otherwise.
  62. */
  63. setCellDirection: function (column, direction) {
  64. this.$el.removeClass("ascending").removeClass("descending");
  65. if (column.cid == this.column.cid) this.$el.addClass(direction);
  66. },
  67. /**
  68. Event handler for the `click` event on the cell's anchor. If the column is
  69. sortable, clicking on the anchor will cycle through 3 sorting orderings -
  70. `ascending`, `descending`, and default.
  71. */
  72. onClick: function (e) {
  73. e.preventDefault();
  74. var column = this.column;
  75. var collection = this.collection;
  76. var event = "backgrid:sort";
  77. function cycleSort(header, col) {
  78. if (column.get("direction") === "ascending") collection.trigger(event, col, "descending");
  79. else if (column.get("direction") === "descending") collection.trigger(event, col, null);
  80. else collection.trigger(event, col, "ascending");
  81. }
  82. function toggleSort(header, col) {
  83. if (column.get("direction") === "ascending") collection.trigger(event, col, "descending");
  84. else collection.trigger(event, col, "ascending");
  85. }
  86. var sortable = Backgrid.callByNeed(column.sortable(), column, this.collection);
  87. if (sortable) {
  88. var sortType = column.get("sortType");
  89. if (sortType === "toggle") toggleSort(this, column);
  90. else cycleSort(this, column);
  91. }
  92. },
  93. /**
  94. Renders a header cell with a sorter, a label, and a class name for this
  95. column.
  96. */
  97. render: function () {
  98. this.$el.empty();
  99. var column = this.column;
  100. var sortable = Backgrid.callByNeed(column.sortable(), column, this.collection);
  101. var label;
  102. if(sortable){
  103. label = $("<a>").text(column.get("label")).append("<b class='sort-caret'></b>");
  104. } else {
  105. label = document.createTextNode(column.get("label"));
  106. }
  107. this.$el.append(label);
  108. this.$el.addClass(column.get("name"));
  109. this.$el.addClass(column.get("direction"));
  110. this.delegateEvents();
  111. return this;
  112. }
  113. });
  114. /**
  115. HeaderRow is a controller for a row of header cells.
  116. @class Backgrid.HeaderRow
  117. @extends Backgrid.Row
  118. */
  119. var HeaderRow = Backgrid.HeaderRow = Backgrid.Row.extend({
  120. requiredOptions: ["columns", "collection"],
  121. /**
  122. Initializer.
  123. @param {Object} options
  124. @param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns
  125. @param {Backgrid.HeaderCell} [options.headerCell] Customized default
  126. HeaderCell for all the columns. Supply a HeaderCell class or instance to a
  127. the `headerCell` key in a column definition for column-specific header
  128. rendering.
  129. @throws {TypeError} If options.columns or options.collection is undefined.
  130. */
  131. initialize: function () {
  132. Backgrid.Row.prototype.initialize.apply(this, arguments);
  133. },
  134. makeCell: function (column, options) {
  135. var headerCell = column.get("headerCell") || options.headerCell || HeaderCell;
  136. headerCell = new headerCell({
  137. column: column,
  138. collection: this.collection
  139. });
  140. return headerCell;
  141. }
  142. });
  143. /**
  144. Header is a special structural view class that renders a table head with a
  145. single row of header cells.
  146. @class Backgrid.Header
  147. @extends Backbone.View
  148. */
  149. var Header = Backgrid.Header = Backbone.View.extend({
  150. /** @property */
  151. tagName: "thead",
  152. /**
  153. Initializer. Initializes this table head view to contain a single header
  154. row view.
  155. @param {Object} options
  156. @param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns Column metadata.
  157. @param {Backbone.Model} options.model The model instance to render.
  158. @throws {TypeError} If options.columns or options.model is undefined.
  159. */
  160. initialize: function (options) {
  161. this.columns = options.columns;
  162. if (!(this.columns instanceof Backbone.Collection)) {
  163. this.columns = new Columns(this.columns);
  164. }
  165. this.row = new Backgrid.HeaderRow({
  166. columns: this.columns,
  167. collection: this.collection
  168. });
  169. },
  170. /**
  171. Renders this table head with a single row of header cells.
  172. */
  173. render: function () {
  174. this.$el.append(this.row.render().$el);
  175. this.delegateEvents();
  176. return this;
  177. },
  178. /**
  179. Clean up this header and its row.
  180. @chainable
  181. */
  182. remove: function () {
  183. this.row.remove.apply(this.row, arguments);
  184. return Backbone.View.prototype.remove.apply(this, arguments);
  185. }
  186. });