Search code examples
javascriptjquerytwitter-bootstrapmodal-dialogconfirm

change js confirm to a twitter bootstrap modal


I have some JavaScript that displays a confirm dialog box to the user to confirm their action with passed in variables.

However, I must change the plain confirm dialog to a twitter-bootstrap modal.

I have tried several times, read a lot of SO posts and read the twitter bootstrap docs, but I cannot get it to work as my knowledge of js is not good enough. I am getting stuck on the display of the variables in the twitter bootstrap modal.

I am hoping that some one can help me out by giving me some pointers.

Here is my current js dialog code:

    function checkboxUpdated(checkbox, count, label, id) {

        if (checkbox.checked) {

            $('#menu_entry_' + id).show();

        } else {

            if (count > 0) {

                if (! confirm('{% trans "You have '+ count +' saved '+ label +'.\n\nIf you leave this option un-checked your saved '+ label +' will be deleted only after you update this page.\n\nAre you sure you want to delete your ' + count + ' saved ' + label +'?" %}')) {

                    checkbox.checked = true;
                    return;

                }

            }

            $('#menu_entry_' + id).hide();

        }

    }

EDIT: ADDED CODE OF #menu_entry_ as requested in comment:

{% for id, menu_entry, selected, item_count in menu_entries %}

    <li id="menu_entry_{{ id }}" {% if not selected %}style="display:none;"{% endif %}>

        <a 

            {% if id == 4 %}

                href="{% url summary_details %}"

            {% elif id == 8 %}

                href="{% url objective_details %}"

            {% elif id == 12 %}

                href="{% url leading_employment_details %}"

            {% elif id == 16 %}

                href="{% url desired_occupation_details %}"

            ....

            {% elif id == 112 %}

                href="/"

            {% else %}

            href="/"

            {% endif %}

            onclick="showProgressAnimation();">

Note that I need to transform the following js confirm code to twitter bootstrap modal code:

if (! confirm('{% trans "You have '+ count +' saved '+ label +'.\n\nIf you leave this option un-checked your saved '+ label +' will be deleted only after you update this page.\n\nAre you sure you want to delete your ' + count + ' saved ' + label +'?" %}')) {

Solution

  • You can write your own jQuery plugin. First, add the Bootsrap's modal component to your document.

    <div class="modal fade" id="confirm" tabindex="-1" role="dialog" aria-labelledby="confirm-label" aria-hidden="true">
      <div class="modal-dialog modal-sm">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            <h4 class="modal-title" id="confirm-label"></h4>
          </div>
          <div class="modal-body">
            <p class="message"></p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default dismiss" data-dismiss="modal"></button>
            <button type="button" class="btn btn-primary confirm" data-dismiss="modal"></button>
          </div>
        </div>
      </div>
    </div>
    

    Basically, the plugin should display the modal when invoked and trigger a confirm event if the confirmation button is pressed or dismiss otherwise.

    $.fn.confirm = function (options) {
      var settings = $.extend({}, $.fn.confirm.defaults, options);
    
      return this.each(function () {
        var element = this;
    
        $('.modal-title', this).html(settings.title);
        $('.message', this).html(settings.message);
        $('.confirm', this).html(settings.confirm);
        $('.dismiss', this).html(settings.dismiss);
    
        $(this).on('click', '.confirm', function (event) {
          $(element).data('confirm', true);
        });
    
        $(this).on('hide.bs.modal', function (event) {
          if ($(this).data('confirm')) {
            $(this).trigger('confirm', event);
            $(this).removeData('confirm');
          } else {
            $(this).trigger('dismiss', event);
          }
    
          $(this).off('confirm dismiss');
        });
    
        $(this).modal('show');
      });
    };
    

    An improvement we can make to the code above is to expose the default plugin settings.

    $.fn.confirm.defaults = {
      title: 'Modal title',
      message: 'One fine body&hellip;',
      confirm: 'OK',
      dismiss: 'Cancel'
    };
    

    Usage example:

    $('#confirm').confirm().on({
      confirm: function () {
        console.log('confirm');
      },
      dismiss: function () {
        console.log('dismiss');
      }
    });
    

    See live example here: http://jsfiddle.net/cdog/4q9t9pk5/.

    If you don't want to write your own solution you can try existing projects like: https://github.com/nakupanda/bootstrap3-dialog. It looks like what you were looking for. Docs and demos available here: http://nakupanda.github.io/bootstrap3-dialog/.