directive.js 9.0 KB

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