Пример #1
0
 /// <summary>
 /// Merges another JSObject into this one
 /// </summary>
 public JSObject Merge(JSObject obj)
 {
     if (obj == null) return this;
     foreach (var key in obj.Data.Keys)
     {
         _data.Add(key, obj.Data[key]);
     }
     return this;
 }
Пример #2
0
 /// <summary>
 /// Adds a template binding attribute to the element with a foreach attribute for iteration. pass in an empty value to ignore that setting
 /// </summary>
 public TagBuilder bindTemplate(string templateId, string data, string forEach, string afterAdd, string beforeAdd, string templateOptions)
 {
     var templ = new JSObject();
     templ.Add("name", JSBuilder.GetString(templateId))
          .AddIfNotEmpty("data", data)
          .AddIfNotEmpty("foreach", forEach)
          .AddIfNotEmpty("afterAdd", afterAdd)
          .AddIfNotEmpty("beforeAdd", beforeAdd)
          .AddIfNotEmpty("templateOptions", templateOptions);
     return bindTemplate("template", templ.ToString());
 }
Пример #3
0
        /// <summary>
        /// Configures a jquery UI Tabs with the provided selector
        /// </summary>
        /// <param name="selector">The jQuery selector</param>
        /// <param name="otherOptions">Add options to the tabs</param>
        /// <param name="includeScriptTag">Determines whether to include the script tag</param>
        /// <param name="selectFunction">The function to be called when a tab is selected. Receives two parameters (event, ui)</param>
        public string MakeJQTabs(string selector, string selectFunction, JSObject otherOptions, bool includeScriptTag)
        {
            var options = new JSObject();
            if (!string.IsNullOrEmpty(selectFunction)) options.Add("select", selectFunction);
            if (otherOptions != null) options.Merge(otherOptions);

            var builder = options.IsEmpty() ? JQ.Select(selector).AddCommand("tabs()") :
                                              JQ.Select(selector).AddCommand("tabs(" + options + ")");
            return includeScriptTag ? builder.ToScriptBlock()
                                    : builder.ToString();
        }
Пример #4
0
 /// <summary>
 /// Adds a property to the json object. It asumes that it is not a string so it ouputs the value directly. 
 /// if the value is empty then it oupts null as the value for the property. You can nest JSObjects by
 /// adding a property to it, and passing as the value the ToString method call of another JSObject
 /// </summary>
 /// <param name="name">Property name {name}: {value}</param>
 /// <param name="value">Property value</param>
 public JSObject Add(string name, JSObject value)
 {
     if (value == null) return this;
     return Add(name, value.ToString(), ValueTypeEnum.Other);
 }
Пример #5
0
        /// <summary>
        /// Grabs all the values and options specified and creates the proper script to ouput to the page. If you called ton OnReady it outputs the script
        /// within a jQuery on document ready function, if you flagged it to AddScriptTag it sorrounds the whole script with the propery <script></script> tag
        /// </summary>
        /// <returns>Properly formatted javascript to include in the page</returns>
        public override string ToString()
        {
            var options = new JSObject();
            options.AddString("url", _url);
            options.AddString("type", _type);
            options.Merge(_otherOptions);
            if (!string.IsNullOrEmpty(_dataType))
            {
                options.AddString("dataType", _dataType);
            }
            if (!string.IsNullOrEmpty(_target))
            {
                options.AddString("target", _target);
                if (_replaceTarget)
                {
                    options.Add("replaceTarget", "true");
                }
            }

            // If the user does not specify a validation function, then assign a default
            if (string.IsNullOrEmpty(_validateFunction))
                _validateFunction = "jQuery('#" + _formName + "').validate().form()";

            if (!string.IsNullOrEmpty(_beforeSerialize) & _validate == false)
            {
                options.Add("beforeSerialize", _beforeSerialize);
            }
            else if (!string.IsNullOrEmpty(_beforeSerialize) & _validate)
            {
                options.Add("beforeSerialize", "function(){ " + _beforeSerialize + "(); if (!" + _validateFunction + ") return false; }");
            }
            else if (_validate)
            {
                options.Add("beforeSerialize", "function() { if (!" + _validateFunction + ") return false; }");
            }
            if (!string.IsNullOrEmpty(_beforeSubmit))
            {
                options.Add("beforeSubmit", _beforeSubmit);
            }
            if (!string.IsNullOrEmpty(_success) || options.HasMember("error"))
            {
                // Need to check that the response is actually a success before executing it
                var ssb = new StringBuilder();
                ssb.Append("function(responseText, statusText, xhr, $form) { ");

                // Sometimes the response is 0 or undefined but there is not an error in the response 
                //   (this happens sometimes when uploading a form with a file input)
                ssb.Append("    var undefinedWithError = false; ");
                ssb.Append("    try {");
                ssb.Append("         if ((xhr.status == undefined || xhr.status == 0) && responseText.indexOf('<span><h1>Server Error in') != -1) { ");
                ssb.Append("              undefinedWithError = true;");
                ssb.Append("         }");
                ssb.Append("    } catch(e){log('###ERROR: ' + e);}");

                //ssb.Append("    alert('statusIs0OrUndefined: ' + (xhr.status == undefined || xhr.status == 0));")
                //ssb.Append("    alert('statusText: ' + statusText + ' \n status: ' + xhr.status + '\n undefinedWithError: ' + undefinedWithError + '\n\n' + responseText);")
                //ssb.Append("if (undefinedWithError) alert('***ERROR***: StatusText: ' + statusText + ' status: ' + xhr.status);")

                // Trigger an error handler if any
                if (options.HasMember("error"))
                    ssb.Append("if (undefinedWithError)(" + options.GetValue("error") + ")(responseText, statusText, xhr, $form);");

                // Call success only after checking that there were no undefined with errors
                if (!string.IsNullOrEmpty(_success))
                    ssb.Append("if (!undefinedWithError) (" + _success + ")(responseText, statusText, xhr, $form);");

                ssb.Append("}");
                options.Add("success", ssb.ToString());
            }

            if (_resetForm)
            {
                options.Add("resetForm", "true");
            }
            if (_clearForm)
            {
                options.Add("clearForm", "true");
            }

            var script = "";

            // Add the script tag if requested
            if (_wrapScriptTag)
                script += "<script type='text/javascript'>";
            // Add the jquery on ready tag
            if (_wrapOnReady)
                script += "jQuery(function(){";
            script += "jQuery('#" + _formName + "').ajaxForm(" + options + ");";
            // Close the jquery on ready tag
            if (_wrapOnReady)
                script += "});";
            // Close the script onready tag
            if (_wrapScriptTag)
                script += "</script>";

            return script;
        }
Пример #6
0
 /// <summary>
 /// Submits specified form via ajax to the url specified, and updates the supplied updatePanel with the value
 /// returned from the server. It validates the inputs within the specified container.
 /// </summary>
 public JSBuilder SubmitFormViaAjax_ValidateContainer(string formId, string postUrl, string updatePanel, string containerId, JSObject ajaxFormSubmitOptions = null)
 {
     var script = new JSBuilder();
     script.AddLine("bf.submitForm('{0}',{1})", formId, JSObject.Create()
                                                                 .Add("viaAjax", true)
                                                                 .Add("validate", true)
                                                                 .AddString("validatePartialFormId", containerId)
                                                                 .AddString("ajaxPostUrl", postUrl)
                                                                 .AddString("updatePanel", updatePanel)
                                                                 .Add("ajaxOptions", ajaxFormSubmitOptions)
                                                                 .ToString());
     return script;
 }
Пример #7
0
 /// <summary>
 /// Submits the parent form of this element via ajax to the url specified, and updates the supplied updatePanel with the value
 /// returned from the server. Optionally does validation
 /// </summary>
 public JSBuilder SubmitParentFormViaAjax(string postUrl, string updatePanel, bool validate = true, JSObject ajaxFormSubmitOptions = null)
 {
     var script = new JSBuilder();
     script.AddLine("bf.submitParentForm(this,{0})", JSObject.Create()
                                                             .Add("viaAjax", true)
                                                             .Add("validate", validate)
                                                             .AddString("ajaxPostUrl", postUrl)
                                                             .AddString("updatePanel", JQHelper.GetElementID(updatePanel))
                                                             .Add("ajaxOptions", ajaxFormSubmitOptions)
                                                             .ToString());
     return script;
 }
Пример #8
0
 /// <summary>
 /// Posts the specified JSObject literal values
 /// </summary>
 /// <param name="obj">The JSObject to post</param>
 public AjaxRequest PostData(JSObject obj)
 {
     _postData.Merge(obj);
     return this;
 }
Пример #9
0
 /// <summary>
 /// Calls the ajaxSubmit with the specified set of options
 /// JSBuilder.GetString will be called on the url passed in, so you don't have to clean it up
 /// Appends: [.]ajaxSubmit({ url: '[url]', target: '#[updateElement]' [,[otherOptions]]})
 /// </summary>
 public JQHelper ajaxSubmit(JSObject options)
 {
     return AddCommand("ajaxSubmit(" + options + ")");
 }
Пример #10
0
        /// <summary>
        /// Calls the ajaxSubmit with the specified url and updates the specified element with the returned value. 
        /// It also appends the other options if specified.
        /// url passed in will be properly escaped and quoted, so you don't have to clean it up
        /// Appends: [.]ajaxSubmit({ url: '[url]', target: '#[updateElement]' [,[otherOptions]]})
        /// </summary>
        public JQHelper ajaxSubmit(string url, string updateElement, JSObject otherOptions)
        {
            var options = new JSObject();
            options.AddString("url", url).AddString("target", GetElementID(updateElement));
            if (otherOptions != null)
                options.Merge(otherOptions);

            return ajaxSubmit(options);
        }
Пример #11
0
 /// <summary>
 /// Calls the ajaxSubmit with the specified url and updates the specified element with the returned value
 /// url passed in will be properly escaped and quoted, so you don't have to clean it up
 /// Appends: [.]ajaxSubmit({ url: '[url]', target: '#[updateElement]' })
 /// </summary>
 public JQHelper ajaxSubmit(string url, string updateElement)
 {
     var options = new JSObject();
     options.AddString("url", url).AddString("target", GetElementID(updateElement));
     return ajaxSubmit(options);
 }