summaryrefslogtreecommitdiff
path: root/app/frontend/web/TemplateControl.php
blob: aa95d75e30e5589e07e89701b687c04834a49596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php

class TemplateControl extends TTemplateControl {

    public function onPreRender($param) {
        parent::onPreRender($param);
        $scriptFile = $this->_getControlScriptPath(get_class($this));
        if (file_exists($scriptFile)) {
            $this->_registerScriptFile($scriptFile);
        }
        $styleFile = $this->_getControlStylePath(get_class($this));
        if (file_exists($styleFile)) {
            $this->_registerStyleFile($styleFile);
        }
    }

    protected function getExternalScriptDependencies() {
        return [];
    }

    protected function getLibScriptDependencies() {
        return [];
    }

    protected function getPradoScriptDependencies() {
        return [];
    }

    protected function getControlScriptDependencies() {
        return [];
    }

    protected function getExternalStyleDependencies() {
        return [];
    }

    protected function getLibStyleDependencies() {
        return [];
    }

    protected function getControlStyleDependencies() {
        return [];
    }

    private function _getControlScriptPath($className) {
        return Prado::getPathOfNamespace('Application.controls.scripts')
            . DIRECTORY_SEPARATOR
            . $className
            . '.js';
    }

    private function _getControlStylePath($className) {
        return Prado::getPathOfNamespace('Application.controls.styles')
            . DIRECTORY_SEPARATOR
            . $className
            . '.css';
    }

    private function _getLibPath($identifier, $extension = '') {
        return Prado::getPathOfNamespace('Lib')
            . DIRECTORY_SEPARATOR
            . $identifier
            . $extension;
    }

    private function _registerScriptFile($scriptFile) {
        $this->_registerExternalScriptDependencies(
            $this->getExternalScriptDependencies()
        );
        $this->_registerLibScriptDependencies(
            $this->getLibScriptDependencies()
        );
        $this->_registerPradoScriptDependencies(
            $this->getPradoScriptDependencies()
        );
        $this->_registerControlScriptDependencies(
            $this->getControlScriptDependencies()
        );
        $this->Page->ClientScript->registerScriptFile(
            'TemplateControl.' . get_class($this),
            $this->Application->AssetManager->publishFilePath($scriptFile)
        );
    }

    private function _registerExternalScriptDependencies(array $dependencies) {
        foreach ($dependencies as $dependency) {
            $this->Page->ClientScript->registerHeadScriptFile(
                $dependency, $dependency
            );
        }
    }

    private function _registerLibScriptDependencies(array $dependencies) {
        foreach ($dependencies as $dependency) {
            $this->Page->ClientScript->registerScriptFile(
                'LibScript.' . $dependency,
                $this->Application->AssetManager->publishFilePath(
                    $this->_getLibPath($dependency, '.js')
                )
            );
        }
    }

    private function _registerPradoScriptDependencies(array $dependencies) {
        foreach ($dependencies as $dependency) {
            $this->Page->ClientScript->registerPradoScript($dependency);
        }
    }

    private function _registerControlScriptDependencies(array $dependencies) {
        foreach ($dependencies as $dependency) {
            $this->Page->ClientScript->registerScriptFile(
                'TemplateControl.' . $dependency,
                $this->Application->AssetManager->publishFilePath(
                    $this->_getControlScriptPath($dependency)
                )
            );
        }
    }

    private function _registerStyleFile($styleFile) {
        $this->_registerExternalStyleDependencies(
            $this->getExternalStyleDependencies()
        );
        $this->_registerLibStyleDependencies(
            $this->getLibStyleDependencies()
        );
        $this->_registerControlStyleDependencies(
            $this->getControlStyleDependencies()
        );
        if (method_exists($this->Page->ClientScript, 'registerThemeStyleSheetFile')) {
            $this->Page->ClientScript->registerThemeStyleSheetFile(
                'TemplateControl.' . get_class($this),
                $this->Application->AssetManager->publishFilePath($styleFile)
            );
        } else {
            $this->Page->ClientScript->registerStyleSheetFile(
                'TemplateControl.' . get_class($this),
                $this->Application->AssetManager->publishFilePath($styleFile)
            );
        }
    }

    private function _registerExternalStyleDependencies(array $dependencies) {
        foreach ($dependencies as $dependency) {
            $this->Page->ClientScript->registerStyleSheetFile(
                $dependency, $dependency
            );
        }
    }

    private function _registerLibStyleDependencies(array $dependencies) {
        foreach ($dependencies as $dependency) {
            $this->Page->ClientScript->registerStyleSheetFile(
                'LibStyle.' . $dependency,
                $this->Application->AssetManager->publishFilePath(
                    $this->_getLibPath($dependency, '.css')
                )
            );
        }
    }

    private function _registerControlStyleDependencies(array $dependencies) {
        foreach ($dependencies as $dependency) {
            $this->Page->ClientScript->registerStyleSheetFile(
                'TemplateControl.' . $dependency,
                $this->Application->AssetManager->publishFilePath(
                    $this->_getControlStylePath($dependency)
                )
            );
        }
    }

}

?>