BackgridHeader.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. define([ "require", "backbone", "underscore", "utils/Utils", "utils/Globals", "backgrid-filter", "backgrid-paginator", "select2" ], function(require, Backbone, _, Utils) {
  2. "use strict";
  3. var HeaderSearchCell = Backbone.View.extend({
  4. tagName: "td",
  5. className: "backgrid-filter",
  6. template: _.template('<input type="search" <% if (placeholder) { %> placeholder="<%- placeholder %>" <% } %> name="<%- name %>" <% if (style) { %> style="<%- style %>" <% } %> />'),
  7. placeholder: "",
  8. events: {
  9. "keyup input": "evKeyUp",
  10. submit: "search"
  11. },
  12. initialize: function(options) {
  13. _.extend(this, _.pick(options, "column")), this.name = this.column.get("name"),
  14. void 0 !== this.column.get("reName") && (this.name = this.column.get("reName"));
  15. var collection = this.collection, self = this;
  16. Backbone.PageableCollection && collection instanceof Backbone.PageableCollection && (collection.queryParams[this.name] = function() {
  17. return self.searchBox().val() || null;
  18. });
  19. },
  20. render: function() {
  21. return this.$el.empty().append(this.template({
  22. name: this.column.get("name"),
  23. placeholder: this.column.get("placeholder") || "搜索",
  24. style: this.column.get("headerSearchStyle")
  25. })), this.$el.addClass("renderable"), this.delegateEvents(), this;
  26. },
  27. evKeyUp: function(e) {
  28. var $clearButton = this.clearButton(), searchTerms = this.searchBox().val();
  29. e.shiftKey || this.search(), searchTerms ? $clearButton.show() : $clearButton.hide();
  30. },
  31. searchBox: function() {
  32. return this.$el.find("input[type=search]");
  33. },
  34. clearButton: function() {
  35. return this.$el.find(".clear");
  36. },
  37. search: function() {
  38. var data = {}, collection = this.collection;
  39. Backbone.PageableCollection && collection instanceof Backbone.PageableCollection && "server" === collection.mode && (collection.state.currentPage = collection.state.firstPage);
  40. var query = this.searchBox().val();
  41. query && (data[this.name] = query), collection.extraSearchParams && _.extend(data, collection.extraSearchParams),
  42. "server" === collection.mode ? collection.fetch({
  43. data: data,
  44. reset: !0,
  45. success: function() {},
  46. error: function(msResponse) {
  47. Utils.notifyError("Error", "Invalid input data!");
  48. }
  49. }) : "client" === collection.mode;
  50. },
  51. clear: function(e) {
  52. e && e.preventDefault(), this.searchBox().val(null), this.collection.fetch({
  53. reset: !0
  54. });
  55. }
  56. }), HeaderFilterCell = Backbone.View.extend({
  57. tagName: "td",
  58. className: "backgrid-filter",
  59. template: _.template('<select > <option>ALL</option><% _.each(list, function(data) {if(_.isObject(data)){ %><option value="<%= data.value %>"><%= data.label %></option><% }else{ %><option value="<%= data %>"><%= data %></option><% } %><% }); %></select>'),
  60. placeholder: "",
  61. events: {
  62. click: function() {}
  63. },
  64. initialize: function(options) {
  65. _.extend(this, _.pick(options, "column")), this.name = this.column.get("name"),
  66. this.headerFilterOptions = this.column.get("headerFilterOptions");
  67. },
  68. render: function() {
  69. var that = this;
  70. return this.$el.empty().append(this.template({
  71. name: this.column.get("name"),
  72. list: this.headerFilterOptions.filterList
  73. })), this.$el.find("select").select2({
  74. allowClear: !0,
  75. closeOnSelect: !1,
  76. width: this.headerFilterOptions.filterWidth || "100%",
  77. height: this.headerFilterOptions.filterHeight || "20px"
  78. }), this.$el.addClass("renderable"), this.$el.find("select").on("click", function(e) {
  79. that.search(e.currentTarget.value);
  80. }), this;
  81. },
  82. search: function(selectedOptionValue) {
  83. var query, data = {}, collection = this.collection;
  84. Backbone.PageableCollection && collection instanceof Backbone.PageableCollection && "server" === collection.mode && (collection.state.currentPage = collection.state.firstPage),
  85. "ALL" !== selectedOptionValue && (query = selectedOptionValue), query && (data[this.name] = query),
  86. collection.extraSearchParams && _.extend(data, collection.extraSearchParams), collection.fetch({
  87. data: data,
  88. reset: !0
  89. });
  90. }
  91. }), HeaderRow = Backgrid.Row.extend({
  92. requiredOptions: [ "columns", "collection" ],
  93. initialize: function() {
  94. Backgrid.Row.prototype.initialize.apply(this, arguments);
  95. },
  96. makeCell: function(column, options) {
  97. var headerCell;
  98. switch (!0) {
  99. case column.has("canHeaderSearch") && column.get("canHeaderSearch") === !0:
  100. headerCell = new HeaderSearchCell({
  101. column: column,
  102. collection: this.collection
  103. });
  104. break;
  105. case column.has("canHeaderFilter") && column.get("canHeaderFilter") === !0:
  106. headerCell = new HeaderFilterCell({
  107. column: column,
  108. collection: this.collection
  109. });
  110. break;
  111. default:
  112. headerCell = new Backbone.View({
  113. tagName: "td"
  114. });
  115. }
  116. return headerCell;
  117. }
  118. }), Header = Backgrid.Header.extend({
  119. initialize: function(options) {
  120. var args = Array.prototype.slice.apply(arguments);
  121. Backgrid.Header.prototype.initialize.apply(this, args), this.searchRow = new HeaderRow({
  122. columns: this.columns,
  123. collection: this.collection
  124. });
  125. },
  126. render: function() {
  127. var args = Array.prototype.slice.apply(arguments);
  128. return Backgrid.Header.prototype.render.apply(this, args), this.$el.append(this.searchRow.render().$el),
  129. this;
  130. },
  131. remove: function() {
  132. var args = Array.prototype.slice.apply(arguments);
  133. return Backgrid.Header.prototype.remove.apply(this, args), this.searchRow.remove.apply(this.searchRow, arguments),
  134. Backbone.View.prototype.remove.apply(this, arguments);
  135. }
  136. });
  137. return Header;
  138. });