directive.js 8.4 KB

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