Completed
Push — master ( e138c6...ff3199 )
by De Cramer
01:42 queued 01:39
created

Dropdown::getSelectedValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
3
namespace eXpansion\Framework\Gui\Components;
4
5
use eXpansion\Framework\Gui\Layouts\LayoutRow;
6
use FML\Controls\Entry;
7
use FML\Controls\Frame;
8
use FML\Controls\Label as FMLLabel;
9
use FML\Script\Features\ScriptFeature;
10
use FML\Script\Script;
11
use FML\Script\ScriptLabel;
12
use FML\Types\ScriptFeatureable;
13
14
class Dropdown extends AbstractUiElement implements ScriptFeatureable
15
{
16
    /** @var int */
17
    protected $selectedIndex;
18
19
    /** @var bool */
20
    protected $isOpened;
21
22
    /** @var string */
23
    protected $name;
24
25
    /** @var string[] */
26
    protected $options;
27
28
    /** @var string */
29
    protected $id = "";
30
31
    /**
32
     * uiDropdown constructor \n
33
     *
34
     * Options ["display name" => "value"], both needs to be string
35
     *
36
     * @param string $name entry name for return value
37
     * @param array  $options Options ["display name" => "value"], both needs to be string
38
     * @param int    $selectedIndex -1 for none
39
     * @param bool   $isOpened is the box opened by default
40
     */
41
    public function __construct($name, $options, $selectedIndex = -1, $isOpened = false)
42
    {
43
        $this->name = $name;
44
        $this->options = $options;
45
        $this->selectedIndex = $selectedIndex;
46
        $this->isOpened = $isOpened;
47
        $this->setSize(30, 4);
48
    }
49
50
    /**
51
     * Prepare the given Script for rendering by adding the needed Labels, etc.
52
     *
53
     * @param Script $script Script to prepare
54
     * @return void
55
     */
56
    public function prepare(Script $script)
57
    {
58
        $script->addScriptFunction("uiDropdownFunctions", $this->getScriptDropdown());
59
        $script->addCustomScriptLabel(ScriptLabel::MouseClick, $this->getScriptMouseClick());
60
        $script->addCustomScriptLabel(ScriptLabel::OnInit, $this->getScriptInit());
61
    }
62
63
    protected function getScriptInit()
64
    {
65
        return /** language=textmate  prefix=#RequireContext\n */
66
            <<<'EOD'
67
            Page.GetClassChildren ("uiContainer", Page.MainFrame, True);
68
            foreach (frame in Page.GetClassChildren_Result) {
69
               if (frame.HasClass("uiDropdown")) {
70
					uiRenderDropdown((frame as CMlFrame));				
71
				}					
72
            }
73
74
EOD;
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    protected function getScriptMouseClick()
81
    {
82
        return /** language=textmate  prefix=#RequireContext\n */
83
            <<<'EOD'
84
            
85
            if (Event.Control.HasClass("uiSelectElement")) {
86
                if (Event.Control.Parent.HasClass("uiDropdown")) {									
87
                    uiToggleDropdown(Event.Control.Parent);	
88
                }		
89
                if (Event.Control.Parent.HasClass("uiDropdownSelect")) {			
90
                    uiSelectDropdown((Event.Control as CMlLabel));	
91
                }
92
		    }	
93
		    																
94
EOD;
95
    }
96
97
98
    /**
99
     * @return string
100
     */
101
    protected function getScriptDropdown()
102
    {
103
        return /** @lang textmate */
104
            <<<'EOD'
105
106
            Void uiRenderDropdown(CMlFrame frame) {
107
                declare selected = TextLib::ToInteger(frame.DataAttributeGet("selected"));
108
                declare index = 0;
109
                                             
110
                declare options = (frame.Controls[3] as CMlFrame);
111
                
112
                    if (frame.DataAttributeGet("open") == "1") {		   
113
                        frame.Controls[3].Show();
114
                    } else {
115
                         frame.Controls[3].Hide();
116
                    }                                
117
                                                      
118
                    foreach (option in options.Controls) {                        
119
                        if (selected == index) {                                                                          
120
                            (frame.Controls[1] as CMlLabel).Value = (option as CMlLabel).Value;
121
                            (frame.Controls[2] as CMlEntry).Value = option.DataAttributeGet("value");
122
                        }										
123
                    index+= 1;
124
                }
125
            }	
126
            
127
            Void uiToggleDropdown (CMlFrame frame) { 
128
                if (frame.DataAttributeGet("open") == "1") {	
129
                    frame.DataAttributeSet("open","0");
130
                } else {
131
                    frame.DataAttributeSet("open","1");
132
                }
133
                 uiRenderDropdown(frame);
134
            }
135
            
136
            Void uiSelectDropdown (CMlLabel label) {
137
                declare uiDropdown = label.Parent.Parent;
138
                uiDropdown.DataAttributeSet("selected", label.DataAttributeGet("index"));
139
                uiDropdown.DataAttributeSet("value", label.DataAttributeGet("value"));           
140
                uiToggleDropdown(uiDropdown);
141
                +++onSelectDropdown+++
142
            }
143
144
EOD;
145
    } // end of getScriptRenderCheckbox
146
147
    /**
148
     * Render the XML element
149
     *
150
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
151
     * @return \DOMElement
152
     *
153
     * <frame pos="40 20" class="uiContainer uiDropdown" data-selected="1" data-open="0">
154
     * <label pos="20 0" z-index="0" size="5 5" text="⏷"  focusareacolor1="0000" focusareacolor2="0000"/>
155
     * <label pos="0 0" z-index="0" size="25 5" text="select" focusareacolor1="000" focusareacolor2="111" scriptevents="1" class="uiElement"/>
156
     * <entry pos="45 -3" z-index="0" size="26 6" textemboss="1" text="1" textsize="3" valign="center2" halign="center" textformat="Basic" name="checkbox"  hidden="0"/>
157
     * <frame pos="0 -5" class="uiDropdownSelect" size="40 40">
158
     * <label pos="0 0" z-index="0" size="25 5" text="option 1" data-value="asd" data-index="0" focusareacolor1="000" focusareacolor2="222" scriptevents="1" class="uiElement"/>
159
     * <label pos="0 -5" z-index="0" size="25 5" text="option 2" data-value="das" data-index="1" focusareacolor1="000" focusareacolor2="222" scriptevents="1" class="uiElement"/>
160
     * <label pos="0 -10" z-index="0" size="25 5" text="option 3" data-value="sad" data-index="2" focusareacolor1="000" focusareacolor2="222" scriptevents="1" class="uiElement"/>
161
     * </frame>
162
     * </frame>
163
     * @throws \Exception
164
     */
165
    public function render(\DOMDocument $domDocument)
166
    {
167
        $frame = new Frame();
168
        $frame->addClasses(['uiContainer uiDropdown'])
169
            ->setPosition($this->posX, $this->posY, $this->posZ)
170
            ->addDataAttribute("selected", $this->selectedIndex)
171
            ->addDataAttribute("open", $this->isOpened ? "1" : "0");
172
173
        if ($this->id) {
174
            $frame->setId($this->id);
175
        }
176
177
        $labelMark = new Label("⏷");
178
        $labelMark->setAlign("right", "center");
179
        $labelMark->setPosition($this->width - 0.5, -($this->height / 2));
180
        $labelMark->setTextColor("fff");
181
        $labelMark->setZ(2);
182
        $labelMark->setSize(5, 4);
183
        $labelMark->setScriptEvents(false)->addClass("uiSelectElement");
184
185
        $baseLabel = new FMLLabel();
186
        $baseLabel->setAreaColor("000")->setAreaFocusColor("333")
187
            ->setScriptEvents(true)->addClass("uiSelectElement")
188
            ->setSize($this->width, $this->height)
189
            ->setPosition(0, -($this->height / 2))
190
            ->setTextPrefix(" ")
191
            ->setTextSize(1)
192
            ->setAlign("left", "center")
193
            ->addClasses($this->_classes)
194
            ->setDataAttributes($this->_dataAttributes);
195
196
        $labelTitle = clone $baseLabel;
197
        $labelTitle->setText("Select...")
198
            ->setSize($this->width, $this->height);
199
200
        $entry = new Entry();
201
        $entry->setPosition(900, 900)
202
            ->setName($this->name);
203
204
        $frameOptions = new LayoutRow(0, -($this->height + ($this->height / 2)), [], 0);
205
        $frameOptions->addClass('uiDropdownSelect');
206
207
        $idx = 0;
208
        foreach ($this->options as $key => $value) {
209
            $option = clone $baseLabel;
210
            $option->setText($key)->addDataAttribute('value', $value)
211
                ->addDataAttribute("index", $idx);
212
            $frameOptions->addChild($option);
213
            $idx += 1;
214
        }
215
216
        $frame->addChild($labelMark);
217
        $frame->addChild($labelTitle);
218
        $frame->addChild($entry);
219
        $frame->addChild($frameOptions);
220
221
        return $frame->render($domDocument);
222
    }
223
224
    /**
225
     * @return mixed
226
     */
227
    public function getIsOpened()
228
    {
229
        return $this->isOpened;
230
    }
231
232
    /**
233
     * @param mixed $isOpened
234
     */
235
    public function setIsOpened($isOpened)
236
    {
237
        $this->isOpened = $isOpened;
238
    }
239
240
    /**
241
     * @return mixed
242
     */
243
    public function getSelectedIndex()
244
    {
245
        return $this->selectedIndex;
246
    }
247
248
    public function getSelectedValue()
249
    {
250
        $x = 0;
251
        foreach ($this->options as $option) {
252
            if ($x == $this->selectedIndex) {
253
                return $option;
254
            }
255
            $x++;
256
        }
257
258
        return "";
259
    }
260
261
    /**
262
     * @param mixed $selectedIndex
263
     */
264
    public function setSelectedIndex($selectedIndex)
265
    {
266
        $this->selectedIndex = $selectedIndex;
267
    }
268
269
270
    /**
271
     * Sets selected index by entry return value
272
     * @param $value
273
     */
274
    public function setSelectedByValue($value)
275
    {
276
        $x = 0;
277
        foreach ($this->options as $idx => $data) {
278
            if ($value == $data) {
279
                $this->setSelectedIndex($x);
280
281
                return;
282
            }
283
            $x++;
284
        }
285
286
        $this->setSelectedIndex(-1);
287
    }
288
289
    /**
290
     * Get the Script Features
291
     *
292
     * @return ScriptFeature[]
293
     */
294
    public function getScriptFeatures()
295
    {
296
        return ScriptFeature::collect($this);
297
    }
298
299
    /**
300
     * @return string
301
     */
302
    public function getId(): string
303
    {
304
        return $this->id;
305
    }
306
307
    /**
308
     * @param string $id
309
     */
310
    public function setId(string $id)
311
    {
312
        $this->id = $id;
313
    }
314
}
315