Completed
Pull Request — master (#99)
by De Cramer
02:57
created

uiDropdown::getScriptFeatures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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