Commit | Line | Data |
---|---|---|
daa4e55a CW |
1 | (function(angular, $, _) { |
2 | "use strict"; | |
3 | ||
4 | angular.module('crmSearchAdmin').component('crmSearchAdminLinkGroup', { | |
5 | bindings: { | |
6 | group: '<', | |
7 | apiEntity: '<', | |
8 | apiParams: '<', | |
9 | links: '<' | |
10 | }, | |
11 | templateUrl: '~/crmSearchAdmin/crmSearchAdminLinkGroup.html', | |
12 | controller: function ($scope, $element, $timeout, searchMeta) { | |
33e81cf6 | 13 | var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), |
9446fbaa CW |
14 | ctrl = this, |
15 | linkProps = ['path', 'entity', 'action', 'join', 'target', 'icon', 'text', 'style']; | |
daa4e55a CW |
16 | |
17 | this.styles = CRM.crmSearchAdmin.styles; | |
18 | ||
2dbf2d72 CW |
19 | this.getStyle = function(item) { |
20 | return _.findWhere(this.styles, {key: item.style}); | |
daa4e55a CW |
21 | }; |
22 | ||
23 | this.sortableOptions = { | |
24 | containment: 'tbody', | |
25 | direction: 'vertical', | |
26 | helper: function(e, ui) { | |
27 | // Prevent table row width from changing during drag | |
28 | ui.children().each(function() { | |
29 | $(this).width($(this).width()); | |
30 | }); | |
31 | return ui; | |
32 | } | |
33 | }; | |
34 | ||
daa4e55a CW |
35 | $scope.pickIcon = function(index) { |
36 | searchMeta.pickIcon().then(function(icon) { | |
37 | ctrl.group[index].icon = icon; | |
38 | }); | |
39 | }; | |
40 | ||
9446fbaa CW |
41 | this.addItem = function(item) { |
42 | ctrl.group.push(_.pick(item, linkProps)); | |
daa4e55a CW |
43 | }; |
44 | ||
9446fbaa CW |
45 | this.onChangeLink = function(item, newValue) { |
46 | if (newValue.path === 'civicrm/') { | |
47 | newValue = JSON.parse(this.default); | |
a3203747 | 48 | } |
9446fbaa CW |
49 | _.each(linkProps, function(prop) { |
50 | item[prop] = newValue[prop] || ''; | |
51 | }); | |
a3203747 CW |
52 | }; |
53 | ||
9446fbaa CW |
54 | this.serialize = JSON.stringify; |
55 | ||
daa4e55a | 56 | this.$onInit = function() { |
9446fbaa CW |
57 | this.default = this.serialize({ |
58 | style: 'default', | |
59 | text: ts('Link'), | |
60 | icon: 'fa-external-link', | |
61 | path: 'civicrm/' | |
62 | }); | |
d6704532 CW |
63 | var defaultLinks = _.filter(ctrl.links, function(link) { |
64 | return !link.join; | |
65 | }); | |
daa4e55a | 66 | if (!ctrl.group.length) { |
d6704532 | 67 | if (defaultLinks.length) { |
9446fbaa | 68 | _.each(defaultLinks, ctrl.addItem); |
daa4e55a | 69 | } else { |
9446fbaa | 70 | ctrl.addItem(JSON.parse(this.default)); |
daa4e55a CW |
71 | } |
72 | } | |
2dbf2d72 | 73 | $element.on('change', 'select.crm-search-admin-add-link', function() { |
daa4e55a CW |
74 | var $select = $(this); |
75 | $scope.$apply(function() { | |
9446fbaa | 76 | ctrl.addItem(JSON.parse($select.val())); |
2dbf2d72 | 77 | $select.val(''); |
daa4e55a CW |
78 | }); |
79 | }); | |
80 | }; | |
81 | ||
daa4e55a CW |
82 | } |
83 | }); | |
84 | ||
85 | })(angular, CRM.$, CRM._); |