Translated using Weblate (Indonesian)
[phpmyadmin.git] / libraries / Scripts.class.php
blobc0d64d8ab15d0c4d60cebdff7be5cb95c14a64bc
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 $params = array();
55 foreach ($files as $value) {
56 if (strpos($value['filename'], "?") === false) {
57 $include = true;
58 if ($value['conditional_ie'] !== false && PMA_USR_BROWSER_AGENT === 'IE') {
59 if ($value['conditional_ie'] === true) {
60 $include = true;
61 } else if ($value['conditional_ie'] == PMA_USR_BROWSER_VER) {
62 $include = true;
63 } else {
64 $include = false;
67 if ($include) {
68 $params[] = "scripts[]=" . $value['filename'];
70 } else {
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>",
76 implode("&", $params)
78 return $static_scripts . $dynamic_scripts;
81 /**
82 * Generates new PMA_Scripts objects
84 * @return PMA_Scripts object
86 public function __construct()
88 $this->_files = array();
89 $this->_code = '';
90 $this->_events = array();
94 /**
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
101 * @return void
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
133 return 0;
134 } else {
135 return 1;
140 * Adds a new code snippet to the code to be executed
142 * @param string $code The JS code to be added
144 * @return void
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(); }'
157 * or 'doSomething'
159 * @return void
161 public function addEvent($event, $function)
163 $this->_events[] = array(
164 'event' => $event,
165 'function' => $function
170 * Returns a list with filenames and a flag to indicate
171 * whether to register onload events for this file
173 * @return array
175 public function getFiles()
177 $retval = array();
178 foreach ($this->_files as $file) {
179 if (strpos($file['filename'], "?") === false) {
180 if (! $file['conditional_ie'] || PMA_USR_BROWSER_AGENT == 'IE') {
181 $retval[] = array(
182 'name' => $file['filename'],
183 'fire' => $file['has_onload']
188 return $retval;
192 * Renders all the JavaScript file inclusions, code and events
194 * @return string
196 public function getDisplay()
198 $retval = '';
200 if (count($this->_files) > 0) {
201 $retval .= $this->_includeFiles(
202 $this->_files
206 $code = 'AJAX.scriptHandler';
207 foreach ($this->_files as $file) {
208 $code .= sprintf(
209 '.add("%s",%d)',
210 PMA_escapeJsString($file['filename']),
211 $file['has_onload'] ? 1 : 0
214 $code .= ';';
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']);
222 $code .= '");';
225 $code .= '});';
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) {
232 $retval .= sprintf(
233 "$(window).bind('%s', %s);\n",
234 $js_event['event'],
235 $js_event['function']
238 $retval .= '// ]]>';
239 $retval .= '</script>';
241 return $retval;