forked from 18F/fedramp-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.controller.js
207 lines (189 loc) · 6.62 KB
/
search.controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
(function () {
'use strict';
angular
.module('fedramp')
.controller('SearchController', SearchController);
SearchController.$inject = ['$log', '$sce', '$http', '$stateParams', 'fedrampData', 'helperService'];
/**
* @constructor
* @memberof Controllers
*/
function SearchController ($log, $sce, $http, $stateParams, fedrampData, helperService) {
var self = this;
/**
* Flag if there was an error receiving a response
*
* @member {boolean}
* @memberof Controllers.SearchController
*/
self.error = false;
/**
* The search query
*
* @member {string}
* @memberof Controllers.SearchController
*/
self.query = $stateParams.query;
/**
* The search results.
*
* @member {array}
* @memberof Controllers.SearchController
*/
self.results = [];
/**
* The external search link.
*
* @member {string}
* @memberof Controllers.SearchController
*/
self.externalLink ='https://search.usa.gov/search?utf8=✓&affiliate=fedramp&format=html&output=embed&commit=Search&query=' + self.query;
/**
* Get the absolute URL of an internal link
*
* @public
* @memberof Controllers.SearchController
*
* @param {string} path
* @param {string} name
*
* @returns
* The absolute URL
*/
self.internalLink = function (path, name) {
let loc = window.location;
return loc.protocol + '//' + loc.host + loc.pathname + '#/' + path + '/' + helperService.slugify(name);
};
/**
* Determines what extension (if any) the URI is referencing
*
* @public
* @memberof Controllers.SearchController
*
* @param {string} url
* The URL
*
* @returns
* The extension abbreviation
*/
self.extension = function (url) {
if (url) {
let m = url.match(/(.*)[\/\\]([^\/\\]+)\.(\w+)$/);
if (m && m.length >= 3) {
return '[' + m[3].toUpperCase() + ']';
}
}
return '';
};
/**
* Parses possible markdown, or other encoded text, as HTML
*
* @public
* @memberof Controllers.SearchController
*
* @param {string} text
* The text to parse
*
* @returns
* The text in HTML format
*/
self.markdown = function (text) {
text = text.replace('\ue000', '**').replace('\ue001', '**');
text = text.replace('\u2013', '-');
return $sce.trustAsHtml(new showdown.Converter().makeHtml(text));
};
/**
* Filters arrays of objects by their name
*
* @private
* @memberof Controllers.SearchController
*
* @param {array} items
* The array of items to iterate
* @param {string} query
* The filter query
*
* @returns
* An array of matching items
*/
function filterByName (items, query) {
let q = query.toLowerCase();
return items.filter(x => {
if (x.name.toLowerCase().indexOf(query) !== -1) {
return true;
}
if (x.type === 'product' && x.provider.toLowerCase().indexOf(query) !== -1) {
return true;
}
return false;
});
}
(function () {
filterByName(fedrampData.products(), self.query).forEach(x => {
self.results.push({
title: x.provider + ' - ' + x.name,
content: '',
unescapedUrl: self.internalLink('product', x.name),
publishedAt: null,
siteLinks: []
});
});
filterByName(fedrampData.agencies(), self.query).forEach(x => {
self.results.push({
title: x.name,
content: '',
unescapedUrl: self.internalLink('agency', x.name),
publishedAt: null,
siteLinks: []
});
});
filterByName(fedrampData.assessors(), self.query).forEach(x => {
self.results.push({
title: x.name,
content: '',
unescapedUrl: self.internalLink('assessor', x.name),
publishedAt: null,
siteLinks: []
});
});
// Attempt to query using the form parameters but returning as JSON.
// This will have issues in development due to CORS.
$http
.get('https://search.usa.gov/search', {
params: {
utf8: '✓',
affiliate: 'fedramp',
format: 'json',
commit: 'Search',
query: self.query
}
})
.then(function (response) {
// Sample response:
//
// {
// "total": 35,
// "startrecord": 1,
// "endrecord": 20,
// "results": [
// {
// "title": "www.\ue000fedramp.gov\ue001",
// "content": "\ue000Test\ue001 Cases \u2013 If the system is a PaaS or SaaS that is leveraging another system, the Control Summary Worksheet should indicate which controls will be tested and ...",
// "unescapedUrl": "https://www.fedramp.gov/files/2015/08/FedRAMP-SAP-Detailed-Review-Checklist-Template-v2-0.xlsx",
// "publishedAt": null,
// "sitelinks": []
// }
// ],
// "related": []
// }
if (response && response.data) {
if (response.data.results) {
self.results = response.data.results;
}
}
}, function (response) {
self.error = true;
});
})();
}
})();