Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / Scripts.class.php
blobc1053fcac5381ec9cb5e18d6fa53a781f5af3185
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, 'navigation.js') !== false
178 || strpos($filename, 'get_image.js.php') !== false
179 || strpos($filename, 'cross_framing_protection.js') !== false
181 return 0;
184 return 1;
188 * Adds a new code snippet to the code to be executed
190 * @param string $code The JS code to be added
192 * @return void
194 public function addCode($code)
196 $this->_code .= "$code\n";
200 * Adds a new event to the list of events
202 * @param string $event The name of the event to register
203 * @param string $function The code to execute when the event fires
204 * E.g: 'function () { doSomething(); }'
205 * or 'doSomething'
207 * @return void
209 public function addEvent($event, $function)
211 $this->_events[] = array(
212 'event' => $event,
213 'function' => $function
218 * Returns a list with filenames and a flag to indicate
219 * whether to register onload events for this file
221 * @return array
223 public function getFiles()
225 $retval = array();
226 foreach ($this->_files as $file) {
227 //If filename contains a "?", continue.
228 if (strpos($file['filename'], "?") !== false) {
229 continue;
232 if (! $file['conditional_ie'] || PMA_USR_BROWSER_AGENT == 'IE') {
233 $retval[] = array(
234 'name' => $file['filename'],
235 'fire' => $file['has_onload']
239 return $retval;
243 * Renders all the JavaScript file inclusions, code and events
245 * @return string
247 public function getDisplay()
249 $retval = '';
251 if (count($this->_files) > 0) {
252 $retval .= $this->_includeFiles(
253 $this->_files
257 $code = 'AJAX.scriptHandler';
258 foreach ($this->_files as $file) {
259 $code .= sprintf(
260 '.add("%s",%d)',
261 PMA_escapeJsString($file['filename']),
262 $file['has_onload'] ? 1 : 0
265 $code .= ';';
266 $this->addCode($code);
268 $code = '$(function() {';
269 foreach ($this->_files as $file) {
270 if ($file['has_onload']) {
271 $code .= 'AJAX.fireOnload("';
272 $code .= PMA_escapeJsString($file['filename']);
273 $code .= '");';
276 $code .= '});';
277 $this->addCode($code);
279 $retval .= '<script data-cfasync="false" type="text/javascript">';
280 $retval .= "// <![CDATA[\n";
281 $retval .= $this->_code;
282 foreach ($this->_events as $js_event) {
283 $retval .= sprintf(
284 "$(window).bind('%s', %s);\n",
285 $js_event['event'],
286 $js_event['function']
289 $retval .= '// ]]>';
290 $retval .= '</script>';
292 return $retval;