| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
require_once(sfConfig::get('sf_symfony_lib_dir') . '/helper/JavascriptBaseHelper.php'); |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
if (sfConfig::get('sf_jquery_core', null) !== false) |
|---|
| 20 |
{ |
|---|
| 21 |
if (!$jq_path = sfConfig::get('sf_jquery_path')) |
|---|
| 22 |
{ |
|---|
| 23 |
$jq_path = sfConfig::get('sf_jquery_web_dir', '/sfJqueryReloadedPlugin') . |
|---|
| 24 |
'/js/' . sfConfig::get('sf_jquery_core', 'jquery-1.4.2.min.js'); |
|---|
| 25 |
} |
|---|
| 26 |
sfContext::getInstance()->getResponse()->addJavascript($jq_path, 'first'); |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
function jq_add_plugins_by_name($args = array()) { |
|---|
| 48 |
|
|---|
| 49 |
* When adding the capability to use a new plugin you must |
|---|
| 50 |
* extend this array, and keep it up to date when you update to |
|---|
| 51 |
* a new version. You must also update the plugin's |
|---|
| 52 |
* default config/settings.yml file |
|---|
| 53 |
*/ |
|---|
| 54 |
|
|---|
| 55 |
$plugins = array( |
|---|
| 56 |
|
|---|
| 57 |
'sortable' => 'jquery-ui-1.7.3.custom.min.js', |
|---|
| 58 |
'ui' => 'jquery-ui-1.7.3.custom.min.js', |
|---|
| 59 |
'autocomplete' => 'jquery.autocomplete.min.js' |
|---|
| 60 |
); |
|---|
| 61 |
|
|---|
| 62 |
$pluginPaths = sfConfig::get('sf_jquery_plugin_paths'); |
|---|
| 63 |
foreach ($args as $name) |
|---|
| 64 |
{ |
|---|
| 65 |
if (!isset($plugins[$name])) |
|---|
| 66 |
{ |
|---|
| 67 |
throw new Exception("Unknown jQuery plugin name $name"); |
|---|
| 68 |
} |
|---|
| 69 |
if (!isset($pluginPaths[$name])) |
|---|
| 70 |
{ |
|---|
| 71 |
$filename = sfConfig::get("sf_jquery_$name", $plugins[$name]); |
|---|
| 72 |
$filename = sfConfig::get('sf_jquery_web_dir', '/sfJqueryReloadedPlugin') . "/js/plugins/$filename"; |
|---|
| 73 |
} else { |
|---|
| 74 |
$filename = $pluginPaths[$name]; |
|---|
| 75 |
} |
|---|
| 76 |
sfContext::getInstance()->getResponse()->addJavascript($filename); |
|---|
| 77 |
} |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
function jq_add_plugin($options = array()) { |
|---|
| 86 |
|
|---|
| 87 |
// really should accept a non-array argument |
|---|
| 88 |
if (!is_array($options)) |
|---|
| 89 |
{ |
|---|
| 90 |
$options = array($options); |
|---|
| 91 |
} |
|---|
| 92 |
foreach ( $options as $o ) { |
|---|
| 93 |
$file = sfConfig::get('sf_jquery_web_dir', '/sfJqueryReloadedPlugin') . "/js/plugins/$o"; |
|---|
| 94 |
sfContext::getInstance ()->getResponse ()->addJavascript ($file); |
|---|
| 95 |
} |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
function jq_periodically_call_remote($options = array()) |
|---|
| 105 |
{ |
|---|
| 106 |
$frequency = isset($options['frequency']) ? $options['frequency'] : 10; |
|---|
| 107 |
$code = 'setInterval(function() {'.jq_remote_function($options).'}, '.($frequency * 1000).')'; |
|---|
| 108 |
|
|---|
| 109 |
return javascript_tag($code); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
function jq_button_to_remote($name, $options = array(), $html_options = array()) |
|---|
| 120 |
{ |
|---|
| 121 |
return jq_button_to_function($name, jq_remote_function($options), $html_options); |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
function jq_link_to_remote($name, $options = array(), $html_options = array()) |
|---|
| 224 |
{ |
|---|
| 225 |
return jq_link_to_function($name, jq_remote_function($options), $html_options); |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
function jq_update_element_function($element_id, $options = array()) |
|---|
| 248 |
{ |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
$content = escape_javascript(isset($options['content']) ? $options['content'] : ''); |
|---|
| 252 |
|
|---|
| 253 |
$value = isset($options['action']) ? $options['action'] : 'update'; |
|---|
| 254 |
switch ($value) |
|---|
| 255 |
{ |
|---|
| 256 |
case 'update': |
|---|
| 257 |
$updateMethod = _update_method(isset($options['position']) ? $options['position'] : ''); |
|---|
| 258 |
$javascript_function = "jQuery('#$element_id').$updateMethod('$content')"; |
|---|
| 259 |
break; |
|---|
| 260 |
|
|---|
| 261 |
case 'empty': |
|---|
| 262 |
$javascript_function = "jQuery('#$element_id').empty()"; |
|---|
| 263 |
break; |
|---|
| 264 |
|
|---|
| 265 |
case 'remove': |
|---|
| 266 |
$javascript_function = "jQuery('#$element_id').remove()"; |
|---|
| 267 |
break; |
|---|
| 268 |
|
|---|
| 269 |
default: |
|---|
| 270 |
throw new sfException('Invalid action, choose one of update, remove, empty'); |
|---|
| 271 |
} |
|---|
| 272 |
|
|---|
| 273 |
$javascript_function .= ";\n"; |
|---|
| 274 |
|
|---|
| 275 |
return (isset($options['binding']) ? $javascript_function.$options['binding'] : $javascript_function); |
|---|
| 276 |
} |
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
function jq_remote_function($options) |
|---|
| 289 |
{ |
|---|
| 290 |
|
|---|
| 291 |
if (isset($options['update']) && is_array($options['update'])) |
|---|
| 292 |
{ |
|---|
| 293 |
|
|---|
| 294 |
if (isset($options['update']['success'])) $update_success = "#".$options['update']['success']; |
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
if (isset($options['update']['failure'])) $update_failure = $options['update']['failure']; |
|---|
| 298 |
} |
|---|
| 299 |
else if (isset($options['update'])) $update_success = "#".$options['update']; |
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
$updateMethod = _update_method(isset($options['position']) ? $options['position'] : ''); |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
if (isset($options['loading'])) $callback_loading = $options['loading']; |
|---|
| 306 |
if (isset($options['complete'])) $callback_complete = $options['complete']; |
|---|
| 307 |
if (isset($options['success'])) $callback_success = $options['success']; |
|---|
| 308 |
|
|---|
| 309 |
if (isset($options['cache'])) { |
|---|
| 310 |
if($options['cache']){ |
|---|
| 311 |
$cache = 'true'; |
|---|
| 312 |
} else { |
|---|
| 313 |
$cache = 'false'; |
|---|
| 314 |
} |
|---|
| 315 |
} |
|---|
| 316 |
$execute = 'false'; |
|---|
| 317 |
if ((isset($options['script'])) && ($options['script'] == '1')) $execute = 'true'; |
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
if (isset($options['dataType'])) |
|---|
| 321 |
{ |
|---|
| 322 |
$dataType = $options['dataType']; |
|---|
| 323 |
} |
|---|
| 324 |
elseif ($execute) |
|---|
| 325 |
{ |
|---|
| 326 |
$dataType = 'html'; |
|---|
| 327 |
} |
|---|
| 328 |
else |
|---|
| 329 |
{ |
|---|
| 330 |
$dataType = 'text'; |
|---|
| 331 |
} |
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
$method = 'POST'; |
|---|
| 335 |
if ((isset($options['method'])) && (strtoupper($options['method']) == 'GET')) $method = $options['method']; |
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
if ((isset($options['type'])) && ($options['type'] == 'synchronous')) $type = 'false'; |
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
if (isset($options['form'])) $formData = 'jQuery(this).serialize()'; |
|---|
| 342 |
elseif (isset($options['submit'])) $formData = '{\'#'.$options['submit'].'\'}.serialize()'; |
|---|
| 343 |
|
|---|
| 344 |
// that way, see the Symfony documentation for the original remote_function |
|---|
| 345 |
elseif (isset($options['with'])) $formData = $options['with']; |
|---|
| 346 |
|
|---|
| 347 |
elseif(isset($options['csrf']) && $options['csrf'] == '1') |
|---|
| 348 |
{ |
|---|
| 349 |
|
|---|
| 350 |
// CSRF works properly according to Jose Fernando Castillo Rosas |
|---|
| 351 |
$form = new BaseForm(); |
|---|
| 352 |
if ($form->isCSRFProtected()) |
|---|
| 353 |
{ |
|---|
| 354 |
$formData = '{'.$form->getCSRFFieldName().': \''.$form->getCSRFToken().'\'}'; |
|---|
| 355 |
} |
|---|
| 356 |
} |
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
$function = "jQuery.ajax({"; |
|---|
| 360 |
$function .= 'type:\''.$method.'\''; |
|---|
| 361 |
$function .= ',dataType:\'' . $dataType . '\''; |
|---|
| 362 |
if (isset($type)) $function .= ',async:'.$type; |
|---|
| 363 |
if (isset($cache)) $function .= ',cache:'.$cache; |
|---|
| 364 |
if (isset($formData)) $function .= ',data:'.$formData; |
|---|
| 365 |
if (isset($update_success) and !isset($callback_success)) $function .= ',success:function(data, textStatus){jQuery(\''.$update_success.'\').'.$updateMethod.'(data);}'; |
|---|
| 366 |
if (isset($update_failure)) $function .= ',error:function(XMLHttpRequest, textStatus, errorThrown){'.$update_failure.'}'; |
|---|
| 367 |
if (isset($callback_loading)) $function .= ',beforeSend:function(XMLHttpRequest){'.$callback_loading.'}'; |
|---|
| 368 |
if (isset($callback_complete)) $function .= ',complete:function(XMLHttpRequest, textStatus){'.$callback_complete.'}'; |
|---|
| 369 |
if (isset($callback_success)) $function .= ',success:function(data, textStatus){'.$callback_success.'}'; |
|---|
| 370 |
$function .= ',url:\''.url_for($options['url']).'\''; |
|---|
| 371 |
$function .= '})'; |
|---|
| 372 |
|
|---|
| 373 |
if (isset($options['before'])) |
|---|
| 374 |
{ |
|---|
| 375 |
$function = $options['before'].'; '.$function; |
|---|
| 376 |
} |
|---|
| 377 |
if (isset($options['after'])) |
|---|
| 378 |
{ |
|---|
| 379 |
$function = $function.'; '.$options['after']; |
|---|
| 380 |
} |
|---|
| 381 |
if (isset($options['condition'])) |
|---|
| 382 |
{ |
|---|
| 383 |
$function = 'if ('.$options['condition'].') { '.$function.'; }'; |
|---|
| 384 |
} |
|---|
| 385 |
if (isset($options['confirm'])) |
|---|
| 386 |
{ |
|---|
| 387 |
$function = "if (confirm('".escape_javascript($options['confirm'])."')) { $function; }"; |
|---|
| 388 |
if (isset($options['cancel'])) |
|---|
| 389 |
{ |
|---|
| 390 |
$function = $function.' else { '.$options['cancel'].' }'; |
|---|
| 391 |
} |
|---|
| 392 |
} |
|---|
| 393 |
|
|---|
| 394 |
return $function.( (isset($options['stop_propagation']) && $options['stop_propagation']) ? '; if(event && event.stopPropagation) {event.stopPropagation();} else {window.event.cancelBubble = true;}' : ''); |
|---|
| 395 |
} |
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
function jq_form_remote_tag($options = array(), $options_html = array()) |
|---|
| 420 |
{ |
|---|
| 421 |
$options = _parse_attributes($options); |
|---|
| 422 |
$options_html = _parse_attributes($options_html); |
|---|
| 423 |
|
|---|
| 424 |
$options['form'] = true; |
|---|
| 425 |
|
|---|
| 426 |
$options_html['onsubmit'] = jq_remote_function($options).'; return false;'; |
|---|
| 427 |
$options_html['action'] = isset($options_html['action']) ? $options_html['action'] : url_for($options['url']); |
|---|
| 428 |
$options_html['method'] = isset($options_html['method']) ? $options_html['method'] : 'post'; |
|---|
| 429 |
|
|---|
| 430 |
return tag('form', $options_html, true); |
|---|
| 431 |
} |
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
|
|---|
| 472 |
|
|---|
| 473 |
|
|---|
| 474 |
|
|---|
| 475 |
|
|---|
| 476 |
|
|---|
| 477 |
|
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 |
|
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 |
|
|---|
| 495 |
function jq_visual_effect($effect, $element_id = false, $js_options = array()) |
|---|
| 496 |
{ |
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
if(preg_match("/^(slide|fade)/i", $effect, $matches)) |
|---|
| 500 |
{ |
|---|
| 501 |
$count = strtolower($matches[1]) == 'fade'? 3 : 4; |
|---|
| 502 |
$effect = preg_replace("/(^|_|-)+(.)/e", '', $effect); |
|---|
| 503 |
$effect = preg_replace('/\ +/', '', $effect); |
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 |
|
|---|
| 507 |
$effect = trim(strtolower($matches[1]).ucfirst(strtolower(substr($effect, $count)))); |
|---|
| 508 |
} |
|---|
| 509 |
else |
|---|
| 510 |
{ |
|---|
| 511 |
$effect = trim(strtolower($effect)); |
|---|
| 512 |
} |
|---|
| 513 |
|
|---|
| 514 |
$element = $element_id ? "'$element_id'" : 'this'; |
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
$speed = isset($js_options['speed'])? is_numeric($js_options['speed'] )?$js_options['speed'] : "'". $js_options['speed'] ."'": "'normal'"; |
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 |
$opacity = isset($js_options['opacity']) && is_numeric($js_options['opacity'] )?$js_options['opacity'] >= 0 && $js_options['opacity'] <= 1?$js_options['opacity'] :0.5:0.5; |
|---|
| 521 |
|
|---|
| 522 |
|
|---|
| 523 |
$callback = isset($js_options['callback']) ? ", ". $js_options['callback'] :null; |
|---|
| 524 |
|
|---|
| 525 |
|
|---|
| 526 |
|
|---|
| 527 |
if(in_array($effect, array('hide', 'show','slideDown', 'slideUp', 'slideToggle', 'fadeIn', 'fadeOut'))) |
|---|
| 528 |
{ |
|---|
| 529 |
return sprintf("jQuery(%s).%s(%s %s );", $element, $effect, $speed, $callback); |
|---|
| 530 |
} |
|---|
| 531 |
elseif($effect == "fadeTo") |
|---|
| 532 |
{ |
|---|
| 533 |
return sprintf("jQuery(%s).%s(%s, %s %s);", $element, $effect, $speed, $opacity, $callback); |
|---|
| 534 |
} |
|---|
| 535 |
else |
|---|
| 536 |
{ |
|---|
| 537 |
return sprintf("jQuery(%s).%s();", $element, $effect); |
|---|
| 538 |
} |
|---|
| 539 |
} |
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
function jq_submit_to_remote($name, $value, $options = array(), $options_html = array()) |
|---|
| 547 |
{ |
|---|
| 548 |
$options = _parse_attributes($options); |
|---|
| 549 |
$options_html = _parse_attributes($options_html); |
|---|
| 550 |
|
|---|
| 551 |
if (!isset($options['with'])) |
|---|
| 552 |
{ |
|---|
| 553 |
$options['with'] = 'jQuery(this.form.elements).serialize()'; |
|---|
| 554 |
} |
|---|
| 555 |
|
|---|
| 556 |
$options_html['type'] = 'button'; |
|---|
| 557 |
$options_html['onclick'] = jq_remote_function($options).'; return false;'; |
|---|
| 558 |
$options_html['name'] = $name; |
|---|
| 559 |
$options_html['value'] = $value; |
|---|
| 560 |
|
|---|
| 561 |
return tag('input', $options_html, false); |
|---|
| 562 |
} |
|---|
| 563 |
|
|---|
| 564 |
|
|---|
| 565 |
|
|---|
| 566 |
|
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 |
function jq_submit_image_to_remote($name, $source, $options = array(), $options_html = array()) |
|---|
| 570 |
{ |
|---|
| 571 |
$options = _parse_attributes($options); |
|---|
| 572 |
$options_html = _parse_attributes($options_html); |
|---|
| 573 |
|
|---|
| 574 |
if (!isset($options['with'])) |
|---|
| 575 |
{ |
|---|
| 576 |
$options['with'] = 'jQuery(this.form.elements).serialize()'; |
|---|
| 577 |
} |
|---|
| 578 |
|
|---|
| 579 |
$options_html['type'] = 'image'; |
|---|
| 580 |
$options_html['onclick'] = jq_remote_function($options).'; return false;'; |
|---|
| 581 |
$options_html['name'] = $name; |
|---|
| 582 |
$options_html['src'] = image_path($source); |
|---|
| 583 |
|
|---|
| 584 |
if (!isset($options_html['alt'])) |
|---|
| 585 |
{ |
|---|
| 586 |
$path_pos = strrpos($source, '/'); |
|---|
| 587 |
$dot_pos = strrpos($source, '.'); |
|---|
| 588 |
$begin = $path_pos ? $path_pos + 1 : 0; |
|---|
| 589 |
$nb_str = ($dot_pos ? $dot_pos : strlen($source)) - $begin; |
|---|
| 590 |
$options_html['alt'] = ucfirst(substr($source, $begin, $nb_str)); |
|---|
| 591 |
} |
|---|
| 592 |
|
|---|
| 593 |
return tag('input', $options_html, false); |
|---|
| 594 |
} |
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
|
|---|
| 601 |
|
|---|
| 602 |
|
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 |
|
|---|
| 606 |
|
|---|
| 607 |
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 |
|
|---|
| 613 |
|
|---|
| 614 |
|
|---|
| 615 |
|
|---|
| 616 |
|
|---|
| 617 |
|
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
function jq_sortable_element($selector, $options = array()) |
|---|
| 621 |
{ |
|---|
| 622 |
|
|---|
| 623 |
// we have a catch-all ui package, which is minimized to contain only the |
|---|
| 624 |
// features that actually get used by the plugin. If you want fewer features, |
|---|
| 625 |
// or more features, from jQuery ui then get your own minimized package download |
|---|
| 626 |
// from the jquery ui site |
|---|
| 627 |
jq_add_plugins_by_name(array("ui")); |
|---|
| 628 |
$options = _parse_attributes($options); |
|---|
| 629 |
$options['url'] = url_for($options['url']); |
|---|
| 630 |
$options['type'] = 'POST'; |
|---|
| 631 |
$selector = json_encode($selector); |
|---|
| 632 |
$options = json_encode($options); |
|---|
| 633 |
|
|---|
| 634 |
$result = <<<EOM |
|---|
| 635 |
$(document).ready( |
|---|
| 636 |
function() |
|---|
| 637 |
{ |
|---|
| 638 |
$($selector).sortable( |
|---|
| 639 |
{ |
|---|
| 640 |
update: function(e, ui) |
|---|
| 641 |
{ |
|---|
| 642 |
var serial = jQuery($selector).sortable('serialize', {}); |
|---|
| 643 |
var options = $options; |
|---|
| 644 |
options['data'] = serial; |
|---|
| 645 |
$.ajax(options); |
|---|
| 646 |
} |
|---|
| 647 |
} ); |
|---|
| 648 |
}); |
|---|
| 649 |
EOM; |
|---|
| 650 |
return javascript_tag($result); |
|---|
| 651 |
} |
|---|
| 652 |
|
|---|
| 653 |
|
|---|
| 654 |
|
|---|
| 655 |
|
|---|
| 656 |
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 |
|
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 |
|
|---|
| 667 |
|
|---|
| 668 |
|
|---|
| 669 |
|
|---|
| 670 |
|
|---|
| 671 |
|
|---|
| 672 |
|
|---|
| 673 |
|
|---|
| 674 |
function jq_input_auto_complete_tag($name, $value, $url, $tag_options = array(), $completion_options = array()) { |
|---|
| 675 |
|
|---|
| 676 |
jq_add_plugins_by_name(array("autocomplete")); |
|---|
| 677 |
|
|---|
| 678 |
$tag_options = _convert_options($tag_options); |
|---|
| 679 |
$comp_options = _convert_options($completion_options); |
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
$jsonOptions = ''; |
|---|
| 683 |
foreach ($comp_options as $key => $val) |
|---|
| 684 |
{ |
|---|
| 685 |
if ($jsonOptions!='') |
|---|
| 686 |
{ |
|---|
| 687 |
$jsonOptions .= ', '; |
|---|
| 688 |
} |
|---|
| 689 |
switch($key) { |
|---|
| 690 |
case 'formatItem': |
|---|
| 691 |
case 'formatResult': |
|---|
| 692 |
$jsonOptions .= "$key: " . $val; |
|---|
| 693 |
break; |
|---|
| 694 |
default: |
|---|
| 695 |
$jsonOptions .= "$key: " . json_encode($val); |
|---|
| 696 |
break; |
|---|
| 697 |
} |
|---|
| 698 |
} |
|---|
| 699 |
|
|---|
| 700 |
|
|---|
| 701 |
$context = sfContext::getInstance(); |
|---|
| 702 |
$response = $context->getResponse(); |
|---|
| 703 |
$comp_options = _convert_options($completion_options); |
|---|
| 704 |
if (isset($comp_options['use_style']) && $comp_options['use_style'] == true) |
|---|
| 705 |
{ |
|---|
| 706 |
$response->addStylesheet(sfConfig::get('sf_jquery_web_dir').'/css/JqueryAutocomplete'); |
|---|
| 707 |
} |
|---|
| 708 |
|
|---|
| 709 |
|
|---|
| 710 |
$tag_options['id'] = get_id_from_name(isset($tag_options['id']) ? $tag_options['id'] : $name); |
|---|
| 711 |
|
|---|
| 712 |
|
|---|
| 713 |
$javascript = tag('input', array_merge(array('type' => 'text', 'name' => $name, 'value' => $value), _convert_options($tag_options))); |
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 |
$autocomplete_script = sprintf('$("#%s").autocomplete("%s",{ %s });',$name,$url,$jsonOptions); |
|---|
| 717 |
$javascript .= javascript_tag($autocomplete_script); |
|---|
| 718 |
|
|---|
| 719 |
return $javascript; |
|---|
| 720 |
} |
|---|
| 721 |
|
|---|
| 722 |
|
|---|
| 723 |
|
|---|
| 724 |
|
|---|
| 725 |
|
|---|
| 726 |
|
|---|
| 727 |
|
|---|
| 728 |
|
|---|
| 729 |
|
|---|
| 730 |
|
|---|
| 731 |
|
|---|
| 732 |
|
|---|
| 733 |
function jq_draggable_element($selector, $options = array()) |
|---|
| 734 |
{ |
|---|
| 735 |
|
|---|
| 736 |
jq_add_plugins_by_name(array("ui")); |
|---|
| 737 |
$options = json_encode(_parse_attributes($options)); |
|---|
| 738 |
$selector = json_encode($selector); |
|---|
| 739 |
return javascript_tag("jQuery($selector).draggable($options)"); |
|---|
| 740 |
} |
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
|
|---|
| 747 |
|
|---|
| 748 |
|
|---|
| 749 |
|
|---|
| 750 |
|
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 |
|
|---|
| 754 |
|
|---|
| 755 |
function jq_drop_receiving_element($selector, $options = array()) |
|---|
| 756 |
{ |
|---|
| 757 |
jq_add_plugins_by_name(array("ui")); |
|---|
| 758 |
if (!isset($options['with'])) |
|---|
| 759 |
{ |
|---|
| 760 |
$options['with'] = "'id=' + encodeURIComponent(element.id)"; |
|---|
| 761 |
} |
|---|
| 762 |
if (!isset($options['drop'])) |
|---|
| 763 |
{ |
|---|
| 764 |
$options['drop'] = "function(element){".jq_remote_function($options)."}"; |
|---|
| 765 |
} |
|---|
| 766 |
|
|---|
| 767 |
|
|---|
| 768 |
if (isset($options['hoverclass'])) |
|---|
| 769 |
{ |
|---|
| 770 |
$options['hoverClass'] = $options['hoverclass']; |
|---|
| 771 |
} |
|---|
| 772 |
$options['hoverClass'] = json_encode('hoverclass'); |
|---|
| 773 |
|
|---|
| 774 |
foreach (jq_get_ajax_options() as $key) |
|---|
| 775 |
{ |
|---|
| 776 |
unset($options[$key]); |
|---|
| 777 |
} |
|---|
| 778 |
|
|---|
| 779 |
if (isset($options['accept'])) |
|---|
| 780 |
{ |
|---|
| 781 |
$options['accept'] = json_encode($options['accept']); |
|---|
| 782 |
} |
|---|
| 783 |
$options = jq_options_for_javascript($options); |
|---|
| 784 |
$selector = json_encode($selector); |
|---|
| 785 |
return javascript_tag("jQuery($selector).droppable($options);"); |
|---|
| 786 |
} |
|---|
| 787 |
|
|---|
| 788 |
function _update_method($position) { |
|---|
| 789 |
|
|---|
| 790 |
$updateMethod = 'html'; |
|---|
| 791 |
switch ($position) { |
|---|
| 792 |
case 'before':$updateMethod='before';break; |
|---|
| 793 |
case 'after':$updateMethod='after';break; |
|---|
| 794 |
case 'top':$updateMethod='prepend';break; |
|---|
| 795 |
case 'bottom':$updateMethod='append';break; |
|---|
| 796 |
} |
|---|
| 797 |
|
|---|
| 798 |
return $updateMethod; |
|---|
| 799 |
} |
|---|
| 800 |
|
|---|
| 801 |
|
|---|
| 802 |
|
|---|
| 803 |
|
|---|
| 804 |
|
|---|
| 805 |
|
|---|
| 806 |
|
|---|
| 807 |
|
|---|
| 808 |
function jq_link_to_function($name, $function, $html_options = array()) |
|---|
| 809 |
{ |
|---|
| 810 |
$html_options = _parse_attributes($html_options); |
|---|
| 811 |
|
|---|
| 812 |
$html_options['href'] = isset($html_options['href']) ? $html_options['href'] : '#'; |
|---|
| 813 |
if ( isset($html_options['confirm']) ) |
|---|
| 814 |
{ |
|---|
| 815 |
$confirm = escape_javascript($html_options['confirm']); |
|---|
| 816 |
$html_options['onclick'] = "if(confirm('$confirm')){ $function;}; return false;"; |
|---|
| 817 |
|
|---|
| 818 |
// (we could call window.confirm, but there is no reason to have the |
|---|
| 819 |
// nonstandard confirm attribute) |
|---|
| 820 |
unset($html_options['confirm']); |
|---|
| 821 |
} |
|---|
| 822 |
else |
|---|
| 823 |
{ |
|---|
| 824 |
$html_options['onclick'] = $function.'; return false;'; |
|---|
| 825 |
} |
|---|
| 826 |
|
|---|
| 827 |
return content_tag('a', $name, $html_options); |
|---|
| 828 |
} |
|---|
| 829 |
|
|---|
| 830 |
|
|---|
| 831 |
function jq_button_to_function($name, $function, $html_options = array()) |
|---|
| 832 |
{ |
|---|
| 833 |
return button_to_function($name, $function, $html_options); |
|---|
| 834 |
} |
|---|
| 835 |
|
|---|
| 836 |
|
|---|
| 837 |
|
|---|
| 838 |
function jq_javascript_tag($content = null) |
|---|
| 839 |
{ |
|---|
| 840 |
return javascript_tag($content); |
|---|
| 841 |
} |
|---|
| 842 |
|
|---|
| 843 |
|
|---|
| 844 |
|
|---|
| 845 |
|
|---|
| 846 |
function jq_javascript_cdata_section($content) |
|---|
| 847 |
{ |
|---|
| 848 |
return javascript_cdata_section($content); |
|---|
| 849 |
} |
|---|
| 850 |
|
|---|
| 851 |
|
|---|
| 852 |
|
|---|
| 853 |
function jq_if_javascript() |
|---|
| 854 |
{ |
|---|
| 855 |
return if_javascript(); |
|---|
| 856 |
} |
|---|
| 857 |
|
|---|
| 858 |
|
|---|
| 859 |
|
|---|
| 860 |
function jq_end_javascript_tag() |
|---|
| 861 |
{ |
|---|
| 862 |
return end_javascript_tag(); |
|---|
| 863 |
} |
|---|
| 864 |
|
|---|
| 865 |
function _options_for_javascript($options) |
|---|
| 866 |
{ |
|---|
| 867 |
$opts = array(); |
|---|
| 868 |
foreach ($options as $key => $value) |
|---|
| 869 |
{ |
|---|
| 870 |
$opts[] = "$key:$value"; |
|---|
| 871 |
} |
|---|
| 872 |
sort($opts); |
|---|
| 873 |
|
|---|
| 874 |
return '{'.join(', ', $opts).'}'; |
|---|
| 875 |
} |
|---|