예제 #1
0
        public static void AddDefaultMethodTranslators()
        {
            var lengthMethod = new StringJsMethodCompiler("{0}.length");

            AddPropertyGetterTranslator(typeof(Array), nameof(Array.Length), lengthMethod);
            AddPropertyGetterTranslator(typeof(ICollection), nameof(ICollection.Count), lengthMethod);
            AddPropertyGetterTranslator(typeof(ICollection <>), nameof(ICollection.Count), lengthMethod);
            AddPropertyGetterTranslator(typeof(string), nameof(string.Length), lengthMethod);
            AddMethodTranslator(typeof(object), "ToString", new StringJsMethodCompiler("String({0})", (m, c, a) => ToStringCheck(c)), 0);
            AddMethodTranslator(typeof(Convert), "ToString", new StringJsMethodCompiler("String({1})", (m, c, a) => ToStringCheck(a[0])), 1, true);
            //AddMethodTranslator(typeof(Enumerable), nameof(Enumerable.Count), lengthMethod, new[] { typeof(IEnumerable) });

            BindingPageInfo.RegisterJavascriptTranslations();
        }
예제 #2
0
        public void AddDefaultMethodTranslators()
        {
            var lengthMethod = new GenericMethodCompiler(a => a[0].Member("length"));

            AddPropertyGetterTranslator(typeof(Array), nameof(Array.Length), lengthMethod);
            AddPropertyGetterTranslator(typeof(ICollection), nameof(ICollection.Count), lengthMethod);
            AddPropertyGetterTranslator(typeof(ICollection <>), nameof(ICollection.Count), lengthMethod);
            AddPropertyGetterTranslator(typeof(string), nameof(string.Length), lengthMethod);
            AddMethodTranslator(typeof(Enumerable), "Count", parameterCount: 1, translator: new GenericMethodCompiler(a => a[1].Member("length")));
            AddMethodTranslator(typeof(object), "ToString", new GenericMethodCompiler(
                                    a => new JsIdentifierExpression("String").Invoke(a[0]), (m, c, a) => ToStringCheck(c)), 0);
            AddMethodTranslator(typeof(Convert), "ToString", new GenericMethodCompiler(
                                    a => new JsIdentifierExpression("String").Invoke(a[1]), (m, c, a) => ToStringCheck(a[0])), 1, true);
            AddMethodTranslator(typeof(Enums), "GetNames", new EnumGetNamesMethodTranslator(), 0);

            JsExpression indexer(JsExpression[] args, MethodInfo method) =>
            BuildIndexer(args[0], args[1], method.DeclaringType.GetProperty("Item"));

            AddMethodTranslator(typeof(IList), "get_Item", new GenericMethodCompiler(indexer));
            AddMethodTranslator(typeof(IList <>), "get_Item", new GenericMethodCompiler(indexer));
            AddMethodTranslator(typeof(List <>), "get_Item", new GenericMethodCompiler(indexer));
            AddMethodTranslator(typeof(Enumerable).GetMethod("ElementAt", BindingFlags.Static | BindingFlags.Public), new GenericMethodCompiler((args, method) =>
                                                                                                                                                BuildIndexer(args[1], args[2], method)));
            AddPropertyGetterTranslator(typeof(Nullable <>), "Value", new GenericMethodCompiler((args, method) => args[0]));
            AddPropertyGetterTranslator(typeof(Nullable <>), "HasValue",
                                        new GenericMethodCompiler(args => new JsBinaryExpression(args[0], BinaryOperatorType.NotEqual, new JsLiteral(null))));
            //AddMethodTranslator(typeof(Enumerable), nameof(Enumerable.Count), lengthMethod, new[] { typeof(IEnumerable) });

            BindingApi.RegisterJavascriptTranslations(this);
            BindingPageInfo.RegisterJavascriptTranslations(this);
            BindingCollectionInfo.RegisterJavascriptTranslations(this);

            // string formatting
            var stringFormatTranslator = new GenericMethodCompiler(
                args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("format").Invoke(args[1], new JsArrayExpression(args.Skip(2)))
                );

            // TODO: string.Format could be two-way
            AddMethodTranslator(typeof(string).GetMethod("Format", new[] { typeof(string), typeof(object) }), stringFormatTranslator);
            AddMethodTranslator(typeof(string).GetMethod("Format", new[] { typeof(string), typeof(object), typeof(object) }), stringFormatTranslator);
            AddMethodTranslator(typeof(string).GetMethod("Format", new[] { typeof(string), typeof(object), typeof(object), typeof(object) }), stringFormatTranslator);
            AddMethodTranslator(typeof(string).GetMethod("Format", new[] { typeof(string), typeof(object[]) }), new GenericMethodCompiler(
                                    args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("format").Invoke(args[1], args[2])
                                    ));
            AddMethodTranslator(typeof(DateTime).GetMethod("ToString", Type.EmptyTypes), new GenericMethodCompiler(
                                    args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("bindingDateToString")
                                    .WithAnnotation(new RequiredRuntimeResourcesBindingProperty(ImmutableArray.Create("globalize")))
                                    .Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))
                                    ));
            AddMethodTranslator(typeof(DateTime).GetMethod("ToString", new[] { typeof(string) }), new GenericMethodCompiler(
                                    args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("bindingDateToString")
                                    .WithAnnotation(new RequiredRuntimeResourcesBindingProperty(ImmutableArray.Create("globalize")))
                                    .Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance), args[1])
                                    ));
            AddMethodTranslator(typeof(DateTime?).GetMethod("ToString", Type.EmptyTypes), new GenericMethodCompiler(
                                    args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("bindingDateToString")
                                    .WithAnnotation(new RequiredRuntimeResourcesBindingProperty(ImmutableArray.Create("globalize")))
                                    .Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))
                                    ));

            foreach (var num in ReflectionUtils.NumericTypes.Except(new[] { typeof(char) }))
            {
                AddMethodTranslator(num.GetMethod("ToString", Type.EmptyTypes), new GenericMethodCompiler(
                                        args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("bindingNumberToString")
                                        .WithAnnotation(new RequiredRuntimeResourcesBindingProperty(ImmutableArray.Create("globalize")))
                                        .Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))
                                        .WithAnnotation(ResultIsObservableAnnotation.Instance)
                                        ));
                AddMethodTranslator(num.GetMethod("ToString", new[] { typeof(string) }), new GenericMethodCompiler(
                                        args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("bindingNumberToString")
                                        .WithAnnotation(new RequiredRuntimeResourcesBindingProperty(ImmutableArray.Create("globalize")))
                                        .Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance), args[1])
                                        .WithAnnotation(ResultIsObservableAnnotation.Instance)
                                        ));
                AddMethodTranslator(typeof(Nullable <>).MakeGenericType(num).GetMethod("ToString", Type.EmptyTypes), new GenericMethodCompiler(
                                        args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("bindingNumberToString")
                                        .WithAnnotation(new RequiredRuntimeResourcesBindingProperty(ImmutableArray.Create("globalize")))
                                        .Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))
                                        .WithAnnotation(ResultIsObservableAnnotation.Instance)
                                        ));
            }

            AddPropertyGetterTranslator(typeof(Task <>), "Result", new GenericMethodCompiler(args => FunctionalExtensions.ApplyAction(args[0], a => a.RemoveAnnotations(typeof(ViewModelInfoAnnotation)))));

            AddMethodTranslator(typeof(DotvvmBindableObject).GetMethods(BindingFlags.Instance | BindingFlags.Public).Single(m => m.Name == "GetValue" && !m.ContainsGenericParameters), new GenericMethodCompiler(
                                    args => {
                var dotvvmproperty = ((DotvvmProperty)((JsLiteral)args[1]).Value);
                return(JavascriptTranslationVisitor.TranslateViewModelProperty(args[0], (MemberInfo)dotvvmproperty.PropertyInfo ?? dotvvmproperty.PropertyType.GetTypeInfo(), name: dotvvmproperty.Name));
            }
                                    ));
        }
예제 #3
0
        public void AddDefaultMethodTranslators()
        {
            var lengthMethod = new GenericMethodCompiler(a => a[0].Member("length"));

            AddPropertyGetterTranslator(typeof(Array), nameof(Array.Length), lengthMethod);
            AddPropertyGetterTranslator(typeof(ICollection), nameof(ICollection.Count), lengthMethod);
            AddPropertyGetterTranslator(typeof(ICollection <>), nameof(ICollection.Count), lengthMethod);
            AddPropertyGetterTranslator(typeof(string), nameof(string.Length), lengthMethod);
            AddMethodTranslator(typeof(Enumerable), "Count", parameterCount: 1, translator: new GenericMethodCompiler(a => a[1].Member("length")));
            AddMethodTranslator(typeof(object), "ToString", new GenericMethodCompiler(
                                    a => new JsIdentifierExpression("String").Invoke(a[0]), (m, c, a) => ToStringCheck(c)), 0);
            AddMethodTranslator(typeof(Convert), "ToString", new GenericMethodCompiler(
                                    a => new JsIdentifierExpression("String").Invoke(a[1]), (m, c, a) => ToStringCheck(a[0])), 1, true);

            JsExpression indexer(JsExpression[] args, MethodInfo method) =>
            BuildIndexer(args[0], args[1], method.DeclaringType.GetProperty("Item"));

            AddMethodTranslator(typeof(IList), "get_Item", new GenericMethodCompiler(indexer));
            AddMethodTranslator(typeof(IList <>), "get_Item", new GenericMethodCompiler(indexer));
            AddMethodTranslator(typeof(List <>), "get_Item", new GenericMethodCompiler(indexer));
            AddMethodTranslator(typeof(Enumerable).GetMethod("ElementAt", BindingFlags.Static | BindingFlags.Public), new GenericMethodCompiler((args, method) =>
                                                                                                                                                BuildIndexer(args[1], args[2], method)));
            AddPropertyGetterTranslator(typeof(Nullable <>), "Value", new GenericMethodCompiler((args, method) => args[0]));
            AddPropertyGetterTranslator(typeof(Nullable <>), "HasValue",
                                        new GenericMethodCompiler(args => new JsBinaryExpression(args[0], BinaryOperatorType.NotEqual, new JsLiteral(null))));
            //AddMethodTranslator(typeof(Enumerable), nameof(Enumerable.Count), lengthMethod, new[] { typeof(IEnumerable) });

            AddMethodTranslator(typeof(Api), nameof(Api.RefreshOnChange),
                                new GenericMethodCompiler(a =>
                                                          new JsIdentifierExpression("dotvvm").Member("apiRefreshOn").Invoke(
                                                              a[1].WithAnnotation(ShouldBeObservableAnnotation.Instance),
                                                              a[2].EnsureObservableWrapped())
                                                          .WithAnnotation(a[1].Annotation <ResultIsObservableAnnotation>())
                                                          .WithAnnotation(a[1].Annotation <ViewModelInfoAnnotation>())
                                                          .WithAnnotation(a[1].Annotation <MayBeNullAnnotation>())
                                                          ));
            AddMethodTranslator(typeof(Api), nameof(Api.RefreshOnEvent),
                                new GenericMethodCompiler(a =>
                                                          new JsIdentifierExpression("dotvvm").Member("apiRefreshOn").Invoke(
                                                              a[1].WithAnnotation(ShouldBeObservableAnnotation.Instance),
                                                              new JsIdentifierExpression("dotvvm").Member("eventHub").Member("get").Invoke(a[2]))
                                                          .WithAnnotation(a[1].Annotation <ResultIsObservableAnnotation>())
                                                          .WithAnnotation(a[1].Annotation <ViewModelInfoAnnotation>())
                                                          .WithAnnotation(a[1].Annotation <MayBeNullAnnotation>())
                                                          ));
            BindingPageInfo.RegisterJavascriptTranslations(this);
            BindingCollectionInfo.RegisterJavascriptTranslations(this);

            // string formatting
            var stringFormatTranslator = new GenericMethodCompiler(
                args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("format").Invoke(args[1], new JsArrayExpression(args.Skip(2)))
                );

            // TODO: string.Format could be two-way
            AddMethodTranslator(typeof(string).GetMethod("Format", new[] { typeof(string), typeof(object) }), stringFormatTranslator);
            AddMethodTranslator(typeof(string).GetMethod("Format", new[] { typeof(string), typeof(object), typeof(object) }), stringFormatTranslator);
            AddMethodTranslator(typeof(string).GetMethod("Format", new[] { typeof(string), typeof(object), typeof(object), typeof(object) }), stringFormatTranslator);
            AddMethodTranslator(typeof(string).GetMethod("Format", new[] { typeof(string), typeof(object[]) }), new GenericMethodCompiler(
                                    args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("format").Invoke(args[1], args[2])
                                    ));
            AddMethodTranslator(typeof(DateTime).GetMethod("ToString", Type.EmptyTypes), new GenericMethodCompiler(
                                    args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("bindingDateToString").Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))
                                    ));
            AddMethodTranslator(typeof(DateTime).GetMethod("ToString", new[] { typeof(string) }), new GenericMethodCompiler(
                                    args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("bindingDateToString").Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance), args[1])
                                    ));
            AddMethodTranslator(typeof(DateTime?).GetMethod("ToString", Type.EmptyTypes), new GenericMethodCompiler(
                                    args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("bindingDateToString").Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))
                                    ));

            foreach (var num in ReflectionUtils.NumericTypes.Except(new[] { typeof(char) }))
            {
                AddMethodTranslator(num.GetMethod("ToString", Type.EmptyTypes), new GenericMethodCompiler(
                                        args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("bindingNumberToString")
                                        .Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))
                                        .WithAnnotation(ResultIsObservableAnnotation.Instance)
                                        ));
                AddMethodTranslator(num.GetMethod("ToString", new[] { typeof(string) }), new GenericMethodCompiler(
                                        args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("bindingNumberToString")
                                        .Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance), args[1])
                                        .WithAnnotation(ResultIsObservableAnnotation.Instance)
                                        ));
                AddMethodTranslator(typeof(Nullable <>).MakeGenericType(num).GetMethod("ToString", Type.EmptyTypes), new GenericMethodCompiler(
                                        args => new JsIdentifierExpression("dotvvm").Member("globalize").Member("bindingNumberToString")
                                        .Invoke(args[0].WithAnnotation(ShouldBeObservableAnnotation.Instance))
                                        .WithAnnotation(ResultIsObservableAnnotation.Instance)
                                        ));
            }

            AddPropertyGetterTranslator(typeof(Task <>), "Result", new GenericMethodCompiler(args => FunctionalExtensions.ApplyAction(args[0], a => a.RemoveAnnotations(typeof(ViewModelInfoAnnotation)))));
        }