| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TClientScriptLoader class file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TClientScriptLoader.php 1827 2007-04-02 06:19:55Z wei $
|
|
|
10 |
* @package System.Web.UI.WebControls
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* The TClientScriptLoader publish a collection of javascript files as assets.
|
|
|
15 |
* The {@link PackagePath setPackagePath} property can be an existing asset directory
|
|
|
16 |
* or a namespace path to the directory containing javascript files. E.g.
|
|
|
17 |
* <code>
|
|
|
18 |
* <com:TClientScriptLoader PackagePath=<%~ mylib/js %> />
|
|
|
19 |
* <com:TClientScriptLoader PackagePath="Application.myscripts" />
|
|
|
20 |
* </code>
|
|
|
21 |
*
|
|
|
22 |
* When the files in the {@link PackagePath setPackagePath} are published as assets, a script loader
|
|
|
23 |
* php file corresponding to TClientScriptManager::SCRIPT_LOADER is also copied to that asset directory.
|
|
|
24 |
*
|
|
|
25 |
* The script loader, combines multiple javascript files and serve up as gzip if possible.
|
|
|
26 |
* Allowable scripts and script dependencies can be specified in a "packages.php" file
|
|
|
27 |
* with the following format. This "packages.php" is optional, if absent the filenames
|
|
|
28 |
* without ".js" extension are used. The "packages.php" must be in the directory given by
|
|
|
29 |
* {@link PackagePath setPackagePath}.
|
|
|
30 |
*
|
|
|
31 |
* <code>
|
|
|
32 |
* <?php
|
|
|
33 |
* $packages = array(
|
|
|
34 |
* 'package1' => array('file1.js', 'file2.js'),
|
|
|
35 |
* 'package2' => array('file3.js', 'file4.js'));
|
|
|
36 |
*
|
|
|
37 |
* $deps = array(
|
|
|
38 |
* 'package1' => array('package1'),
|
|
|
39 |
* 'package2' => array('package1', 'package2')); //package2 requires package1 first.
|
|
|
40 |
*
|
|
|
41 |
* return array($packages,$deps); //must return $packages and $deps in an array
|
|
|
42 |
* </code>
|
|
|
43 |
*
|
|
|
44 |
* Set the {@link PackageScripts setPackageScripts} property with value 'package1' to serve
|
|
|
45 |
* up the 'package1' scripts. A maxium of 25 packages separated by commas is allowed.
|
|
|
46 |
*
|
|
|
47 |
* Dependencies of the packages are automatically resolved by the script loader php file.
|
|
|
48 |
* E.g.
|
|
|
49 |
* <code>
|
|
|
50 |
* <com:TClientScriptLoader PackagePath=<%~ mylib/js %> PackageScripts="package2" />
|
|
|
51 |
* </code>
|
|
|
52 |
*
|
|
|
53 |
* The {@link setDebugMode DebugMode} property when false
|
|
|
54 |
* removes comments and whitespaces from the published javascript files. If
|
|
|
55 |
* the DebugMode property is not set, the debug mode is determined from the application mode.
|
|
|
56 |
*
|
|
|
57 |
* The {@link setEnableGzip EnableGzip} property (default is true) enables the
|
|
|
58 |
* published javascripts to be served as zipped if the browser and php server allows it.
|
|
|
59 |
*
|
|
|
60 |
* If the DebugMode is false either explicitly or when the application mode is non-debug,
|
|
|
61 |
* then cache headers are also sent to inform the browser and proxies to cache the file.
|
|
|
62 |
* Moreover, the post-processed (comments removed and zipped) are saved in the assets
|
|
|
63 |
* directory for the next requests. That is, in non-debug mode the scripts are cached
|
|
|
64 |
* in the assets directory until they are deleted.
|
|
|
65 |
*
|
|
|
66 |
* @author Wei Zhuo<weizhuo[at]gmail[dot]com>
|
|
|
67 |
* @version $Id$
|
|
|
68 |
* @package System.Web.UI.WebControls
|
|
|
69 |
* @since 3.1
|
|
|
70 |
*/
|
|
|
71 |
class TClientScriptLoader extends TWebControl
|
|
|
72 |
{
|
|
|
73 |
/**
|
|
|
74 |
* @return string tag name of the script element
|
|
|
75 |
*/
|
|
|
76 |
protected function getTagName()
|
|
|
77 |
{
|
|
|
78 |
return 'script';
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Adds attribute name-value pairs to renderer.
|
|
|
83 |
* This overrides the parent implementation with additional button specific attributes.
|
|
|
84 |
* @param THtmlWriter the writer used for the rendering purpose
|
|
|
85 |
*/
|
|
|
86 |
protected function addAttributesToRender($writer)
|
|
|
87 |
{
|
|
|
88 |
$writer->addAttribute('type','text/javascript');
|
|
|
89 |
$writer->addAttribute('src',$this->getClientScriptUrl());
|
|
|
90 |
parent::addAttributesToRender($writer);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* @return string clientscript.php url.
|
|
|
95 |
*/
|
|
|
96 |
protected function getClientScriptUrl()
|
|
|
97 |
{
|
|
|
98 |
$scripts = preg_split('/\s*[, ]+\s*/', $this->getPackageScripts());
|
|
|
99 |
$cs = $this->getPage()->getClientScript();
|
|
|
100 |
return $cs->registerJavascriptPackages($this->getPackagePath(),
|
|
|
101 |
$scripts, $this->getDebugMode(), $this->getEnableGzip());
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* @param string custom javascript library directory.
|
|
|
106 |
*/
|
|
|
107 |
public function setPackagePath($value)
|
|
|
108 |
{
|
|
|
109 |
$this->setViewState('PackagePath', $value);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* @return string custom javascript library directory.
|
|
|
114 |
*/
|
|
|
115 |
public function getPackagePath()
|
|
|
116 |
{
|
|
|
117 |
return $this->getViewState('PackagePath');
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* @param string load specific packages from the javascript library in the PackagePath,
|
|
|
122 |
* comma delimited package names. A maximum of 25 packages is allowed.
|
|
|
123 |
*/
|
|
|
124 |
public function setPackageScripts($value)
|
|
|
125 |
{
|
|
|
126 |
$this->setViewState('PackageScripts', $value,'');
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* @return string comma delimited list of javascript library packages to load.
|
|
|
131 |
*/
|
|
|
132 |
public function getPackageScripts()
|
|
|
133 |
{
|
|
|
134 |
return $this->getViewState('PackageScripts','');
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* @param boolean enables gzip compression of the javascript.
|
|
|
139 |
*/
|
|
|
140 |
public function setEnableGzip($value)
|
|
|
141 |
{
|
|
|
142 |
$this->setViewState('EnableGzip', TPropertyValue::ensureBoolean($value), true);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* @return boolean enables gzip compression of the javascript if possible, default is true.
|
|
|
147 |
*/
|
|
|
148 |
public function getEnableGzip()
|
|
|
149 |
{
|
|
|
150 |
return $this->getViewState('EnableGzip', true);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* @return boolean javascript comments stripped in non-debug mode.
|
|
|
155 |
* Debug mode will depend on the application mode if null.
|
|
|
156 |
*/
|
|
|
157 |
public function getDebugMode()
|
|
|
158 |
{
|
|
|
159 |
return $this->getViewState('DebugMode');
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* @param boolean true to enable debug mode, default is null thus dependent on the application mode.
|
|
|
164 |
*/
|
|
|
165 |
public function setDebugMode($value)
|
|
|
166 |
{
|
|
|
167 |
$this->setViewState('DebugMode', TPropertyValue::ensureBoolean($value), null);
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
|