Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Øyvind Kristiansen 14 posts 54 karma points
    Jan 02, 2012 @ 11:13
    Øyvind Kristiansen
    0

    Displaying a JavaScript confirm dialog on Document.MoveToTrash

    I would like to display a JavaScript confirm dialog whenever a user makes a change to a document of a certain type. This works for the Document.BeforePublish event with the following code:

    namespace MyNamespace {
        public class UmbracoEventHandlers : ApplicationBase
        {
            public UmbracoEventHandlers()
            {
                Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);
                Document.BeforeMoveToTrash += new Document.MoveToTrashEventHandler(Document_BeforeMoveToTrash);
            }
            void Document_BeforeMoveToTrash(Document sender, MoveToTrashEventArgs e)
            {
                ShowConfirm(sender);
            }
            void Document_BeforePublish(Document sender, PublishEventArgs e)
            {
                ShowConfirm(sender);
            }

    /* Output JavaScript showing a confirm dialog. */
            void ShowConfirm(Document sender)
            {
                if (sender.ContentType.MasterContentType == 1042)
                {
                    StringBuilder sb = new StringBuilder("<script type=\"text/javascript\">");
                    sb.AppendLine("var answer = confirm('Question')");
                    sb.AppendLine("if (answer){");
                    sb.AppendLine("    $.get('/Base/RestExtensions/DoSomething/" + sender.Id.ToString() + ".aspx');");
                    sb.AppendLine("}");
                    sb.AppendLine("</script>");

                    ClientScriptManager csm = ((BasePage)HttpContext.Current.CurrentHandler).ClientScript;
                    csm.RegisterClientScriptBlock(typeof(Page), "ConfirmDialog", sb.ToString());
                }
            }
        } }

    When the BeforePublish event is triggered, a confirm dialog pops up when the document is published. However, when deleting a document, the event fires, but no confirm dialog appears. Instead, the following exception is thrown on the line where the ClientScriptManager is initialized:

    Unable to cast object of type 'HandlerWrapper' to type 'umbraco.BasePages.BasePage'.

    I suspect this has something to do with the HttpContext being handled differently when deleting compared to publishing. Is there another way I can manage to inject the JavaScript also when deleting?

     

  • Rodion Novoselov 694 posts 859 karma points
    Jan 02, 2012 @ 12:39
    Rodion Novoselov
    1

    Hi. It looks like that the publishing process runs in the context of an ajax request. So that the actual request handler is not a page at all.

  • Øyvind Kristiansen 14 posts 54 karma points
    Jan 02, 2012 @ 14:44
    Øyvind Kristiansen
    0

    Hi Rodion.

    The thing is, I am able to get the request handler when publishing (Document.BeforePublish); but not on Document.MoveToTrash. Any thoughts as to why this is handled differently on these two events?

    If you or anyone else have any other suggestions as to how I might be able to add the JavaScript to the page, it would be greatly appreciated. Is it, for example, possible to redirect to the admin interface with some querystring parameters (http://mypage.com/umbraco/?action=ShowConfirm&documentId=1234), and then add the JavaScript to the admin page when it is rendered using the data from the querystring?

  • Rodion Novoselov 694 posts 859 karma points
    Jan 02, 2012 @ 18:54
    Rodion Novoselov
    0

    I'm not sure if it solves your problem, but if you want just something like confirmation then it's possible just to modify sources a bit.

    In the file ~/umbraco/dialogs/publish.aspx replace the string (at the line 88)

    <asp:Button ID="ok" runat="server" CssClass="guiInputButton">

    to something like:

    <asp:Button ID="ok" runat="server" CssClass="guiInputButton" OnClientClick="return confirm('Are you sure?')"> 

    or like that

  • Øyvind Kristiansen 14 posts 54 karma points
    Jan 05, 2012 @ 10:26
    Øyvind Kristiansen
    0

    Rodion,

    thank you for your answer, but as I said, this works fine for publishing. The problem is related to deleting (Document.MoveToTrash). Besides, your suggestion would only work when right clicking the node in the tree and choosing publish, and would apply to all document types.

    So I'm still looking for a solution to my problem. Any other suggestions to how I might be able to display a confirm dialog? Using Umbraco's ClientTools.OpenModalWindow is not working either, since this is also depending on an IHttpHandler.

    Although the best thing would be to retrieve the current handler somehow like when publishing, it would be sufficient if I could be able to be able to write the JavaScript stub to the page in some other way.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 05, 2012 @ 15:04
    Tom Fulton
    0

    Hi,

    Even if you inject some javascript, I'm not sure how you're going to hook it into the "Delete" context menu action.  You might need to just write your own Action and use that in place of the standard "Delete" action.

    Hope this helps,
    Tom

Please Sign in or register to post replies

Write your reply to:

Draft