Completed
Pull Request — master (#104)
by
unknown
04:57 queued 02:15
created

uiDropdown::render()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 46
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

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