directive.js 9.4 KB

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