APIs

Show:
  1. /**
  2. @module ember-flexberry
  3. */
  4.  
  5. import Mixin from '@ember/object/mixin';
  6.  
  7. import { predicateForFilter } from 'ember-flexberry/utils/filter';
  8.  
  9. import Condition from 'ember-flexberry-data/query/condition';
  10. import { ComplexPredicate } from 'ember-flexberry-data/query/predicate';
  11.  
  12. /**
  13. Mixin for route, that restrictions on the list form.
  14.  
  15. @example
  16. ```javascript
  17. // app/controllers/employees.js
  18. import Controller from '@ember/controller';
  19. import LimitedController from 'ember-flexberry/mixins/limited-controller'
  20. export default Controller.extend(LimitedController, {
  21. ...
  22. });
  23. ```
  24.  
  25. ```javascript
  26. // app/routes/employees.js
  27. import Route from '@ember/routing/route';
  28. import LimitedRoute from 'ember-flexberry/mixins/limited-route'
  29. export default Route.extend(LimitedRoute, {
  30. ...
  31. });
  32. ```
  33.  
  34. ```handlebars
  35. <!-- app/templates/employees.hbs -->
  36. ...
  37. {{flexberry-objectlistview
  38. ...
  39. enableFilters=enableFilters
  40. filters=filters
  41. applyFilters=(action "applyFilters")
  42. resetFilters=(action "resetFilters")
  43. ...
  44. filterButton=true
  45. filterText=filter
  46. filterByAnyMatch=(action 'filterByAnyMatch')
  47. ...
  48. }}
  49. ...
  50. ```
  51.  
  52. @class LimitedRouteMixin
  53. @uses <a href="https://www.emberjs.com/api/ember/release/classes/Mixin">Mixin</a>
  54. */
  55. export default Mixin.create({
  56. /**
  57. Configuration hash for this route's queryParams. [More info](https://www.emberjs.com/api/ember/release/classes/Route/properties/queryParams?anchor=queryParams).
  58.  
  59. @property queryParams
  60. @type Object
  61. */
  62. queryParams: {
  63. filter: { refreshModel: true },
  64. filterCondition: { refreshModel: true }
  65. },
  66.  
  67. /**
  68. String with search query.
  69.  
  70. @property filter
  71. @type String
  72. @default null
  73. */
  74. filter: null,
  75.  
  76. /**
  77. Result predicate with all restrictions for olv.
  78.  
  79. @property resultPredicate
  80. @type BasePredicate
  81. @default null
  82. */
  83. resultPredicate: null,
  84.  
  85. /**
  86. Result predicate with filters restrictions for olv.
  87.  
  88. @property filterPredicate
  89. @type BasePredicate
  90. @default null
  91. */
  92. filterPredicate: null,
  93.  
  94. /**
  95. Builds predicate for filter.
  96.  
  97. @example
  98. ```javascript
  99. // app/routes/example.js
  100. ...
  101. predicateForFilter(filter) {
  102. if (filter.type === 'string' && filter.condition === 'like') {
  103. return new StringPredicate(filter.name).contains(filter.pattern);
  104. }
  105.  
  106. return this._super(...arguments);
  107. },
  108. ...
  109. ```
  110.  
  111. @method predicateForFilter
  112. @param {Object} filter Object (`{ name, condition, pattern }`) with parameters for filter.
  113. @return {BasePredicate|null} Predicate to filter through.
  114. */
  115. predicateForFilter: predicateForFilter,
  116.  
  117. /**
  118. Return predicate for `QueryBuilder` or `undefined`.
  119.  
  120. @method _filtersPredicate
  121. @return {BasePredicate|undefined} Predicate for `QueryBuilder` or `undefined`.
  122. @private
  123. */
  124. _filtersPredicate() {
  125. let filters = this.controllerFor(this.routeName).get('filters');
  126. if (filters) {
  127. let predicates = [];
  128. for (let filter in filters) {
  129. if (filters.hasOwnProperty(filter)) {
  130. let predicate = this.predicateForFilter(filters[filter]);
  131. if (predicate) {
  132. predicates.push(predicate);
  133. }
  134. }
  135. }
  136.  
  137. return predicates.length ? predicates.length > 1 ? new ComplexPredicate(Condition.And, ...predicates) : predicates[0] : undefined;
  138. }
  139.  
  140. return undefined;
  141. }
  142. });
  143.