Skip to content

Commit

Permalink
js and entity name (#69)
Browse files Browse the repository at this point in the history
* Js, neccesary method serializeForm.
  • Loading branch information
gonzaloalonsod authored and alterphp committed Oct 17, 2018
1 parent 25cc173 commit 5d60b75
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$view->vars['attr']['data-easyadmin-autocomplete-create-action-url'] = $this->router->generate(
'easyadmin', array('action' => 'newAjax', 'entity' => $view->vars['autocomplete_entity_name'])
);
$view->vars['attr']['data-easyadmin-autocomplete-create-field-name'] = $view->vars['name'];
$view->vars['attr']['data-easyadmin-autocomplete-create-field-name'] = strtolower($view->vars['autocomplete_entity_name']);
$view->vars['attr']['data-easyadmin-autocomplete-create-button-text'] = $this->translator->trans(
'action.add_new_item', array(), 'EasyAdminBundle'
);
Expand Down
162 changes: 81 additions & 81 deletions src/Resources/public/js/autocomplete-create.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,100 @@
function createAutoCompleteCreateFields() {
var autocompleteCreateFields = $('[data-easyadmin-autocomplete-create-action-url]');
var autocompleteCreateFields = $('[data-easyadmin-autocomplete-create-action-url]');

autocompleteCreateFields.each(function () {
var $this = $(this),
url = $this.data('easyadmin-autocomplete-url'),
url_action = $this.data('easyadmin-autocomplete-create-action-url'),
field_name = $this.data('easyadmin-autocomplete-create-field-name'),
button_text = $this.data('easyadmin-autocomplete-create-button-text'),
select_id = $this.attr('id');
autocompleteCreateFields.each(function () {
var $this = $(this),
url = $this.data('easyadmin-autocomplete-url'),
url_action = $this.data('easyadmin-autocomplete-create-action-url'),
field_name = $this.data('easyadmin-autocomplete-create-field-name'),
button_text = $this.data('easyadmin-autocomplete-create-button-text'),
select_id = $this.attr('id');

$this.select2({
theme: 'bootstrap',
ajax: {
url: url,
dataType: 'json',
delay: 250,
data: function (params) {
return { 'query': params.term, 'page': params.page };
},
// to indicate that infinite scrolling can be used
processResults: function (data, params) {
return {
results: data.results,
pagination: {
more: data.has_next_page
}
};
},
cache: true
},
placeholder: '',
allowClear: true,
minimumInputLength: 1,
language: {
noResults: function () {
return '<a href="#" class="btn btn-info" onclick="switchToEntityCreation(\''+url_action+'\', \''+select_id+'\', \''+field_name+'\');return false;">'+button_text+' '+field_name+'</a>';
}
},
escapeMarkup: function (markup) {
return markup;
$this.select2({
theme: 'bootstrap',
ajax: {
url: url,
dataType: 'json',
delay: 250,
data: function (params) {
return { 'query': params.term, 'page': params.page };
},
// to indicate that infinite scrolling can be used
processResults: function (data, params) {
return {
results: data.results,
pagination: {
more: data.has_next_page
}
});
};
},
cache: true
},
placeholder: '',
allowClear: true,
minimumInputLength: 1,
language: {
noResults: function () {
return '<a href="#" class="btn btn-info" onclick="switchToEntityCreation(\''+url_action+'\', \''+select_id+'\', \''+field_name+'\');return false;">'+button_text+' '+field_name+'</a>';
}
},
escapeMarkup: function (markup) {
return markup;
}
});
});
}

function switchToEntityCreation(url_action, select_id, field_name) {
$('#'+select_id).select2('close');
$.ajax({
url : url_action,
type: 'GET',
success: function(data) {
openCreateEntityModal(data, url_action, field_name, select_id);
$('#create-entity-modal').modal({ backdrop: true, keyboard: true });
}
});
$('#'+select_id).select2('close');
$.ajax({
url : url_action,
type: 'GET',
success: function(data) {
openCreateEntityModal(data, url_action, field_name, select_id);
$('#create-entity-modal').modal({ backdrop: true, keyboard: true });
}
});
}

function openCreateEntityModal(data, url_action, field_name, select_id) {
$('#create-entity-modal .modal-body').html(data.html);
$('form[name="'+field_name+'"]').attr('action', url_action);
initCreateEntityAjaxForm(field_name, select_id);
$('#create-entity-modal .modal-body').html(data.html);
$('form[name="'+field_name+'"]').attr('action', url_action);
initCreateEntityAjaxForm(field_name, select_id);
}

function initCreateEntityAjaxForm(field_name, select_id) {
$('form[name="'+field_name+'"]').submit(function( event ) {
event.preventDefault();
var url_action = $(this).attr('action');
$.ajax({
url: url_action,
type: $(this).attr('method'),
data: $(this).serialize(),
cache: false,
contentType: false,
processData: false,
success: function(data) {
if (data.hasOwnProperty('option')) {
$('#create-entity-modal').modal('hide');
var newOption = new Option(data.option.text, data.option.id, true, true);
$('#'+select_id).append(newOption).trigger('change');
// manually trigger the `select2:select` event
$('#'+select_id).trigger({
type: 'select2:select',
params: { data: data.option }
});
}
if (data.hasOwnProperty('html')) {
openCreateEntityModal(data, url_action, field_name, select_id);
}
},
error: function(error){
console.log(error);
}
});
$('form[name="'+field_name+'"]').submit(function( event ) {
event.preventDefault();
var url_action = $(this).attr('action');
$.ajax({
url: url_action,
type: $(this).attr('method'),
data: serializeForm($(this)),
cache: false,
contentType: false,
processData: false,
success: function(data) {
if (data.hasOwnProperty('option')) {
$('#create-entity-modal').modal('hide');
var newOption = new Option(data.option.text, data.option.id, true, true);
$('#'+select_id).append(newOption).trigger('change');
// manually trigger the `select2:select` event
$('#'+select_id).trigger({
type: 'select2:select',
params: { data: data.option }
});
}
if (data.hasOwnProperty('html')) {
openCreateEntityModal(data, url_action, field_name, select_id);
}
},
error: function(error){
console.log(error);
}
});
});
}

$(function () {
createAutoCompleteCreateFields();
createAutoCompleteCreateFields();
});
84 changes: 46 additions & 38 deletions src/Resources/public/js/easyadmin-extension.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function reloadEmbeddedList(identifier, toggleBaseUrl)
{
function reloadEmbeddedList(identifier, toggleBaseUrl) {
var containerPrefix = '.embedded-list[for="'+identifier+'"]';

$(containerPrefix).find('table .toggle input[type="checkbox"]').each(function (idx, el) {
Expand All @@ -8,50 +7,50 @@ function reloadEmbeddedList(identifier, toggleBaseUrl)

// Reload sorted/paginated list in the embedded-list container
$(containerPrefix)
.on('click', 'th[data-property-name] a', function (e) {
e.preventDefault();
$.ajax({
url: e.target.href,
dataType: 'html',
success: function (data, textStatus, jqXHR) {
$(containerPrefix).replaceWith(data);
}
});
})
.on('click', '.list-pagination a', function (e) {
e.preventDefault();
$.ajax({
url: e.target.href,
dataType: 'html',
success: function (data, textStatus, jqXHR) {
$(containerPrefix).replaceWith(data);
}
});
})
.on('click', 'th[data-property-name] a', function (e) {
e.preventDefault();
$.ajax({
url: e.target.href,
dataType: 'html',
success: function (data, textStatus, jqXHR) {
$(containerPrefix).replaceWith(data);
}
});
})
.on('click', '.list-pagination a', function (e) {
e.preventDefault();
$.ajax({
url: e.target.href,
dataType: 'html',
success: function (data, textStatus, jqXHR) {
$(containerPrefix).replaceWith(data);
}
});
})
;

$(containerPrefix).find('table .toggle input[type="checkbox"]').change(function() {
var toggle = $(this);
var newValue = toggle.prop('checked');
var oldValue = !newValue;
var toggle = $(this);
var newValue = toggle.prop('checked');
var oldValue = !newValue;

var columnIndex = $(this).closest('td').index() + 1;
var propertyName = $(containerPrefix + ' table th.toggle:nth-child(' + columnIndex + ')').data('property-name');
var columnIndex = $(this).closest('td').index() + 1;
var propertyName = $(containerPrefix + ' table th.toggle:nth-child(' + columnIndex + ')').data('property-name');

var toggleUrl = toggleBaseUrl
+ "&id=" + $(this).closest('tr').data('id')
+ "&property=" + propertyName
+ "&newValue=" + newValue.toString();
var toggleUrl = toggleBaseUrl
+ "&id=" + $(this).closest('tr').data('id')
+ "&property=" + propertyName
+ "&newValue=" + newValue.toString();

var toggleRequest = $.ajax({ type: "GET", url: toggleUrl, data: {} });
var toggleRequest = $.ajax({ type: "GET", url: toggleUrl, data: {} });

toggleRequest.done(function(result) {});
toggleRequest.done(function(result) {});

toggleRequest.fail(function() {
// in case of error, restore the original value and disable the toggle
toggle.bootstrapToggle(oldValue == true ? 'on' : 'off');
toggle.bootstrapToggle('disable');
});
toggleRequest.fail(function() {
// in case of error, restore the original value and disable the toggle
toggle.bootstrapToggle(oldValue == true ? 'on' : 'off');
toggle.bootstrapToggle('disable');
});
});
}

Expand Down Expand Up @@ -85,3 +84,12 @@ $(function() {
e.stopPropagation();
});
});

function serializeForm(form) {
var formData = new FormData();
var params = form.serializeArray();
$.each(params, function (i, val) {
formData.append(val.name, val.value);
});
return formData;
};

0 comments on commit 5d60b75

Please sign in to comment.