Refactored ConfigFile class so that it is no longer a singleton
[phpmyadmin.git] / libraries / Scripts.class.php
blob1ceb3706327ff3d27de43662e841dcd898ec6e44
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * JavaScript management
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Collects information about which JavaScript
14 * files and objects are necessary to render
15 * the page and generates the relevant code.
17 * @package PhpMyAdmin
19 class PMA_Scripts
21 /**
22 * An array of SCRIPT tags
24 * @access private
25 * @var array of strings
27 private $_files;
28 /**
29 * An array of discrete javascript code snippets
31 * @access private
32 * @var array of strings
34 private $_code;
35 /**
36 * An array of event names to bind and javascript code
37 * snippets to fire for the corresponding events
39 * @access private
40 * @var array
42 private $_events;
44 /**
45 * Returns HTML code to include javascript file.
47 * @param array $files The list of js file to include
49 * @return string HTML code for javascript inclusion.
51 private function _includeFiles($files)
53 $dynamic_scripts = "";
54 $params = array();
55 foreach ($files as $value) {
56 if (strpos($value['filename'], "?") === false) {
57 $include = true;
58 if ($value['conditional_ie'] !== false
59 && PMA_USR_BROWSER_AGENT === 'IE'
60 ) {
61 if ($value['conditional_ie'] === true) {
62 $include = true;
63 } else if ($value['conditional_ie'] == PMA_USR_BROWSER_VER) {
64 $include = true;
65 } else {
66 $include = false;
69 if ($include) {
70 $scripts[] = "scripts[]=" . $value['filename'];
72 } else {
73 $dynamic_scripts .= "<script type='text/javascript' src='js/"
74 . $value['filename'] . "'></script>";
77 $static_scripts = sprintf(
78 '<script type="text/javascript" '
79 . 'src="js/get_scripts.js.php%s&%s"></script>',
80 PMA_URL_getCommon(array(), 'none'), implode("&", $scripts)
82 return $static_scripts . $dynamic_scripts;
85 /**
86 * Generates new PMA_Scripts objects
88 * @return PMA_Scripts object
90 public function __construct()
92 $this->_files = array();
93 $this->_code = '';
94 $this->_events = array();
98 /**
99 * Adds a new file to the list of scripts
101 * @param string $filename The name of the file to include
102 * @param bool $conditional_ie Whether to wrap the script tag in
103 * conditional comments for IE
105 * @return void
107 public function addFile($filename, $conditional_ie = false)
109 $hash = md5($filename);
110 if (empty($this->_files[$hash])) {
111 $has_onload = $this->_eventBlacklist($filename);
112 $this->_files[$hash] = array(
113 'has_onload' => $has_onload,
114 'filename' => $filename,
115 'conditional_ie' => $conditional_ie
121 * Determines whether to fire up an onload event for a file
123 * @param string $filename The name of the file to be checked
124 * against the blacklist
126 * @return int 1 to fire up the event, 0 not to
128 private function _eventBlacklist($filename)
130 if ( strpos($filename, 'jquery') !== false
131 || strpos($filename, 'codemirror') !== false
132 || strpos($filename, 'messages.php') !== false
133 || strpos($filename, 'ajax.js') !== false
134 || strpos($filename, 'navigation.js') !== false
135 || strpos($filename, 'get_image.js.php') !== false
136 || strpos($filename, 'cross_framing_protection.js') !== false
138 return 0;
139 } else {
140 return 1;
145 * Adds a new code snippet to the code to be executed
147 * @param string $code The JS code to be added
149 * @return void
151 public function addCode($code)
153 $this->_code .= "$code\n";
157 * Adds a new event to the list of events
159 * @param string $event The name of the event to register
160 * @param string $function The code to execute when the event fires
161 * E.g: 'function () { doSomething(); }'
162 * or 'doSomething'
164 * @return void
166 public function addEvent($event, $function)
168 $this->_events[] = array(
169 'event' => $event,
170 'function' => $function
175 * Returns a list with filenames and a flag to indicate
176 * whether to register onload events for this file
178 * @return array
180 public function getFiles()
182 $retval = array();
183 foreach ($this->_files as $file) {
184 if (strpos($file['filename'], "?") === false) {
185 if (! $file['conditional_ie'] || PMA_USR_BROWSER_AGENT == 'IE') {
186 $retval[] = array(
187 'name' => $file['filename'],
188 'fire' => $file['has_onload']
193 return $retval;
197 * Renders all the JavaScript file inclusions, code and events
199 * @return string
201 public function getDisplay()
203 $retval = '';
205 if (count($this->_files) > 0) {
206 $retval .= $this->_includeFiles(
207 $this->_files
211 $code = 'AJAX.scriptHandler';
212 foreach ($this->_files as $file) {
213 $code .= sprintf(
214 '.add("%s",%d)',
215 PMA_escapeJsString($file['filename']),
216 $file['has_onload'] ? 1 : 0
219 $code .= ';';
220 $this->addCode($code);
222 $code = '$(function() {';
223 foreach ($this->_files as $file) {
224 if ($file['has_onload']) {
225 $code .= 'AJAX.fireOnload("';
226 $code .= PMA_escapeJsString($file['filename']);
227 $code .= '");';
230 $code .= '});';
231 $this->addCode($code);
233 $retval .= '<script type="text/javascript">';
234 $retval .= "// <![CDATA[\n";
235 $retval .= $this->_code;
236 foreach ($this->_events as $js_event) {
237 $retval .= sprintf(
238 "$(window).bind('%s', %s);\n",
239 $js_event['event'],
240 $js_event['function']
243 $retval .= '// ]]>';
244 $retval .= '</script>';
246 return $retval;