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 $dynamic_scripts = "";
55 foreach ($files as $value) {
56 if (strpos($value['filename'], "?") !== false) {
57 $dynamic_scripts .= "<script type='text/javascript' src='js/"
58 . $value['filename'] . "'></script>";
62 if ($value['conditional_ie'] !== false
63 && PMA_USR_BROWSER_AGENT
=== 'IE'
65 if ($value['conditional_ie'] === true) {
67 } else if ($value['conditional_ie'] == PMA_USR_BROWSER_VER
) {
74 $scripts[] = "scripts[]=" . $value['filename'];
77 $separator = PMA_URL_getArgSeparator();
78 $url = 'js/get_scripts.js.php'
79 . PMA_URL_getCommon(array(), 'none')
80 . $separator . implode($separator, $scripts);
82 $static_scripts = sprintf(
83 '<script type="text/javascript" src="%s"></script>',
84 htmlspecialchars($url)
86 return $static_scripts . $dynamic_scripts;
90 * Generates new PMA_Scripts objects
92 * @return PMA_Scripts object
94 public function __construct()
96 $this->_files
= array();
98 $this->_events
= array();
103 * Adds a new file to the list of scripts
105 * @param string $filename The name of the file to include
106 * @param bool $conditional_ie Whether to wrap the script tag in
107 * conditional comments for IE
111 public function addFile($filename, $conditional_ie = false)
113 $hash = md5($filename);
114 if (!empty($this->_files
[$hash])) {
118 $has_onload = $this->_eventBlacklist($filename);
119 $this->_files
[$hash] = array(
120 'has_onload' => $has_onload,
121 'filename' => $filename,
122 'conditional_ie' => $conditional_ie
127 * Determines whether to fire up an onload event for a file
129 * @param string $filename The name of the file to be checked
130 * against the blacklist
132 * @return int 1 to fire up the event, 0 not to
134 private function _eventBlacklist($filename)
136 if ( strpos($filename, 'jquery') !== false
137 ||
strpos($filename, 'codemirror') !== false
138 ||
strpos($filename, 'messages.php') !== false
139 ||
strpos($filename, 'ajax.js') !== false
140 ||
strpos($filename, 'navigation.js') !== false
141 ||
strpos($filename, 'get_image.js.php') !== false
142 ||
strpos($filename, 'cross_framing_protection.js') !== false
151 * Adds a new code snippet to the code to be executed
153 * @param string $code The JS code to be added
157 public function addCode($code)
159 $this->_code
.= "$code\n";
163 * Adds a new event to the list of events
165 * @param string $event The name of the event to register
166 * @param string $function The code to execute when the event fires
167 * E.g: 'function () { doSomething(); }'
172 public function addEvent($event, $function)
174 $this->_events
[] = array(
176 'function' => $function
181 * Returns a list with filenames and a flag to indicate
182 * whether to register onload events for this file
186 public function getFiles()
189 foreach ($this->_files
as $file) {
190 //If filename contains a "?", continue.
191 if (strpos($file['filename'], "?") !== false) {
195 if (! $file['conditional_ie'] || PMA_USR_BROWSER_AGENT
== 'IE') {
197 'name' => $file['filename'],
198 'fire' => $file['has_onload']
206 * Renders all the JavaScript file inclusions, code and events
210 public function getDisplay()
214 if (count($this->_files
) > 0) {
215 $retval .= $this->_includeFiles(
220 $code = 'AJAX.scriptHandler';
221 foreach ($this->_files
as $file) {
224 PMA_escapeJsString($file['filename']),
225 $file['has_onload'] ?
1 : 0
229 $this->addCode($code);
231 $code = '$(function() {';
232 foreach ($this->_files
as $file) {
233 if ($file['has_onload']) {
234 $code .= 'AJAX.fireOnload("';
235 $code .= PMA_escapeJsString($file['filename']);
240 $this->addCode($code);
242 $retval .= '<script type="text/javascript">';
243 $retval .= "// <![CDATA[\n";
244 $retval .= $this->_code
;
245 foreach ($this->_events
as $js_event) {
247 "$(window).bind('%s', %s);\n",
249 $js_event['function']
253 $retval .= '</script>';