Completed
Pull Request — master (#270)
by
unknown
03:25
created

Input::setWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace eXpansion\Framework\Gui\Components;
4
5
use FML\Controls\Entry;
6
use FML\Controls\Frame;
7
use FML\Controls\Quad;
8
use FML\Script\Features\ScriptFeature;
9
use FML\Script\Script;
10
use FML\Script\ScriptLabel;
11
use FML\Types\Renderable;
12
use FML\Types\ScriptFeatureable;
13
14
class Input extends AbstractUiElement implements Renderable, ScriptFeatureable
15
{
16
17
    const TYPE_DEFAULT = "Basic";
18
    const TYPE_PASSWORD = "Password";
19
20
    /**
21
     * @var string
22
     */
23
    protected $name;
24
    /**
25
     * @var string
26
     */
27
    protected $default;
28
29
    /** @var null|string */
30
    protected $action = null;
31
32
    protected $textFormat = "Basic";
33
34
    /** @var null|string */
35
    protected $id = null;
36
37
    protected $horizontalAlign = "left";
38
39 View Code Duplication
    public function __construct($name, $default = "", $width = 30, $textFormat = "Basic")
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $this->name = $name;
42
        $this->default = $default;
43
        $this->width = $width;
44
        $this->setSize($width, 4);
45
        $this->textFormat = $textFormat;
46
    }
47
48
    /**
49
     * Render the XML element
50
     *
51
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
52
     * @return \DOMElement
53
     */
54
    public function render(\DOMDocument $domDocument)
55
    {
56
        $frame = new Frame();
57
        $frame->setPosition($this->posX, $this->posY)
58
            ->addClasses(["uiContainer", "uiInput"]);
59
60
        $quad = new Quad();
61
        $quad->setSize($this->width * 2, $this->height * 2)
62
            ->setScale(0.5)
63
            ->setPosition($this->width / 2, -$this->height / 2)
64
            ->setStyles('Bgs1', 'BgColorContour')
65
            ->setAlign("center", "center2")
66
            ->setBackgroundColor('FFFA')
67
            ->addClass("uiInput")
68
            ->setScriptEvents(true)
69
            ->setDataAttributes($this->_dataAttributes)->addClasses($this->_classes);
70
71
        $input = new Entry();
72
        $posX = 0;
73
        if ($this->horizontalAlign == "center") {
74
            $posX = $this->width / 2;
75
        }
76
        $input->setSize($this->width, $this->height)
77
            ->setPosition($posX, -($this->height / 2))
78
            ->setDefault($this->default)
79
            ->setSelectText(true)
80
            ->setAlign($this->horizontalAlign, "center")
81
            ->setAreaColor("0005")
82
            ->setAreaFocusColor('000a')
83
            ->setTextFormat($this->textFormat)
84
            ->setName($this->name)
85
            ->setTextSize(1.5)
86
            ->setScriptEvents(true)
87
            ->setId($this->id);
88
        if ($this->action !== null) {
89
            $input->addDataAttribute("action", $this->action);
90
        }
91
92
        $frame->addChild($quad);
93
        $frame->addChild($input);
94
95
        return $frame->render($domDocument);
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getName()
102
    {
103
        return $this->name;
104
    }
105
106
    /**
107
     * @param string $name
108
     */
109
    public function setName($name)
110
    {
111
        $this->name = $name;
112
113
        return $this;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getDefault()
120
    {
121
        return $this->default;
122
    }
123
124
    /**
125
     * @param string $default
126
     */
127
    public function setDefault($default)
128
    {
129
        $this->default = $default;
130
131
        return $this;
132
    }
133
134
    /**
135
     * @return integer
136
     */
137
    public function getWidth()
138
    {
139
        return $this->width;
140
    }
141
142
    /**
143
     * @param float $width
144
     */
145
    public function setWidth($width)
146
    {
147
        $this->width = $width;
0 ignored issues
show
Documentation Bug introduced by
The property $width was declared of type integer, but $width is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
148
149
        return $this;
150
    }
151
152
    public function getHeight()
153
    {
154
        return $this->height + 1;
155
    }
156
157
    /**
158
     * Prepare the given Script for rendering by adding the needed Labels, etc.
159
     *
160
     * @param Script $script Script to prepare
161
     */
162
    public function prepare(Script $script)
163
    {
164
        $script->addCustomScriptLabel(ScriptLabel::MouseClick, $this->getScriptMouseClick());
165
        if ($this->action !== null) {
166
            $script->addCustomScriptLabel(ScriptLabel::EntrySubmit, $this->getScriptEntrySubmit());
167
        }
168
    }
169
170
    /**
171
     * Get the Script Features
172
     *
173
     * @return ScriptFeature[]
174
     */
175
    public function getScriptFeatures()
176
    {
177
        return ScriptFeature::collect($this);
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    public function getTextFormat()
184
    {
185
        return $this->textFormat;
186
    }
187
188
    /**
189
     * @param string $textFormat
190
     * @return $this
191
     */
192
    public function setTextFormat($textFormat)
193
    {
194
        $this->textFormat = $textFormat;
195
196
        return $this;
197
    }
198
199
    protected function getScriptMouseClick()
200
    {
201
        return <<<EOL
202
             if (Event.Control.HasClass("uiInput") ) {              
203
                 declare CMlFrame frame <=> Event.Control.Parent;
204
                 (frame.Controls[1] as CMlEntry).StartEdition();               
205
            }					
206
EOL;
207
208
    }
209
210
    protected function getScriptEntrySubmit()
211
    {
212
        return <<<EOL
213
             if (Event.Control.DataAttributeExists("action") ) {              
214
                TriggerPageAction(Event.Control.DataAttributeGet("action"));            
215
            }					
216
EOL;
217
218
    }
219
220
221
    /**
222
     * @return null|string
223
     */
224
    public function getAction()
225
    {
226
        return $this->action;
227
    }
228
229
    /**
230
     * @param null|string $action
231
     * @return $this
232
     */
233
    public function setAction($action)
234
    {
235
        $this->action = $action;
236
237
        return $this;
238
    }
239
240
    /**
241
     * @return null|string
242
     */
243
    public function getId(): string
244
    {
245
        return $this->id;
246
    }
247
248
    /**
249
     * @param null|string $id
250
     */
251
    public function setId(string $id)
252
    {
253
        $this->id = $id;
254
    }
255
256
    /**
257
     * @return string
258
     */
259
    public function getHorizontalAlign(): string
260
    {
261
        return $this->horizontalAlign;
262
    }
263
264
    /**
265
     * @param string $horizontalAlign
266
     */
267
    public function setHorizontalAlign(string $horizontalAlign)
268
    {
269
        $this->horizontalAlign = $horizontalAlign;
270
    }
271
}
272