Commit | Line | Data |
---|---|---|
f9cf8797 CW |
1 | (function(angular, $, _) { |
2 | "use strict"; | |
3 | ||
4 | angular.module('crmSearchAdmin').component('crmSearchAdminLinkSelect', { | |
5 | bindings: { | |
6 | column: '<', | |
c7e96654 CW |
7 | apiEntity: '<', |
8 | apiParams: '<' | |
f9cf8797 CW |
9 | }, |
10 | templateUrl: '~/crmSearchAdmin/crmSearchAdminLinkSelect.html', | |
c7e96654 | 11 | controller: function ($scope, $element, $timeout, searchMeta) { |
f9cf8797 CW |
12 | var ts = $scope.ts = CRM.ts(), |
13 | ctrl = this; | |
14 | ||
c7e96654 CW |
15 | // Return all possible links to main entity or join entities |
16 | function getLinks() { | |
17 | var links = _.cloneDeep(searchMeta.getEntity(ctrl.apiEntity).paths || []); | |
18 | _.each(ctrl.apiParams.join, function(join) { | |
19 | var joinName = join[0].split(' AS '), | |
20 | joinEntity = searchMeta.getEntity(joinName[0]); | |
21 | _.each(joinEntity.paths, function(path) { | |
22 | var link = _.cloneDeep(path); | |
23 | link.path = link.path.replace(/\[/g, '[' + joinName[1] + '.'); | |
24 | links.push(link); | |
25 | }); | |
26 | }); | |
27 | return links; | |
28 | } | |
29 | ||
f9cf8797 CW |
30 | function onChange() { |
31 | var val = $('select', $element).val(); | |
32 | if (val !== ctrl.column.link) { | |
33 | var link = ctrl.getLink(val); | |
34 | if (link) { | |
35 | ctrl.column.link = link.path; | |
36 | ctrl.column.title = link.title; | |
37 | } else if (val === 'civicrm/') { | |
38 | ctrl.column.link = val; | |
39 | $timeout(function() { | |
40 | $('input', $element).focus(); | |
41 | }); | |
42 | } else { | |
43 | ctrl.column.link = ''; | |
44 | ctrl.column.title = ''; | |
45 | } | |
46 | } | |
47 | } | |
48 | ||
49 | this.$onInit = function() { | |
c7e96654 CW |
50 | this.links = getLinks(); |
51 | ||
f9cf8797 CW |
52 | $('select', $element).on('change', function() { |
53 | $scope.$apply(onChange); | |
54 | }); | |
55 | }; | |
56 | ||
57 | this.getLink = function(path) { | |
58 | return _.findWhere(ctrl.links, {path: path}); | |
59 | }; | |
60 | ||
61 | } | |
62 | }); | |
63 | ||
64 | })(angular, CRM.$, CRM._); |