/// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        /// <param name="modeType"></param>
        /// <param name="scriptProcessorType"></param>
        public ScriptProcessorEngine(AjaxClassItem item, WebModeType modeType = WebModeType.MvcController, ScriptProcessorType?scriptProcessorType = null)
        {
            if (item == null)
            {
                return;
            }
            this.AjaxClassItem = item;
            this.ProcessorType = scriptProcessorType.HasValue ? scriptProcessorType.Value : item.AjaxClass.ScriptProcessor;
            this.ModeType      = modeType;
            switch (this.ProcessorType)
            {
            case ScriptProcessorType.JQuery:
                this.ScriptTemplate = new JQuery();
                break;

            case ScriptProcessorType.ExtJs:
                this.ScriptTemplate = new ExtJs();
                break;

            case ScriptProcessorType.Dojo:
                this.ScriptTemplate = new Dojo();
                break;

            case ScriptProcessorType.Prototype:
                this.ScriptTemplate = new Prototype();
                break;

            case ScriptProcessorType.Custom:
                if (item.AjaxClass.ScriptTemplate == null)
                {
                    throw new ArgumentNullException("ScriptTemplate");
                }
                this.ScriptTemplate = item.AjaxClass.ScriptTemplate;
                break;
            }

            this.ScriptTemplate.Mode = modeType;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create the method body
        /// </summary>
        /// <param name="sbWork">StringBuilder to append method</param>
        /// <param name="classNamespace">class name</param>
        /// <param name="methodName">method name</param>
        /// <param name="method">Ajax item method</param>
        /// <param name="parameters">List of parameters</param>
        /// <param name="ajaxClass"> Ajax class item</param>
        /// <param name="useTraditionalParameterForm"> Use traditional parameters, like function(parm1,parm2,parm3) instead function(options), calling myFunc({data:'data'});</param>
        /// <returns></returns>
        public override void CreateMethodBody(ref System.Text.StringBuilder sbWork, string classNamespace, string methodName, AjaxMethodItem method, IList <Entities.Parameter> parameters, AjaxClassItem ajaxClass, bool useTraditionalParameterForm = false)
        {
            var functionBody = new StringBuilder("function(");

            if (useTraditionalParameterForm)
            {
                for (var index = 0; index < parameters.Count; index++)
                {
                    if (index != 0)
                    {
                        functionBody.Append(",");
                    }
                    var parameter = parameters[index];
                    functionBody.Append(parameter.Name);
                }
                functionBody.Append((parameters.Count > 0 ? "," : "") + "callbackSuccess,callbackError,callbackComplete,uploadProgress");
            }
            else
            {
                functionBody.Append("options");
            }

            functionBody.Append("){" + GetNewLineTab(1));


            functionBody.Append(GetNewLineTab(1));
            functionBody.Append("var mtnOptions = (typeof options === 'undefined' || options == null)?{}:options;");
            functionBody.Append(GetNewLineTab(1));
            functionBody.Append("mtnOptions.data = mtnOptions.data || {};");
            functionBody.Append(GetNewLineTab(1));
            if (useTraditionalParameterForm)
            {
                functionBody.Append(GetNewLineTab(1));
                functionBody.
                Append("mtnOptions.callbackSuccess = callbackSuccess;").Append(GetNewLineTab(1)).
                Append("mtnOptions.callbackError = callbackError;").Append(GetNewLineTab(1)).
                Append("mtnOptions.callbackComplete = callbackComplete;").Append(GetNewLineTab(1)).
                Append("mtnOptions.uploadProgress = uploadProgress;");
                functionBody.Append(GetNewLineTab(1));

                if (parameters.Count > 0)
                {
                    functionBody.Append("mtnOptions.data = {");

                    for (var index = 0; index < parameters.Count; index++)
                    {
                        var parameter = parameters[index];
                        functionBody.Append(GetNewLineTab(2));
                        functionBody.Append((index != 0 ? "," : ""))
                        .Append(parameter.Name).Append(":").Append(parameter.Name);
                    }


                    functionBody.Append(GetNewLineTab(1));
                    functionBody.Append("};");
                }
                functionBody.Append(GetNewLineTab(1));
            }

            for (var index = 0; index < parameters.Count; index++)
            {
                var parameter = parameters[index];
                if (parameter.Type.IsPrimitiveMtn() || parameter.Type.FullName.ToLower().Contains("httppostedfile"))
                {
                    continue;
                }

                functionBody.Append(GetNewLineTab(1));
                functionBody.AppendFormat("mtnOptions.data.{0} = JSON.stringify(mtnOptions.data.{0});", parameter.Name);
                functionBody.Append(GetNewLineTab(1));
            }

            functionBody.AppendFormat("mtnOptions.data.__MtnClassHash = '{0}';", ajaxClass.Key);
            functionBody.Append(GetNewLineTab(1));
            functionBody.AppendFormat("mtnOptions.data.__MtnMethodHash = '{0}';", method.Key);
            functionBody.Append(GetNewLineTab(1));
            switch (Mode)
            {
            case WebModeType.AjaxHandler:
                functionBody.Append("mtnOptions.url='")
                .Append(HostUrl).Append("/")
                .Append(classNamespace.ToLowerInvariant()).Append(".").Append(methodName)
                .Append(".").Append(Utils.Parameter.AjaxExtension)
                .Append("';");
                break;

            case WebModeType.MvcController:
                functionBody.Append("mtnOptions.url='")
                .Append(HostUrl).Append("/{MtnMvcRoute}")
                .Append(classNamespace.Replace(".", Web.Utils.Parameter.SeoCharacter).ToLowerInvariant()).Append("/").Append(methodName.ToLowerInvariant())
                .Append("/';");
                break;

            default:
                functionBody.Append("mtnOptions.url='")
                .Append(HostUrl).Append("/{MtnMvcRoute}")
                .Append(classNamespace.Replace(".", Web.Utils.Parameter.SeoCharacter).ToLowerInvariant()).Append("/").Append(methodName.ToLowerInvariant())
                .Append("/';");
                break;
            }
            functionBody.Append(GetNewLineTab(1));

            functionBody.AppendFormat("mtnOptions.responseType = '{0}';", method.AjaxMethod.ResponseType.ToString().ToLowerInvariant());
            functionBody.Append(GetNewLineTab(1));
            string ajaxTmpl = "";

            switch (method.AjaxMethod.RequestType)
            {
            case RequestType.Post:
                ajaxTmpl = AjaxPostFunctionTemplate;
                break;

            case RequestType.Get:
                ajaxTmpl = AjaxGetFunctionTemplate;
                break;
            }
            switch (method.AjaxMethod.ResponseType)
            {
            case ResponseType.Json:
                break;

            case ResponseType.Xml:
                break;

            case ResponseType.Text:
                break;

            case ResponseType.JsonP:
                ajaxTmpl = AjaxJsonPFunctionTemplate;
                break;

            case ResponseType.Html:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            ajaxTmpl = String.Format(ajaxTmpl, HasFileUpload(parameters).ToString().ToLower());
            functionBody.AppendLine(ajaxTmpl);

            functionBody.AppendLine("};");
            if (RootNamespace.IsNullOrWhiteSpaceMtn())
            {
                sbWork.AppendFormat("{0}.{1} = {2}", classNamespace, methodName, functionBody.ToString());
            }
            else
            {
                sbWork.AppendFormat("{0}.{1}.{2}  = {3}", RootNamespace, classNamespace, methodName, functionBody.ToString());
            }
            sbWork.AppendLine();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Add a AjaxClassItem on collection
        /// </summary>
        /// <param name="asm">Assembly where classes have AjaxClassAtribute and AjaxMethod's</param>
        /// <param name="ajaxClassItemCollectionGlobal"></param>
        public static void AddAjaxClassItem(Assembly asm, ref Dictionary <String, AjaxClassItem> ajaxClassItemCollectionGlobal)
        {
            //const bool cacheAjaxOnStartup = false; // Web.Utils.Parameter.CacheAjaxOnStartup - initial context is always null;
            //WebModeType modeType;
            //if (asm.FullName.ToLower().Contains("mtn.library"))
            //    return;

            var ajaxClassItemCollection = ajaxClassItemCollectionGlobal;

            // ToDo : Verificar dlls que nao tem customattribute
            List <Type> listClasses;

            try
            {
                listClasses = asm.GetTypes().Where(tp => tp.GetCustomAttributes(typeof(AjaxClassAttribute), true).Any()).ToList();
            }
            catch (Exception ex)
            {
                if (!Parameter.EnableStatistics)
                {
                    return;
                }
                var msg = "Source:" + ex.Source + "\n\nMessage:" + ex.Message + "\n\nStack:" + ex.StackTrace;
                Statistics.Add(msg);
                return;
            }

            var sync = new Object();

            foreach (var ajaxReflectionClass in listClasses)
            {
                lock (sync)
                {
                    var ajaxClass     = new AjaxClassItem();
                    var ajaxAttribute = (AjaxClassAttribute)
                                        ajaxReflectionClass.GetCustomAttributes(typeof(AjaxClassAttribute), true).
                                        FirstOrDefault();

                    if (ajaxAttribute != null)
                    {
                        if (ajaxAttribute.Name.IsNullOrWhiteSpaceMtn())
                        {
                            ajaxAttribute.Name = ajaxReflectionClass.FullName;
                        }

                        var name = ajaxAttribute.Name;


                        // ajaxAttribute.ClassName.IsNullOrWhiteSpaceMtn() ? ajaxReflectionClass.FullName : ajaxAttribute.ClassName;

                        while (name.Contains(".."))
                        {
                            name = name.Replace("..", ".");
                        }

                        var key = ajaxAttribute.HashTag.IsNullOrWhiteSpaceMtn() ? name.ToLowerInvariant().Md5Mtn(true) : ajaxAttribute.HashTag.HtmlRemoveMtn().Replace("&", "").Replace("?", "").Replace(".", "").Replace("=", "");
                        key = key.IsNullOrWhiteSpaceMtn() ? name.ToLowerInvariant().Md5Mtn(true) : key;

                        ajaxClass.ClassName = name;
                        ajaxClass.AjaxMethodItemCollection = new Dictionary <string, AjaxMethodItem>();
                        ajaxClass.Key       = key;
                        ajaxClass.AjaxClass = ajaxAttribute;

                        var methodList =
                            ajaxReflectionClass.GetMethods().Where(
                                ajx => ajx.GetCustomAttributes(typeof(AjaxMethod), true).Length > 0);


                        //ScriptProcessorEngine scriptProcessor = null;
                        //AjaxScriptItem ajaxScriptItem = null;
                        //if (cacheAjaxOnStartup)
                        //{
                        //    scriptProcessor = ScriptProcessorEngine.CreateInstance(ajaxClass, modeType);

                        //    ajaxScriptItem = new AjaxScriptItem();
                        //    ajaxScriptItem.UseTraditionalParameterForm = ajaxClass.AjaxClass.UseTraditionalParameterForm;
                        //    ajaxScriptItem.AjaxClassKey = ajaxClass.Key;
                        //    ajaxScriptItem.Key = ajaxClass.Key;
                        //    ajaxScriptItem.ScriptProcessor = ajaxClass.AjaxClass.ScriptProcessor;
                        //    ajaxScriptItem.ProcessorPrefix = ajaxClass.AjaxClass.ProcessorPrefix;
                        //    ajaxScriptItem.ScriptTemplate = ajaxClass.AjaxClass.ScriptTemplate;
                        //    if(!ajaxScriptItem.FullScript.IsNullOrWhiteSpaceMtn())
                        //        ajaxScriptItem.FullScriptMinified = Utils.JavaScriptMinifier.MinifyCode(ajaxScriptItem.FullScript);
                        //}

                        foreach (var method in methodList)
                        {
                            var newAjaxMethodItem = new AjaxMethodItem();
                            newAjaxMethodItem.Method = method;
                            if (method.ReflectedType != null)
                            {
                                newAjaxMethodItem.ConstructorHash = method.ReflectedType.FullName.Md5Mtn(true);
                            }
                            newAjaxMethodItem.AjaxMethod = method.GetCustomAttributes(typeof(AjaxMethod), true)
                                                           .Select(i => (i as AjaxMethod)).FirstOrDefault();

                            if (newAjaxMethodItem.AjaxMethod != null)
                            {
                                newAjaxMethodItem.Parameters = (from mt in newAjaxMethodItem.Method.GetParameters()
                                                                where mt.IsRetval == false
                                                                orderby mt.Position
                                                                select
                                                                new Entities.Parameter(mt.Name, "",
                                                                                       mt.Position,
                                                                                       mt.ParameterType.Name,
                                                                                       mt.ParameterType)
                                                                ).ToList();
                                if (newAjaxMethodItem.AjaxMethod.Name.IsNullOrWhiteSpaceMtn())
                                {
                                    newAjaxMethodItem.AjaxMethod.Name = method.Name;
                                }

                                var nameOfMethod = newAjaxMethodItem.AjaxMethod.Name;

                                var keyMeth = newAjaxMethodItem.AjaxMethod.HashTag.IsNullOrWhiteSpaceMtn() ? nameOfMethod.ToLowerInvariant().Md5Mtn(true) : newAjaxMethodItem.AjaxMethod.HashTag.HtmlRemoveMtn().Replace("&", "").Replace("?", "").Replace(".", "").Replace("=", "");
                                keyMeth = keyMeth.IsNullOrWhiteSpaceMtn() ? nameOfMethod.ToLowerInvariant().Md5Mtn(true) : keyMeth;

                                newAjaxMethodItem.Key = keyMeth;
                                if (ajaxClass.AjaxMethodItemCollection.Any(x => x.Key == newAjaxMethodItem.Key))
                                {
                                    ajaxClass.AjaxMethodItemCollection.Remove(newAjaxMethodItem.Key);
                                }

                                ajaxClass.AjaxMethodItemCollection.Add(newAjaxMethodItem.Key, newAjaxMethodItem);

                                //if (cacheAjaxOnStartup)
                                //{
                                //    var ajaxScriptMethodItem = new AjaxScriptMethodItem();
                                //    ajaxScriptMethodItem.Script = scriptProcessor.CreateMethodCode(newAjaxMethodItem, ajaxClass.AjaxClass.UseTraditionalParameterForm);
                                //    ajaxScriptMethodItem.ScriptMinified =
                                //        Utils.JavaScriptMinifier.MinifyCode(ajaxScriptMethodItem.Script);
                                //    ajaxScriptItem.AjaxScriptMethodCollection.Add(newAjaxMethodItem.Key, ajaxScriptMethodItem);
                                //}
                            }
                            if (ajaxClassItemCollection.Any(x => x.Key.Equals(ajaxClass.Key)) != false)
                            {
                                continue;
                            }
                            //if (cacheAjaxOnStartup)
                            //{
                            //    ajaxScriptItem.FullScript = scriptProcessor.CreateFullCode(ajaxClass.AjaxClass.UseTraditionalParameterForm);
                            //    var _ajaxScriptCollection = AjaxScriptCollection;
                            //    _ajaxScriptCollection.Add(ajaxScriptItem.Key, ajaxScriptItem);
                            //    AjaxScriptCollection = _ajaxScriptCollection;
                            //}
                            try
                            {
                                if (ajaxClassItemCollection.Any(x => x.Key.Equals(ajaxClass.Key)) == false)
                                {
                                    ajaxClassItemCollection.Add(ajaxClass.Key, ajaxClass);
                                }
                            }
                            catch (Exception ex)
                            {
                                Statistics.Add(ex.GetAllMessagesMtn());
                            }
                        }
                        ;
                    }
                }
            }
            ;
            ajaxClassItemCollectionGlobal = ajaxClassItemCollection;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        /// <param name="modeType"></param>
        /// <param name="scriptProcessorType"></param>
        /// <returns></returns>
        public static ScriptProcessorEngine CreateInstance(AjaxClassItem item, WebModeType modeType = WebModeType.MvcController, ScriptProcessorType?scriptProcessorType = null)
        {
            var scriptInstance = new ScriptProcessorEngine(item, modeType, scriptProcessorType);

            return(scriptInstance);
        }