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

ConfirmButton::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace eXpansion\Framework\Gui\Components;
4
5
use FML\Script\Script;
6
use FML\Script\ScriptLabel;
7
8
class ConfirmButton extends Button
9
{
10
    public function __construct(string $text = "button", string $type = self::TYPE_DEFAULT)
11
    {
12
        parent::__construct($text, $type);
13
        $this->buttonLabel->removeAllClasses();
14
        $this->buttonLabel->addClass("uiConfirmButtonElement");
15
        $this->buttonLabel->addClasses($this->_classes);
16
    }
17
18
    public function prepare(Script $script)
19
    {
20
        $script->addCustomScriptLabel(ScriptLabel::MouseClick, $this->getScriptMouseClick());
21
        $script->addScriptFunction("", $this->getScriptFunction());
22
    }
23
24
    protected function getScriptMouseClick()
25
    {
26
        return /** language=textmate  prefix=#RequireContext\n */
27
            <<<'EOD'
28
            if (Event.Control.HasClass("uiConfirmButtonElement") ) {           
29
                TriggerConfirmButtonClick((Event.Control as CMlLabel));                           
30
            }
31
EOD;
32
    }
33
34
    protected function getScriptFunction()
35
    {
36
        return /** language=textmate  prefix=#RequireContext\n */
37
            <<<'EOD'
38
39
            ***FML_OnInit***
40
            ***
41
                declare Integer[Ident] pendingConfirms for Page = Integer[Ident];     
42
                declare Text[Ident] pendingConfirmIds for Page = Text[Ident];
43
                declare CMlLabel[Ident] pendingConfirmControls for Page = CMlLabel[Ident];     
44
                
45
                pendingConfirms.clear();
46
                pendingConfirmIds.clear();
47
                pendingConfirmControls.clear();
48
            ***
49
            
50
            ***FML_Loop***
51
            ***
52
            foreach (Id => Time in pendingConfirms) {                  
53
                if (Now > Time + (3 * 1000) ) { 
54
                   if (pendingConfirmIds.existskey(Id))  {             
55
                        pendingConfirmControls[Id].Value = pendingConfirmIds[Id];
56
                        pendingConfirmIds.removekey(Id);
57
                        pendingConfirms.removekey(Id);    
58
                        pendingConfirmControls.removekey(Id);                    
59
                    }
60
                }
61
            }
62
            ***
63
                        
64
           
65
            Void TriggerConfirmButtonClick(CMlLabel Control) {                     
66
                   declare Integer[Ident] pendingConfirms for Page = Integer[Ident];     
67
                   declare Text[Ident] pendingConfirmIds for Page = Text[Ident];
68
                   declare CMlLabel[Ident] pendingConfirmControls for Page = CMlLabel[Ident];              
69
                   
70
                   if (Control.Parent.HasClass("uiButton")) {                  
71
                          if (pendingConfirmIds.existskey(Control.Id) == False) {
72
                                pendingConfirmIds[Control.Id] = Control.Value;
73
                                pendingConfirmControls[Control.Id] = Control;
74
                                pendingConfirms[Control.Id] = Now;
75
                                Control.Value = "Confirm ?";                                                               
76
                                Control.Parent.RelativeScale = 0.75;
77
                                AnimMgr.Add(Control.Parent, "<elem scale=\"1.\" />", 200, CAnimManager::EAnimManagerEasing::QuadIn);
78
                          } else {
79
                             Control.Value = pendingConfirmIds[Control.Id];
80
                             pendingConfirmIds.removekey(Control.Id);            
81
                             pendingConfirms.removekey(Control.Id);
82
                             pendingConfirmControls.removekey(Control.Id);                                                                            
83
                             Control.Parent.RelativeScale = 0.75;
84
                             AnimMgr.Add(Control.Parent, "<elem scale=\"1.\" />", 200, CAnimManager::EAnimManagerEasing::QuadIn); 
85
                             TriggerPageAction(Control.Parent.DataAttributeGet("action"));
86
                         }                                                                                                                                              
87
                   }                
88
            }
89
            
90
 
91
            Void TriggerConfirmButtonClick(Text ControlId) {            
92
                declare CMlLabel Control = (Page.GetFirstChild(ControlId) as CMlLabel);
93
                TriggerConfirmButtonClick(Control);                               
94
            }
95
96
97
EOD;
98
    }
99
100
}
101