1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Nikita Melnikov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package ext.mainscript
|
8
|
|
|
*/
|
9
|
|
|
class SClientScript extends CClientScript
|
10
|
|
|
{
|
11
|
|
|
public $coreScriptPosition = self::POS_END;
|
12
|
|
|
|
13
|
|
|
public $defaultScriptFilePosition = self::POS_END;
|
14
|
|
|
|
15
|
|
|
/**
|
16
|
|
|
* @param string $output
|
17
|
|
|
*
|
18
|
|
|
* @return void
|
19
|
|
|
*/
|
20
|
7 |
|
public function render(&$output)
|
21
|
|
|
{
|
22
|
7 |
|
$this->raiseEvent('onBeforeRenderClientScript', new CEvent($this));
|
23
|
|
|
|
24
|
7 |
|
$this->prepareScripts();
|
25
|
|
|
|
26
|
7 |
|
parent::render($output);
|
27
|
7 |
|
}
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* @param $url
|
31
|
|
|
* @param null $position
|
32
|
|
|
*
|
33
|
|
|
* @return $this
|
34
|
|
|
*/
|
35
|
|
|
public function unregisterScriptFile($url, $position = null)
|
36
|
|
|
{
|
37
|
|
|
if( $position === null )
|
38
|
|
|
$position = $this->defaultScriptFilePosition;
|
39
|
|
|
|
40
|
|
|
unset($this->scriptFiles[$position][$url]);
|
41
|
|
|
|
42
|
|
|
return $this;
|
43
|
|
|
}
|
44
|
|
|
|
45
|
|
|
public function onBeforeRenderClientScript(CEvent $event)
|
46
|
|
|
{
|
47
|
|
|
}
|
48
|
|
|
|
49
|
|
|
/**
|
50
|
|
|
* Происходит очистка уже загруженных скриптов,
|
51
|
|
|
* для того, чтобы в нужном порядке загрузить список скриптов
|
52
|
|
|
* По порядку идут:
|
53
|
|
|
* 1) файлы скриптов из ядра Yii
|
54
|
|
|
* 2) основной скрипт
|
55
|
|
|
* 3) восстанавливаются остальные скрипты
|
56
|
|
|
*/
|
57
|
7 |
|
private function prepareScripts()
|
58
|
|
|
{
|
59
|
|
|
//Clean total scripts
|
60
|
7 |
|
$totalScripts = $this->scriptFiles;
|
61
|
7 |
|
$this->scriptFiles = array();
|
62
|
|
|
|
63
|
7 |
|
$this->prepareCoreScripts();
|
64
|
7 |
|
$this->prepareMainScript();
|
65
|
7 |
|
$this->restoreScripts($totalScripts);
|
66
|
7 |
|
}
|
67
|
|
|
|
68
|
7 |
|
private function prepareMainScript()
|
69
|
|
|
{
|
70
|
7 |
|
foreach(Yii::app()->mainscript->getModel()->getScriptList() as $path)
|
71
|
|
|
{
|
72
|
7 |
|
$url = Yii::app()->assetManager->publish($path, true);
|
73
|
|
|
|
74
|
7 |
|
$this->registerScriptFile($url);
|
75
|
|
|
|
76
|
7 |
|
if( file_exists($sourceMap = $path.'.map') )
|
77
|
7 |
|
Yii::app()->assetManager->publish($sourceMap, true);
|
78
|
7 |
|
}
|
79
|
7 |
|
}
|
80
|
|
|
|
81
|
7 |
|
private function prepareCoreScripts()
|
82
|
|
|
{
|
83
|
7 |
|
if( Yii::app()->mainscript->mode === 'frontend' )
|
84
|
7 |
|
$this->coreScripts = array();
|
85
|
7 |
|
}
|
86
|
|
|
|
87
|
|
|
/**
|
88
|
|
|
* @param array $totalScripts
|
89
|
|
|
*/
|
90
|
7 |
|
private function restoreScripts($totalScripts)
|
91
|
|
|
{
|
92
|
7 |
|
foreach($totalScripts as $position => $scriptList)
|
93
|
|
|
{
|
94
|
6 |
|
foreach($scriptList as $script)
|
95
|
|
|
{
|
96
|
6 |
|
$this->registerScriptFile($script, $position);
|
97
|
6 |
|
}
|
98
|
7 |
|
}
|
99
|
7 |
|
}
|
100
|
|
|
}
|
101
|
|
|
|