|
3 | 3 |
|
4 | 4 | ionic.views.Toggle = ionic.views.View.inherit({
|
5 | 5 | initialize: function(opts) {
|
| 6 | + var self = this; |
| 7 | + |
6 | 8 | this.el = opts.el;
|
7 | 9 | this.checkbox = opts.checkbox;
|
8 | 10 | this.track = opts.track;
|
9 | 11 | this.handle = opts.handle;
|
10 | 12 | this.openPercent = -1;
|
| 13 | + this.onChange = opts.onChange || function() {}; |
| 14 | + |
| 15 | + this.triggerThreshold = opts.triggerThreshold || 20; |
| 16 | + |
| 17 | + this.dragStartHandler = function(e) { |
| 18 | + self.dragStart(e); |
| 19 | + }; |
| 20 | + this.dragHandler = function(e) { |
| 21 | + self.drag(e); |
| 22 | + }; |
| 23 | + this.holdHandler = function(e) { |
| 24 | + self.hold(e); |
| 25 | + }; |
| 26 | + this.releaseHandler = function(e) { |
| 27 | + self.release(e); |
| 28 | + }; |
| 29 | + |
| 30 | + this.dragStartGesture = ionic.onGesture('dragstart', this.dragStartHandler, this.el); |
| 31 | + this.dragGesture = ionic.onGesture('drag', this.dragHandler, this.el); |
| 32 | + this.dragHoldGesture = ionic.onGesture('hold', this.holdHandler, this.el); |
| 33 | + this.dragReleaseGesture = ionic.onGesture('release', this.releaseHandler, this.el); |
| 34 | + }, |
| 35 | + |
| 36 | + destroy: function() { |
| 37 | + ionic.offGesture(this.dragStartGesture, 'dragstart', this.dragStartGesture); |
| 38 | + ionic.offGesture(this.dragGesture, 'drag', this.dragGesture); |
| 39 | + ionic.offGesture(this.dragHoldGesture, 'hold', this.holdHandler); |
| 40 | + ionic.offGesture(this.dragReleaseGesture, 'release', this.releaseHandler); |
11 | 41 | },
|
12 | 42 |
|
13 | 43 | tap: function(e) {
|
|
16 | 46 | }
|
17 | 47 | },
|
18 | 48 |
|
| 49 | + dragStart: function(e) { |
| 50 | + if(this.checkbox.disabled) return; |
| 51 | + |
| 52 | + this._dragInfo = { |
| 53 | + width: this.el.offsetWidth, |
| 54 | + left: this.el.offsetLeft, |
| 55 | + right: this.el.offsetLeft + this.el.offsetWidth, |
| 56 | + triggerX: this.el.offsetWidth / 2, |
| 57 | + initialState: this.checkbox.checked |
| 58 | + }; |
| 59 | + |
| 60 | + // Stop any parent dragging |
| 61 | + e.gesture.srcEvent.preventDefault(); |
| 62 | + |
| 63 | + // Trigger hold styles |
| 64 | + this.hold(e); |
| 65 | + }, |
| 66 | + |
19 | 67 | drag: function(e) {
|
20 |
| - var slidePageLeft = this.track.offsetLeft + (this.handle.offsetWidth / 2); |
21 |
| - var slidePageRight = this.track.offsetLeft + this.track.offsetWidth - (this.handle.offsetWidth / 2); |
22 |
| - |
23 |
| - if(e.pageX >= slidePageRight - 4) { |
24 |
| - this.val(true); |
25 |
| - } else if(e.pageX <= slidePageLeft) { |
26 |
| - this.val(false); |
27 |
| - } else { |
28 |
| - this.setOpenPercent( Math.round( (1 - ((slidePageRight - e.pageX) / (slidePageRight - slidePageLeft) )) * 100) ); |
29 |
| - } |
| 68 | + var self = this; |
| 69 | + if(!this._dragInfo) { return; } |
| 70 | + |
| 71 | + // Stop any parent dragging |
| 72 | + e.gesture.srcEvent.preventDefault(); |
| 73 | + |
| 74 | + ionic.requestAnimationFrame(function(amount) { |
| 75 | + |
| 76 | + var slidePageLeft = self.track.offsetLeft + (self.handle.offsetWidth / 2); |
| 77 | + var slidePageRight = self.track.offsetLeft + self.track.offsetWidth - (self.handle.offsetWidth / 2); |
| 78 | + var dx = e.gesture.deltaX; |
| 79 | + |
| 80 | + var px = e.gesture.touches[0].pageX - self._dragInfo.left; |
| 81 | + var mx = self._dragInfo.width - self.triggerThreshold; |
| 82 | + |
| 83 | + // The initial state was on, so "tend towards" on |
| 84 | + if(self._dragInfo.initialState) { |
| 85 | + if(px < self.triggerThreshold) { |
| 86 | + self.setOpenPercent(0); |
| 87 | + } else if(px > self._dragInfo.triggerX) { |
| 88 | + self.setOpenPercent(100); |
| 89 | + } |
| 90 | + } else { |
| 91 | + // The initial state was off, so "tend towards" off |
| 92 | + if(px < self._dragInfo.triggerX) { |
| 93 | + self.setOpenPercent(0); |
| 94 | + } else if(px > mx) { |
| 95 | + self.setOpenPercent(100); |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | + }, |
| 100 | + |
| 101 | + endDrag: function(e) { |
| 102 | + this._dragInfo = null; |
| 103 | + }, |
| 104 | + |
| 105 | + hold: function(e) { |
| 106 | + this.el.classList.add('dragging'); |
| 107 | + }, |
| 108 | + release: function(e) { |
| 109 | + this.el.classList.remove('dragging'); |
| 110 | + this.endDrag(e); |
30 | 111 | },
|
31 | 112 |
|
| 113 | + |
32 | 114 | setOpenPercent: function(openPercent) {
|
33 | 115 | // only make a change if the new open percent has changed
|
34 | 116 | if(this.openPercent < 0 || (openPercent < (this.openPercent - 3) || openPercent > (this.openPercent + 3) ) ) {
|
|
46 | 128 | }
|
47 | 129 | },
|
48 | 130 |
|
49 |
| - release: function(e) { |
50 |
| - this.val( this.openPercent >= 50 ); |
51 |
| - }, |
52 |
| - |
53 | 131 | val: function(value) {
|
54 | 132 | if(value === true || value === false) {
|
55 | 133 | if(this.handle.style[ionic.CSS.TRANSFORM] !== "") {
|
56 | 134 | this.handle.style[ionic.CSS.TRANSFORM] = "";
|
57 | 135 | }
|
58 | 136 | this.checkbox.checked = value;
|
59 | 137 | this.openPercent = (value ? 100 : 0);
|
| 138 | + this.onChange && this.onChange(); |
60 | 139 | }
|
61 | 140 | return this.checkbox.checked;
|
62 | 141 | }
|
|
0 commit comments