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, 'get_image.js.php') !== false
178 ||
strpos($filename, 'cross_framing_protection.js') !== false
187 * Adds a new code snippet to the code to be executed
189 * @param string $code The JS code to be added
193 public function addCode($code)
195 $this->_code
.= "$code\n";
199 * Adds a new event to the list of events
201 * @param string $event The name of the event to register
202 * @param string $function The code to execute when the event fires
203 * E.g: 'function () { doSomething(); }'
208 public function addEvent($event, $function)
210 $this->_events
[] = array(
212 'function' => $function
217 * Returns a list with filenames and a flag to indicate
218 * whether to register onload events for this file
222 public function getFiles()
225 foreach ($this->_files
as $file) {
226 //If filename contains a "?", continue.
227 if (strpos($file['filename'], "?") !== false) {
231 if (! $file['conditional_ie'] || PMA_USR_BROWSER_AGENT
== 'IE') {
233 'name' => $file['filename'],
234 'fire' => $file['has_onload']
242 * Renders all the JavaScript file inclusions, code and events
246 public function getDisplay()
250 if (count($this->_files
) > 0) {
251 $retval .= $this->_includeFiles(
256 $code = 'AJAX.scriptHandler';
257 foreach ($this->_files
as $file) {
260 PMA_escapeJsString($file['filename']),
261 $file['has_onload'] ?
1 : 0
265 $this->addCode($code);
267 $code = '$(function() {';
268 foreach ($this->_files
as $file) {
269 if ($file['has_onload']) {
270 $code .= 'AJAX.fireOnload("';
271 $code .= PMA_escapeJsString($file['filename']);
276 $this->addCode($code);
278 $retval .= '<script data-cfasync="false" type="text/javascript">';
279 $retval .= "// <![CDATA[\n";
280 $retval .= $this->_code
;
281 foreach ($this->_events
as $js_event) {
283 "$(window).bind('%s', %s);\n",
285 $js_event['function']
289 $retval .= '</script>';