Loading ...
Sorry, an error occurred while loading the content.

Build custom confirm() with SimpleDialog

Expand Messages
  • mhaertl73
    I try to build a method that works like a usual confirm() dialog. It should display a SimpleDialog and _return_ true or false. My problem: SimpleDialog only
    Message 1 of 5 , Feb 2, 2008
    View Source
    • 0 Attachment
      I try to build a method that works like a usual confirm() dialog. It
      should display a SimpleDialog and _return_ true or false. My problem:
      SimpleDialog only supplies handler for the buttons. I don't know how
      to acutally return a value instead of handling button events.

      Here an example:

      // replacement for confirm()
      myConfirm=function(msg) {

      var handleYes=function() {};
      var handleNo=function() {};

      ConfirmDialog= new YAHOO.widget.SimpleDialog("myConfirm", {
      width: "300px",
      visible: false,
      draggable: true,
      close: true,
      text: msg,
      icon: YAHOO.widget.SimpleDialog.ICON_HELP,
      buttons: [
      { text:"Yes", handler:handleYes, isDefault:true },
      { text:"No", handler:handleNo } ]
      });
      ConfirmDialog.render();


      // How do i get the right return value (true/false) here?
      return ???
      }

      Any ideas welcome.
      Thanks
    • bretlevy
      You have to do it in steps . I create a master set of functions in a js file that handle popus as follows: var popupMessageBoxForm; function popupMessageBox
      Message 2 of 5 , Feb 3, 2008
      View Source
      • 0 Attachment
        You have to do it in "steps". I create a master set of functions in
        a js file that handle popus as follows:

        var popupMessageBoxForm;

        function popupMessageBox (title, message, icon, buttons) {

        // our container must exist for this to work

        if (!YAHOO.util.Dom.inDocument('popupMessageBoxContainer'))
        return;

        // create the confirm dialog

        var ybuttons = [];
        for (var i=0; i<buttons.length; i++) {
        ybuttons[i] = { text:buttons[i].label, handler:buttons
        [i].func, isDefault:(i==0?true:false) };
        }

        popupMessageBoxForm = new YAHOO.widget.SimpleDialog
        ("popupMessageBoxContainer", {
        modal: true,
        width: "300px",
        fixedcenter: true,
        visible: false,
        draggable: false,
        close: true,
        text: message,
        icon: icon,
        constraintoviewport: true,
        buttons: ybuttons,
        zIndex: 5000
        });
        popupMessageBoxForm.setHeader(title);
        popupMessageBoxForm.render("popupMessageBoxContainer");
        popupMessageBoxForm.show();

        }

        function popupMessageBoxHide () {
        popupMessageBoxForm.hide();
        }

        function YAlert (title, msg, icon) {
        if (icon === undefined) icon =
        YAHOO.widget.SimpleDialog.ICON_ALARM;
        popupMessageBox (title, msg, icon,
        [{label:'OK',func:popupMessageBoxHide}]);
        }

        function YConfirm (msg, yeshandler) {
        popupMessageBox ('Please Confirm...', msg,
        YAHOO.widget.SimpleDialog.ICON_WARN, [{label:'Yes',func:function()
        {popupMessageBoxHide();yeshandler();}},
        {label:'No',func:popupMessageBoxHide}]);
        }

        There are more of these Y... functions too. In my case, the basic
        idea is I only care if the answer is "yes", so in my page, I have
        something like this:

        YConfirm ('Are you sure you want to delete this foobar?', doDelete);

        And then the actual function that "continues the work" if the answer
        is yes:

        function doDelete () {
        // do something here, in my case its an async request
        }

        My "page framework" adds in the empty div "popupMessageBoxContainer"
        on every page as the page is sent to the browser, so each "page" does
        not really have to do anything more for this functionality to work.

        Hope that's helpful...

        ~~bret





        --- In ydn-javascript@yahoogroups.com, "mhaertl73" <ydn@...> wrote:
        >
        > I try to build a method that works like a usual confirm() dialog. It
        > should display a SimpleDialog and _return_ true or false. My
        problem:
        > SimpleDialog only supplies handler for the buttons. I don't know how
        > to acutally return a value instead of handling button events.
        >
        > Here an example:
        >
        > // replacement for confirm()
        > myConfirm=function(msg) {
        >
        > var handleYes=function() {};
        > var handleNo=function() {};
        >
        > ConfirmDialog= new YAHOO.widget.SimpleDialog("myConfirm", {
        > width: "300px",
        > visible: false,
        > draggable: true,
        > close: true,
        > text: msg,
        > icon: YAHOO.widget.SimpleDialog.ICON_HELP,
        > buttons: [
        > { text:"Yes", handler:handleYes, isDefault:true },
        > { text:"No", handler:handleNo } ]
        > });
        > ConfirmDialog.render();
        >
        >
        > // How do i get the right return value (true/false) here?
        > return ???
        > }
        >
        > Any ideas welcome.
        > Thanks
        >
      • mhaertl73
        ... Thanks for your answer but i guess it doesn t solve my problem. I still need a separate handler for each confirmation. What i need is an exact replacement
        Message 3 of 5 , Feb 4, 2008
        View Source
        • 0 Attachment
          --- In ydn-javascript@yahoogroups.com, "bretlevy" <bret@...> wrote:
          >
          >
          > You have to do it in "steps". I create a master set of functions in
          > a js file that handle popus as follows:

          Thanks for your answer but i guess it doesn't solve my problem. I
          still need a separate handler for each confirmation.

          What i need is an exact replacement for confirm() that returns true or
          false. I have many of these in my code:

          if (!hasChanged || confirm("Are you sure you want to quit?")) {
          ...
          }

          I want to replace the confirm dialog with a nicer SimpleDialog. But i
          don't want to write a separate handler for each confirmation.
        • Satyen Desai
          Hi, Since you re working within the confines of the browser s JavaScript engine, it s not possible to achieve this without splitting up the code using
          Message 4 of 5 , Feb 5, 2008
          View Source
          • 0 Attachment
            Hi,

            Since you're working within the confines of the browser's JavaScript engine, it's not possible to achieve this without splitting up the code using callbacks to "continue with work" as Bret suggests.

            You only have 1 thread available to run JavaScript on, so you can't "wait" in that thread after your custom dialog is displayed, and then execute some other JavaScript (e.g. script which needs handle UI events) on another*. Additionally you have no way to "wake" a thread which is "waiting".

            The browsers "confirm" implementation is native. So it can halt the JS thread, and still provide a responsive native UI.

            Regards,
            Satyen

            * There are caveats here, but they involve displaying native windows [alert, confirm, prompt] and don't fit your use case.

            ________________________________________
            From: ydn-javascript@yahoogroups.com [mailto:ydn-javascript@yahoogroups.com] On Behalf Of mhaertl73
            Sent: Monday, February 04, 2008 3:32 AM
            To: ydn-javascript@yahoogroups.com
            Subject: [ydn-javascript] Re: Build custom confirm() with SimpleDialog

            --- In ydn-javascript@yahoogroups.com, "bretlevy" <bret@...> wrote:
            >
            >
            > You have to do it in "steps". I create a master set of functions in
            > a js file that handle popus as follows:

            Thanks for your answer but i guess it doesn't solve my problem. I
            still need a separate handler for each confirmation.

            What i need is an exact replacement for confirm() that returns true or
            false. I have many of these in my code:

            if (!hasChanged || confirm("Are you sure you want to quit?")) {
            ...
            }

            I want to replace the confirm dialog with a nicer SimpleDialog. But i
            don't want to write a separate handler for each confirmation.
          • Xzibit
            Any solution for this? We have the same scenario. I want a return value of the simpledialog. Please let me know if you have a solution for this. Thanks.
            Message 5 of 5 , Mar 4, 2008
            View Source
            • 0 Attachment
              Any solution for this? We have the same scenario. I want a return
              value of the simpledialog. Please let me know if you have a solution
              for this. Thanks.

              --- In ydn-javascript@yahoogroups.com, "mhaertl73" <ydn@...> wrote:
              >
              > --- In ydn-javascript@yahoogroups.com, "bretlevy" <bret@> wrote:
              > >
              > >
              > > You have to do it in "steps". I create a master set of functions in
              > > a js file that handle popus as follows:
              >
              > Thanks for your answer but i guess it doesn't solve my problem. I
              > still need a separate handler for each confirmation.
              >
              > What i need is an exact replacement for confirm() that returns true or
              > false. I have many of these in my code:
              >
              > if (!hasChanged || confirm("Are you sure you want to quit?")) {
              > ...
              > }
              >
              > I want to replace the confirm dialog with a nicer SimpleDialog. But i
              > don't want to write a separate handler for each confirmation.
              >
            Your message has been successfully submitted and would be delivered to recipients shortly.