Skip to content

Commit

Permalink
[Tokeninput] moved jquery configs to a configs key
Browse files Browse the repository at this point in the history
  • Loading branch information
bamarni committed Apr 29, 2013
1 parent 894ee20 commit 74b0b18
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 167 deletions.
100 changes: 35 additions & 65 deletions Form/JQuery/Type/TokeninputType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,6 @@ class TokeninputType extends AbstractType
{
private $widget;

/**
* Available options to set
*
* @var array
*/
protected $_availableTokeninputOptions = array(
'method',
'queryParam',
'searchDelay',
'minChars',
'propertyToSearch',
'jsonContainer',
'crossDomain',
'prePopulate',
'hintText',
'noResultsText',
'searchingText',
'deleteText',
'animateDropdown',
'theme',
'resultsFormatter',
'tokenFormatter',
'tokenLimit',
'tokenDelimiter',
'preventDuplicates',
'tokenValue'
);

public function __construct($widget)
{
$this->widget = $widget;
Expand All @@ -66,12 +38,12 @@ public function __construct($widget)
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if (isset($options['tokenLimit']) && is_numeric($options['tokenLimit']) && $options['tokenLimit'] > 0) {
$options['multiple'] = (1 != $options['tokenLimit']);
if (isset($options['configs']['tokenLimit']) && is_numeric($options['configs']['tokenLimit']) && $options['configs']['tokenLimit'] > 0) {
$options['multiple'] = (1 != $options['configs']['tokenLimit']);
}

if (!$options['multiple']) {
$options['tokenLimit'] = 1;
$options['configs']['tokenLimit'] = 1;
}

$builder
Expand Down Expand Up @@ -104,13 +76,8 @@ public function buildView(FormView $view, FormInterface $form, array $options)
}

$view->vars['tokeninput_value'] = $value;
$view->vars['route_name'] = $form->getAttribute('route_name');

foreach ($this->_availableTokeninputOptions as $option) {
if (null !== $options[$option]) {
$view->vars[$option] = $options[$option];
}
}
$view->vars['configs'] = $options['configs'];
$view->vars['route_name'] = $form->getConfig()->getAttribute('route_name');

array_splice(
$view->vars['block_prefixes'],
Expand All @@ -125,40 +92,43 @@ public function buildView(FormView $view, FormInterface $form, array $options)
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{

$defaults = array_flip($this->_availableTokeninputOptions);
array_walk($defaults, function(&$option, $value) {
$option = null;
});

$widget = $this->widget;

$defaults = array_merge($defaults, array(
'route_name' => null,
'ajax' => function (Options $options, $previousValue) {
if (null === $previousValue) {
if (false === empty($options['route_name'])) {
return true;
}
}

return false;
},
'choice_list' => function (Options $options, $previousValue) use ($widget) {
if (!in_array($widget, array('entity', 'document', 'model'))) {
return new AjaxSimpleChoiceList($options['choices'], $options['ajax']);
}

return $previousValue;
},
$defaults = array(
'queryParam' => 'term',
'preventDuplicates' => true,
'tokenValue' => 'value',
'propertyToSearch' => 'label',
'theme' => 'facebook'
));
'theme' => 'facebook',
);

$resolver
->setDefaults(array(
'route_name' => null,
'ajax' => function (Options $options, $previousValue) {
if (null === $previousValue) {
if (false === empty($options['route_name'])) {
return true;
}
}

return false;
},
'choice_list' => function (Options $options, $previousValue) use ($widget) {
if (!in_array($widget, array('entity', 'document', 'model'))) {
return new AjaxSimpleChoiceList($options['choices'], $options['ajax']);
}

$resolver->setDefaults($defaults);
return $previousValue;
},
'configs' => $defaults,
))
->setNormalizers(array(
'configs' => function (Options $options, $configs) use ($defaults) {
return array_merge($defaults, $configs);
},
))
;
}

/**
Expand Down
124 changes: 24 additions & 100 deletions Resources/views/Form/jquery_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -202,116 +202,40 @@
var update_hidden_sourceinput = function(item) {
var $val = $tokeninput.tokenInput('get');
{% if multiple is empty %} // get first (and only one) element from object
{% if multiple is empty %}
for (first in $val) break;
$val = $val[first];{% endif %}
$val = $val[first];
{% endif %}
$field.val(JSON.stringify($val));
};
{% if value is not empty %}
{% if multiple is empty %}
{% set prePopulate = '[' ~ value ~ ']' %}
{% else %}
{% set prePopulate = value %}
{% if value is not empty %}
{% if multiple is empty %}
{% set prePopulate = '[' ~ value ~ ']' %}
{% else %}
{% set prePopulate = value %}
{% endif %}
{% endif %}
{% endif %}
var $configs = {
{% if method is defined %} // The HTTP method (eg. GET, POST) to use for the server request. default: “GET”.
method: '{{ method }}',
{% endif %}
{% if queryParam is defined %} // The name of the query param which you expect to contain the search term on the server-side. default: “q”.
queryParam: '{{ queryParam }}',
{% endif %}
{% if searchDelay is defined %} // The delay, in milliseconds, between the user finishing typing and the search being performed. default: 300
searchDelay: {{ searchDelay }},
{% endif %}
{% if minChars is defined %} // The minimum number of characters the user must enter before a search is performed. default: 1
minChars: {{ minChars }},
{% endif %}
{% if propertyToSearch is defined %} // The javascript/json object attribute to search. default: “name”
propertyToSearch: '{{ propertyToSearch }}',
{% endif %}
{% if tokenValue is defined %} // The value of the token input when the input is submitted.
// Set it to id in order to get a concatenation of token IDs, or to name in order to get a concatenation of names.
// default: id
tokenValue: '{{ tokenValue }}',
{% endif %}
{% if jsonContainer is defined %} // The name of the json object in the response which contains the search results. This is typically used
// when your endpoint returns other data in addition to your search results. Use null to use the top level
// response object. default: null.
jsonContainer: '{{ jsonContainer }}',
{% endif %}
{% if crossDomain is defined %} // Force JSONP cross-domain communication to the server instead of a normal ajax request.
// Note: JSONP is automatically enabled if we detect the search request is a cross-domain request.
// default: false.
crossDomain: {{ crossDomain }},
{% endif %}
{% if prePopulate is defined %} // Prepopulate the tokeninput with existing data. Set to an array of JSON objects,
// eg: [{id: 3, name: "test"}, {id: 5, name: "awesome"}] to pre-fill the input. default: null.
var $configs = $.extend({{ configs|json_encode|raw }}, {
prePopulate: {{ prePopulate|raw }},
{% endif %}
{% if hintText is defined %} // The text to show in the dropdown label which appears when you first click in the search field.
// default: “Type in a search term”
hintText: '{{ hintText }}',
{% endif %}
{% if noResultsText is defined %} // The text to show in the dropdown label when no results are found which match the current query.
// default: “No results”.
noResultsText: '{{ noResultsText }}',
{% endif %}
{% if searchingText is defined %} // The text to show in the dropdown label when a search is currently in progress.
// default: “Searching…”.
searchingText: '{{ searchingText }}',
{% endif %}
{% if deleteText is defined %} // The text to show on each token which deletes the token when clicked.
// If you wish to hide the delete link, provide an empty string here.
// Alternatively you can provide a html string here if you would like to show an image for deleting tokens.
// default: ×.
deleteText: '{{ deleteText }}',
{% endif %}
{% if animateDropdown is defined %} // Set this to false to disable animation of the dropdown default: true.
animateDropdown: {{ animateDropdown }},
{% endif %}
{% if theme is defined %} // Set this to a string, eg “facebook” when including theme css files to set the css class suffix.
theme: '{{ theme }}',
{% endif %}
{% if resultsFormatter is defined %} // A function that returns an interpolated HTML string for each result.
// Use this function with a templating system of your choice, such as jresig microtemplates or mustache.js.
// Use this when you want to include images or multiline formatted results
// default: function(item){ return “<li>” + item.propertyToSearch + “</li>” }.
resultsFormatter: {{ resultsFormatter }},
{% endif %}
{% if tokenFormatter is defined %} // A function that returns an interpolated HTML string for each token.
// Use this function with a templating system of your choice, such as jresig microtemplates or mustache.js.
// Use this when you want to include images or multiline formatted tokens.
// Quora’s people invite token field that returns avatar tokens is a good example of what can be done this option.
// default: function(item){ return “<li><p>” + item.propertyToSearch + “</p></li>” }.
tokenFormatter: {{ tokenFormatter }},
{% endif %}
{% if tokenLimit is defined or multiple %} // The maximum number of results allowed to be selected by the user. Use null to allow unlimited selections.
// default: null.
tokenLimit: {% if multiple %}{% if tokenLimit is defined %}{{ tokenLimit }}{% else %}null{% endif %}{% else %}1{% endif %},
{% endif %}
{% if tokenDelimiter is defined %} // The separator to use when sending the results back to the server. default: “,”.
tokenDelimiter: '{{ tokenDelimiter }}',
{% endif %}
{% if preventDuplicates is defined %} // Prevent user from selecting duplicate values by setting this to true. default: false.
preventDuplicates: {{ preventDuplicates }},
{% endif %}
onAdd: update_hidden_sourceinput,
onDelete: update_hidden_sourceinput
};
});
$tokeninput.tokenInput(
{% if route_name %}
'{{ path(route_name) }}'
{% else %}
{% set sourceChoices = [] %}
{% for choice in choices %}
{% set sourceChoices = sourceChoices|merge([{'value': choice.value, 'label': choice.label}]) %}
{% endfor %}
{{ sourceChoices|json_encode|raw }}
{% endif %},
$configs);
{% if route_name %}
'{{ path(route_name) }}'
{% else %}
{% set sourceChoices = [] %}
{% for choice in choices %}
{% set sourceChoices = sourceChoices|merge([{'value': choice.value, 'label': choice.label}]) %}
{% endfor %}
{{ sourceChoices|json_encode|raw }}
{% endif %},
$configs
);
});
</script>
{% endspaceless %}
Expand Down
4 changes: 2 additions & 2 deletions UPGRADE 2.1.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
UPGRADE to Symfony 2.1
======================
UPGRADE to 2.1
==============

### JQuery Chosen

Expand Down
25 changes: 25 additions & 0 deletions UPGRADE 2.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
UPGRADE to 2.2
==============

### JQuery Tokeninput

JQuery widget options have to be moved to a ```configs``` key :

Before :
``` php
$formBuilder
->add('country', 'genemu_jquerytokeninput_country', array(
'tokenLimit' => 2,
'theme' => 'facebook',
));
```
Now :
``` php
$formBuilder
->add('country', 'genemu_jquerytokeninput_country', array(
'configs' => array(
'tokenLimit' => 2,
'theme' => 'facebook',
)
));
```

0 comments on commit 74b0b18

Please sign in to comment.