public void Init(ButtonPressed onButtonPressed, string _text, Action _action, ModalButtonType _type)
    {
        OnButtonPressed = onButtonPressed;
        text.text       = _text;
        action          = _action;

        button.onClick.AddListener(new UnityEngine.Events.UnityAction(Click));
    }
Beispiel #2
0
 public Modal(string text, ModalButtonType buttonType)
 {
     this.Bounds = new Rectangle(50, 50, 400, 400);
 }
        /// <summary>
        /// Constructs a generic modal with the given attributes. Modal body content is made up of an empty div.
        /// Target the empty div with an ajax repsonse to populate the modal content. Also useful for when you need
        /// a modal, but you dont need it to handle any data and are just using it as some sort of prompt.
        /// </summary>
        /// <param name="modalId">Your modals id.</param>
        /// <param name="titleText">The title display of the modal.</param>
        /// <param name="partialTargetId">The id of the innermost div for partial pages.</param>
        /// <param name="formId">The id of the form to submit. This lets the form button exist outside of the form.</param>
        /// <returns>An html string of the modal.</returns>
        public static IHtmlString Modal(string modalId, string titleText, string partialTargetId, string formId, ModalButtonType type = ModalButtonType.Submit)
        {
            //Structure from outer most to inner is as follows
            //modal(dialog(content(header(close, title), body(partial), footer)))

            string modalFooterSubmit;

            switch (type)
            {
            case ModalButtonType.Submit:
                modalFooterSubmit = "<button type='submit' form='{0}' class='btn btn-primary modalSubmitButton'>Save changes</button>";
                break;

            case ModalButtonType.Delete:
                modalFooterSubmit = "<button type='submit' form='{0}' class='btn btn-primary modalSubmitButton modalDeleteButton'>Delete</button>";
                break;

            default:
                modalFooterSubmit = "<button type='submit' form='{0}' class='btn btn-primary modalSubmitButton'>Save changes</button>";
                break;
            }

            string partialTarget    = "<div id='{0}'></div>";
            string modalBody        = "<div class='modal-body'>{0}</div>";
            string modalHeader      = "<div class='modal-header'>{0}{1}</div>";
            string modalHeaderClose = "<button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>";
            string modalHeaderTitle = "<h4 class='modal-title' id='{0}Label'>{1}</h4>"; //0 = myModal 1= title
            string modalContent     = "<div class='modal-content'>{0}{1}{2}</div>";
            string modalDialog      = "<div class='modal-dialog' role='document'>{0}</div>";
            string modalFooter      = "<div class='modal-footer'>{0}{1}</div>";
            string modalFooterClose = "<button type='button' class='btn btn-default modalCloseButton' data-dismiss='modal'>Close</button>";
            // string modalFooterSubmit = "<button type='submit' form='{0}' class='btn btn-primary modalSubmitButton'>Save changes</button>";
            string modal = "<div class='modal fade' id='{0}' tabindex='-1' role='dialog' aria-labelledby='{0}Label'>{1}</div>"; //0 = myModal 1=

            //string fullModal = String.Format(modal, modalId, modalDialog);

            //build body
            partialTarget = String.Format(partialTarget, partialTargetId);
            modalBody     = String.Format(modalBody, partialTarget);

            //build header
            modalHeaderTitle = String.Format(modalHeaderTitle, modalId, titleText);
            modalHeader      = String.Format(modalHeader, modalHeaderClose, modalHeaderTitle);

            //build footer
            modalFooterSubmit = String.Format(modalFooterSubmit, formId);
            modalFooter       = String.Format(modalFooter, modalFooterClose, modalFooterSubmit);

            //build content
            modalContent = String.Format(modalContent, modalHeader, modalBody, modalFooter);

            //build dialog
            modalDialog = String.Format(modalDialog, modalContent);

            //build modal
            modal = String.Format(modal, modalId, modalDialog);

            return(new HtmlString(modal));
        }
        /// <summary>
        /// Constructs a modal and an ajax handler for passing data from a partial in a modal to a controller action.
        /// This method is most useful when you're trying to do data validation with a partial view inside of a modal.
        /// By default forms will attempt to redirect to a returned action result which will close the modal. This
        /// modal prevents the form from navigating away while using ajax to speak with the controller asynchronously. That
        /// way if a partial validation is returned it can be reinserted into the modal without disorienting users.
        /// </summary>
        /// <param name="modalId">Your modals id.</param>
        /// <param name="titleText">The title display of the modal.</param>
        /// <param name="partialTargetId">The id of the innermost div for partial pages.</param>
        /// <param name="formId">The id of the form to submit. This lets the form button exist outside of the form.</param>
        /// <returns>An html string of the modal.</returns>
        public static IHtmlString ModalPreventDefault(string modalId, string titleText, string partialTargetId, string formId, ModalButtonType type = ModalButtonType.Submit)
        {
            //0 is modalId, 1 is formId ,2 is ajax
            string script   = "<script type='text/javascript'>{0}</script>";
            string function = " $('#{0}').on('submit',function(e){{e.preventDefault(); {1}{2}{3}{4} }});";

            string action = "var action = $('#{0}').attr('action');"; // 0 is formId
            string method = "var method = $('#{0}').attr('method');";
            string form   = "var form = $('#{0}');";                  // 0 is formid

            string ajaxCall = "$.ajax({{ url: action, type: method, data: form.serialize(), success: function(response)" +
                              "{{ $('#{0}').html(response);}} }});";

            //build action
            action   = String.Format(action, formId);
            method   = String.Format(method, formId);
            ajaxCall = String.Format(ajaxCall, partialTargetId);
            form     = String.Format(form, formId);

            function = String.Format(function, modalId, action, method, form, ajaxCall);

            script = String.Format(script, function);

            IHtmlString modal = Modal(modalId, titleText, partialTargetId, formId, type);

            string modalWithJS = String.Format("{0}{1}", modal.ToString(), script);

            return(new HtmlString(modalWithJS));
        }