-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdom-helper.js
More file actions
164 lines (130 loc) · 4.53 KB
/
dom-helper.js
File metadata and controls
164 lines (130 loc) · 4.53 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
angular.module('jdalt.toolBox')
.factory('DomHelper', function(
$httpBackend
) {
function DomHelper(root) {
var booleanInputs = 'input[type=checkbox], input[type=radio], option'
function normalizeText(str) {
if (typeof str !== 'string') throw new Error('normalizeWhitespace called with a non-string argument: ' + typeof str)
return str.replace(/\s+/g, ' ').trim()
}
function getVal(input) {
input = input.first()
return input.is(booleanInputs) ? input.prop(input.is('option') ? 'selected' : 'checked') : input.val()
}
function setVal(input, value) {
// input.focus() // triggers directive binding
input.filter(booleanInputs).each(setBooleanInput)
input.not(booleanInputs).val(value).triggerHandler('change')//.triggerHandler('blur')
function setBooleanInput() {
var $input = angular.element(this)
var isOption = input.is('option')
var prop = isOption ? 'selected' : 'checked'
var $changer = isOption ? $input.parents('select') : $input
var triggerChange = !!value !== $input.prop(prop)
$input.prop(prop, !!value) // Force attribute state for $setViewValue
if (!isOption) $input.triggerHandler('click') // Trigger click handler (will call $setViewValue)
if (triggerChange) $changer.triggerHandler('change')
// if (scope) scope.$digest() // just in case something is $watching
}
}
return {
$el: root, // jQuery wrapped element
element: root[0], // first or only plain dom object
flush: $httpBackend.flush,
log: function() {
root.each(function(index, el) {
console.log('element', index, el)
})
return this
},
click: function(selector, nth) {
if(!selector) { // proxy click on root
root.click()
return this
}
var clickEl = root.find(selector)
if(!clickEl.length) {
throw new Error('Element "'+ selector +'" not found to click')
}
if(nth != null) clickEl = clickEl.eq(nth)
clickEl.click()
return DomHelper(clickEl)
},
triggerEvent: function(type, eventProps) {
var ev = $.Event(type, eventProps)
root.trigger(ev)
return this
},
syntheticMouseEvent: function(type, x, y, overrideEl) {
var el
if(overrideEl) {
el = overrideEl
} else {
el = root[0]
}
if(!x && !y) {
x = y = 0
}
var ev = document.createEvent('MouseEvent')
ev.initMouseEvent(
type,
true /* bubble */, true /* cancelable */,
window, null,
x, y, x, y, /* coordinates */
false, false, false, false, /* modifier keys */
0 /*left*/, null
)
el.dispatchEvent(ev)
},
clickButton: function(buttonText) {
var buttonEl = root.find('button:contains("' + buttonText + '")')
if(buttonEl.length == 0) throw new Error('<button>' + buttonText + '</button> not found')
if(buttonEl.length >= 2) throw new Error('More than one <button>' + buttonText + '</button> found')
buttonEl.click()
return DomHelper(buttonEl)
},
setInputValue: function(selector, inputVal) {
var inputEl = root.find(selector)
if(!inputEl.length) {
throw new Error('Element "'+ selector +'" not found to setInputValue')
}
setVal(inputEl, inputVal)
return DomHelper(inputEl)
},
val: function(value) {
if(arguments.length > 0) setVal(root, value)
return getVal(root)
},
cssClasses: function() {
return root.prop('class')
},
hasClass: function(cssClass) {
return root.hasClass(cssClass)
},
count: function(selector) {
var el = root
if(selector) el = root.find(selector)
return el.length
},
find: function(selector, nth) {
var el = root.find(selector)
if(nth != null) el = el.eq(nth)
return DomHelper(el)
},
text: function(selector, nth, options) {
var el = root
if(selector) el = root.find(selector)
if(!options && nth && !angular.isNumber(nth)) options = nth
if(nth != null && angular.isNumber(nth)) el = el.eq(nth)
if(options && options.normalize === false) {
return el.text()
} else {
return normalizeText(el.text())
}
},
normalizeText: normalizeText
}
}
return DomHelper
})