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) {
58 if ($value['conditional_ie'] !== false
59 && PMA_USR_BROWSER_AGENT
=== 'IE'
61 if ($value['conditional_ie'] === true) {
63 } else if ($value['conditional_ie'] == PMA_USR_BROWSER_VER
) {
70 $scripts[] = "scripts[]=" . $value['filename'];
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;
86 * Generates new PMA_Scripts objects
88 * @return PMA_Scripts object
90 public function __construct()
92 $this->_files
= array();
94 $this->_events
= array();
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
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
145 * Adds a new code snippet to the code to be executed
147 * @param string $code The JS code to be added
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(); }'
166 public function addEvent($event, $function)
168 $this->_events
[] = array(
170 'function' => $function
175 * Returns a list with filenames and a flag to indicate
176 * whether to register onload events for this file
180 public function getFiles()
183 foreach ($this->_files
as $file) {
184 if (strpos($file['filename'], "?") === false) {
185 if (! $file['conditional_ie'] || PMA_USR_BROWSER_AGENT
== 'IE') {
187 'name' => $file['filename'],
188 'fire' => $file['has_onload']
197 * Renders all the JavaScript file inclusions, code and events
201 public function getDisplay()
205 if (count($this->_files
) > 0) {
206 $retval .= $this->_includeFiles(
211 $code = 'AJAX.scriptHandler';
212 foreach ($this->_files
as $file) {
215 PMA_escapeJsString($file['filename']),
216 $file['has_onload'] ?
1 : 0
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']);
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) {
238 "$(window).bind('%s', %s);\n",
240 $js_event['function']
244 $retval .= '</script>';