directive.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. (function (module) {
  2. module.directive('focusMe', function ($timeout) {
  3. return {
  4. link: function (scope, element, attrs) {
  5. $timeout(function () {
  6. element[0].focus();
  7. }, 150);
  8. }
  9. };
  10. });
  11. module.directive('ionAmapPlace', [
  12. '$ionicTemplateLoader',
  13. '$ionicBackdrop',
  14. '$ionicPlatform',
  15. '$q',
  16. '$timeout',
  17. '$rootScope',
  18. '$document',
  19. function ($ionicTemplateLoader, $ionicBackdrop, $ionicPlatform, $q, $timeout, $rootScope, $document) {
  20. return {
  21. require: '?ngModel',
  22. restrict: 'E',
  23. template: '<input type="text" readonly="readonly" class="ion-amap-place" autocomplete="off">',
  24. replace: true,
  25. scope: {
  26. ngModel: '=?'
  27. },
  28. link: function (scope, element, attrs, ngModel) {
  29. var unbindBackButtonAction;
  30. scope.locations = [];
  31. var autocomplete;
  32. AMap.plugin('AMap.Autocomplete', function () { //回调函数
  33. //实例化Autocomplete
  34. var autoOptions = {
  35. city: "" //城市,默认全国
  36. };
  37. autocomplete = new AMap.Autocomplete(autoOptions);
  38. });
  39. //var geocoder = new google.maps.Geocoder();
  40. var searchEventTimeout = undefined;
  41. var POPUP_TPL = [
  42. '<div class="ion-amap-place-container modal">',
  43. '<div class="bar bar-header item-input-inset">',
  44. '<label class="item-input-wrapper">',
  45. '<i class="icon ion-ios7-search placeholder-icon"></i>',
  46. '<input class="amap-place-search" type="search" ng-model="searchQuery" placeholder="' + (attrs.searchPlaceholder || '输入地址') + '">',
  47. '</label>',
  48. '<button class="button button-balanced">',
  49. attrs.labelCancel || '取消',
  50. '</button>',
  51. '</div>',
  52. '<ion-content class="has-header has-header">',
  53. '<ion-list>',
  54. '<ion-item ng-repeat="location in locations" type="item-text-wrap" ng-click="selectLocation(location)">',
  55. '{{location.name}}({{location.district}})',
  56. '</ion-item>',
  57. '</ion-list>',
  58. '</ion-content>',
  59. '</div>'
  60. ].join('');
  61. var popupPromise = $ionicTemplateLoader.compile({
  62. template: POPUP_TPL,
  63. scope: scope,
  64. appendTo: $document[0].body
  65. });
  66. popupPromise.then(function (el) {
  67. var searchInputElement = angular.element(el.element.find('input'));
  68. scope.selectLocation = function (location) {
  69. ngModel.$setViewValue(location);
  70. el.element.css('display', 'none');
  71. $ionicBackdrop.release();
  72. if (unbindBackButtonAction) {
  73. unbindBackButtonAction();
  74. unbindBackButtonAction = null;
  75. }
  76. };
  77. scope.$watch('searchQuery', function (query) {
  78. if (searchEventTimeout) $timeout.cancel(searchEventTimeout);
  79. searchEventTimeout = $timeout(function () {
  80. if (!query) return;
  81. if (query.length < 2) return;
  82. autocomplete.search(query, function (status, result) {
  83. if (status === "complete") {
  84. scope.$apply(function () {
  85. scope.locations = result.tips;
  86. });
  87. } else {
  88. //todo
  89. }
  90. });
  91. //geocoder.geocode(req, function (results, status) {
  92. // if (status == google.maps.GeocoderStatus.OK) {
  93. // scope.$apply(function () {
  94. // scope.locations = results;
  95. // });
  96. // } else {
  97. // // @TODO: Figure out what to do when the geocoding fails
  98. // }
  99. //});
  100. }, 350); // we're throttling the input by 350ms to be nice to google's API
  101. });
  102. var onClick = function (e) {
  103. e.preventDefault();
  104. e.stopPropagation();
  105. $ionicBackdrop.retain();
  106. unbindBackButtonAction = $ionicPlatform.registerBackButtonAction(closeOnBackButton, 250);
  107. el.element.css('display', 'block');
  108. searchInputElement[0].focus();
  109. setTimeout(function () {
  110. searchInputElement[0].focus();
  111. }, 0);
  112. };
  113. var onCancel = function (e) {
  114. scope.searchQuery = '';
  115. $ionicBackdrop.release();
  116. el.element.css('display', 'none');
  117. if (unbindBackButtonAction) {
  118. unbindBackButtonAction();
  119. unbindBackButtonAction = null;
  120. }
  121. };
  122. closeOnBackButton = function (e) {
  123. e.preventDefault();
  124. el.element.css('display', 'none');
  125. $ionicBackdrop.release();
  126. if (unbindBackButtonAction) {
  127. unbindBackButtonAction();
  128. unbindBackButtonAction = null;
  129. }
  130. }
  131. element.bind('click', onClick);
  132. element.bind('touchend', onClick);
  133. el.element.find('button').bind('click', onCancel);
  134. });
  135. if (attrs.placeholder) {
  136. element.attr('placeholder', attrs.placeholder);
  137. }
  138. ngModel.$formatters.unshift(function (modelValue) {
  139. if (!modelValue) return '';
  140. return modelValue;
  141. });
  142. ngModel.$parsers.unshift(function (viewValue) {
  143. return viewValue;
  144. });
  145. ngModel.$render = function () {
  146. if (!ngModel.$modelValue) {
  147. element.val('');
  148. } else {
  149. element.val(ngModel.$modelValue.district + ngModel.$modelValue.name || '');
  150. }
  151. };
  152. scope.$watch(function () {
  153. return ngModel.$modelValue;
  154. }, function (modelValue) {
  155. ngModel.$render();
  156. });
  157. scope.$on("$destroy", function () {
  158. if (unbindBackButtonAction) {
  159. unbindBackButtonAction();
  160. unbindBackButtonAction = null;
  161. }
  162. });
  163. }
  164. };
  165. }
  166. ]);
  167. })(angular.module('app.directives'));