/mapstory/static/mapstory/js/src/search/autocomplete.controllers.js

https://github.com/MapStory/mapstory · JavaScript · 215 lines · 157 code · 29 blank · 29 comment · 5 complexity · ca0340775fabc7733a9b3c08a4e1f3be MD5 · raw file

  1. (function() {
  2. angular
  3. .module("mapstory.search")
  4. .controller("interestsController", interestsController)
  5. .controller("tagsController", tagsController)
  6. .controller("citiesController", citiesController)
  7. .controller("searchController", searchController)
  8. .controller("storytellerController", storytellerController)
  9. .controller("countriesController", countriesController);
  10. /*
  11. * Interests Autocomplete Controller
  12. */
  13. function interestsController(
  14. $injector,
  15. $scope,
  16. chipFieldFactory,
  17. dataService
  18. ) {
  19. const vm = this;
  20. vm.interests = new chipFieldFactory("interests", "slug");
  21. vm.async = function(searchText) {
  22. return dataService.getInterests({ slug__icontains: searchText });
  23. };
  24. function interestChipSync() {
  25. const currentQuery = vm.interests.tidy($scope.query);
  26. // updates countryChips model with tidy, transformed list of actively selected countries
  27. vm.chips = vm.interests.transform(currentQuery);
  28. vm.disabled = vm.chips.length >= 1;
  29. }
  30. $scope.$on("updateSelection", () => {
  31. interestChipSync();
  32. });
  33. interestChipSync();
  34. }
  35. /*
  36. * Tag / Keyword Autocomplete Controller
  37. */
  38. function tagsController($injector, $scope, chipFieldFactory, dataService) {
  39. const vm = this;
  40. vm.field = new chipFieldFactory("keywords__slug__in", "slug");
  41. vm.async = function(searchText) {
  42. console.log(dataService.getKeywords({ slug__icontains: searchText }));
  43. return dataService.getKeywords({ slug__icontains: searchText });
  44. };
  45. function tagChipSync() {
  46. const currentQuery = vm.field.tidy($scope.query);
  47. vm.chips = vm.field.transform(currentQuery);
  48. vm.placeholder =
  49. vm.chips.length > 0 ? "Expand your Tag Filter..." : "Filter by tag...";
  50. }
  51. $scope.$on("updateSelection", () => {
  52. tagChipSync();
  53. });
  54. tagChipSync();
  55. }
  56. /*
  57. * Cities Autocomplete Controller
  58. */
  59. function citiesController($injector, $scope, chipFieldFactory, dataService) {
  60. const vm = this;
  61. vm.cities = new chipFieldFactory("city__icontains", "city");
  62. vm.async = function(searchText) {
  63. return dataService.getOwners({ city__icontains: searchText });
  64. };
  65. function cityChipSync() {
  66. const currentQuery = vm.cities.tidy($scope.query);
  67. vm.cityChips = vm.cities.transform(currentQuery);
  68. if (vm.cityChips) vm.disabled = vm.cityChips.length >= 1;
  69. }
  70. $scope.$on("updateSelection", () => {
  71. cityChipSync();
  72. });
  73. cityChipSync();
  74. }
  75. /*
  76. * Search Autocomplete Controller
  77. */
  78. function searchController($injector, $scope, chipFieldFactory, dataService) {
  79. const vm = this;
  80. vm.search = new chipFieldFactory("q", "title");
  81. vm.async = function(searchText) {
  82. console.log(dataService.getSearchData({ q: searchText }));
  83. return dataService.getSearchData({ q: searchText });
  84. };
  85. function searchChipSync() {
  86. const currentQuery = vm.search.tidy($scope.query);
  87. vm.searchChips = vm.search.transform(currentQuery);
  88. vm.placeholder =
  89. vm.searchChips.length > 0
  90. ? "Expand search for layers or stories..."
  91. : "Search for layers or stories...";
  92. }
  93. $scope.$on("updateSelection", () => {
  94. searchChipSync();
  95. });
  96. searchChipSync();
  97. }
  98. /*
  99. * StoryTeller Autocomplete Controller
  100. */
  101. function storytellerController(
  102. $injector,
  103. $scope,
  104. chipFieldFactory,
  105. dataService
  106. ) {
  107. const vm = this;
  108. vm.author = new chipFieldFactory("owner__username__in", "username");
  109. vm.async = function(searchText) {
  110. console.log(dataService.getOwners({ q: searchText }));
  111. return dataService.getOwners({ q: searchText });
  112. };
  113. function authorChipSync() {
  114. const currentQuery = vm.author.tidy($scope.query);
  115. vm.userChips = vm.author.transform(currentQuery);
  116. vm.placeholder =
  117. vm.userChips.length > 0
  118. ? "Expand your StoryTeller Filter..."
  119. : "Filter by StoryTeller...";
  120. }
  121. $scope.$on("updateSelection", () => {
  122. authorChipSync();
  123. });
  124. authorChipSync();
  125. }
  126. /*
  127. * Country Autocomplete Controller (TODO: need to update to async with code+name query)
  128. */
  129. function countriesController(
  130. $injector,
  131. $scope,
  132. chipFieldFactory,
  133. dataService
  134. ) {
  135. const vm = this;
  136. vm.country = new chipFieldFactory("country");
  137. function querySearch(query) {
  138. const list = this.list;
  139. const results = query ? list.filter(createFilter(query)) : [];
  140. return results;
  141. }
  142. function createFilter(query) {
  143. const lowercaseQuery = query.toLowerCase();
  144. return function filterFn(entry) {
  145. return _.some(entry._lower, i => i.indexOf(lowercaseQuery) > -1);
  146. };
  147. }
  148. vm.country.querySearch = querySearch;
  149. vm.country.createFilter = createFilter;
  150. vm.countryChips = [];
  151. vm.country.transform = function() {
  152. // tidy any country filters on scope into a single array
  153. const currentQuery = vm.country.tidy($scope.query);
  154. // transforms each query entry into country chip
  155. return _.map(currentQuery, item => vm.country.byCodes[item]) || [];
  156. };
  157. // / get the things, sync chips with query on delete ///
  158. $scope.$on("updateSelection", () => {
  159. countryChipSync();
  160. });
  161. if (!$scope.autocomplete.countries) {
  162. // if explore controller doesn't have list of regions / countries yet,
  163. // get it from Region API
  164. dataService.getRegions().then(data => {
  165. setAutocomplete(data);
  166. });
  167. } else {
  168. // else initialize autocomplete with list from explore controller
  169. setAutocomplete($scope.autocomplete.countries);
  170. }
  171. function setAutocomplete(info) {
  172. // store response in exploreController scope
  173. $scope.autocomplete.countries = info;
  174. // extend chipField object w/ list of regions and an index by country code
  175. vm.country.byCodes = info.byCodes;
  176. vm.country.list = info.all;
  177. // update countryChips model
  178. countryChipSync();
  179. }
  180. function countryChipSync() {
  181. // updates countryChips model with tidy, transformed list of actively selected countries
  182. if (vm.country.byCodes) vm.countryChips = vm.country.transform();
  183. vm.disabled = vm.countryChips.length >= 1;
  184. }
  185. }
  186. })();