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 && PMA_USR_BROWSER_AGENT
=== 'IE') {
59 if ($value['conditional_ie'] === true) {
61 } else if ($value['conditional_ie'] == PMA_USR_BROWSER_VER
) {
68 $params[] = "scripts[]=" . $value['filename'];
71 $dynamic_scripts .= "<script type='text/javascript' src='js/" . $value['filename'] . "'></script>";
74 $static_scripts = sprintf(
75 "<script type='text/javascript' src='js/get_scripts.js.php?%s'></script>",
78 return $static_scripts . $dynamic_scripts;
82 * Generates new PMA_Scripts objects
84 * @return PMA_Scripts object
86 public function __construct()
88 $this->_files
= array();
90 $this->_events
= array();
95 * Adds a new file to the list of scripts
97 * @param string $filename The name of the file to include
98 * @param bool $conditional_ie Whether to wrap the script tag in
99 * conditional comments for IE
103 public function addFile($filename, $conditional_ie = false)
105 $hash = md5($filename);
106 if (empty($this->_files
[$hash])) {
107 $has_onload = $this->_eventBlacklist($filename);
108 $this->_files
[$hash] = array(
109 'has_onload' => $has_onload,
110 'filename' => $filename,
111 'conditional_ie' => $conditional_ie
117 * Determines whether to fire up an onload event for a file
119 * @param string $filename The name of the file to be checked
120 * against the blacklist
122 * @return int 1 to fire up the event, 0 not to
124 private function _eventBlacklist($filename)
126 if ( strpos($filename, 'jquery') !== false
127 ||
strpos($filename, 'codemirror') !== false
128 ||
strpos($filename, 'messages.php') !== false
129 ||
strpos($filename, 'ajax.js') !== false
130 ||
strpos($filename, 'navigation.js') !== false
131 ||
strpos($filename, 'get_image.js.php') !== false
140 * Adds a new code snippet to the code to be executed
142 * @param string $code The JS code to be added
146 public function addCode($code)
148 $this->_code
.= "$code\n";
152 * Adds a new event to the list of events
154 * @param string $event The name of the event to register
155 * @param string $function The code to execute when the event fires
156 * E.g: 'function () { doSomething(); }'
161 public function addEvent($event, $function)
163 $this->_events
[] = array(
165 'function' => $function
170 * Returns a list with filenames and a flag to indicate
171 * whether to register onload events for this file
175 public function getFiles()
178 foreach ($this->_files
as $file) {
179 if (strpos($file['filename'], "?") === false) {
180 if (! $file['conditional_ie'] || PMA_USR_BROWSER_AGENT
== 'IE') {
182 'name' => $file['filename'],
183 'fire' => $file['has_onload']
192 * Renders all the JavaScript file inclusions, code and events
196 public function getDisplay()
200 if (count($this->_files
) > 0) {
201 $retval .= $this->_includeFiles(
206 $code = 'AJAX.scriptHandler';
207 foreach ($this->_files
as $file) {
210 PMA_escapeJsString($file['filename']),
211 $file['has_onload'] ?
1 : 0
215 $this->addCode($code);
217 $code = '$(function() {';
218 foreach ($this->_files
as $file) {
219 if ($file['has_onload']) {
220 $code .= 'AJAX.fireOnload("';
221 $code .= PMA_escapeJsString($file['filename']);
226 $this->addCode($code);
228 $retval .= '<script type="text/javascript">';
229 $retval .= "// <![CDATA[\n";
230 $retval .= $this->_code
;
231 foreach ($this->_events
as $js_event) {
233 "$(window).bind('%s', %s);\n",
235 $js_event['function']
239 $retval .= '</script>';