00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class JavascriptHelper extends Helper{
00036 var $_cachedEvents = array();
00037 var $_cacheEvents = false;
00038 var $_cacheToFile = false;
00039 var $_cacheAll = false;
00040 var $_rules = array();
00041
00042
00043
00044
00045
00046
00047
00048
00049 function codeBlock($script, $allowCache = true) {
00050 if ($this->_cacheEvents && $this->_cacheAll && $allowCache) {
00051 $this->_cachedEvents[] = $script;
00052 } else {
00053 return sprintf($this->tags['javascriptblock'], $script);
00054 }
00055 }
00056
00057
00058
00059
00060
00061
00062
00063 function link($url) {
00064 if (strpos($url, '.js') === false) {
00065 $url .= ".js";
00066 }
00067 return sprintf($this->tags['javascriptlink'], $this->webroot . $this->themeWeb . JS_URL . $url);
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 function linkOut($url) {
00077 if (strpos($url, '.js') === false && strpos($url, '?') === false) {
00078 $url .= '.js';
00079 }
00080 return sprintf($this->tags['javascriptlink'], $url);
00081 }
00082
00083
00084
00085
00086
00087
00088
00089 function escapeScript($script) {
00090 $script = r(array("\r\n", "\n", "\r"), '\n', $script);
00091 $script = r(array('"', "'"), array('\"', "\\'"), $script);
00092 return $script;
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 function escapeString($string) {
00109 $escape = array("\r\n" => '\n', "\r" => '\n', "\n" => '\n', '"' => '\"', "'" => "\\'");
00110 return r(array_keys($escape), array_values($escape), $string);
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 function event($object, $event, $observer = null, $useCapture = false) {
00123
00124 if ($useCapture == true) {
00125 $useCapture = "true";
00126 } else {
00127 $useCapture = "false";
00128 }
00129
00130 if ($object == 'window' || strpos($object, '$(') !== false || strpos($object, '"') !== false || strpos($object, '\'') !== false) {
00131 $b = "Event.observe($object, '$event', function(event) { $observer }, $useCapture);";
00132 } else {
00133 $chars = array('#', ' ', ', ', '.', ':');
00134 $found = false;
00135 foreach ($chars as $char) {
00136 if (strpos($object, $char) !== false) {
00137 $found = true;
00138 break;
00139 }
00140 }
00141 if ($found) {
00142 $this->_rules[$object] = $event;
00143 } else {
00144 $b = "Event.observe(\$('$object'), '$event', function(event) { $observer }, $useCapture);";
00145 }
00146 }
00147
00148 if (isset($b) && !empty($b)) {
00149 if ($this->_cacheEvents === true) {
00150 $this->_cachedEvents[] = $b;
00151 return;
00152 } else {
00153 return $this->codeBlock($b);
00154 }
00155 }
00156 }
00157
00158
00159
00160
00161
00162
00163
00164
00165 function cacheEvents($file = false, $all = false) {
00166 $this->_cacheEvents = true;
00167 $this->_cacheToFile = $file;
00168 $this->_cacheAll = $all;
00169 }
00170
00171
00172
00173
00174
00175
00176 function writeEvents() {
00177
00178 $rules = array();
00179 if (!empty($this->_rules)) {
00180 foreach ($this->_rules as $sel => $event) {
00181 $rules[] = "\t'{$sel}': function(element, event) {\n\t\t{$event}\n\t}";
00182 }
00183 $this->_cacheEvents = true;
00184 }
00185
00186 if ($this->_cacheEvents) {
00187
00188 $this->_cacheEvents = false;
00189 $events = $this->_cachedEvents;
00190 $data = implode("\n", $events);
00191 $this->_cachedEvents = array();
00192
00193 if (!empty($rules)) {
00194 $data .= "\n\nvar SelectorRules = {\n" . implode(",\n\n", $rules) . "\n}\n";
00195 $data .= "\nEventSelectors.start(SelectorRules);\n";
00196 }
00197
00198 if (!empty($events) || !empty($rules)) {
00199 if ($this->_cacheToFile) {
00200 $filename = md5($data);
00201 if (!file_exists(JS . $filename . '.js')) {
00202 cache(r(WWW_ROOT, '', JS) . $filename . '.js', $data, '+999 days', 'public');
00203 }
00204 return $this->link($filename);
00205 } else {
00206 return $this->codeBlock("\n" . $data . "\n");
00207 }
00208 }
00209 }
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 function includeScript($script = "") {
00224 if ($script == "") {
00225 $dh = opendir(JS);
00226 while (false !== ($filename = readdir($dh))) {
00227 $files[] = $filename;
00228 }
00229 sort($files);
00230 $javascript = '';
00231 foreach ($files as $file) {
00232 if (substr($file, -3) == '.js') {
00233 $javascript .= file_get_contents(JS . "{$file}") . "\n\n";
00234 }
00235 }
00236 } else {
00237 $javascript = file_get_contents(JS . "$script.js") . "\n\n";
00238 }
00239 return $this->codeBlock("\n\n" . $javascript);
00240 }
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 function object($data = array(), $block = false, $prefix = '', $postfix = '', $stringKeys = array(), $quoteKeys = true, $q = "\"") {
00256 if (is_object($data)) {
00257 $data = get_object_vars($data);
00258 }
00259
00260 $out = array();
00261 $key = array();
00262
00263 if (is_array($data)) {
00264 $keys = array_keys($data);
00265 }
00266
00267 $numeric = true;
00268 if (!empty($keys)) {
00269 $numeric = (array_values($keys) === array_keys(array_values($keys)));
00270 }
00271
00272 foreach ($data as $key => $val) {
00273 if (is_array($val) || is_object($val)) {
00274 $val = $this->object($val, false, '', '', $stringKeys, $quoteKeys, $q);
00275 } else {
00276 if ((!count($stringKeys) && !is_numeric($val) && !is_bool($val)) || ($quoteKeys && in_array($key, $stringKeys, true)) || (!$quoteKeys && !in_array($key, $stringKeys, true))) {
00277 $val = $q . $this->escapeString($val) . $q;
00278 }
00279 if ($val === null) {
00280 $val = 'null';
00281 }
00282 if (is_bool($val)) {
00283 $val = ife($val, 'true', 'false');
00284 }
00285 }
00286
00287 if (!$numeric) {
00288 $val = $q . $key . $q . ':' . $val;
00289 }
00290
00291 $out[] = $val;
00292 }
00293
00294 if (!$numeric) {
00295 $rt = '{' . join(', ', $out) . '}';
00296 } else {
00297 $rt = '[' . join(', ', $out) . ']';
00298 }
00299 $rt = $prefix . $rt . $postfix;
00300
00301 if ($block) {
00302 $rt = $this->codeBlock($rt);
00303 }
00304
00305 return $rt;
00306 }
00307
00308
00309
00310
00311
00312
00313 function afterRender() {
00314 echo $this->writeEvents();
00315 }
00316 }
00317 ?>