123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- jQuery(document).ready(function() {
- jQuery('html').removeClass('no-js');
- if (window.SONATA_CONFIG.CONFIRM_EXIT) {
- jQuery('.sonata-ba-form form').confirmExit();
- }
- Admin.add_pretty_errors(document);
- Admin.add_filters(document);
- Admin.set_object_field_value(document);
- Admin.setup_collection_buttons(document);
- Admin.setup_per_page_switcher(document);
- Admin.setup_form_tabs_for_errors(document);
- Admin.setup_inline_form_errors(document);
- });
- var Admin = {
- /**
- * render log message
- * @param mixed
- */
- log: function() {
- var msg = '[Sonata.Admin] ' + Array.prototype.join.call(arguments,', ');
- if (window.console && window.console.log) {
- window.console.log(msg);
- } else if (window.opera && window.opera.postError) {
- window.opera.postError(msg);
- }
- },
- /**
- * display related errors messages
- *
- * @param subject
- */
- add_pretty_errors: function(subject) {
- jQuery('div.sonata-ba-field-error', subject).each(function(index, element) {
- var input = jQuery('input, textarea, select', element);
- var message = jQuery('div.sonata-ba-field-error-messages', element).html();
- jQuery('div.sonata-ba-field-error-messages', element).html('');
- if (!message) {
- message = '';
- }
- if (message.length == 0) {
- return;
- }
- var target;
- /* Hack to handle qTip on select */
- if(jQuery(input).is('select')) {
- input.wrap('<span></span>');
- target = input.parent();
- }
- else {
- target = input;
- }
- target.popover({
- content: message,
- trigger:'focus',
- html: true,
- placement: 'right',
- template: '<div class="popover"><div class="arrow"></div><div class="popover-inner"><div class="popover-content alert-error"><p></p></div></div></div>'
- });
- });
- },
- stopEvent: function(event) {
- // https://github.com/sonata-project/SonataAdminBundle/issues/151
- //if it is a standard browser use preventDefault otherwise it is IE then return false
- if(event.preventDefault) {
- event.preventDefault();
- } else {
- event.returnValue = false;
- }
- //if it is a standard browser get target otherwise it is IE then adapt syntax and get target
- if (typeof event.target != 'undefined') {
- targetElement = event.target;
- } else {
- targetElement = event.srcElement;
- }
- return targetElement;
- },
- add_filters: function(subject) {
- jQuery('div.filter_container .sonata-filter-option', subject).hide();
- jQuery('fieldset.filter_legend', subject).click(function(event) {
- jQuery('div.filter_container .sonata-filter-option', jQuery(event.target).parent()).toggle();
- });
- },
- /**
- * Change object field value
- * @param subject
- */
- set_object_field_value: function(subject) {
- this.log(jQuery('a.sonata-ba-edit-inline', subject));
- jQuery('a.sonata-ba-edit-inline', subject).click(function(event) {
- Admin.stopEvent(event);
- var subject = jQuery(this);
- jQuery.ajax({
- url: subject.attr('href'),
- type: 'POST',
- success: function(json) {
- if(json.status === "OK") {
- var elm = jQuery(subject).parent();
- elm.children().remove();
- // fix issue with html comment ...
- elm.html(jQuery(json.content.replace(/<!--[\s\S]*?-->/g, "")).html());
- elm.effect("highlight", {'color' : '#57A957'}, 2000);
- Admin.set_object_field_value(elm);
- } else {
- jQuery(subject).parent().effect("highlight", {'color' : '#C43C35'}, 2000);
- }
- }
- });
- });
- },
- setup_collection_buttons: function(subject) {
- jQuery(subject).on('click', '.sonata-collection-add', function(event) {
- Admin.stopEvent(event);
- var container = jQuery(this).closest('[data-prototype]');
- var proto = container.attr('data-prototype');
- var protoName = container.attr('data-prototype-name') || '__name__';
- // Set field id
- var idRegexp = new RegExp(container.attr('id')+'_'+protoName,'g');
- proto = proto.replace(idRegexp, container.attr('id')+'_'+(container.children().length - 1));
- // Set field name
- var parts = container.attr('id').split('_');
- var nameRegexp = new RegExp(parts[parts.length-1]+'\\]\\['+protoName,'g');
- proto = proto.replace(nameRegexp, parts[parts.length-1]+']['+(container.children().length - 1));
- jQuery(proto).insertBefore(jQuery(this).parent());
- jQuery(this).trigger('sonata-collection-item-added');
- });
- jQuery(subject).on('click', '.sonata-collection-delete', function(event) {
- Admin.stopEvent(event);
- jQuery(this).closest('.sonata-collection-row').remove();
- jQuery(this).trigger('sonata-collection-item-deleted');
- });
- },
- setup_per_page_switcher: function(subject) {
- jQuery('select.per-page').change(function(event) {
- jQuery('input[type=submit]').hide();
- window.top.location.href=this.options[this.selectedIndex].value;
- });
- },
- setup_form_tabs_for_errors: function(subject) {
- // Switch to first tab with server side validation errors on page load
- jQuery('form', subject).each(function() {
- Admin.show_form_first_tab_with_errors(jQuery(this), '.sonata-ba-field-error');
- });
- // Switch to first tab with HTML5 errors on form submit
- jQuery(subject)
- .on('click', 'form [type="submit"]', function() {
- Admin.show_form_first_tab_with_errors(jQuery(this).closest('form'), ':invalid');
- })
- .on('keypress', 'form [type="text"]', function(e) {
- if (13 === e.which) {
- Admin.show_form_first_tab_with_errors(jQuery(this), ':invalid');
- }
- })
- ;
- },
- show_form_first_tab_with_errors: function(form, errorSelector) {
- var tabs = form.find('.nav-tabs a'),
- firstTabWithErrors;
- tabs.each(function() {
- var id = jQuery(this).attr('href'),
- tab = jQuery(this),
- icon = tab.find('.has-errors');
- if (jQuery(id).find(errorSelector).length > 0) {
- // Only show first tab with errors
- if (!firstTabWithErrors) {
- tab.tab('show');
- firstTabWithErrors = tab;
- }
- icon.removeClass('hide');
- } else {
- icon.addClass('hide');
- }
- });
- },
- setup_inline_form_errors: function(subject) {
- var deleteCheckboxSelector = '.sonata-ba-field-inline-table [id$="_delete"][type="checkbox"]';
- jQuery(deleteCheckboxSelector, subject).each(function() {
- Admin.switch_inline_form_errors(jQuery(this));
- });
- $(subject).on('change', deleteCheckboxSelector, function() {
- Admin.switch_inline_form_errors(jQuery(this));
- });
- },
- /**
- * Disable inline form errors when the row is marked for deletion
- */
- switch_inline_form_errors: function(deleteCheckbox) {
- var row = deleteCheckbox.closest('.sonata-ba-field-inline-table'),
- errors = row.find('.sonata-ba-field-error-messages')
- ;
- if (deleteCheckbox.is(':checked')) {
- row
- .find('[required]')
- .removeAttr('required')
- .attr('data-required', 'required')
- ;
- errors.hide();
- } else {
- row
- .find('[data-required]')
- .attr('required', 'required')
- ;
- errors.show();
- }
- }
- };
|