Esempio n. 1
0
        internal static IBindingsBuilder <T> InnerClientViewModel <T>(
            this HtmlHelper htmlHelper,
            string uniqueName,
            T model,
            string partialPrefix,
            string prefix,
            string htmlElementId,
            bool initialSave,
            bool applyBindings,
            bool applyDependencies = true
            )
            where T : class, new()
        {
            if (partialPrefix == null)
            {
                partialPrefix = string.Empty;
            }
            if (prefix == null)
            {
                prefix = string.Empty;
            }
            if (uniqueName == null)
            {
                throw (new ArgumentNullException("uniqueName"));
            }

            string validationType = null;

            switch (MvcEnvironment.Validation(htmlHelper))
            {
            case ValidationType.StandardClient: validationType = "StandardClient"; break;

            case ValidationType.UnobtrusiveClient: validationType = "UnobtrusiveClient"; break;

            default: validationType = "Server"; break;
            }
            if (model == null)
            {
                return(new BindingsBuilder <T>(null, uniqueName, prefix, validationType, null, htmlHelper));
            }

            string script = null;

            if (applyBindings)
            {
                if (htmlElementId == null)
                {
                    script = rootBingingScript;
                }
                else
                {
                    script = elementBingingScript;
                }
            }
            else if (applyDependencies)
            {
                script = modelScript;
            }
            else
            {
                script = modelScriptNoDep;
            }

            ModelTranslator <T> translator = new ModelTranslator <T>();

            translator.ImportFromModel(model);

            htmlHelper.ViewContext.Writer.Write(
                BasicHtmlHelper.RenderDisplayInfo(htmlHelper,
                                                  typeof(ModelTranslator <T>),
                                                  partialPrefix, true));
            object ocount = htmlHelper.ViewContext.Controller.ViewData["__ClientModelsCount__"];
            int    count  = 0;

            if (ocount != null)
            {
                count = (int)ocount;
            }
            count++;
            htmlHelper.ViewContext.Controller.ViewData["__ClientModelsCount__"] = count;

            string jsonHiddenId = BasicHtmlHelper.IdFromName(
                BasicHtmlHelper.AddField(prefix, "$.JSonModel" + count.ToString()));

            htmlHelper.ViewContext.Writer.Write(
                BasicHtmlHelper.SafeHiddenUC(htmlHelper,
                                             BasicHtmlHelper.AddField(partialPrefix,
                                                                      "$.JSonModel"),
                                             string.Empty,
                                             jsonHiddenId
                                             )
                );
            string saveScript = string.Empty;

            if (initialSave)
            {
                saveScript = uniqueName + ".save();";
            }
            string assignement = uniqueName;

            if (!assignement.Contains('.'))
            {
                assignement = "var " + assignement;
            }
            string jsonModel = string.Format(script, assignement, uniqueName, translator.JSonModel, saveScript, htmlElementId, jsonHiddenId, validationType);

            htmlHelper.ViewContext.Writer.Write(jsonModel);


            IBindingsBuilder <T> result =
                new BindingsBuilder <T>(htmlHelper.ViewContext.Writer, uniqueName, prefix, validationType, jsonHiddenId, htmlHelper);

            /*       result.AddMethod("save", string.Format(@"
             *             function(){{
             *                     document.getElementById('{0}').value = ko.mapping.toJSON(this);
             *             }}",
             *                jsonHiddenId));
             *     result.AddMethod("validateAndSave", string.Format(@"
             *             function(){{
             *                 if(MvcControlsToolkit_FormIsValid('{0}', '{1}')){{
             *                     document.getElementById('{0}').value = ko.mapping.toJSON(this);
             *                     return true;
             *                 }}
             *                 return false;
             *             }}",
             *                jsonHiddenId, validationType));
             *     result.AddMethod("saveAndSubmit", string.Format(@"
             *             function(){{
             *                 if(this.validateAndSave()){{
             *                     $('#{0}').parents('form').submit();
             *                 }}
             *             }}",
             *                jsonHiddenId
             *                ));
             *     result.AddMethod("saveAndSubmitAlone", string.Format(@"
             *             function(formId){{
             *                 if(MvcControlsToolkit_FormIsValid(formId, '{1}')){{
             *                     this.save();
             *                     $('#{0}').parents('form').submit();
             *                 }}
             *             }}",
             *                jsonHiddenId,
             *                validationType
             *                )); */
            return(result);
        }
        public static MvcHtmlString ClientTemplate <T>(this HtmlHelper htmlHelper, string uniqueName, object template, bool?applyAutomaticBindings = null, T prototype = null)
            where T : class
        {
            if (string.IsNullOrWhiteSpace(uniqueName))
            {
                throw new ArgumentNullException("uniqueName");
            }
            if (template == null)
            {
                throw new ArgumentNullException("template");
            }
            htmlHelper.ViewContext.HttpContext.Items["ClientTemplateOn"] = new object();
            bool oldValidation = htmlHelper.ViewContext.ClientValidationEnabled;

            if (!MvcEnvironment.UnobtrusiveAjaxOn(htmlHelper))
            {
                htmlHelper.ViewContext.ClientValidationEnabled = false;
            }
            FormContext oldFormContext = htmlHelper.ViewContext.FormContext;

            htmlHelper.ViewContext.FormContext = new FormContext();
            string validationType    = null;
            bool   automaticBindings = false;

            if (applyAutomaticBindings.HasValue)
            {
                automaticBindings = applyAutomaticBindings.Value;
            }
            else
            {
                automaticBindings = htmlHelper.ViewData["ClientBindings"] != null;
            }
            switch (MvcEnvironment.Validation(htmlHelper))
            {
            case ValidationType.StandardClient: validationType = "StandardClient"; break;

            case ValidationType.UnobtrusiveClient: validationType = "UnobtrusiveClient"; break;

            default: validationType = "Server"; break;
            }
            IBindingsBuilder <T> bindings = new BindingsBuilder <T>(htmlHelper.ViewContext.Writer, string.Empty, templateSymbol + "0.A", validationType, null, htmlHelper);
            string stringTemplate         = new TemplateInvoker <T>(template, bindings).InvokeVirtual(htmlHelper, templateSymbol + "0.A");

            if (automaticBindings)
            {
                stringTemplate = new KoAutomaticBinder <T>(stringTemplate,
                                                           bindings).ToString();
            }
            else
            {
                stringTemplate = new KoAutomaticBinder <T>(stringTemplate, null).ToString();
            }

            /* stringTemplate = stringTemplate.Replace(templateSymbol + ".A", "${MvcControlsToolkit_TemplateName($item)}")
             *   .Replace(templateSymbol + "_A", "${MvcControlsToolkit_TemplateId($item)}");*/
            /*  stringTemplate = stringTemplate.Replace(templateSymbol + ".A", templateSymbol+"0.A")
             *    .Replace(templateSymbol + "_A", templateSymbol+"0_A");   */
            string globalEvals = string.Empty;

            if (htmlHelper.ViewContext.HttpContext.Items.Contains("GlobalEvalsRequired"))
            {
                StringBuilder globalEvalsBuilder = htmlHelper.ViewContext.HttpContext.Items["GlobalEvalsRequired"]
                                                   as StringBuilder;
                globalEvals = globalEvalsBuilder.ToString();
                htmlHelper.ViewContext.HttpContext.Items.Remove("GlobalEvalsRequired");
            }
            string prototypeDeclaration = string.Empty;

            if (prototype != null)
            {
                ModelTranslator <T> model = new ModelTranslator <T>();
                model.ImportFromModel(prototype);
                prototypeDeclaration = string.Format(prototypeFormat, uniqueName, model.JSonModel);
            }
            MvcHtmlString res = MvcHtmlString.Create(string.Format(clientTemplateScript,
                                                                   uniqueName,
                                                                   stringTemplate,
                                                                   globalEvals,
                                                                   prototypeDeclaration
                                                                   ));

            htmlHelper.ViewContext.FormContext             = oldFormContext;
            htmlHelper.ViewContext.ClientValidationEnabled = oldValidation;
            htmlHelper.ViewContext.HttpContext.Items.Remove("ClientTemplateOn");
            return(res);
        }