source: main/trunk/model-interfaces-dev/heritage-nz/iframe/heritage-nz-dl_files/registerSearchService.js@ 32796

Last change on this file since 32796 was 32796, checked in by davidb, 5 years ago

Initial set of files to provide look and feel of Heritage NZ site, plus SVN clickable map in an iframe

  • Property svn:executable set to *
File size: 17.4 KB
Line 
1(function (ng) {
2 angular.module('search')
3 .factory('RegisterSearchService', ['$http', function ($http) {
4
5 var registerApiUrl = "/api/register/GetRegisterSearchResults",
6 registerCsvApiUrl = "/api/register/GetRegisterResultsCsv",
7 registerApiEnableCsvParams = "&format=csv",
8 fullRegisterCsvExportUrl = registerCsvApiUrl + "/?format=csv";
9
10 var SearchData = function () {
11 var that = {};
12 that.address = '';
13 that.search = [];
14 that.chooseARegion = [];
15 that.registrationType = [];
16 that.uses = [];
17 that.theme = '';
18 that.constructionProfessional = '';
19 that.localAuthority = [];
20 that.searchAll = '';
21 that.lat = 0;
22 that.lng = 0;
23 that.userLocationLat = 0;
24 that.userLocationLng = 0;
25 that.pageNumber = 1;
26 that.pageSize = 20;
27 that.requestedUrl = '';
28 that.searchPassed = false;
29 that.searchedTerm = '';
30 that.googleAutoCompleteResults = {};
31 that.searchResultsCount = 0;
32 that.fullRegisterCsvExportUrl = fullRegisterCsvExportUrl;
33 that.pagedRegisterCsvExportUrl = "";
34 that.townCity = [];
35 that.place = [];
36
37 that.validSearchData = function () {
38 return ((!this.lat || this.lat == 0) &&
39 (!this.lng || this.lng == 0) &&
40 (!this.search || !this.search.length) &&
41 (!this.chooseARegion || !this.chooseARegion.length) &&
42 (!this.registrationType || !this.registrationType.length) &&
43 (!this.localAuthority || !this.localAuthority.length) &&
44 (!this.uses || !this.uses.length) &&
45 (!this.theme || !this.theme.length) &&
46 (!this.constructionProfessional || !this.constructionProfessional.length) &&
47 (!this.searchAll || !this.searchAll.length)) &&
48 (!this.townCity || !this.townCity.length) &&
49 (!this.place || !this.place.length)
50 ? false
51 : true;
52 };
53
54 return that;
55 };
56
57 var Select2Options = function () {
58 var select2 = {
59 options: {
60 multiple: true,
61 simple_tags: true,
62 maximumSelectionSize: 1,
63 minimumInputLength: 1,
64 separator: "|",
65 ajax: {
66 quietMillis: 500,
67 url: "/api/register/GetTypeaheadValues",
68 data: function (term, page) {
69 return {
70 value: term
71 }; // query params go here
72 },
73 results: function (data, page) { // parse the results into the format expected by Select2.
74 // since we are using custom formatting functions we do not need to alter remote JSON data
75 return { results: data };
76 }
77 },
78 formatResult: function (result, container, query, escapeMarkup) {
79 container.addClass('needsclick');
80 return result.text;
81 },
82 //Allow manually entered text in drop down.
83 createSearchChoice: function (term, data) {
84 if ($(data).filter(function () {
85 return this.text.localeCompare(term) === 0;
86 }).length === 0) {
87 return { id: term, text: term };
88 }
89 }
90 },
91 listNumbers: {
92 multiple: true,
93 simple_tags: true,
94 maximumSelectionSize: 1,
95 minimumInputLength: 1,
96 separator: "|",
97 ajax: {
98 quietMillis: 500,
99 url: "/api/register/GetListNzaaNumbers",
100 data: function (term, page) {
101 return {
102 value: term
103 }; // query params go here
104 },
105 results: function (data, page) { // parse the results into the format expected by Select2.
106 // since we are using custom formatting functions we do not need to alter remote JSON data
107 return { results: data };
108 }
109 },
110 formatResult: function (result, container, query, escapeMarkup) {
111 container.addClass('needsclick');
112 return result.text;
113 },
114
115 },
116 places: {
117 multiple: true,
118 simple_tags: true,
119 maximumSelectionSize: 1,
120 minimumInputLength: 1,
121 separator: "|",
122 ajax: {
123 quietMillis: 500,
124 url: "/api/register/GetPlaceNames",
125 data: function (term, page) {
126 return {
127 value: term
128 }; // query params go here
129 },
130 results: function (data, page) { // parse the results into the format expected by Select2.
131 // since we are using custom formatting functions we do not need to alter remote JSON data
132 return { results: data };
133 }
134 },
135 formatResult: function (result, container, query, escapeMarkup) {
136 container.addClass('needsclick');
137 return result.text;
138 },
139 //Allow manually entered text in drop down.
140 createSearchChoice: function (term, data) {
141 if ($(data).filter(function () {
142 return this.text.localeCompare(term) === 0;
143 }).length === 0) {
144 return { id: term, text: term };
145 }
146 }
147 },
148 townCity: {
149 multiple: true,
150 simple_tags: true,
151 maximumSelectionSize: 5,
152 minimumInputLength: 1,
153 separator: "|",
154 formatResult: function (result, container, query, escapeMarkup) {
155 container.addClass('needsclick');
156 return result.text;
157 },
158 },
159 defaults: {
160 maximumSelectionSize: 5,
161 formatResult: function (result, container, query, escapeMarkup) {
162 container.addClass('needsclick');
163 return result.text;
164 }
165 }
166 };
167
168 return select2;
169 };
170
171 var RegisterSearch = function (searchData) {
172 angular.extend(this, searchData);
173
174 this.totalResultsCount = 0;
175 this.allResults = [];
176 this.pagedResults = [];
177 this.pagedRegisterCsvExportUrl = "";
178 this.busy = false;
179
180 this.initialSearchHasRun = false;
181 this.allItemsLoaded = function () {
182 return (this.pagedResults.length >= this.totalResultsCount);
183 };
184 this.isAddressSearch = function () {
185 return this.address;
186 };
187 };
188
189 var RegisterSearchDto = function (registerSearch) {
190 this.keyword = registerSearch.search;
191 this.searchAll = registerSearch.searchAll;
192 this.address = registerSearch.address;
193 this.lat = registerSearch.lat;
194 this.lng = registerSearch.lng;
195 this.userLocationLat = registerSearch.userLocationLat;
196 this.userLocationLng = registerSearch.userLocationLng;
197 this.regions = registerSearch.chooseARegion;
198 this.registrationType = registerSearch.registrationType;
199 this.uses = registerSearch.uses;
200 this.theme = registerSearch.theme;
201 this.constructionProfessional = registerSearch.constructionProfessional;
202 this.localAuthorities = registerSearch.localAuthority;
203 this.pageNumber = registerSearch.pageNumber;
204 this.pageSize = registerSearch.pageSize;
205 this.includeAllResults = false;
206 this.townCity = registerSearch.townCity;
207 this.place = registerSearch.place;
208 };
209
210 var buildUrl = function (baseUrl, object) {
211 return baseUrl + "?" + $.param(object, true);
212 };
213
214 RegisterSearch.prototype.getAllRegisterSearchResults = function () {
215 this.busy = true;
216
217 var searchDto = new RegisterSearchDto(this);
218 searchDto.includeAllResults = true;
219
220 // $http returns a promise, which has a then function, which also returns a promise
221 return $http({ cache: false, dataType: 'json', url: registerApiUrl, method: 'GET', params: searchDto })
222 .then(function (response) {
223
224 searchDto.includeAllResults = false;
225 this.totalResultsCount = response.data.Count;
226 this.pagedRegisterCsvExportUrl = buildUrl(registerCsvApiUrl, searchDto) + registerApiEnableCsvParams;
227
228 searchDto.pageSize = this.totalResultsCount;
229 this.allRegisterResultsCsvExportUrl = buildUrl(registerCsvApiUrl, searchDto) + registerApiEnableCsvParams;
230
231 this.allResults = response.data.Results;
232
233 if (this.isAddressSearch()) {
234 if(this.allResults[0] != null)
235 this.allResults[0].zIndex = 999998;
236 if (this.allResults[1] != null)
237 this.allResults[1].zIndex = 999999;
238
239 this.pagedResults = response.data.Results.slice(1, this.pageSize + 1);
240 } else {
241 this.pagedResults = response.data.Results.slice(0, this.pageSize);
242 }
243
244 this.pageNumber++;
245 this.initialSearchHasRun = true;
246 this.busy = false;
247 }.bind(this));
248 };
249
250 RegisterSearch.prototype.nextPage = function () {
251 if (this.busy || this.allItemsLoaded()) {
252 return;
253 }
254
255 this.busy = true;
256
257 // Push next results to array
258 var startIndex = (this.pageNumber - 1) * this.pageSize;
259 if (this.isAddressSearch()) {
260 startIndex = startIndex + 1;
261 }
262
263 var resultsForThisPage = this.allResults.slice(startIndex, startIndex + this.pageSize);
264 this.pagedResults.push.apply(this.pagedResults, resultsForThisPage);
265
266 // Build register CSV export url
267 var searchDto = new RegisterSearchDto(this);
268 var searchDtoClone = angular.copy(searchDto);
269 searchDtoClone.pageSize = searchDtoClone.pageSize * searchDtoClone.pageNumber;
270 searchDtoClone.pageNumber = 1;
271 //this.pagedRegisterCsvExportUrl = buildUrl(registerApiUrl, searchDtoClone);
272
273 this.pageNumber++;
274 this.busy = false;
275 };
276
277 return {
278 getInstance: function (searchData) {
279 return new RegisterSearch(searchData);
280 },
281 getRegions: function () {
282 // $http returns a promise, which has a then function, which also returns a promise
283 return $http({ cache: true, dataType: 'json', url: '/api/register/GetRegions', method: 'GET' })
284 .then(function (response) {
285 return response.data;
286 });
287 },
288 getGroupedUses: function () {
289 // $http returns a promise, which has a then function, which also returns a promise
290 return $http({ cache: true, dataType: 'json', url: '/api/register/GetGroupedUses', method: 'GET' })
291 .then(function (response) {
292 return response.data;
293 });
294 },
295 getUses: function () {
296 // $http returns a promise, which has a then function, which also returns a promise
297 return $http({ cache: true, dataType: 'json', url: '/api/register/GetUses', method: 'GET' })
298 .then(function (response) {
299 return response.data;
300 });
301 },
302 getLocalAuthority: function () {
303 // $http returns a promise, which has a then function, which also returns a promise
304 return $http({ cache: true, dataType: 'json', url: '/api/register/GetLocalAuthority', method: 'GET' })
305 .then(function (response) {
306 return response.data;
307 });
308 },
309 getLocalAuthorityByRegion: function (regions) {
310 // $http returns a promise, which has a then function, which also returns a promise
311 return $http({ cache: false, dataType: 'json', url: '/api/register/GetLocalAuthorityByRegion', method: 'GET', params: { regions: regions } })
312 .then(function (response) {
313 return response.data;
314 });
315 },
316 getRegistrationType: function () {
317 // $http returns a promise, which has a then function, which also returns a promise
318 return $http({ cache: true, dataType: 'json', url: '/api/register/GetRegistrationType', method: 'GET' })
319 .then(function (response) {
320 return response.data;
321 });
322 },
323 getTypeaheadValues: function () {
324 // $http returns a promise, which has a then function, which also returns a promise
325 return $http({ cache: true, dataType: 'json', url: '/api/register/GetTypeaheadValues', method: 'GET' })
326 .then(function (response) {
327 return response.data;
328 });
329 },
330
331 getPlaceNames: function () {
332 // $http returns a promise, which has a then function, which also returns a promise
333 return $http({ cache: true, dataType: 'json', url: '/api/register/GetPlaceNames', method: 'GET' })
334 .then(function (response) {
335 return response.data;
336 });
337 },
338
339 getFilteredTypeaheadValues: function (value) {
340 // $http returns a promise, which has a then function, which also returns a promise
341 return $http({ cache: false, dataType: 'json', url: '/api/register/GetTypeaheadValues?value=' + value, method: 'GET' })
342 .then(function (response) {
343 return response.data;
344 });
345 },
346
347 fullRegisterCsvExportUrl: fullRegisterCsvExportUrl,
348
349 searchData: SearchData,
350
351 select2Options: Select2Options
352 };
353
354 }]);
355})(angular);
Note: See TracBrowser for help on using the repository browser.