angular.module('push') .directive('backImg', function () { return function (scope, element, attrs) { attrs.$observe('backImg', function (value) { element.css({ 'background-image': 'url(' + value + ')', 'background-size': '100% 100%', 'background-position': 'center' }); }); }; }) .directive('myfocus', function ($timeout, $parse) { return { link: function (scope, element, attrs) { var model = $parse(attrs.myfocus); scope.$watch(model, function (value) { if (value === true) { $timeout(function () { element[0].focus(); }); } else if (value === false) { $timeout(function () { element[0].blur(); }); } }); } }; }) .directive('selectUniversity', function () { return { restrict: 'EA', replace: true, transclude: true, scope: false, template: '', controller: function ($scope, $ionicModal, $http, CommentService) { $scope.universityname = ""; //接收高校选择数据 var reciveuniversity = $scope.$on("recuniversity", function (event, data) { $scope.universityname = data; }); //初始化高校数据 this.$onInit = function () { CommentService.getUniversityList().then(function (response) { $scope.universitylist = response.universityList; }, function () { }); $ionicModal.fromTemplateUrl('./modules/common/views/selectUniversity.html', { scope: $scope, animation: 'slide-in-up' }).then(function (modal) { $scope.selUni = modal; }) }; //打开高校列表弹窗 $scope.showUniversity = function () { $scope.selUni.show(); }; //关闭高校列表弹窗 $scope.backModel = function () { $scope.selUni.hide(); }; //选择高校 $scope.saveUniversity = function (university) { $scope.universityname = university.universityname; var tempobj = { id: university.universityid, name: university.universityname }; $scope.$emit('university', tempobj); $scope.selUni.hide(); }; $scope.$on('$destroy', function () { $scope.selUni.hide(); reciveuniversity(); }); } }; }) .directive('hmsPctSelect', function () { var TAG = 'hmsPCTSelectDirective'; return { restrict: 'EA', scope: { default: '=defaultdata' }, replace: true, transclude: true, template: '', controller: function ($scope, ConfigService, $ionicModal, $http, $ionicSlideBoxDelegate, $timeout, $rootScope, $ionicScrollDelegate) { var selectedAddress = {}; $scope.registplace = ""; var addressData; var areaid = ""; //接收城市选择数据 var reciveregplace = $scope.$on("recregistplace", function (event, data) { $scope.registplace = data; }); this.$onInit = function () { selectedAddress = {}; $scope.selectedAddress = {}; // $on、$emit和$broadcast使得event、data在controller之间的传递变的简单。 // $emit:子传父 传递event与data // $broadcast:父传子 child controller传递event与data // $on:监听或接收数据。。用于接收event与data var url = ConfigService.server + "getRegionsList.action?level=0"; $http.get(url).success(function (res) { addressData = res; $scope.provincesData = addressData['86']; }).error(function (err) { console.log('area_datas err = ' + angular.toJson(err)); }); $ionicModal.fromTemplateUrl('./modules/common/views/selectCity.html', { scope: $scope, animation: 'slide-in-up' }).then(function (modal) { $scope.PCTModal = modal; }) }; $scope.lockSlide = function () { $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').enableSlide(false); }; $scope.$watch('default', function (newValue) { if (newValue) { $scope.selectedAddress = newValue; } }); $scope.toSetDefaultPosition = function () { $scope.showBackBtn = false; $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').slide(0); $ionicScrollDelegate.$getByHandle('PCTSelectProvince').scrollTop(); $scope.PCTModal.show(); }; //选择省 $scope.chooseProvince = function (selectedProvince) { var selectedProvinceIndex; angular.forEach($scope.provincesData, function (item, index) { if (item === selectedProvince) { selectedProvinceIndex = index; } }); selectedAddress = {}; $scope.showBackBtn = true; areaid = selectedProvinceIndex; $scope.citiesData = addressData['' + selectedProvinceIndex + '']; $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').next(); $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').update(); $ionicScrollDelegate.$getByHandle('PCTSelectCity').scrollTop(); selectedAddress.province = selectedProvince; $scope.registplace = selectedProvince; var tempobj = { areaid: areaid, registplace: $scope.registplace }; $scope.$emit('registplace', tempobj); }; //选择市 $scope.chooseCity = function (selectedCity) { var selectedCityIndex; angular.forEach($scope.citiesData, function (item, index) { if (item === selectedCity) { selectedCityIndex = index; } }); areaid = selectedCityIndex; $scope.townData = addressData['' + selectedCityIndex + '']; selectedAddress.city = selectedCity; $scope.registplace = $scope.registplace + selectedAddress.city; var tempobj = { areaid: areaid, registplace: $scope.registplace }; $scope.$emit('registplace', tempobj); if (!$scope.townData) { selectedAddress.town = ''; $scope.selectedAddress = selectedAddress; $rootScope.$broadcast('PCTSELECT_SUCCESS', {result: $scope.selectedAddress}); $timeout(function () { $scope.PCTModal.hide(); }, 200); } else { $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').next(); $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').update(); $ionicScrollDelegate.$getByHandle('PCTSelectTown').scrollTop(); } }; //选择县 $scope.chooseTown = function (selectedTown) { var selectedTownIndex; angular.forEach($scope.townData, function (item, index) { if (item === selectedTown) { selectedTownIndex = index; } }); areaid = selectedTownIndex; $scope.streetData = addressData['' + selectedTownIndex + '']; selectedAddress.town = selectedTown; $scope.registplace = $scope.registplace + selectedAddress.town; var tempobj = { areaid: areaid, registplace: $scope.registplace }; $scope.$emit('registplace', tempobj); if (!$scope.streetData) { selectedAddress.street = ''; $scope.selectedAddress = selectedAddress; $rootScope.$broadcast('PCTSELECT_SUCCESS', {result: $scope.selectedAddress}); $timeout(function () { $scope.PCTModal.hide(); }, 200); } else { $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').next(); $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').update(); $ionicScrollDelegate.$getByHandle('PCTSelectStreet').scrollTop(); } }; //选择街道 $scope.chooseStreet = function (selectedStreet) { var selectedStreetIndex; angular.forEach($scope.streetData, function (item, index) { if (item === selectedStreet) { selectedStreetIndex = index; } }); areaid = selectedStreetIndex; selectedAddress.street = selectedStreet; $scope.registplace = $scope.registplace + selectedAddress.street; var tempobj = { areaid: areaid, registplace: $scope.registplace }; $scope.$emit('registplace', tempobj); $scope.selectedAddress = selectedAddress; $rootScope.$broadcast('PCTSELECT_SUCCESS', {result: $scope.selectedAddress}); $timeout(function () { $scope.PCTModal.hide(); }, 200); }; //slide返回上一级 $scope.goBackSlide = function () { var currentIndex = $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').currentIndex(); if (currentIndex > 0) { $ionicSlideBoxDelegate.$getByHandle('PCTSelectDelegate').previous(); } if (currentIndex === 1) { $scope.showBackBtn = false; } }; $scope.cancelSelect = function () { $scope.PCTModal.hide(); }; $scope.$on('$destroy', function () { $scope.PCTModal.remove(); reciveregplace(); }); } }; }) .directive('slideScroll', function () { return { restrict: 'AE', link: function (scope, element, attr) { var itsWatch = scope.$watch("its", function (newvalue, oldvalue) { itsWatch(); var i = 0; //element是ul var length = element[0].children.length; console.log(length); setInterval(function () { // if (i == length / 2) { if (i > length-1 ) { i = 0;//初始位置 element[0].style.top = "0px"; } // var topscorll = -(i * 60); var topscorll = -(i * 40); feeltoTop(topscorll); i++; }, 2500); //向上滚动 function feeltoTop(topscorll) {//console.log(topscorll):topscorll是top值 var buchang = -10; var feelTimer = setInterval(function () { element[0].style.top = parseInt(element[0].style.top) + buchang + "px"; if (topscorll == -100) { i = 0; } if (parseInt(element[0].style.top) <= topscorll) { element[0].style.top = topscorll + "px"; window.clearInterval(feelTimer); } }, 100); } }) } } }) .directive('keyboardshow', function($rootScope, $ionicPlatform, $timeout) { return { restrict: 'A', link: function(scope, element) { window.addEventListener('native.keyboardshow',function (e){ // var hHeight=e.keyboardHeight; // console.log(hHeight); // if(hHeight==271){ // e.keyboardHeight=271; // }else if(hHeight<=271){ // e.keyboardHeight=271; // } // $timeout(function(){ // if (device.platform != "Android") { // angular.element(element).css({ // 'bottom':e.keyboardHeight + 'px' // }); // }else{ // angular.element(element).css({ // 'bottom':e.keyboardHeight +30+ 'px' // }); // } // },50); $timeout(function(){ if (device.platform != "Android") { angular.element(element).css({ 'bottom':0 + 'px' }); }else{ angular.element(element).css({ 'bottom':30+ 'px' }); } },50); }); window.addEventListener('native.keyboardhide',function (e){ angular.element(element).css({ 'bottom':0 }); cordova.plugins.Keyboard.isVisible = true; $timeout(function() { cordova.plugins.Keyboard.isVisible = false; }, 50); }); } }; }) .directive('foucusshow', function ($ionicScrollDelegate, $window, $timeout, $ionicPosition) { return { restrict: 'A', scope: false, link: function (scope, element) { if (scope.app&&device.platform == "Android") { window.addEventListener('native.keyboardshow',function (e){ var wHeigth=window.innerHeight; var eHeight=wHeigth-e.keyboardHeight; var top = $ionicScrollDelegate.getScrollPosition().top; var eleTop = ($ionicPosition.offset(element).top); var realTop = eleTop + top; $timeout(function () { if (eleTop<=eHeight) { $ionicScrollDelegate.$getByHandle('mainScroll').scrollTo(0,realTop); $ionicScrollDelegate.$getByHandle('mainScroll').resize(); } else { try { var aim = $(".commonScrollForm .scroll"); aim.css('transform', 'translate3d(0px,' + '-' + realTop + 'px, 0px) scale(1)'); aim.css('height', '230%'); $timeout(function () { element[0].focus(); }, 10) } catch (e) { } } }, 50) }); } } } });