| @@ 16-466 (lines=451) @@ | ||
| 13 | * @since Version 0.2 |
|
| 14 | * @filesource |
|
| 15 | */ |
|
| 16 | class Prototype extends JavaScript |
|
| 17 | { |
|
| 18 | public $CALLBACKS = [ |
|
| 19 | 'uninitialized', |
|
| 20 | 'loading', |
|
| 21 | 'loaded', |
|
| 22 | 'interactive', |
|
| 23 | 'complete', |
|
| 24 | 'failure', |
|
| 25 | 'success' |
|
| 26 | ]; |
|
| 27 | ||
| 28 | public $AJAX_OPTIONS = [ |
|
| 29 | 'before', |
|
| 30 | 'after', |
|
| 31 | 'condition', |
|
| 32 | 'url', |
|
| 33 | 'asynchronous', |
|
| 34 | 'method', |
|
| 35 | 'insertion', |
|
| 36 | 'position', |
|
| 37 | 'form', |
|
| 38 | 'with', |
|
| 39 | 'update', |
|
| 40 | 'script', |
|
| 41 | 'uninitialized', |
|
| 42 | 'loading', |
|
| 43 | 'loaded', |
|
| 44 | 'interactive', |
|
| 45 | 'complete', |
|
| 46 | 'failure', |
|
| 47 | 'success' |
|
| 48 | ]; |
|
| 49 | ||
| 50 | /** |
|
| 51 | * @return string |
|
| 52 | */ |
|
| 53 | public function evaluate_remote_response() |
|
| 54 | { |
|
| 55 | return 'eval(request.responseText)'; |
|
| 56 | } |
|
| 57 | ||
| 58 | /** |
|
| 59 | * @param $options |
|
| 60 | * @return string |
|
| 61 | */ |
|
| 62 | public function form_remote_tag($options) |
|
| 63 | { |
|
| 64 | $options['form'] = true; |
|
| 65 | ||
| 66 | return '<form action="' . $options['url'] . '" onsubmit="' . $this->remote_function($options) . '; return false;" method="' . (isset($options['method']) ? $options['method'] : 'post') . '" >'; |
|
| 67 | } |
|
| 68 | ||
| 69 | /** |
|
| 70 | * @param $name |
|
| 71 | * @param null $options |
|
| 72 | * @param null $html_options |
|
| 73 | * @return string |
|
| 74 | */ |
|
| 75 | public function link_to_remote($name, $options = null, $html_options = null) |
|
| 76 | { |
|
| 77 | return $this->link_to_function($name, $this->remote_function($options), $html_options); |
|
| 78 | } |
|
| 79 | ||
| 80 | /** |
|
| 81 | * @param $field_id |
|
| 82 | * @param null $options |
|
| 83 | * @return string |
|
| 84 | */ |
|
| 85 | public function observe_field($field_id, $options = null) |
|
| 86 | { |
|
| 87 | if (isset($options['frequency']) && $options['frequency'] > 0) { |
|
| 88 | return $this->_build_observer('Form.Element.Observer', $field_id, $options); |
|
| 89 | } else { |
|
| 90 | return $this->_build_observer('Form.Element.EventObserver', $field_id, $options); |
|
| 91 | } |
|
| 92 | } |
|
| 93 | ||
| 94 | /** |
|
| 95 | * @param $form_id |
|
| 96 | * @param null $options |
|
| 97 | * @return string |
|
| 98 | */ |
|
| 99 | public function observe_form($form_id, $options = null) |
|
| 100 | { |
|
| 101 | if (isset($options['frequency'])) { |
|
| 102 | return $this->_build_observer('Form.Observer', $form_id, $options); |
|
| 103 | } else { |
|
| 104 | return $this->_build_observer('Form.EventObserver', $form_id, $options); |
|
| 105 | } |
|
| 106 | } |
|
| 107 | ||
| 108 | /** |
|
| 109 | * @param null $options |
|
| 110 | * @return string |
|
| 111 | */ |
|
| 112 | public function periodically_call_remote($options = null) |
|
| 113 | { |
|
| 114 | $frequency = isset($options['frequency']) ? $options['frequency'] : 10; |
|
| 115 | $code = 'new PeriodicalExecuter(function() {' . $this->remote_function($options) . '},' . $frequency . ')'; |
|
| 116 | ||
| 117 | return $code; |
|
| 118 | } |
|
| 119 | ||
| 120 | /** |
|
| 121 | * @param $options |
|
| 122 | * @return string |
|
| 123 | */ |
|
| 124 | public function remote_function($options) |
|
| 125 | { |
|
| 126 | $javascript_options = $this->_options_for_ajax($options); |
|
| 127 | ||
| 128 | $update = ''; |
|
| 129 | ||
| 130 | if (isset($options['update']) && is_array($options['update'])) { |
|
| 131 | $update = isset($options['update']['success']) ? 'success: ' . $options['update']['success'] : ''; |
|
| 132 | $update .= empty($update) ? '' : ','; |
|
| 133 | $update .= isset($options['update']['failure']) ? 'failure: ' . $options['update']['failure'] : ''; |
|
| 134 | } else { |
|
| 135 | $update .= isset($options['update']) ? $options['update'] : ''; |
|
| 136 | } |
|
| 137 | ||
| 138 | $ajax_function = empty($update) ? 'new Ajax.Request(' : 'new Ajax.Updater(\'' . $update . '\','; |
|
| 139 | ||
| 140 | $ajax_function .= "'" . $options['url'] . "'"; |
|
| 141 | $ajax_function .= ',' . $javascript_options . ')'; |
|
| 142 | ||
| 143 | $ajax_function = isset($options['before']) ? $options['before'] . ';' . $ajax_function : $ajax_function; |
|
| 144 | $ajax_function = isset($options['after']) ? $ajax_function . ';' . $options['after'] : $ajax_function; |
|
| 145 | $ajax_function = isset($options['condition']) ? 'if (' . $options['condition'] . ') {' . $ajax_function . '}' : $ajax_function; |
|
| 146 | $ajax_function = isset($options['confirm']) ? 'if ( confirm(\'' . $options['confirm'] . '\' ) ) { ' . $ajax_function . ' } ' : $ajax_function; |
|
| 147 | ||
| 148 | return $ajax_function; |
|
| 149 | } |
|
| 150 | ||
| 151 | /** |
|
| 152 | * @param $name |
|
| 153 | * @param $value |
|
| 154 | * @param $options |
|
| 155 | * @return string |
|
| 156 | */ |
|
| 157 | public function submit_to_remote($name, $value, $options) |
|
| 158 | { |
|
| 159 | if (isset($options['with'])) { |
|
| 160 | $options['with'] = 'Form.serialize(this.form)'; |
|
| 161 | } |
|
| 162 | ||
| 163 | return '<input type="button" onclick="' . $this->remote_function($options) . '" name="' . $name . '" value ="' . $value . '">'; |
|
| 164 | } |
|
| 165 | ||
| 166 | /** |
|
| 167 | * @param $element_id |
|
| 168 | * @param null $options |
|
| 169 | * @param $block |
|
| 170 | */ |
|
| 171 | public function update_element_function($element_id, $options = null, $block) |
|
| 172 | { |
|
| 173 | $content = isset($options['content']) ? $options['content'] : ''; |
|
| 174 | $content = $this->escape($content); |
|
| 175 | } |
|
| 176 | ||
| 177 | /** |
|
| 178 | * @param $block |
|
| 179 | */ |
|
| 180 | public function update_page($block) |
|
| 181 | { |
|
| 182 | } |
|
| 183 | ||
| 184 | /** |
|
| 185 | * @param $block |
|
| 186 | * @return string |
|
| 187 | */ |
|
| 188 | public function update_page_tag(& $block) |
|
| 189 | { |
|
| 190 | return $this->tag($block); |
|
| 191 | } |
|
| 192 | ||
| 193 | ///////////////////////////////////////////////////////////////////////////////////// |
|
| 194 | // Private functions |
|
| 195 | ///////////////////////////////////////////////////////////////////////////////////// |
|
| 196 | ||
| 197 | /** |
|
| 198 | * @param $options |
|
| 199 | * @return array |
|
| 200 | */ |
|
| 201 | public function _build_callbacks($options) |
|
| 202 | { |
|
| 203 | $callbacks = []; |
|
| 204 | foreach ($options as $callback => $code) { |
|
| 205 | if (in_array($callback, $this->CALLBACKS)) { |
|
| 206 | $name = 'on' . ucfirst($callback); |
|
| 207 | $callbacks[$name] = 'function(request){' . $code . '}'; |
|
| 208 | } |
|
| 209 | } |
|
| 210 | ||
| 211 | return $callbacks; |
|
| 212 | } |
|
| 213 | ||
| 214 | /** |
|
| 215 | * @param $klass |
|
| 216 | * @param $name |
|
| 217 | * @param null $options |
|
| 218 | * @return string |
|
| 219 | */ |
|
| 220 | public function _build_observer($klass, $name, $options = null) |
|
| 221 | { |
|
| 222 | if (isset($options['with']) && false === strpos($options['with'], '=')) { |
|
| 223 | $options['with'] = '\'' . $options['with'] . '=\' + value'; |
|
| 224 | } elseif (isset($options['with']) && isset($options['update'])) { |
|
| 225 | $options['with'] = 'value'; |
|
| 226 | } |
|
| 227 | ||
| 228 | $callback = $options['function'] ?: $this->remote_function($options); |
|
| 229 | ||
| 230 | $javascript = "new $klass('$name', "; |
|
| 231 | $javascript .= isset($options['frequency']) ? $options['frequency'] . ', ' : ''; |
|
| 232 | $javascript .= 'function (element,value) { '; |
|
| 233 | $javascript .= $callback; |
|
| 234 | $javascript .= isset($options['on']) ? ', ' . $options['on'] : ''; |
|
| 235 | $javascript .= '})'; |
|
| 236 | ||
| 237 | return $javascript; |
|
| 238 | } |
|
| 239 | ||
| 240 | /** |
|
| 241 | * @param $method |
|
| 242 | * @return string |
|
| 243 | */ |
|
| 244 | public function _method_option_to_s($method) |
|
| 245 | { |
|
| 246 | return false !== strpos($method, "'") ? $method : "'$method'"; |
|
| 247 | } |
|
| 248 | ||
| 249 | /** |
|
| 250 | * @param $options |
|
| 251 | * @return string |
|
| 252 | */ |
|
| 253 | public function _options_for_ajax($options) |
|
| 254 | { |
|
| 255 | $js_options = is_array($options) ? $this->_build_callbacks($options) : []; |
|
| 256 | ||
| 257 | if (isset($options['type']) && 'synchronous' === $option['type']) { |
|
| 258 | $js_options['asynchronous'] = 'false'; |
|
| 259 | } |
|
| 260 | ||
| 261 | if (isset($options['method'])) { |
|
| 262 | $js_options['method'] = $this->_method_option_to_s($options['method']); |
|
| 263 | } |
|
| 264 | ||
| 265 | if (isset($options['position'])) { |
|
| 266 | $js_options['insertion'] = 'Insertion.' . ucfirst($options['position']); |
|
| 267 | } |
|
| 268 | ||
| 269 | $js_options['evalScripts'] = isset($options['script']) ? $options['script'] : 'true'; |
|
| 270 | ||
| 271 | if (isset($options['form'])) { |
|
| 272 | $js_options['parameters'] = 'Form.serialize(this)'; |
|
| 273 | } elseif (isset($options['parameters'])) { |
|
| 274 | $js_options['parameters'] = 'Form.serialize(\'' . $options['submit'] . '\')'; |
|
| 275 | } elseif (isset($options['with'])) { |
|
| 276 | $js_options['parameters'] = $options['with']; |
|
| 277 | } |
|
| 278 | ||
| 279 | return $this->_options_for_javascript($js_options); |
|
| 280 | } |
|
| 281 | ||
| 282 | ///////////////////////////////////////////////////////////////////////////////////// |
|
| 283 | // Mergerd Javascript Generator helpers |
|
| 284 | ///////////////////////////////////////////////////////////////////////////////////// |
|
| 285 | ||
| 286 | /** |
|
| 287 | * @param $javascript |
|
| 288 | */ |
|
| 289 | public function dump($javascript) |
|
| 290 | { |
|
| 291 | echo $javascript; |
|
| 292 | } |
|
| 293 | ||
| 294 | /** |
|
| 295 | * @param $id |
|
| 296 | * @param null $extend |
|
| 297 | * @return string |
|
| 298 | */ |
|
| 299 | public function ID($id, $extend = null) |
|
| 300 | { |
|
| 301 | return "$('$id')" . (!empty($extend)) ? '.' . $extend . '()' : ''; |
|
| 302 | } |
|
| 303 | ||
| 304 | /** |
|
| 305 | * @param $message |
|
| 306 | * @return string |
|
| 307 | */ |
|
| 308 | public function alert($message) |
|
| 309 | { |
|
| 310 | return $this->call('alert', $message); |
|
| 311 | } |
|
| 312 | ||
| 313 | /** |
|
| 314 | * @param $variable |
|
| 315 | * @param $value |
|
| 316 | * @return string |
|
| 317 | */ |
|
| 318 | public function assign($variable, $value) |
|
| 319 | { |
|
| 320 | return "$variable = $value;"; |
|
| 321 | } |
|
| 322 | ||
| 323 | /** |
|
| 324 | * @param $function |
|
| 325 | * @param null $args |
|
| 326 | * @return string |
|
| 327 | */ |
|
| 328 | public function call($function, $args = null) |
|
| 329 | { |
|
| 330 | $arg_str = ''; |
|
| 331 | if (is_array($args)) { |
|
| 332 | foreach ($args as $arg) { |
|
| 333 | if (!empty($arg_str)) { |
|
| 334 | $arg_str .= ', '; |
|
| 335 | } |
|
| 336 | if (is_string($arg)) { |
|
| 337 | $arg_str .= "'$arg'"; |
|
| 338 | } else { |
|
| 339 | $arg_str .= $arg; |
|
| 340 | } |
|
| 341 | } |
|
| 342 | } else { |
|
| 343 | if (is_string($args)) { |
|
| 344 | $arg_str .= "'$args'"; |
|
| 345 | } else { |
|
| 346 | $arg_str .= $args; |
|
| 347 | } |
|
| 348 | } |
|
| 349 | ||
| 350 | return "$function($arg_str)"; |
|
| 351 | } |
|
| 352 | ||
| 353 | /** |
|
| 354 | * @param int $seconds |
|
| 355 | * @param string $script |
|
| 356 | * @return string |
|
| 357 | */ |
|
| 358 | public function delay($seconds = 1, $script = '') |
|
| 359 | { |
|
| 360 | return "setTimeout( function() { $script } , " . ($seconds * 1000) . ' )'; |
|
| 361 | } |
|
| 362 | ||
| 363 | /** |
|
| 364 | * @param $id |
|
| 365 | * @return string |
|
| 366 | */ |
|
| 367 | public function hide($id) |
|
| 368 | { |
|
| 369 | return $this->call('Element.hide', $id); |
|
| 370 | } |
|
| 371 | ||
| 372 | /** |
|
| 373 | * @param $position |
|
| 374 | * @param $id |
|
| 375 | * @param null $options_for_render |
|
| 376 | * @return string |
|
| 377 | */ |
|
| 378 | public function insert_html($position, $id, $options_for_render = null) |
|
| 379 | { |
|
| 380 | $args = array_merge([$id], (is_array($options_for_render) ? $options_for_render : [$options_for_render])); |
|
| 381 | ||
| 382 | return $this->call('new Insertion.' . ucfirst($position), $args); |
|
| 383 | } |
|
| 384 | ||
| 385 | /** |
|
| 386 | * @param $location |
|
| 387 | * @return string |
|
| 388 | */ |
|
| 389 | public function redirect_to($location) |
|
| 390 | { |
|
| 391 | return $this->assign('window.location.href', $location); |
|
| 392 | } |
|
| 393 | ||
| 394 | /** |
|
| 395 | * @param $id |
|
| 396 | * @return string |
|
| 397 | */ |
|
| 398 | public function remove($id) |
|
| 399 | { |
|
| 400 | if (is_array($id)) { |
|
| 401 | $arr_str = ''; |
|
| 402 | foreach ($id as $obj) { |
|
| 403 | if (!empty($arg_str)) { |
|
| 404 | $arg_str .= ', '; |
|
| 405 | } |
|
| 406 | $arg_str .= "'$arg'"; |
|
| 407 | } |
|
| 408 | ||
| 409 | return "$A[$arg_str].each(Element.remove)"; |
|
| 410 | } else { |
|
| 411 | return "Element.remove('$id')"; |
|
| 412 | } |
|
| 413 | } |
|
| 414 | ||
| 415 | /** |
|
| 416 | * @param $id |
|
| 417 | * @param $options_for_render |
|
| 418 | * @return string |
|
| 419 | */ |
|
| 420 | public function replace($id, $options_for_render) |
|
| 421 | { |
|
| 422 | $args = array_merge([$id], (is_array($options_for_render) ? $options_for_render : [$options_for_render])); |
|
| 423 | ||
| 424 | return $this->call('Element.replace', $args); |
|
| 425 | } |
|
| 426 | ||
| 427 | /** |
|
| 428 | * @param $id |
|
| 429 | * @param $options_for_render |
|
| 430 | * @return string |
|
| 431 | */ |
|
| 432 | public function replace_html($id, $options_for_render) |
|
| 433 | { |
|
| 434 | $args = array_merge([$id], (is_array($options_for_render) ? $options_for_render : [$options_for_render])); |
|
| 435 | ||
| 436 | return $this->call('Element.update', $args); |
|
| 437 | } |
|
| 438 | ||
| 439 | /** |
|
| 440 | * @param $pattern |
|
| 441 | * @param null $extend |
|
| 442 | * @return string |
|
| 443 | */ |
|
| 444 | public function select($pattern, $extend = null) |
|
| 445 | { |
|
| 446 | return "$$('$pattern')" . (!empty($extend)) ? '.' . $extend : ''; |
|
| 447 | } |
|
| 448 | ||
| 449 | /** |
|
| 450 | * @param $id |
|
| 451 | * @return string |
|
| 452 | */ |
|
| 453 | public function show($id) |
|
| 454 | { |
|
| 455 | return $this->call('Element.show', $id); |
|
| 456 | } |
|
| 457 | ||
| 458 | /** |
|
| 459 | * @param $id |
|
| 460 | * @return string |
|
| 461 | */ |
|
| 462 | public function toggle($id) |
|
| 463 | { |
|
| 464 | return $this->call('Element.toggle', $id); |
|
| 465 | } |
|
| 466 | } |
|
| 467 | ||
| @@ 16-466 (lines=451) @@ | ||
| 13 | * @since Version 0.2 |
|
| 14 | * @filesource |
|
| 15 | */ |
|
| 16 | class Prototype extends JavaScript |
|
| 17 | { |
|
| 18 | public $CALLBACKS = [ |
|
| 19 | 'uninitialized', |
|
| 20 | 'loading', |
|
| 21 | 'loaded', |
|
| 22 | 'interactive', |
|
| 23 | 'complete', |
|
| 24 | 'failure', |
|
| 25 | 'success' |
|
| 26 | ]; |
|
| 27 | ||
| 28 | public $AJAX_OPTIONS = [ |
|
| 29 | 'before', |
|
| 30 | 'after', |
|
| 31 | 'condition', |
|
| 32 | 'url', |
|
| 33 | 'asynchronous', |
|
| 34 | 'method', |
|
| 35 | 'insertion', |
|
| 36 | 'position', |
|
| 37 | 'form', |
|
| 38 | 'with', |
|
| 39 | 'update', |
|
| 40 | 'script', |
|
| 41 | 'uninitialized', |
|
| 42 | 'loading', |
|
| 43 | 'loaded', |
|
| 44 | 'interactive', |
|
| 45 | 'complete', |
|
| 46 | 'failure', |
|
| 47 | 'success' |
|
| 48 | ]; |
|
| 49 | ||
| 50 | /** |
|
| 51 | * @return string |
|
| 52 | */ |
|
| 53 | public function evaluate_remote_response() |
|
| 54 | { |
|
| 55 | return 'eval(request.responseText)'; |
|
| 56 | } |
|
| 57 | ||
| 58 | /** |
|
| 59 | * @param $options |
|
| 60 | * @return string |
|
| 61 | */ |
|
| 62 | public function form_remote_tag($options) |
|
| 63 | { |
|
| 64 | $options['form'] = true; |
|
| 65 | ||
| 66 | return '<form action="' . $options['url'] . '" onsubmit="' . $this->remote_function($options) . '; return false;" method="' . (isset($options['method']) ? $options['method'] : 'post') . '" >'; |
|
| 67 | } |
|
| 68 | ||
| 69 | /** |
|
| 70 | * @param $name |
|
| 71 | * @param null $options |
|
| 72 | * @param null $html_options |
|
| 73 | * @return string |
|
| 74 | */ |
|
| 75 | public function link_to_remote($name, $options = null, $html_options = null) |
|
| 76 | { |
|
| 77 | return $this->link_to_function($name, $this->remote_function($options), $html_options); |
|
| 78 | } |
|
| 79 | ||
| 80 | /** |
|
| 81 | * @param $field_id |
|
| 82 | * @param null $options |
|
| 83 | * @return string |
|
| 84 | */ |
|
| 85 | public function observe_field($field_id, $options = null) |
|
| 86 | { |
|
| 87 | if (isset($options['frequency']) && $options['frequency'] > 0) { |
|
| 88 | return $this->_build_observer('Form.Element.Observer', $field_id, $options); |
|
| 89 | } else { |
|
| 90 | return $this->_build_observer('Form.Element.EventObserver', $field_id, $options); |
|
| 91 | } |
|
| 92 | } |
|
| 93 | ||
| 94 | /** |
|
| 95 | * @param $form_id |
|
| 96 | * @param null $options |
|
| 97 | * @return string |
|
| 98 | */ |
|
| 99 | public function observe_form($form_id, $options = null) |
|
| 100 | { |
|
| 101 | if (isset($options['frequency'])) { |
|
| 102 | return $this->_build_observer('Form.Observer', $form_id, $options); |
|
| 103 | } else { |
|
| 104 | return $this->_build_observer('Form.EventObserver', $form_id, $options); |
|
| 105 | } |
|
| 106 | } |
|
| 107 | ||
| 108 | /** |
|
| 109 | * @param null $options |
|
| 110 | * @return string |
|
| 111 | */ |
|
| 112 | public function periodically_call_remote($options = null) |
|
| 113 | { |
|
| 114 | $frequency = isset($options['frequency']) ? $options['frequency'] : 10; |
|
| 115 | $code = 'new PeriodicalExecuter(function() {' . $this->remote_function($options) . '},' . $frequency . ')'; |
|
| 116 | ||
| 117 | return $code; |
|
| 118 | } |
|
| 119 | ||
| 120 | /** |
|
| 121 | * @param $options |
|
| 122 | * @return string |
|
| 123 | */ |
|
| 124 | public function remote_function($options) |
|
| 125 | { |
|
| 126 | $javascript_options = $this->_options_for_ajax($options); |
|
| 127 | ||
| 128 | $update = ''; |
|
| 129 | ||
| 130 | if (isset($options['update']) && is_array($options['update'])) { |
|
| 131 | $update = isset($options['update']['success']) ? 'success: ' . $options['update']['success'] : ''; |
|
| 132 | $update .= empty($update) ? '' : ','; |
|
| 133 | $update .= isset($options['update']['failure']) ? 'failure: ' . $options['update']['failure'] : ''; |
|
| 134 | } else { |
|
| 135 | $update .= isset($options['update']) ? $options['update'] : ''; |
|
| 136 | } |
|
| 137 | ||
| 138 | $ajax_function = empty($update) ? 'new Ajax.Request(' : 'new Ajax.Updater(\'' . $update . '\','; |
|
| 139 | ||
| 140 | $ajax_function .= "'" . $options['url'] . "'"; |
|
| 141 | $ajax_function .= ',' . $javascript_options . ')'; |
|
| 142 | ||
| 143 | $ajax_function = isset($options['before']) ? $options['before'] . ';' . $ajax_function : $ajax_function; |
|
| 144 | $ajax_function = isset($options['after']) ? $ajax_function . ';' . $options['after'] : $ajax_function; |
|
| 145 | $ajax_function = isset($options['condition']) ? 'if (' . $options['condition'] . ') {' . $ajax_function . '}' : $ajax_function; |
|
| 146 | $ajax_function = isset($options['confirm']) ? 'if ( confirm(\'' . $options['confirm'] . '\' ) ) { ' . $ajax_function . ' } ' : $ajax_function; |
|
| 147 | ||
| 148 | return $ajax_function; |
|
| 149 | } |
|
| 150 | ||
| 151 | /** |
|
| 152 | * @param $name |
|
| 153 | * @param $value |
|
| 154 | * @param $options |
|
| 155 | * @return string |
|
| 156 | */ |
|
| 157 | public function submit_to_remote($name, $value, $options) |
|
| 158 | { |
|
| 159 | if (isset($options['with'])) { |
|
| 160 | $options['with'] = 'Form.serialize(this.form)'; |
|
| 161 | } |
|
| 162 | ||
| 163 | return '<input type="button" onclick="' . $this->remote_function($options) . '" name="' . $name . '" value ="' . $value . '">'; |
|
| 164 | } |
|
| 165 | ||
| 166 | /** |
|
| 167 | * @param $element_id |
|
| 168 | * @param null $options |
|
| 169 | * @param $block |
|
| 170 | */ |
|
| 171 | public function update_element_function($element_id, $options = null, $block) |
|
| 172 | { |
|
| 173 | $content = isset($options['content']) ? $options['content'] : ''; |
|
| 174 | $content = $this->escape($content); |
|
| 175 | } |
|
| 176 | ||
| 177 | /** |
|
| 178 | * @param $block |
|
| 179 | */ |
|
| 180 | public function update_page($block) |
|
| 181 | { |
|
| 182 | } |
|
| 183 | ||
| 184 | /** |
|
| 185 | * @param $block |
|
| 186 | * @return string |
|
| 187 | */ |
|
| 188 | public function update_page_tag(& $block) |
|
| 189 | { |
|
| 190 | return $this->tag($block); |
|
| 191 | } |
|
| 192 | ||
| 193 | ///////////////////////////////////////////////////////////////////////////////////// |
|
| 194 | // Private functions |
|
| 195 | ///////////////////////////////////////////////////////////////////////////////////// |
|
| 196 | ||
| 197 | /** |
|
| 198 | * @param $options |
|
| 199 | * @return array |
|
| 200 | */ |
|
| 201 | public function _build_callbacks($options) |
|
| 202 | { |
|
| 203 | $callbacks = []; |
|
| 204 | foreach ($options as $callback => $code) { |
|
| 205 | if (in_array($callback, $this->CALLBACKS)) { |
|
| 206 | $name = 'on' . ucfirst($callback); |
|
| 207 | $callbacks[$name] = 'function(request){' . $code . '}'; |
|
| 208 | } |
|
| 209 | } |
|
| 210 | ||
| 211 | return $callbacks; |
|
| 212 | } |
|
| 213 | ||
| 214 | /** |
|
| 215 | * @param $klass |
|
| 216 | * @param $name |
|
| 217 | * @param null $options |
|
| 218 | * @return string |
|
| 219 | */ |
|
| 220 | public function _build_observer($klass, $name, $options = null) |
|
| 221 | { |
|
| 222 | if (isset($options['with']) && false === strpos($options['with'], '=')) { |
|
| 223 | $options['with'] = '\'' . $options['with'] . '=\' + value'; |
|
| 224 | } elseif (isset($options['with']) && isset($options['update'])) { |
|
| 225 | $options['with'] = 'value'; |
|
| 226 | } |
|
| 227 | ||
| 228 | $callback = $options['function'] ?: $this->remote_function($options); |
|
| 229 | ||
| 230 | $javascript = "new $klass('$name', "; |
|
| 231 | $javascript .= isset($options['frequency']) ? $options['frequency'] . ', ' : ''; |
|
| 232 | $javascript .= 'function (element,value) { '; |
|
| 233 | $javascript .= $callback; |
|
| 234 | $javascript .= isset($options['on']) ? ', ' . $options['on'] : ''; |
|
| 235 | $javascript .= '})'; |
|
| 236 | ||
| 237 | return $javascript; |
|
| 238 | } |
|
| 239 | ||
| 240 | /** |
|
| 241 | * @param $method |
|
| 242 | * @return string |
|
| 243 | */ |
|
| 244 | public function _method_option_to_s($method) |
|
| 245 | { |
|
| 246 | return false !== strpos($method, "'") ? $method : "'$method'"; |
|
| 247 | } |
|
| 248 | ||
| 249 | /** |
|
| 250 | * @param $options |
|
| 251 | * @return string |
|
| 252 | */ |
|
| 253 | public function _options_for_ajax($options) |
|
| 254 | { |
|
| 255 | $js_options = is_array($options) ? $this->_build_callbacks($options) : []; |
|
| 256 | ||
| 257 | if (isset($options['type']) && 'synchronous' === $option['type']) { |
|
| 258 | $js_options['asynchronous'] = 'false'; |
|
| 259 | } |
|
| 260 | ||
| 261 | if (isset($options['method'])) { |
|
| 262 | $js_options['method'] = $this->_method_option_to_s($options['method']); |
|
| 263 | } |
|
| 264 | ||
| 265 | if (isset($options['position'])) { |
|
| 266 | $js_options['insertion'] = 'Insertion.' . ucfirst($options['position']); |
|
| 267 | } |
|
| 268 | ||
| 269 | $js_options['evalScripts'] = isset($options['script']) ? $options['script'] : 'true'; |
|
| 270 | ||
| 271 | if (isset($options['form'])) { |
|
| 272 | $js_options['parameters'] = 'Form.serialize(this)'; |
|
| 273 | } elseif (isset($options['parameters'])) { |
|
| 274 | $js_options['parameters'] = 'Form.serialize(\'' . $options['submit'] . '\')'; |
|
| 275 | } elseif (isset($options['with'])) { |
|
| 276 | $js_options['parameters'] = $options['with']; |
|
| 277 | } |
|
| 278 | ||
| 279 | return $this->_options_for_javascript($js_options); |
|
| 280 | } |
|
| 281 | ||
| 282 | ///////////////////////////////////////////////////////////////////////////////////// |
|
| 283 | // Mergerd Javascript Generator helpers |
|
| 284 | ///////////////////////////////////////////////////////////////////////////////////// |
|
| 285 | ||
| 286 | /** |
|
| 287 | * @param $javascript |
|
| 288 | */ |
|
| 289 | public function dump($javascript) |
|
| 290 | { |
|
| 291 | echo $javascript; |
|
| 292 | } |
|
| 293 | ||
| 294 | /** |
|
| 295 | * @param $id |
|
| 296 | * @param null $extend |
|
| 297 | * @return string |
|
| 298 | */ |
|
| 299 | public function ID($id, $extend = null) |
|
| 300 | { |
|
| 301 | return "$('$id')" . (!empty($extend)) ? '.' . $extend . '()' : ''; |
|
| 302 | } |
|
| 303 | ||
| 304 | /** |
|
| 305 | * @param $message |
|
| 306 | * @return string |
|
| 307 | */ |
|
| 308 | public function alert($message) |
|
| 309 | { |
|
| 310 | return $this->call('alert', $message); |
|
| 311 | } |
|
| 312 | ||
| 313 | /** |
|
| 314 | * @param $variable |
|
| 315 | * @param $value |
|
| 316 | * @return string |
|
| 317 | */ |
|
| 318 | public function assign($variable, $value) |
|
| 319 | { |
|
| 320 | return "$variable = $value;"; |
|
| 321 | } |
|
| 322 | ||
| 323 | /** |
|
| 324 | * @param $function |
|
| 325 | * @param null $args |
|
| 326 | * @return string |
|
| 327 | */ |
|
| 328 | public function call($function, $args = null) |
|
| 329 | { |
|
| 330 | $arg_str = ''; |
|
| 331 | if (is_array($args)) { |
|
| 332 | foreach ($args as $arg) { |
|
| 333 | if (!empty($arg_str)) { |
|
| 334 | $arg_str .= ', '; |
|
| 335 | } |
|
| 336 | if (is_string($arg)) { |
|
| 337 | $arg_str .= "'$arg'"; |
|
| 338 | } else { |
|
| 339 | $arg_str .= $arg; |
|
| 340 | } |
|
| 341 | } |
|
| 342 | } else { |
|
| 343 | if (is_string($args)) { |
|
| 344 | $arg_str .= "'$args'"; |
|
| 345 | } else { |
|
| 346 | $arg_str .= $args; |
|
| 347 | } |
|
| 348 | } |
|
| 349 | ||
| 350 | return "$function($arg_str)"; |
|
| 351 | } |
|
| 352 | ||
| 353 | /** |
|
| 354 | * @param int $seconds |
|
| 355 | * @param string $script |
|
| 356 | * @return string |
|
| 357 | */ |
|
| 358 | public function delay($seconds = 1, $script = '') |
|
| 359 | { |
|
| 360 | return "setTimeout( function() { $script } , " . ($seconds * 1000) . ' )'; |
|
| 361 | } |
|
| 362 | ||
| 363 | /** |
|
| 364 | * @param $id |
|
| 365 | * @return string |
|
| 366 | */ |
|
| 367 | public function hide($id) |
|
| 368 | { |
|
| 369 | return $this->call('Element.hide', $id); |
|
| 370 | } |
|
| 371 | ||
| 372 | /** |
|
| 373 | * @param $position |
|
| 374 | * @param $id |
|
| 375 | * @param null $options_for_render |
|
| 376 | * @return string |
|
| 377 | */ |
|
| 378 | public function insert_html($position, $id, $options_for_render = null) |
|
| 379 | { |
|
| 380 | $args = array_merge([$id], (is_array($options_for_render) ? $options_for_render : [$options_for_render])); |
|
| 381 | ||
| 382 | return $this->call('new Insertion.' . ucfirst($position), $args); |
|
| 383 | } |
|
| 384 | ||
| 385 | /** |
|
| 386 | * @param $location |
|
| 387 | * @return string |
|
| 388 | */ |
|
| 389 | public function redirect_to($location) |
|
| 390 | { |
|
| 391 | return $this->assign('window.location.href', $location); |
|
| 392 | } |
|
| 393 | ||
| 394 | /** |
|
| 395 | * @param $id |
|
| 396 | * @return string |
|
| 397 | */ |
|
| 398 | public function remove($id) |
|
| 399 | { |
|
| 400 | if (is_array($id)) { |
|
| 401 | $arr_str = ''; |
|
| 402 | foreach ($id as $obj) { |
|
| 403 | if (!empty($arg_str)) { |
|
| 404 | $arg_str .= ', '; |
|
| 405 | } |
|
| 406 | $arg_str .= "'$arg'"; |
|
| 407 | } |
|
| 408 | ||
| 409 | return "$A[$arg_str].each(Element.remove)"; |
|
| 410 | } else { |
|
| 411 | return "Element.remove('$id')"; |
|
| 412 | } |
|
| 413 | } |
|
| 414 | ||
| 415 | /** |
|
| 416 | * @param $id |
|
| 417 | * @param $options_for_render |
|
| 418 | * @return string |
|
| 419 | */ |
|
| 420 | public function replace($id, $options_for_render) |
|
| 421 | { |
|
| 422 | $args = array_merge([$id], (is_array($options_for_render) ? $options_for_render : [$options_for_render])); |
|
| 423 | ||
| 424 | return $this->call('Element.replace', $args); |
|
| 425 | } |
|
| 426 | ||
| 427 | /** |
|
| 428 | * @param $id |
|
| 429 | * @param $options_for_render |
|
| 430 | * @return string |
|
| 431 | */ |
|
| 432 | public function replace_html($id, $options_for_render) |
|
| 433 | { |
|
| 434 | $args = array_merge([$id], (is_array($options_for_render) ? $options_for_render : [$options_for_render])); |
|
| 435 | ||
| 436 | return $this->call('Element.update', $args); |
|
| 437 | } |
|
| 438 | ||
| 439 | /** |
|
| 440 | * @param $pattern |
|
| 441 | * @param null $extend |
|
| 442 | * @return string |
|
| 443 | */ |
|
| 444 | public function select($pattern, $extend = null) |
|
| 445 | { |
|
| 446 | return "$$('$pattern')" . (!empty($extend)) ? '.' . $extend : ''; |
|
| 447 | } |
|
| 448 | ||
| 449 | /** |
|
| 450 | * @param $id |
|
| 451 | * @return string |
|
| 452 | */ |
|
| 453 | public function show($id) |
|
| 454 | { |
|
| 455 | return $this->call('Element.show', $id); |
|
| 456 | } |
|
| 457 | ||
| 458 | /** |
|
| 459 | * @param $id |
|
| 460 | * @return string |
|
| 461 | */ |
|
| 462 | public function toggle($id) |
|
| 463 | { |
|
| 464 | return $this->call('Element.toggle', $id); |
|
| 465 | } |
|
| 466 | } |
|
| 467 | ||