Merge pull request #21057 from eileenmcnaughton/tok_resolve
[civicrm-core.git] / ext / search_kit / ang / crmSearchDisplay / traits / searchDisplaySortableTrait.service.js
1 (function(angular, $, _) {
2 "use strict";
3
4 // Trait shared by any search display controllers which allow sorting
5 angular.module('crmSearchDisplay').factory('searchDisplaySortableTrait', function() {
6 var ts = CRM.ts('org.civicrm.search_kit');
7
8 // Trait properties get mixed into display controller using angular.extend()
9 return {
10
11 sort: [],
12
13 getSort: function(col) {
14 var dir = _.reduce(this.sort, function(dir, item) {
15 return item[0] === col.key ? item[1] : dir;
16 }, null);
17 if (dir) {
18 return 'fa-sort-' + dir.toLowerCase();
19 }
20 return 'fa-sort disabled';
21 },
22
23 setSort: function(col, $event) {
24 if (col.type !== 'field') {
25 return;
26 }
27 var dir = this.getSort(col) === 'fa-sort-asc' ? 'DESC' : 'ASC';
28 if (!$event.shiftKey || !this.sort) {
29 this.sort = [];
30 }
31 var index = _.findIndex(this.sort, [col.key]);
32 if (index > -1) {
33 this.sort[index][1] = dir;
34 } else {
35 this.sort.push([col.key, dir]);
36 }
37 this.getResults();
38 }
39
40 };
41 });
42
43 })(angular, CRM.$, CRM._);