directive.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. angular.module('push')
  2. .directive('backImg', function () {
  3. return function (scope, element, attrs) {
  4. attrs.$observe('backImg', function (value) {
  5. element.css({
  6. 'background-image': 'url(' + value + ')',
  7. 'background-size': '100% 100%',
  8. 'background-position': 'center'
  9. });
  10. });
  11. };
  12. })
  13. .directive('myfocus', function ($timeout, $parse) {
  14. return {
  15. link: function (scope, element, attrs) {
  16. var model = $parse(attrs.myfocus);
  17. scope.$watch(model, function (value) {
  18. if (value === true) {
  19. $timeout(function () {
  20. element[0].focus();
  21. });
  22. } else if (value === false) {
  23. $timeout(function () {
  24. element[0].blur();
  25. });
  26. }
  27. });
  28. }
  29. };
  30. })
  31. .directive('selectUniversity', function () {
  32. return {
  33. restrict: 'EA',
  34. replace: true,
  35. transclude: true,
  36. scope: false,
  37. template: '<input type="text" placeholder="请填写所属院校" readonly="readonly" ng-model="universityname" style="margin-right: 30px" ng-click="showUniversity()">',
  38. controller: function ($scope, $ionicModal, $http, CommentService) {
  39. $scope.universityname = "";
  40. //接收高校选择数据
  41. var reciveuniversity = $scope.$on("recuniversity", function (event, data) {
  42. $scope.universityname = data;
  43. });
  44. //初始化高校数据
  45. this.$onInit = function () {
  46. CommentService.getUniversityList().then(function (response) {
  47. $scope.universitylist = response.universityList;
  48. }, function () {
  49. });
  50. $ionicModal.fromTemplateUrl('./modules/common/views/selectUniversity.html', {
  51. scope: $scope,
  52. animation: 'slide-in-up'
  53. }).then(function (modal) {
  54. $scope.selUni = modal;
  55. })
  56. };
  57. //打开高校列表弹窗
  58. $scope.showUniversity = function () {
  59. $scope.selUni.show();
  60. };
  61. //关闭高校列表弹窗
  62. $scope.backModel = function () {
  63. $scope.selUni.hide();
  64. };
  65. //选择高校
  66. $scope.saveUniversity = function (university) {
  67. $scope.universityname = university.universityname;
  68. var tempobj = {
  69. id: university.universityid,
  70. name: university.universityname
  71. };
  72. $scope.$emit('university', tempobj);
  73. $scope.selUni.hide();
  74. };
  75. $scope.$on('$destroy', function () {
  76. $scope.selUni.hide();
  77. reciveuniversity();
  78. });
  79. }
  80. };
  81. })
  82. .directive('hmsPctSelect', function () {
  83. var TAG = 'hmsPCTSelectDirective';
  84. return {
  85. restrict: 'EA',
  86. scope: {
  87. default: '=defaultdata'
  88. },
  89. replace: true,
  90. transclude: true,
  91. template: '<input type="text" id="activitySchedule_location" placeholder="请选择省/市/区/街道" readonly="readonly" ng-model="registplace" ng-click="toSetDefaultPosition();">',
  92. controller: function ($scope, ConfigService, $ionicModal, $http, $ionicSlideBoxDelegate, $timeout, $rootScope, $ionicScrollDelegate) {
  93. var selectedAddress = {};
  94. $scope.registplace = "";
  95. var addressData;
  96. var areaid = "";
  97. //接收城市选择数据
  98. var reciveregplace = $scope.$on("recregistplace", function (event, data) {
  99. $scope.registplace = data;
  100. });
  101. this.$onInit = function () {
  102. selectedAddress = {};
  103. $scope.selectedAddress = {};
  104. // $on、$emit和$broadcast使得event、data在controller之间的传递变的简单。
  105. // $emit:子传父 传递event与data
  106. // $broadcast:父传子 child controller传递event与data
  107. // $on:监听或接收数据。。用于接收event与data
  108. var url = ConfigService.server + "getRegionsList.action?level=0";
  109. $http.get(url).success(function (res) {
  110. addressData = res;
  111. $scope.provincesData = addressData['86'];
  112. }).error(function (err) {
  113. console.log('area_datas err = ' + angular.toJson(err));
  114. });
  115. $ionicModal.fromTemplateUrl('./modules/common/views/selectCity.html', {
  116. scope: $scope,
  117. animation: 'slide-in-up'
  118. }).then(function (modal) {
  119. $scope.PCTModal = modal;
  120. })
  121. };
  122. $scope.lockSlide = function () {
  123. $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').enableSlide(false);
  124. };
  125. $scope.$watch('default', function (newValue) {
  126. if (newValue) {
  127. $scope.selectedAddress = newValue;
  128. }
  129. });
  130. $scope.toSetDefaultPosition = function () {
  131. $scope.showBackBtn = false;
  132. $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').slide(0);
  133. $ionicScrollDelegate.$getByHandle('PCTSelectProvince').scrollTop();
  134. $scope.PCTModal.show();
  135. };
  136. //选择省
  137. $scope.chooseProvince = function (selectedProvince) {
  138. var selectedProvinceIndex;
  139. angular.forEach($scope.provincesData, function (item, index) {
  140. if (item === selectedProvince) {
  141. selectedProvinceIndex = index;
  142. }
  143. });
  144. selectedAddress = {};
  145. $scope.showBackBtn = true;
  146. areaid = selectedProvinceIndex;
  147. $scope.citiesData = addressData['' + selectedProvinceIndex + ''];
  148. $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').next();
  149. $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').update();
  150. $ionicScrollDelegate.$getByHandle('PCTSelectCity').scrollTop();
  151. selectedAddress.province = selectedProvince;
  152. $scope.registplace = selectedProvince;
  153. var tempobj = {
  154. areaid: areaid,
  155. registplace: $scope.registplace
  156. };
  157. $scope.$emit('registplace', tempobj);
  158. };
  159. //选择市
  160. $scope.chooseCity = function (selectedCity) {
  161. var selectedCityIndex;
  162. angular.forEach($scope.citiesData, function (item, index) {
  163. if (item === selectedCity) {
  164. selectedCityIndex = index;
  165. }
  166. });
  167. areaid = selectedCityIndex;
  168. $scope.townData = addressData['' + selectedCityIndex + ''];
  169. selectedAddress.city = selectedCity;
  170. $scope.registplace = $scope.registplace + selectedAddress.city;
  171. var tempobj = {
  172. areaid: areaid,
  173. registplace: $scope.registplace
  174. };
  175. $scope.$emit('registplace', tempobj);
  176. if (!$scope.townData) {
  177. selectedAddress.town = '';
  178. $scope.selectedAddress = selectedAddress;
  179. $rootScope.$broadcast('PCTSELECT_SUCCESS', {result: $scope.selectedAddress});
  180. $timeout(function () {
  181. $scope.PCTModal.hide();
  182. }, 200);
  183. } else {
  184. $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').next();
  185. $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').update();
  186. $ionicScrollDelegate.$getByHandle('PCTSelectTown').scrollTop();
  187. }
  188. };
  189. //选择县
  190. $scope.chooseTown = function (selectedTown) {
  191. var selectedTownIndex;
  192. angular.forEach($scope.townData, function (item, index) {
  193. if (item === selectedTown) {
  194. selectedTownIndex = index;
  195. }
  196. });
  197. areaid = selectedTownIndex;
  198. $scope.streetData = addressData['' + selectedTownIndex + ''];
  199. selectedAddress.town = selectedTown;
  200. $scope.registplace = $scope.registplace + selectedAddress.town;
  201. var tempobj = {
  202. areaid: areaid,
  203. registplace: $scope.registplace
  204. };
  205. $scope.$emit('registplace', tempobj);
  206. if (!$scope.streetData) {
  207. selectedAddress.street = '';
  208. $scope.selectedAddress = selectedAddress;
  209. $rootScope.$broadcast('PCTSELECT_SUCCESS', {result: $scope.selectedAddress});
  210. $timeout(function () {
  211. $scope.PCTModal.hide();
  212. }, 200);
  213. } else {
  214. $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').next();
  215. $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').update();
  216. $ionicScrollDelegate.$getByHandle('PCTSelectStreet').scrollTop();
  217. }
  218. };
  219. //选择街道
  220. $scope.chooseStreet = function (selectedStreet) {
  221. var selectedStreetIndex;
  222. angular.forEach($scope.streetData, function (item, index) {
  223. if (item === selectedStreet) {
  224. selectedStreetIndex = index;
  225. }
  226. });
  227. areaid = selectedStreetIndex;
  228. selectedAddress.street = selectedStreet;
  229. $scope.registplace = $scope.registplace + selectedAddress.street;
  230. var tempobj = {
  231. areaid: areaid,
  232. registplace: $scope.registplace
  233. };
  234. $scope.$emit('registplace', tempobj);
  235. $scope.selectedAddress = selectedAddress;
  236. $rootScope.$broadcast('PCTSELECT_SUCCESS', {result: $scope.selectedAddress});
  237. $timeout(function () {
  238. $scope.PCTModal.hide();
  239. }, 200);
  240. };
  241. //slide返回上一级
  242. $scope.goBackSlide = function () {
  243. var currentIndex = $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').currentIndex();
  244. if (currentIndex > 0) {
  245. $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').previous();
  246. }
  247. if (currentIndex === 1) {
  248. $scope.showBackBtn = false;
  249. }
  250. };
  251. $scope.cancelSelect = function () {
  252. $scope.PCTModal.hide();
  253. };
  254. $scope.$on('$destroy', function () {
  255. $scope.PCTModal.remove();
  256. reciveregplace();
  257. });
  258. }
  259. };
  260. })
  261. .directive('slideScroll', function () {
  262. return {
  263. restrict: 'AE',
  264. link: function (scope, element, attr) {
  265. var itsWatch = scope.$watch("its", function (newvalue, oldvalue) {
  266. itsWatch();
  267. var i = 0; //element是ul
  268. var length = element[0].children.length;
  269. console.log(length);
  270. setInterval(function () {
  271. // if (i == length / 2) {
  272. if (i > length-1 ) {
  273. i = 0;//初始位置
  274. element[0].style.top = "0px";
  275. }
  276. // var topscorll = -(i * 60);
  277. var topscorll = -(i * 40);
  278. feeltoTop(topscorll);
  279. i++;
  280. }, 2500);
  281. //向上滚动
  282. function feeltoTop(topscorll) {//console.log(topscorll):topscorll是top值
  283. var buchang = -10;
  284. var feelTimer = setInterval(function () {
  285. element[0].style.top = parseInt(element[0].style.top) + buchang + "px";
  286. if (topscorll == -100) {
  287. i = 0;
  288. }
  289. if (parseInt(element[0].style.top) <= topscorll) {
  290. element[0].style.top = topscorll + "px";
  291. window.clearInterval(feelTimer);
  292. }
  293. }, 100);
  294. }
  295. })
  296. }
  297. }
  298. })
  299. .directive('keyboardshow', function($rootScope, $ionicPlatform, $timeout) {
  300. return {
  301. restrict: 'A',
  302. link: function(scope, element) {
  303. window.addEventListener('native.keyboardshow',function (e){
  304. // var hHeight=e.keyboardHeight;
  305. // console.log(hHeight);
  306. // if(hHeight==271){
  307. // e.keyboardHeight=271;
  308. // }else if(hHeight<=271){
  309. // e.keyboardHeight=271;
  310. // }
  311. // $timeout(function(){
  312. // if (device.platform != "Android") {
  313. // angular.element(element).css({
  314. // 'bottom':e.keyboardHeight + 'px'
  315. // });
  316. // }else{
  317. // angular.element(element).css({
  318. // 'bottom':e.keyboardHeight +30+ 'px'
  319. // });
  320. // }
  321. // },50);
  322. $timeout(function(){
  323. if (device.platform != "Android") {
  324. angular.element(element).css({
  325. 'bottom':0 + 'px'
  326. });
  327. }else{
  328. angular.element(element).css({
  329. 'bottom':30+ 'px'
  330. });
  331. }
  332. },50);
  333. });
  334. window.addEventListener('native.keyboardhide',function (e){
  335. angular.element(element).css({
  336. 'bottom':0
  337. });
  338. cordova.plugins.Keyboard.isVisible = true;
  339. $timeout(function() {
  340. cordova.plugins.Keyboard.isVisible = false;
  341. }, 50);
  342. });
  343. }
  344. };
  345. })
  346. .directive('foucusshow', function ($ionicScrollDelegate, $window, $timeout, $ionicPosition) {
  347. return {
  348. restrict: 'A',
  349. scope: false,
  350. link: function (scope, element) {
  351. if (scope.app&&device.platform == "Android") {
  352. window.addEventListener('native.keyboardshow',function (e){
  353. var wHeigth=window.innerHeight;
  354. var eHeight=wHeigth-e.keyboardHeight;
  355. var top = $ionicScrollDelegate.getScrollPosition().top;
  356. var eleTop = ($ionicPosition.offset(element).top);
  357. var realTop = eleTop + top;
  358. $timeout(function () {
  359. if (eleTop<=eHeight) {
  360. $ionicScrollDelegate.$getByHandle('mainScroll').scrollTo(0,realTop);
  361. $ionicScrollDelegate.$getByHandle('mainScroll').resize();
  362. } else {
  363. try {
  364. var aim = $(".commonScrollForm .scroll");
  365. aim.css('transform', 'translate3d(0px,' + '-' + realTop + 'px, 0px) scale(1)');
  366. aim.css('height', '230%');
  367. $timeout(function () {
  368. element[0].focus();
  369. }, 10)
  370. } catch (e) {
  371. }
  372. }
  373. }, 50)
  374. });
  375. }
  376. }
  377. }
  378. });