1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HiPanel core package |
4
|
|
|
* |
5
|
|
|
* @link https://hipanel.com/ |
6
|
|
|
* @package hipanel-core |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\tests\_support\Page\Widget\Input; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Select2. |
15
|
|
|
* |
16
|
|
|
* Represent Select2 input element. |
17
|
|
|
*/ |
18
|
|
|
class Select2 extends TestableInput |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
protected function getSearchSelector(): string |
24
|
|
|
{ |
25
|
|
|
return self::AS_BASE . "div[data-title='{$this->title}']>select"; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
protected function getFilterSelector(): string |
32
|
|
|
{ |
33
|
|
|
return self::TF_BASE . "select[id*={$this->auxName}]"; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $value |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
public function setValue(string $value): void |
41
|
|
|
{ |
42
|
|
|
if ($value === '') { |
43
|
|
|
$this->removeChosenOption(); |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
$this->open(); |
47
|
|
|
$this->fillSearchField($value); |
48
|
|
|
$this->chooseOption($value); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $value |
53
|
|
|
* @throws \Exception |
54
|
|
|
*/ |
55
|
|
|
public function setValueLike(string $value): void |
56
|
|
|
{ |
57
|
|
|
$this->open(); |
58
|
|
|
$this->fillSearchField($value); |
59
|
|
|
$this->chooseOptionLike($value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return Select2 |
64
|
|
|
*/ |
65
|
|
|
public function open(): Select2 |
66
|
|
|
{ |
67
|
|
|
$this->tester->click($this->getClickSelector()); |
68
|
|
|
$this->seeIsOpened(); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return Select2 |
75
|
|
|
*/ |
76
|
|
|
public function seeIsOpened(): Select2 |
77
|
|
|
{ |
78
|
|
|
$this->tester->seeElement('.select2-container--open'); |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getClickSelector(): string |
87
|
|
|
{ |
88
|
|
|
return $this->selector . ' + span [role=combobox]'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $name |
93
|
|
|
* @throws \Exception |
94
|
|
|
* @return Select2 |
95
|
|
|
*/ |
96
|
|
|
public function fillSearchField(string $name): Select2 |
97
|
|
|
{ |
98
|
|
|
$inputSelector = '.select2-container--open input.select2-search__field'; |
99
|
|
|
$this->tester->fillField($inputSelector, $name); |
100
|
|
|
$this->tester->waitForElementNotVisible('.loading-results', 120); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $optionName |
107
|
|
|
* @return Select2 |
108
|
|
|
*/ |
109
|
|
|
public function chooseOption($optionName): Select2 |
110
|
|
|
{ |
111
|
|
|
$this->tester->executeJS(<<<JS |
112
|
|
|
$(".select2-results li:contains('{$optionName}')").each(function() { |
113
|
|
|
if (this.firstChild.data === '{$optionName}') { |
114
|
|
|
$(this).trigger('mouseup'); |
115
|
|
|
} |
116
|
|
|
}); |
117
|
|
|
JS |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $optionName |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
|
|
public function chooseOptionLike($optionName): Select2 |
128
|
|
|
{ |
129
|
|
|
$this->tester->executeJS(<<<JS |
130
|
|
|
$(".select2-results li:contains('{$optionName}')").each(function() { |
131
|
|
|
if (this.firstChild.data.indexOf('{$optionName}') !== -1) { |
132
|
|
|
$(this).trigger('mouseup'); |
133
|
|
|
} |
134
|
|
|
}); |
135
|
|
|
JS |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return Select2 |
143
|
|
|
*/ |
144
|
|
|
public function removeChosenOption(): Select2 |
145
|
|
|
{ |
146
|
|
|
$this->tester->executeJS(<<<JS |
147
|
|
|
document.querySelector('{$this->getSelector()}').innerHTML += '<option value="" selected></option>' |
148
|
|
|
JS |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|