Example #1
0
        public void RaiseCallbackEvent(string arg)
        {
            Result = null;
            try {
                string cmd, par = string.Empty;
                int i = arg.IndexOf(':');
                if (i == -1) cmd = arg;
                else {
                    cmd = arg.Substring(0, i);
                    par = arg.Substring(i + 1);
                }

                switch (cmd) {
                case "load":
                    Html.DocumentNode html;

                    var preview = PreviewElement; // if Preview element exists load preview else HtmlElement.Children.
                    if (preview != null) html = preview.Children;
                    else html = Element.Children;
                    EditorConverter.Export(html);
                    Services.JavaScriptTextEditor.Converter.Export(html, out Result);
                    break;
                case "save":
                    var oldText = Element.Children.Text; // save old text

                    preview = CreatePreviewElement();
                    Services.JavaScriptTextEditor.Converter.Import(preview.Children, par);
                    EditorConverter.Import(preview.Children);
                    // TODO preview.Children.AddIdentation(preview.ChildIdentation);
                    CheckParseErrors();
                    Element.Preview();

                    // page is now saved, now get the new html via a WebClient request to the saved page.
                    try {
                        var web = new CookieAwareWebClient(); // start a webrequest to the new preview page.
                        web.UseDefaultCredentials = true;
                        web.Encoding = Encoding.UTF8;
                        web.Headers[HttpRequestHeader.UserAgent] = Request.UserAgent;
                        //web.Headers[HttpRequestHeader.TransferEncoding] = "UTF8";
                        //web.Headers[HttpRequestHeader.ContentEncoding] = "UTF8";
                        foreach (var key in Request.Cookies.Keys.OfType<string>()) {
                            var cookie = Request.Cookies[key];
                            var wc = new Cookie(cookie.Name, cookie.Value, cookie.Path, Request.Url.Host);
                            wc.Expires = cookie.Expires;
                            web.Cookies.Add(wc);
                        }

                        Html.Element e = null;
                        var dochtml = web.DownloadString(Request.Url.AbsoluteUri); // get the new preview page's html.
                        if (dochtml != null) {

                            // parse the received html page for the Container control.
                            if (string.IsNullOrEmpty(Container.ClientID)) CreateChildControls(); // this is needed so we know the Container.ClientID, because otherwise Container get's not instantiated in CallbackEvent.
                            var doc = new Html.Document(dochtml);
                            e = doc.Find(Container.ClientID); // get our element out of the html.
                        }
                        if (e != null) {
                            Result = e.Children.Text; // return the element's html
                        } else {
                            Element.Children.Text = oldText;
                            Save();
                            throw new Exception("Invalid HTML.");
                        }

                    } catch (Exception ex) { // exception viewing the new preview page
                        Element.Children.Text = oldText; // reset text
                        Save();
                        Result = null;
                        throw ex;
                    }

                    break;
                default: throw new NotSupportedException("EditableContent: Unknown callback command.");
                }
            } catch (Exception ex) {
                Services.Log.Error("EditableContent callback exception:", ex);
                throw ex;
            }
        }