Completed
Pull Request — master (#127)
by
unknown
02:42
created

uiInput::getScriptEntrySubmit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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 uiInput 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 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...
35
    {
36
        $this->name = $name;
37
        $this->default = $default;
38
        $this->width = $width;
39
        $this->setSize($width, 5);
40
        $this->textFormat = $textFormat;
41
    }
42
43
    /**
44
     * Render the XML element
45
     *
46
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
47
     * @return \DOMElement
48
     */
49
    public function render(\DOMDocument $domDocument)
50
    {
51
        $frame = new Frame();
52
        $frame->setPosition($this->posX, $this->posY)
53
            ->setSize($this->width, $this->height)
54
            ->addClasses(["uiContainer", "uiInput"]);
55
56
        $quad = new Quad();
57
        $quad->setSize($this->width * 2, $this->height * 2)
58
            ->setScale(0.5)
59
            ->setPosition($this->width / 2, -$this->height / 2)
60
            ->setStyles('Bgs1', 'BgColorContour')
61
            ->setAlign("center", "center")
62
            ->setBackgroundColor('FFFA')
63
            ->addClass("uiInput")
64
            ->setScriptEvents(true)
65
            ->setDataAttributes($this->_dataAttributes)->addClasses($this->_classes);
66
67
        $input = new Entry();
68
        $input->setSize($this->width, $this->height)
69
            ->setPosition(0, -$this->height / 2)
70
            ->setDefault($this->default)
71
            ->setSelectText(true)
72
            ->setAlign("left", "center2")
73
            ->setAreaColor("0005")
74
            ->setAreaFocusColor('000a')
75
            ->setTextFormat($this->textFormat)
76
            ->setName($this->name)
77
            ->setTextSize(2);
78
        if ($this->action) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->action of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
79
            $input->addDataAttribute("action", $this->action);
80
        }
81
82
        $frame->addChild($quad);
83
        $frame->addChild($input);
84
85
        return $frame->render($domDocument);
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getName()
92
    {
93
        return $this->name;
94
    }
95
96
    /**
97
     * @param string $name
98
     */
99
    public function setName($name)
100
    {
101
        $this->name = $name;
102
103
        return $this;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getDefault()
110
    {
111
        return $this->default;
112
    }
113
114
    /**
115
     * @param string $default
116
     */
117
    public function setDefault($default)
118
    {
119
        $this->default = $default;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return float
126
     */
127
    public function getWidth()
128
    {
129
        return $this->width;
130
    }
131
132
    /**
133
     * @param float $width
134
     */
135
    public function setWidth($width)
136
    {
137
        $this->width = $width;
138
139
        return $this;
140
    }
141
142
    public function getHeight()
143
    {
144
        return $this->height + 2;
145
    }
146
147
    /**
148
     * Prepare the given Script for rendering by adding the needed Labels, etc.
149
     *
150
     * @param Script $script Script to prepare
151
     */
152
    public function prepare(Script $script)
153
    {
154
        $script->addCustomScriptLabel(ScriptLabel::MouseClick, $this->getScriptMouseClick());
155
        if ($this->action) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->action of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
156
            $script->addCustomScriptLabel(ScriptLabel::EntrySubmit, $this->getScriptEntrySubmit());
157
        }
158
    }
159
160
    /**
161
     * Get the Script Features
162
     *
163
     * @return ScriptFeature[]
164
     */
165
    public function getScriptFeatures()
166
    {
167
        return ScriptFeature::collect($this);
168
    }
169
170
    /**
171
     * @return string
172
     */
173
    public function getTextFormat()
174
    {
175
        return $this->textFormat;
176
    }
177
178
    /**
179
     * @param string $textFormat
180
     * @return $this
181
     */
182
    public function setTextFormat($textFormat)
183
    {
184
        $this->textFormat = $textFormat;
185
186
        return $this;
187
    }
188
189
    protected function getScriptMouseClick()
190
    {
191
        return <<<EOL
192
             if (Event.Control.HasClass("uiInput") ) {              
193
                 declare CMlFrame frame <=> Event.Control.Parent;
194
                 (frame.Controls[1] as CMlEntry).StartEdition();               
195
            }					
196
EOL;
197
198
    }
199
200
    protected function getScriptEntrySubmit()
201
    {
202
        return <<<EOL
203
             if (Event.Control.DataAttributeExists("action") ) {              
204
                TriggerPageAction(Event.Control.DataAttributeGet("action"));            
205
            }					
206
EOL;
207
208
    }
209
210
211
    /**
212
     * @return null|string
213
     */
214
    public function getAction()
215
    {
216
        return $this->action;
217
    }
218
219
    /**
220
     * @param null|string $action
221
     * @return $this
222
     */
223
    public function setAction($action)
224
    {
225
        $this->action = $action;
226
227
        return $this;
228
    }
229
}
230