2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * JavaScript management
8 if (! defined('PHPMYADMIN')) {
13 * Collects information about which JavaScript
14 * files and objects are necessary to render
15 * the page and generates the relevant code.
22 * An array of SCRIPT tags
25 * @var array of strings
29 * An array of discrete javascript code snippets
32 * @var array of strings
36 * An array of event names to bind and javascript code
37 * snippets to fire for the corresponding events
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 $first_dynamic_scripts = "";
54 $dynamic_scripts = "";
56 $separator = PMA_URL_getArgSeparator();
57 foreach ($files as $value) {
58 if (/*overload*/mb_strpos($value['filename'], "?") !== false) {
59 $file_name = $value['filename'] . $separator
60 . PMA_Header
::getVersionParameter();
61 if ($value['before_statics'] === true) {
62 $first_dynamic_scripts
63 .= "<script data-cfasync='false' type='text/javascript' "
64 . "src='js/" . $file_name . "'></script>";
66 $dynamic_scripts .= "<script data-cfasync='false' "
67 . "type='text/javascript' src='js/" . $file_name
73 if ($value['conditional_ie'] !== false
74 && PMA_USR_BROWSER_AGENT
=== 'IE'
76 if ($value['conditional_ie'] === true) {
78 } else if ($value['conditional_ie'] == PMA_USR_BROWSER_VER
) {
85 $scripts[] = "scripts%5B%5D=" . $value['filename'];
88 $separator = PMA_URL_getArgSeparator();
90 // Using chunks of 20 files to avoid too long URLs
91 $script_chunks = array_chunk($scripts, 20);
92 foreach ($script_chunks as $script_chunk) {
93 $url = 'js/get_scripts.js.php?'
94 . implode($separator, $script_chunk)
95 . $separator . PMA_Header
::getVersionParameter();
97 $static_scripts .= sprintf(
98 '<script data-cfasync="false" type="text/javascript" src="%s"></script>',
99 htmlspecialchars($url)
102 return $first_dynamic_scripts . $static_scripts . $dynamic_scripts;
106 * Generates new PMA_Scripts objects
109 public function __construct()
111 $this->_files
= array();
113 $this->_events
= array();
118 * Adds a new file to the list of scripts
120 * @param string $filename The name of the file to include
121 * @param bool $conditional_ie Whether to wrap the script tag in
122 * conditional comments for IE
123 * @param bool $before_statics Whether this dynamic script should be
124 * included before the static ones
128 public function addFile(
130 $conditional_ie = false,
131 $before_statics = false
133 $hash = md5($filename);
134 if (!empty($this->_files
[$hash])) {
138 $has_onload = $this->_eventBlacklist($filename);
139 $this->_files
[$hash] = array(
140 'has_onload' => $has_onload,
141 'filename' => $filename,
142 'conditional_ie' => $conditional_ie,
143 'before_statics' => $before_statics
148 * Add new files to the list of scripts
150 * @param array $filelist The array of file names
151 * @param bool $conditional_ie Whether to wrap the script tag in
152 * conditional comments for IE
156 public function addFiles($filelist, $conditional_ie = false)
158 foreach ($filelist as $filename) {
159 $this->addFile($filename, $conditional_ie);
164 * Determines whether to fire up an onload event for a file
166 * @param string $filename The name of the file to be checked
167 * against the blacklist
169 * @return int 1 to fire up the event, 0 not to
171 private function _eventBlacklist($filename)
173 if (strpos($filename, 'jquery') !== false
174 ||
strpos($filename, 'codemirror') !== false
175 ||
strpos($filename, 'messages.php') !== false
176 ||
strpos($filename, 'ajax.js') !== false
177 ||
strpos($filename, 'navigation.js') !== false
178 ||
strpos($filename, 'get_image.js.php') !== false
179 ||
strpos($filename, 'cross_framing_protection.js') !== false
188 * Adds a new code snippet to the code to be executed
190 * @param string $code The JS code to be added
194 public function addCode($code)
196 $this->_code
.= "$code\n";
200 * Adds a new event to the list of events
202 * @param string $event The name of the event to register
203 * @param string $function The code to execute when the event fires
204 * E.g: 'function () { doSomething(); }'
209 public function addEvent($event, $function)
211 $this->_events
[] = array(
213 'function' => $function
218 * Returns a list with filenames and a flag to indicate
219 * whether to register onload events for this file
223 public function getFiles()
226 foreach ($this->_files
as $file) {
227 //If filename contains a "?", continue.
228 if (strpos($file['filename'], "?") !== false) {
232 if (! $file['conditional_ie'] || PMA_USR_BROWSER_AGENT
== 'IE') {
234 'name' => $file['filename'],
235 'fire' => $file['has_onload']
243 * Renders all the JavaScript file inclusions, code and events
247 public function getDisplay()
251 if (count($this->_files
) > 0) {
252 $retval .= $this->_includeFiles(
257 $code = 'AJAX.scriptHandler';
258 foreach ($this->_files
as $file) {
261 PMA_escapeJsString($file['filename']),
262 $file['has_onload'] ?
1 : 0
266 $this->addCode($code);
268 $code = '$(function() {';
269 foreach ($this->_files
as $file) {
270 if ($file['has_onload']) {
271 $code .= 'AJAX.fireOnload("';
272 $code .= PMA_escapeJsString($file['filename']);
277 $this->addCode($code);
279 $retval .= '<script data-cfasync="false" type="text/javascript">';
280 $retval .= "// <![CDATA[\n";
281 $retval .= $this->_code
;
282 foreach ($this->_events
as $js_event) {
284 "$(window).bind('%s', %s);\n",
286 $js_event['function']
290 $retval .= '</script>';