Commit | Line | Data |
---|---|---|
25523059 CW |
1 | (function(angular, $, _) { |
2 | "use strict"; | |
3 | ||
4 | // Shared between router and searchMeta service | |
5 | var searchEntity; | |
6 | ||
7 | // Declare module and route/controller/services | |
8 | angular.module('search', CRM.angRequires('search')) | |
9 | ||
10 | .config(function($routeProvider) { | |
11 | $routeProvider.when('/:entity', { | |
12 | controller: 'searchRoute', | |
b0422f12 | 13 | template: '<div id="bootstrap-theme" class="crm-search"><crm-search entity="entity"></crm-search></div>', |
25523059 CW |
14 | reloadOnSearch: false |
15 | }); | |
16 | }) | |
17 | ||
18 | // Controller binds entity to route | |
19 | .controller('searchRoute', function($scope, $routeParams, $location) { | |
20 | searchEntity = $scope.entity = $routeParams.entity; | |
21 | ||
22 | // Changing entity will refresh the angular page | |
23 | $scope.$watch('entity', function(newEntity, oldEntity) { | |
24 | if (newEntity && oldEntity && newEntity !== oldEntity) { | |
25 | $location.url('/' + newEntity); | |
26 | } | |
27 | }); | |
28 | }) | |
29 | ||
30 | .factory('searchMeta', function() { | |
31 | function getEntity(entityName) { | |
32 | if (entityName) { | |
33 | entityName = entityName === true ? searchEntity : entityName; | |
34 | return _.find(CRM.vars.search.schema, {name: entityName}); | |
35 | } | |
36 | } | |
37 | function getField(name) { | |
38 | var dotSplit = name.split('.'), | |
39 | joinEntity = dotSplit.length > 1 ? dotSplit[0] : null, | |
40 | fieldName = _.last(dotSplit).split(':')[0], | |
41 | entityName = searchEntity; | |
42 | if (joinEntity) { | |
43 | entityName = _.find(CRM.vars.search.links[entityName], {alias: joinEntity}).entity; | |
44 | } | |
45 | return _.find(getEntity(entityName).fields, {name: fieldName}); | |
46 | } | |
47 | return { | |
48 | getEntity: getEntity, | |
49 | getField: getField, | |
50 | parseExpr: function(expr) { | |
51 | var result = {}, | |
52 | fieldName = expr, | |
53 | bracketPos = expr.indexOf('('); | |
54 | if (bracketPos >= 0) { | |
55 | fieldName = expr.match(/[A-Z( _]*([\w.:]+)/)[1]; | |
56 | result.fn = _.find(CRM.vars.search.functions, {name: expr.substring(0, bracketPos)}); | |
57 | } | |
58 | result.field = getField(fieldName); | |
59 | var split = fieldName.split(':'), | |
60 | dotPos = split[0].indexOf('.'); | |
61 | result.path = split[0]; | |
62 | result.prefix = result.path.substring(0, dotPos + 1); | |
63 | result.suffix = !split[1] ? '' : ':' + split[1]; | |
64 | return result; | |
65 | } | |
66 | }; | |
67 | }) | |
68 | ||
69 | // Reformat an array of objects for compatibility with select2 | |
70 | // Todo this probably belongs in core | |
71 | .factory('formatForSelect2', function() { | |
72 | return function(input, key, label, extra) { | |
73 | return _.transform(input, function(result, item) { | |
74 | var formatted = {id: item[key], text: item[label]}; | |
75 | if (extra) { | |
76 | _.merge(formatted, _.pick(item, extra)); | |
77 | } | |
78 | result.push(formatted); | |
79 | }, []); | |
80 | }; | |
81 | }); | |
82 | ||
83 | })(angular, CRM.$, CRM._); |