1a63886985f6c450ec95505b3af0e3b9f7bf1645
[civicrm-core.git] / ext / search / ang / crmSearchAdmin / crmSearchAdminLinkSelect.component.js
1 (function(angular, $, _) {
2 "use strict";
3
4 angular.module('crmSearchAdmin').component('crmSearchAdminLinkSelect', {
5 bindings: {
6 column: '<',
7 apiEntity: '<',
8 apiParams: '<'
9 },
10 templateUrl: '~/crmSearchAdmin/crmSearchAdminLinkSelect.html',
11 controller: function ($scope, $element, $timeout, searchMeta) {
12 var ts = $scope.ts = CRM.ts(),
13 ctrl = this;
14
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
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() {
50 this.links = getLinks();
51
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._);