Removing old documentation
[openemr.git] / phpmyadmin / libraries / Scripts.class.php
blob0181466d58b369f902751873ba5c15a74546a0b2
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 $first_dynamic_scripts = "";
54 $dynamic_scripts = "";
55 $scripts = array();
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>";
65 } else {
66 $dynamic_scripts .= "<script data-cfasync='false' "
67 . "type='text/javascript' src='js/" . $file_name
68 . "'></script>";
70 continue;
72 $include = true;
73 if ($value['conditional_ie'] !== false
74 && PMA_USR_BROWSER_AGENT === 'IE'
75 ) {
76 if ($value['conditional_ie'] === true) {
77 $include = true;
78 } else if ($value['conditional_ie'] == PMA_USR_BROWSER_VER) {
79 $include = true;
80 } else {
81 $include = false;
84 if ($include) {
85 $scripts[] = "scripts%5B%5D=" . $value['filename'];
88 $separator = PMA_URL_getArgSeparator();
89 $static_scripts = '';
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();
112 $this->_code = '';
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
126 * @return void
128 public function addFile(
129 $filename,
130 $conditional_ie = false,
131 $before_statics = false
133 $hash = md5($filename);
134 if (!empty($this->_files[$hash])) {
135 return;
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
154 * @return void
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
180 return 0;
183 return 1;
187 * Adds a new code snippet to the code to be executed
189 * @param string $code The JS code to be added
191 * @return void
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(); }'
204 * or 'doSomething'
206 * @return void
208 public function addEvent($event, $function)
210 $this->_events[] = array(
211 'event' => $event,
212 'function' => $function
217 * Returns a list with filenames and a flag to indicate
218 * whether to register onload events for this file
220 * @return array
222 public function getFiles()
224 $retval = array();
225 foreach ($this->_files as $file) {
226 //If filename contains a "?", continue.
227 if (strpos($file['filename'], "?") !== false) {
228 continue;
231 if (! $file['conditional_ie'] || PMA_USR_BROWSER_AGENT == 'IE') {
232 $retval[] = array(
233 'name' => $file['filename'],
234 'fire' => $file['has_onload']
238 return $retval;
242 * Renders all the JavaScript file inclusions, code and events
244 * @return string
246 public function getDisplay()
248 $retval = '';
250 if (count($this->_files) > 0) {
251 $retval .= $this->_includeFiles(
252 $this->_files
256 $code = 'AJAX.scriptHandler';
257 foreach ($this->_files as $file) {
258 $code .= sprintf(
259 '.add("%s",%d)',
260 PMA_escapeJsString($file['filename']),
261 $file['has_onload'] ? 1 : 0
264 $code .= ';';
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']);
272 $code .= '");';
275 $code .= '});';
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) {
282 $retval .= sprintf(
283 "$(window).bind('%s', %s);\n",
284 $js_event['event'],
285 $js_event['function']
288 $retval .= '// ]]>';
289 $retval .= '</script>';
291 return $retval;