added japanese language
[openemr.git] / phpmyadmin / libraries / Scripts.class.php
blob7df3858796ee29c8a917a7fb2fe1bfd5a8af829e
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * JavaScript management
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Collects information about which JavaScript
14 * files and objects are necessary to render
15 * the page and generates the relevant code.
17 * @package PhpMyAdmin
19 class PMA_Scripts
21 /**
22 * An array of SCRIPT tags
24 * @access private
25 * @var array of strings
27 private $_files;
28 /**
29 * An array of discrete javascript code snippets
31 * @access private
32 * @var array of strings
34 private $_code;
35 /**
36 * An array of event names to bind and javascript code
37 * snippets to fire for the corresponding events
39 * @access private
40 * @var array
42 private $_events;
44 /**
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 = "";
54 $scripts = array();
55 foreach ($files as $value) {
56 if (strpos($value['filename'], "?") !== false) {
57 $dynamic_scripts .= "<script type='text/javascript' src='js/"
58 . $value['filename'] . "'></script>";
59 continue;
61 $include = true;
62 if ($value['conditional_ie'] !== false
63 && PMA_USR_BROWSER_AGENT === 'IE'
64 ) {
65 if ($value['conditional_ie'] === true) {
66 $include = true;
67 } else if ($value['conditional_ie'] == PMA_USR_BROWSER_VER) {
68 $include = true;
69 } else {
70 $include = false;
73 if ($include) {
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;
89 /**
90 * Generates new PMA_Scripts objects
92 * @return PMA_Scripts object
94 public function __construct()
96 $this->_files = array();
97 $this->_code = '';
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
109 * @return void
111 public function addFile($filename, $conditional_ie = false)
113 $hash = md5($filename);
114 if (!empty($this->_files[$hash])) {
115 return;
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
144 return 0;
147 return 1;
151 * Adds a new code snippet to the code to be executed
153 * @param string $code The JS code to be added
155 * @return void
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(); }'
168 * or 'doSomething'
170 * @return void
172 public function addEvent($event, $function)
174 $this->_events[] = array(
175 'event' => $event,
176 'function' => $function
181 * Returns a list with filenames and a flag to indicate
182 * whether to register onload events for this file
184 * @return array
186 public function getFiles()
188 $retval = array();
189 foreach ($this->_files as $file) {
190 //If filename contains a "?", continue.
191 if (strpos($file['filename'], "?") !== false) {
192 continue;
195 if (! $file['conditional_ie'] || PMA_USR_BROWSER_AGENT == 'IE') {
196 $retval[] = array(
197 'name' => $file['filename'],
198 'fire' => $file['has_onload']
202 return $retval;
206 * Renders all the JavaScript file inclusions, code and events
208 * @return string
210 public function getDisplay()
212 $retval = '';
214 if (count($this->_files) > 0) {
215 $retval .= $this->_includeFiles(
216 $this->_files
220 $code = 'AJAX.scriptHandler';
221 foreach ($this->_files as $file) {
222 $code .= sprintf(
223 '.add("%s",%d)',
224 PMA_escapeJsString($file['filename']),
225 $file['has_onload'] ? 1 : 0
228 $code .= ';';
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']);
236 $code .= '");';
239 $code .= '});';
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) {
246 $retval .= sprintf(
247 "$(window).bind('%s', %s);\n",
248 $js_event['event'],
249 $js_event['function']
252 $retval .= '// ]]>';
253 $retval .= '</script>';
255 return $retval;