mirror of
				https://github.com/pierre42100/ComunicWeb
				synced 2025-11-04 12:14:12 +00:00 
			
		
		
		
	Created a function to prompt user a string.
This commit is contained in:
		@@ -101,6 +101,11 @@ var ComunicWeb = {
 | 
			
		||||
			 * Create and display a confirmation dialog
 | 
			
		||||
			 */
 | 
			
		||||
			confirm: function(message, callback){},
 | 
			
		||||
 | 
			
		||||
			/**
 | 
			
		||||
			 * Prompt the user to input a string
 | 
			
		||||
			 */
 | 
			
		||||
			inputString: function(title, message, defaultValue, callback){},
 | 
			
		||||
		},
 | 
			
		||||
 | 
			
		||||
		/**
 | 
			
		||||
@@ -719,6 +724,13 @@ var ComunicWeb = {
 | 
			
		||||
				//TODO : implement
 | 
			
		||||
			},
 | 
			
		||||
 | 
			
		||||
			/**
 | 
			
		||||
			 * Comments editor
 | 
			
		||||
			 */
 | 
			
		||||
			editor: {
 | 
			
		||||
 | 
			
		||||
			},
 | 
			
		||||
 | 
			
		||||
			/**
 | 
			
		||||
			 * Comments utilities
 | 
			
		||||
			 */
 | 
			
		||||
 
 | 
			
		||||
@@ -147,4 +147,132 @@ ComunicWeb.common.messages.confirm = function(message, callback){
 | 
			
		||||
    //Show the modal
 | 
			
		||||
    $(modal).modal('show');
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Prompt the user to input a string
 | 
			
		||||
 * 
 | 
			
		||||
 * @param {string} title The title of the edit box
 | 
			
		||||
 * @param {string} message The label for the input field
 | 
			
		||||
 * @param {string} defaultValue The default value of the input string
 | 
			
		||||
 * @param {function} callback What to do once the user has made is choice
 | 
			
		||||
 * The callback function must includes one parameters which is a boolean or a string.
 | 
			
		||||
 * - The content of the typed string (if any)
 | 
			
		||||
 * - FALSE if the user decided to cancel the dialog
 | 
			
		||||
 */
 | 
			
		||||
ComunicWeb.common.messages.inputString = function(title, message, defaultValue, callback){
 | 
			
		||||
 | 
			
		||||
    //Create a modal root
 | 
			
		||||
    var modal = createElem2({
 | 
			
		||||
        type: "div",
 | 
			
		||||
        class: "modal modal-primary input-string-modal"
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    var modalDialog = createElem2({
 | 
			
		||||
        appendTo: modal,
 | 
			
		||||
        type: "div",
 | 
			
		||||
        class: "modal-dialog"
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    var modalContent = createElem2({
 | 
			
		||||
        appendTo: modalDialog,
 | 
			
		||||
        type: "div",
 | 
			
		||||
        class: "modal-content",
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    //Modal header
 | 
			
		||||
    var modalHeader = createElem2({
 | 
			
		||||
        appendTo: modalContent,
 | 
			
		||||
        type: "div",
 | 
			
		||||
        class: "modal-header"
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    var closeModal = createElem2({
 | 
			
		||||
        appendTo: modalHeader,
 | 
			
		||||
        type: "button",
 | 
			
		||||
        class: "close",
 | 
			
		||||
    });
 | 
			
		||||
    closeModal.setAttribute("data-confirm", "false");
 | 
			
		||||
 | 
			
		||||
    createElem2({
 | 
			
		||||
        appendTo: closeModal,
 | 
			
		||||
        type: "span",
 | 
			
		||||
        innerHTML: "x"
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    var modalTitle = createElem2({
 | 
			
		||||
        appendTo: modalHeader,
 | 
			
		||||
        type: "h4",
 | 
			
		||||
        class: "modal-title",
 | 
			
		||||
        innerHTML: title
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    //Modal body
 | 
			
		||||
    var modalBody = createElem2({
 | 
			
		||||
        appendTo: modalContent,
 | 
			
		||||
        type: "div",
 | 
			
		||||
        class: "modal-body",
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    //Setup form
 | 
			
		||||
    var textInput = createFormGroup({
 | 
			
		||||
        target: modalBody,
 | 
			
		||||
        type: "text",
 | 
			
		||||
        label: message,
 | 
			
		||||
        placeholder: ""
 | 
			
		||||
    });
 | 
			
		||||
    textInput.value = defaultValue;
 | 
			
		||||
 | 
			
		||||
    //Modal footer
 | 
			
		||||
    var modalFooter = createElem2({
 | 
			
		||||
        appendTo: modalContent,
 | 
			
		||||
        type: "div",
 | 
			
		||||
        class: "modal-footer"
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    var cancelButton = createElem2({
 | 
			
		||||
        appendTo: modalFooter,
 | 
			
		||||
        type: "button",
 | 
			
		||||
        class: "btn btn-default pull-left",
 | 
			
		||||
        innerHTML: "Cancel"
 | 
			
		||||
    });
 | 
			
		||||
    cancelButton.setAttribute("data-confirm", "false");
 | 
			
		||||
 | 
			
		||||
    var submitButton = createElem2({
 | 
			
		||||
        appendTo: modalFooter,
 | 
			
		||||
        type: "button",
 | 
			
		||||
        class: "btn btn-primary",
 | 
			
		||||
        innerHTML: "Submit"
 | 
			
		||||
    });
 | 
			
		||||
    submitButton.setAttribute("data-confirm", "true");
 | 
			
		||||
 | 
			
		||||
    //Create the response function
 | 
			
		||||
    var respond = function(){
 | 
			
		||||
 | 
			
		||||
        //Check if the operation was confirmed or not
 | 
			
		||||
        var cancel = this.getAttribute("data-confirm") != "true";
 | 
			
		||||
 | 
			
		||||
        //Close modal
 | 
			
		||||
        $(modal).modal('hide');
 | 
			
		||||
        emptyElem(modal);
 | 
			
		||||
        modal.remove();
 | 
			
		||||
 | 
			
		||||
        //Call callback
 | 
			
		||||
        //The operation was not cancelled
 | 
			
		||||
        if(!cancel)
 | 
			
		||||
            callback(textInput.value);
 | 
			
		||||
 | 
			
		||||
        //The user cancelled the operation
 | 
			
		||||
        else
 | 
			
		||||
            callback(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //Make the buttons live
 | 
			
		||||
    cancelButton.onclick = respond;
 | 
			
		||||
    submitButton.onclick = respond;
 | 
			
		||||
    closeModal.onclick = respond;
 | 
			
		||||
 | 
			
		||||
    //Show the modal
 | 
			
		||||
    $(modal).modal('show');
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user