Exemplo n.º 1
0
    /// <summary>
    /// Gives focus to an element given its <see cref="ElementReference"/>.
    /// </summary>
    /// <param name="elementReference">A reference to the element to focus.</param>
    /// <param name="preventScroll">
    /// <para>
    ///     A <see cref="bool" /> value indicating whether or not the browser should scroll the document to bring the newly-focused element into view.
    ///     A value of false for preventScroll (the default) means that the browser will scroll the element into view after focusing it.
    ///     If preventScroll is set to true, no scrolling will occur.
    /// </para>
    /// </param>
    /// <returns>The <see cref="ValueTask"/> representing the asynchronous focus operation.</returns>
    public static ValueTask FocusAsync(this ElementReference elementReference, bool preventScroll)
    {
        var jsRuntime = elementReference.GetJSRuntime();

        if (jsRuntime == null)
        {
            throw new InvalidOperationException("No JavaScript runtime found.");
        }

        return(jsRuntime.InvokeVoidAsync(DomWrapperInterop.Focus, elementReference, preventScroll));
    }
        public static ValueTask <int> MudAddEventListenerAsync <T>(this ElementReference elementReference, DotNetObjectReference <T> dotnet, string @event, string callback, bool stopPropagation = false) where T : class
        {
            var parameters     = dotnet.Value.GetType().GetMethods().First(m => m.Name == callback).GetParameters().Select(p => p.ParameterType);
            var parameterSpecs = new object[parameters.Count()];

            for (int i = 0; i < parameters.Count(); ++i)
            {
                parameterSpecs[i] = GetSerializationSpec(parameters.ElementAt(i));
            }
            return(elementReference.GetJSRuntime()?.InvokeAsync <int>("mudElementRef.addEventListener", elementReference, dotnet, @event, callback, parameterSpecs, stopPropagation) ?? ValueTask.FromResult(0));
        }
Exemplo n.º 3
0
        private static async Task <IJSObjectReference?> GetJsObject(this ElementReference elementReference)
        {
            var jsRuntime = elementReference.GetJSRuntime();

            if (jsRuntime is null)
            {
                throw new InvalidOperationException("No JavaScript runtime found.");
            }

#if DEBUG
            var jsName = "Majorsoft.Blazor.Components.Common.JsInterop/scroll.js";
#else
            var jsName = "Majorsoft.Blazor.Components.Common.JsInterop/scroll.min.js";
#endif

            return(await jsRuntime.InvokeAsync <IJSObjectReference>("import", $"./_content/{jsName}"));
        }
 public static ValueTask MudRemoveEventListenerAsync(this ElementReference elementReference, string @event, int eventId) =>
 elementReference.GetJSRuntime()?.InvokeVoidAsync("mudElementRef.removeEventListener", elementReference, eventId) ?? ValueTask.CompletedTask;
 public static ValueTask MudChangeCssVariableAsync(this ElementReference elementReference, string variableName, int value) =>
 elementReference.GetJSRuntime()?.InvokeVoidAsync("mudElementRef.changeCssVariable", elementReference, variableName, value) ?? ValueTask.CompletedTask;
 /// <summary>
 /// Returns true if the element has an ancestor with style position == "fixed"
 /// </summary>
 /// <param name="elementReference"></param>
 public static ValueTask <bool> MudHasFixedAncestorsAsync(this ElementReference elementReference) =>
 elementReference.GetJSRuntime()?
 .InvokeAsync <bool>("mudElementRef.hasFixedAncestors", elementReference) ?? ValueTask.FromResult(false);
 /// <summary>
 /// Gets the client rect of the first child of the element.
 /// Useful when you want to know the dimensions of a render fragment and for that you wrap it into a div
 /// </summary>
 public static ValueTask <BoundingClientRect> MudGetClientRectFromFirstChildAsync(this ElementReference elementReference) =>
 elementReference.GetJSRuntime()?.InvokeAsync <BoundingClientRect>("mudElementRef.getClientRectFromFirstChild", elementReference) ?? ValueTask.FromResult(new BoundingClientRect());
 public static ValueTask MudChangeCssAsync(this ElementReference elementReference, string css) =>
 elementReference.GetJSRuntime()?.InvokeVoidAsync("mudElementRef.changeCss", elementReference, css) ?? ValueTask.CompletedTask;
 public static ValueTask MudSelectRangeAsync(this ElementReference elementReference, int pos1, int pos2) =>
 elementReference.GetJSRuntime()?.InvokeVoidAsync("mudElementRef.selectRange", elementReference, pos1, pos2) ?? ValueTask.CompletedTask;
 public static ValueTask MudSelectAsync(this ElementReference elementReference) =>
 elementReference.GetJSRuntime()?.InvokeVoidAsync("mudElementRef.select", elementReference) ?? ValueTask.CompletedTask;
 public static ValueTask MudRestoreFocusAsync(this ElementReference elementReference) =>
 elementReference.GetJSRuntime()?.InvokeVoidAsync("mudElementRef.restoreFocus", elementReference) ?? ValueTask.CompletedTask;
 public static ValueTask MudFocusLastAsync(this ElementReference elementReference, int skip = 0, int min = 0) =>
 elementReference.GetJSRuntime()?.InvokeVoidAsync("mudElementRef.focusLast", elementReference, skip, min) ?? ValueTask.CompletedTask;