Completed
Pull Request — master (#141)
by
unknown
02:37
created

uiDropdown::getSelectedIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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