Development

Changeset 6712

You must first sign up to be able to contribute.

Changeset 6712

Show
Ignore:
Timestamp:
12/26/07 15:48:29 (5 years ago)
Author:
Tiago.Ribeiro
Message:

Added JSON dataType support
Removed escaping from 'with' parameter

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfJqueryPlugin/README

    r6711 r6712  
    8181These are the methods reconverted into jQuery functions till the `sfJqueryPlugin` last version. Future changes will integrate more functions. 
    8282 
    83 == Todo == 
     83== Using JSON == 
     84http://trac.symfony-project.com/wiki/AjaxAndJSON#jsonwithoutheader and: 
     85{{{ 
     86<?php echo jq_link_to_remote(image_tag('icons/delete.png'), 
     87                             array('url'      => '@delete')), 
     88                                   'dataType' => 'json', 
     89                                   'success'  => 'leaveGroup(request)')) ?> 
     90}}} 
     91 
     92== TODO == 
    8493 
    8594 * Add unit tests 
     
    97106=== Trunk === 
    98107 
     108 * Tiago.Ribeiro: Added JSON dataType support 
     109 * Tiago.Ribeiro: Removed escaping from 'with' parameter 
    99110 * Tiago.Ribeiro: Added request parameter to `complete` callback 
    100111 * francois: Replaced `$` by `jQuery` 
  • plugins/sfJqueryPlugin/lib/helper/jQueryHelper.php

    r6485 r6712  
    1212function jq_periodically_call_remote($options = array()) 
    1313{ 
    14  $frequency = isset($options['frequency']) ? $options['frequency'] : 10; // every ten seconds by default 
    15  $code = 'setInterval(function() {'.jq_remote_function($options).'}, '.($frequency * 1000).')'; 
    16  
    17  return jq_javascript_tag($code); 
     14  $frequency = isset($options['frequency']) ? $options['frequency'] : 10; // every ten seconds by default 
     15  $code = 'setInterval(function() {'.jq_remote_function($options).'}, '.($frequency * 1000).')'; 
     16 
     17  return jq_javascript_tag($code); 
    1818} 
    1919 
     
    2828function jq_link_to_function($name, $function, $html_options = array()) 
    2929{ 
    30  $html_options = _parse_attributes($html_options); 
    31  
    32  $html_options['href'] = isset($html_options['href']) ? $html_options['href'] : '#'; 
    33  $html_options['onclick'] = $function.'; return false;'; 
    34  
    35  return content_tag('a', $name, $html_options); 
     30  $html_options = _parse_attributes($html_options); 
     31 
     32  $html_options['href'] = isset($html_options['href']) ? $html_options['href'] : '#'; 
     33  $html_options['onclick'] = $function.'; return false;'; 
     34 
     35  return content_tag('a', $name, $html_options); 
    3636} 
    3737 
     
    134134function jq_link_to_remote($name, $options = array(), $html_options = array()) 
    135135{ 
    136  return jq_link_to_function($name, jq_remote_function($options), $html_options); 
     136  return jq_link_to_function($name, jq_remote_function($options), $html_options); 
    137137} 
    138138 
     
    157157function jq_update_element_function($element_id, $options = array()) 
    158158{ 
    159  $content = escape_javascript(isset($options['content']) ? $options['content'] : ''); 
    160  
    161  $value = isset($options['action']) ? $options['action'] : 'update'; 
    162  switch ($value) 
    163 
    164    case 'update': 
    165      $updateMethod = _update_method(isset($options['position']) ? $options['position'] : ''); 
    166      $javascript_function = "jQuery('#$element_id').$updateMethod('$content')"; 
    167      break; 
    168  
    169    case 'empty': 
    170      $javascript_function = "jQuery('#$element_id').empty()"; 
    171      break; 
    172  
    173    case 'remove': 
    174      $javascript_function = "jQuery('#$element_id').remove()"; 
    175      break; 
    176  
    177    default: 
    178      throw new sfException('Invalid action, choose one of update, remove, empty'); 
    179 
    180  
    181  $javascript_function .= ";\n"; 
    182  
    183  return (isset($options['binding']) ? $javascript_function.$options['binding'] : $javascript_function); 
     159  $content = escape_javascript(isset($options['content']) ? $options['content'] : ''); 
     160 
     161  $value = isset($options['action']) ? $options['action'] : 'update'; 
     162  switch ($value) 
     163 
     164    case 'update': 
     165      $updateMethod = _update_method(isset($options['position']) ? $options['position'] : ''); 
     166      $javascript_function = "jQuery('#$element_id').$updateMethod('$content')"; 
     167      break; 
     168 
     169    case 'empty': 
     170      $javascript_function = "jQuery('#$element_id').empty()"; 
     171      break; 
     172 
     173    case 'remove': 
     174      $javascript_function = "jQuery('#$element_id').remove()"; 
     175      break; 
     176 
     177    default: 
     178      throw new sfException('Invalid action, choose one of update, remove, empty'); 
     179 
     180 
     181  $javascript_function .= ";\n"; 
     182 
     183  return (isset($options['binding']) ? $javascript_function.$options['binding'] : $javascript_function); 
    184184} 
    185185 
     
    196196function jq_remote_function($options) 
    197197{ 
    198   // Defining elements to update 
    199   if (isset($options['update']) && is_array($options['update'])) 
    200   { 
    201     // On success, update the element with returned data 
    202     if (isset($options['update']['success'])) $update_success = "#".$options['update']['success']; 
    203  
    204     // On failure, execute a client-side function 
    205     if (isset($options['update']['failure'])) $update_failure = $options['update']['failure']; 
    206   } 
    207   else if (isset($options['update'])) $update_success = "#".$options['update']; 
    208  
    209   // Update method 
    210   $updateMethod = _update_method(isset($options['position']) ? $options['position'] : ''); 
    211  
    212   // Callbacks 
    213   if (isset($options['loading'])) $callback_loading = $options['loading']; 
    214   if (isset($options['complete'])) $callback_complete = $options['complete']; 
    215  
    216   // POST or GET ? 
    217   $method = 'POST'; 
    218   if ((isset($options['method'])) && (strtoupper($options['method']) == 'GET')) $method = $options['method']; 
    219  
    220   // async or sync, async is default 
    221   if ((isset($options['type'])) && ($options['type'] == 'synchronous')) $type = 'false'; 
    222  
    223   $execute = 'false'; 
    224   if ((isset($options['script'])) && ($options['script'] == '1')) $execute = 'true'; 
    225  
    226   // Is it a form submitting 
    227   if (isset($options['form'])) $formData = 'jQuery(this).serialize()'; 
    228   elseif (isset($options['submit'])) $formData = '{\'#'.$options['submit'].'\'}.serialize()'; 
    229   elseif (isset($options['with'])) $formData = '\''.$options['with'].'\''; 
    230  
    231   // build the function 
    232   $function = "jQuery.ajax({"; 
    233   $function .= 'type:\''.$method.'\''; 
    234   if ($execute) $function .= ',dataType:\'html\'';else $function .= 'dataType:\'text\''; 
    235   if (isset($type)) $function .= ',async:'.$type; 
    236   if (isset($formData)) $function .= ',data:'.$formData; 
    237   if (isset($update_success)) $function .= ',success:function(i){jQuery(\''.$update_success.'\').'.$updateMethod.'(i);}'; 
    238   if (isset($update_failure)) $function .= ',error:function(){'.$update_failure.'}'; 
    239   if (isset($callback_loading)) $function .= ',beforeSend:function(){'.$callback_loading.'}'; 
    240   if (isset($callback_complete)) $function .= ',complete:function(request){'.$callback_complete.'}'; 
    241   $function .= ',url:\''.url_for($options['url']).'\''; 
    242   $function .= '})'; 
    243  
    244   if (isset($options['before'])) 
    245   { 
    246     $function = $options['before'].'; '.$function; 
    247   } 
    248   if (isset($options['after'])) 
    249   { 
    250     $function = $function.'; '.$options['after']; 
    251   } 
    252   if (isset($options['condition'])) 
    253   { 
    254     $function = 'if ('.$options['condition'].') { '.$function.'; }'; 
    255   } 
    256   if (isset($options['confirm'])) 
    257   { 
    258     $function = "if (confirm('".escape_javascript($options['confirm'])."')) { $function; }"; 
    259     if (isset($options['cancel'])) 
    260     { 
    261       $function = $function.' else { '.$options['cancel'].' }'; 
    262     } 
    263   } 
    264  
    265   return $function; 
     198  // Defining elements to update 
     199  if (isset($options['update']) && is_array($options['update'])) 
     200  { 
     201    // On success, update the element with returned data 
     202    if (isset($options['update']['success'])) $update_success = "#".$options['update']['success']; 
     203 
     204    // On failure, execute a client-side function 
     205    if (isset($options['update']['failure'])) $update_failure = $options['update']['failure']; 
     206  } 
     207  else if (isset($options['update'])) $update_success = "#".$options['update']; 
     208 
     209  // Update method 
     210  $updateMethod = _update_method(isset($options['position']) ? $options['position'] : ''); 
     211 
     212  // Callbacks 
     213  if (isset($options['loading'])) $callback_loading = $options['loading']; 
     214  if (isset($options['complete'])) $callback_complete = $options['complete']; 
     215  if (isset($options['success'])) $callback_success = $options['success']; 
     216 
     217  // Data Type 
     218  if (isset($options['dataType'])) 
     219  { 
     220    $dataType = $options['dataType']; 
     221  } 
     222  elseif ($execute) 
     223  { 
     224    $dataType = 'html'; 
     225  } 
     226  else 
     227  { 
     228    $dataType = 'text'; 
     229  } 
     230 
     231  // POST or GET ? 
     232  $method = 'POST'; 
     233  if ((isset($options['method'])) && (strtoupper($options['method']) == 'GET')) $method = $options['method']; 
     234 
     235  // async or sync, async is default 
     236  if ((isset($options['type'])) && ($options['type'] == 'synchronous')) $type = 'false'; 
     237 
     238  $execute = 'false'; 
     239  if ((isset($options['script'])) && ($options['script'] == '1')) $execute = 'true'; 
     240 
     241  // Is it a form submitting 
     242  if (isset($options['form'])) $formData = 'jQuery(this).serialize()'; 
     243  elseif (isset($options['submit'])) $formData = '{\'#'.$options['submit'].'\'}.serialize()'; 
     244  elseif (isset($options['with'])) $formData = $options['with']; 
     245 
     246  // build the function 
     247  $function = "jQuery.ajax({"; 
     248  $function .= 'type:\''.$method.'\''; 
     249  $function .= ',dataType:\'' . $dataType . '\''; 
     250  if (isset($type)) $function .= ',async:'.$type; 
     251  if (isset($formData)) $function .= ',data:'.$formData; 
     252  if (isset($update_success) and !isset($callback_success)) $function .= ',success:function(i){jQuery(\''.$update_success.'\').'.$updateMethod.'(i);}'; 
     253  if (isset($update_failure)) $function .= ',error:function(){'.$update_failure.'}'; 
     254  if (isset($callback_loading)) $function .= ',beforeSend:function(){'.$callback_loading.'}'; 
     255  if (isset($callback_complete)) $function .= ',complete:function(request){'.$callback_complete.'}'; 
     256  if (isset($callback_success)) $function .= ',success:function(request){'.$callback_success.'}'; 
     257  $function .= ',url:\''.url_for($options['url']).'\''; 
     258  $function .= '})'; 
     259 
     260  if (isset($options['before'])) 
     261  { 
     262    $function = $options['before'].'; '.$function; 
     263  } 
     264  if (isset($options['after'])) 
     265  { 
     266    $function = $function.'; '.$options['after']; 
     267  } 
     268  if (isset($options['condition'])) 
     269  { 
     270    $function = 'if ('.$options['condition'].') { '.$function.'; }'; 
     271  } 
     272  if (isset($options['confirm'])) 
     273  { 
     274    $function = "if (confirm('".escape_javascript($options['confirm'])."')) { $function; }"; 
     275    if (isset($options['cancel'])) 
     276    { 
     277      $function = $function.' else { '.$options['cancel'].' }'; 
     278    } 
     279  } 
     280 
     281  return $function; 
    266282} 
    267283 
     
    290306function jq_form_remote_tag($options = array(), $options_html = array()) 
    291307{ 
    292  $options = _parse_attributes($options); 
    293  $options_html = _parse_attributes($options_html); 
    294  
    295  $options['form'] = true; 
    296  
    297  $options_html['onsubmit'] = jq_remote_function($options).'; return false;'; 
    298  $options_html['action'] = isset($options_html['action']) ? $options_html['action'] : url_for($options['url']); 
    299  $options_html['method'] = isset($options_html['method']) ? $options_html['method'] : 'post'; 
    300  
    301  return tag('form', $options_html, true); 
     308  $options = _parse_attributes($options); 
     309  $options_html = _parse_attributes($options_html); 
     310 
     311  $options['form'] = true; 
     312 
     313  $options_html['onsubmit'] = jq_remote_function($options).'; return false;'; 
     314  $options_html['action'] = isset($options_html['action']) ? $options_html['action'] : url_for($options['url']); 
     315  $options_html['method'] = isset($options_html['method']) ? $options_html['method'] : 'post'; 
     316 
     317  return tag('form', $options_html, true); 
    302318} 
    303319 
     
    308324function jq_submit_to_remote($name, $value, $options = array(), $options_html = array()) 
    309325{ 
    310  $options = _parse_attributes($options); 
    311  $options_html = _parse_attributes($options_html); 
    312  
    313  if (!isset($options['with'])) 
    314 
    315    $options['with'] = 'this.form.serialize()'; 
    316 
    317  
    318  $options_html['type'] = 'button'; 
    319  $options_html['onclick'] = remote_function($options).'; return false;'; 
    320  $options_html['name'] = $name; 
    321  $options_html['value'] = $value; 
    322  
    323  return tag('input', $options_html, false); 
     326  $options = _parse_attributes($options); 
     327  $options_html = _parse_attributes($options_html); 
     328 
     329  if (!isset($options['with'])) 
     330 
     331    $options['with'] = 'this.form.serialize()'; 
     332 
     333 
     334  $options_html['type'] = 'button'; 
     335  $options_html['onclick'] = remote_function($options).'; return false;'; 
     336  $options_html['name'] = $name; 
     337  $options_html['value'] = $value; 
     338 
     339  return tag('input', $options_html, false); 
    324340} 
    325341 
     
    332348function jq_javascript_tag($content) 
    333349{ 
    334  return content_tag('script', jq_javascript_cdata_section($content), array('type' => 'text/javascript')); 
     350  return content_tag('script', jq_javascript_cdata_section($content), array('type' => 'text/javascript')); 
    335351} 
    336352 
    337353function jq_javascript_cdata_section($content) 
    338354{ 
    339  return "\n//".cdata_section("\n$content\n//")."\n"; 
     355  return "\n//".cdata_section("\n$content\n//")."\n"; 
    340356} 
    341357 
    342358function _jq_options_for_javascript($options) 
    343359{ 
    344  $opts = array(); 
    345  foreach ($options as $key => $value) 
    346 
    347    $opts[] = "$key:$value"; 
    348 
    349  sort($opts); 
    350  
    351  return '{'.join(', ', $opts).'}'; 
     360  $opts = array(); 
     361  foreach ($options as $key => $value) 
     362 
     363    $opts[] = "$key:$value"; 
     364 
     365  sort($opts); 
     366 
     367  return '{'.join(', ', $opts).'}'; 
    352368} 
    353369 
    354370function _update_method($position) { 
    355  // Updating method 
    356  $updateMethod = 'html'; 
    357  switch ($position) { 
    358    case 'before':$updateMethod='before';break; 
    359    case 'after':$updateMethod='after';break; 
    360    case 'top':$updateMethod='prepend';break; 
    361    case 'bottom':$updateMethod='append';break; 
    362 
    363  
    364  return $updateMethod; 
    365 } 
     371  // Updating method 
     372  $updateMethod = 'html'; 
     373  switch ($position) { 
     374    case 'before':$updateMethod='before';break; 
     375    case 'after':$updateMethod='after';break; 
     376    case 'top':$updateMethod='prepend';break; 
     377    case 'bottom':$updateMethod='append';break; 
     378 
     379 
     380  return $updateMethod; 
     381}