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