-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.js
123 lines (111 loc) · 3.47 KB
/
default.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var _ = require('../../util')
module.exports = {
bind: function () {
var self = this
var el = this.el
// check params
// - lazy: update model on "change" instead of "input"
var lazy = this._checkParam('lazy') != null
// - number: cast value into number when updating model.
var number = this._checkParam('number') != null
// handle composition events.
// http://blog.evanyou.me/2014/01/03/composition-event/
var cpLocked = false
this.cpLock = function () {
cpLocked = true
}
this.cpUnlock = function () {
cpLocked = false
// in IE11 the "compositionend" event fires AFTER
// the "input" event, so the input handler is blocked
// at the end... have to call it here.
set()
}
_.on(el,'compositionstart', this.cpLock)
_.on(el,'compositionend', this.cpUnlock)
// shared setter
function set () {
self.set(
number ? _.toNumber(el.value) : el.value,
true
)
}
// if the directive has filters, we need to
// record cursor position and restore it after updating
// the input with the filtered value.
// also force update for type="range" inputs to enable
// "lock in range" (see #506)
this.listener = this.filters || el.type === 'range'
? function textInputListener () {
if (cpLocked) return
var charsOffset
// some HTML5 input types throw error here
try {
// record how many chars from the end of input
// the cursor was at
charsOffset = el.value.length - el.selectionStart
} catch (e) {}
// Fix IE10/11 infinite update cycle
// https://github.com/yyx990803/vue/issues/592
/* istanbul ignore if */
if (charsOffset < 0) {
return
}
set()
_.nextTick(function () {
// force a value update, because in
// certain cases the write filters output the
// same result for different input values, and
// the Observer set events won't be triggered.
var newVal = self._watcher.value
self.update(newVal)
if (charsOffset != null) {
var cursorPos =
_.toString(newVal).length - charsOffset
el.setSelectionRange(cursorPos, cursorPos)
}
})
}
: function textInputListener () {
if (cpLocked) return
set()
}
this.event = lazy ? 'change' : 'input'
_.on(el, this.event, this.listener)
// IE9 doesn't fire input event on backspace/del/cut
if (!lazy && _.isIE9) {
this.onCut = function () {
_.nextTick(self.listener)
}
this.onDel = function (e) {
if (e.keyCode === 46 || e.keyCode === 8) {
self.listener()
}
}
_.on(el, 'cut', this.onCut)
_.on(el, 'keyup', this.onDel)
}
// set initial value if present
if (
el.hasAttribute('value') ||
(el.tagName === 'TEXTAREA' && el.value.trim())
) {
this._initValue = number
? _.toNumber(el.value)
: el.value
}
},
update: function (value) {
this.el.value = _.toString(value)
},
unbind: function () {
var el = this.el
_.off(el, this.event, this.listener)
_.off(el,'compositionstart', this.cpLock)
_.off(el,'compositionend', this.cpUnlock)
if (this.onCut) {
_.off(el,'cut', this.onCut)
_.off(el,'keyup', this.onDel)
}
}
}