summaryrefslogtreecommitdiff
path: root/app/php/web/TemplateControl.php
blob: 952bd4f3c97ff58ad58778d4b925fff8218d697a (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
<?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);
        }
    }

    protected function getExternalScriptDependencies() {
        return [];
    }

    protected function getLibScriptDependencies() {
        return [];
    }

    protected function getPradoScriptDependencies() {
        return [];
    }

    protected function getControlScriptDependencies() {
        return [];
    }

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

    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($dependencies) {
        foreach ($dependencies as $dependency) {
            $this->Page->ClientScript->registerHeadScriptFile(
                $dependency, $dependency
            );
        }
    }

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

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

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

}

?>