1+ angular . module ( 'z-range' , [ 'ionic' ] )
2+ . directive ( 'zRange' , function ( ) {
3+ return {
4+ restrict : 'E' ,
5+ scope : {
6+ realValue : '=zValue' ,
7+ rangeId : '@zId' ,
8+ min : '@zMin' ,
9+ max : '@zMax' ,
10+ step :'@zStep' ,
11+ position : '=zPosition' , //向外输出滚动百分比
12+ onReleaseCallBack : '&zOnReleaseCallback' ,
13+ onReleaseCallBackMark : '@zOnReleaseCallbackMark'
14+ } ,
15+ controller : "rangeController" ,
16+ templateUrl : 'range/z-range.html'
17+ } ;
18+ } )
19+ . controller ( 'rangeController' , function ( $ionicScrollDelegate , $timeout , $scope ) {
20+ var mark = 1 ;
21+ $scope . onScroll = function ( ) {
22+ var width = document . getElementById ( $scope . rangeId ) . clientWidth ;
23+ var max = $scope . max ;
24+ var min = $scope . min ;
25+ $scope . position = ( 1 - ( ( $ionicScrollDelegate . $getByHandle ( 'handle' + $scope . rangeId )
26+ . getScrollPosition ( ) . left ) * 0.531241 / 0.47 / width ) ) ;
27+ var newValue = ( min * 1 + Math . round ( ( max - min ) * ( 1 - ( ( $ionicScrollDelegate . $getByHandle ( 'handle' + $scope . rangeId )
28+ . getScrollPosition ( ) . left ) * 0.531241 / 0.47 / width ) ) ) ) ;
29+ //console.log("nw:"+newValue+" max:"+$scope.max+" min:"+$scope.min+" with:"+width);
30+ if ( ! ( $scope . value == newValue ) ) ( $scope . value = newValue ) ;
31+ if ( $scope . value < min ) {
32+ $scope . value = min ;
33+ }
34+ $scope . realValue = $scope . value * $scope . step ;
35+ mark = 1 ;
36+ } ;
37+ $scope . onRelease = function ( ) {
38+
39+ $ionicScrollDelegate . $getByHandle ( 'handle' + $scope . rangeId ) . scrollTo ( $scope . getPosition ( ) , 0 , false ) ;
40+
41+ $ionicScrollDelegate . $getByHandle ( 'handle' + $scope . rangeId ) . freezeScroll ( true ) ;
42+
43+ if ( angular . isDefined ( $scope . onReleaseCallBackMark ) ) {
44+ $scope . onReleaseCallback ( ) ;
45+ //console.log($scope.onReleaseCallback);
46+ }
47+ mark = 0 ;
48+ } ;
49+ $scope . onOutTouch = function ( ) {
50+ if ( $scope . value != $scope . max && $ionicScrollDelegate . $getByHandle ( 'handle' + $scope . rangeId )
51+ . getScrollPosition ( ) . left == 0 ) {
52+ $ionicScrollDelegate . $getByHandle ( 'handle' + $scope . rangeId ) . scrollTo ( $scope . getPosition ( ) , 0 , false ) ;
53+ $ionicScrollDelegate . $getByHandle ( 'handle' + $scope . rangeId ) . freezeScroll ( false ) ;
54+ }
55+
56+ } ;
57+ $scope . onTouch = function ( ) {
58+ $ionicScrollDelegate . $getByHandle ( 'handle' + $scope . rangeId ) . freezeScroll ( false ) ;
59+ } ;
60+ $scope . getPosition = function ( ) {
61+ var width = document . getElementById ( $scope . rangeId ) . clientWidth ;
62+ var max = $scope . max ;
63+ var min = $scope . min ;
64+ $scope . position = ( $scope . value - min * 1 ) / ( max - min ) ;
65+ var position = ( ( 1 - ( $scope . value - min * 1 ) / ( max - min ) ) * width * 0.47 / 0.53 ) ;
66+ //console.log((1 - ($scope.value - min * 1) / (max - min)) * width*0.47/0.53);
67+ return position ;
68+ } ;
69+ $scope . doInit = function ( ) {
70+ angular . element ( document ) . ready ( function ( ) {
71+ mark = 0 ;
72+ if ( angular . isUndefined ( $scope . step ) ) { $scope . step = 1 ; }
73+ $scope . max = $scope . max / $scope . step ;
74+ $scope . min = $scope . min / $scope . step ;
75+ $scope . value = $scope . realValue / $scope . step ;
76+
77+ $ionicScrollDelegate . $getByHandle ( 'handle' + $scope . rangeId ) . scrollTo ( $scope . getPosition ( ) , 0 , false ) ;
78+ $ionicScrollDelegate . $getByHandle ( 'handle' + $scope . rangeId ) . freezeScroll ( true ) ;
79+ //当max过大时,比如3000,range初始化时会出现同步问题,这里微调value,触发双向绑定,使range同步
80+ //var tempValue = $scope.value;
81+ //$scope.value = $scope.value * 1 + 0.01;
82+ //$timeout(function () {
83+ // $scope.value = Math.round(tempValue);
84+ //}, 0);
85+ //当max过大时,比如3000,range初始化时会出现同步问题,这里微调value,触发双向绑定,使range同步
86+ } ) ;
87+ } ;
88+ $scope . addOnClick = function ( event ) {
89+ var width = document . getElementById ( $scope . rangeId ) . clientWidth ;
90+ var max = $scope . max ;
91+ var min = $scope . min ;
92+ var el = document . getElementById ( $scope . rangeId ) ;
93+ for ( var lx = 0 , ly = 0 ; el != null ; lx += el . offsetLeft , ly += el . offsetTop , el = el . offsetParent ) ;
94+ $scope . value = Math . round ( ( event . x - lx ) / width * ( max - min ) + min * 1 ) ;
95+ if ( $scope . value < min ) {
96+ $scope . value = min ;
97+ }
98+ if ( $scope . value > max ) {
99+ $scope . value = max ;
100+ }
101+ $ionicScrollDelegate . $getByHandle ( 'handle' + $scope . rangeId ) . scrollTo ( $scope . getPosition ( ) , 0 , false ) ;
102+ } ;
103+
104+ $scope . $watch ( 'value' , function ( ) {
105+ if ( mark == 0 ) {
106+ var max = $scope . max ;
107+ var min = $scope . min ;
108+ if ( $scope . value * 1 < min * 1 ) {
109+ $scope . value = min ;
110+ }
111+ if ( $scope . value * 1 > max * 1 ) {
112+ $scope . value = max ;
113+ }
114+ $ionicScrollDelegate . $getByHandle ( 'handle' + $scope . rangeId ) . scrollTo ( $scope . getPosition ( ) , 0 , false ) ;
115+ $scope . realValue = $scope . value * $scope . step ;
116+ }
117+ } ) ;
118+
119+ $scope . $watch ( 'realValue' , function ( ) {
120+ if ( mark == 0 ) {
121+ $scope . value = $scope . realValue / $scope . step ;
122+ }
123+ } ) ;
124+
125+ } ) ;
0 commit comments