Esempio n. 1
0
        private async Task ProcessParameters()
        {
            IStyled styled    = Id == null ? StyledService : StyledService.WithId(Id);
            string  classname = null;

            if (ComposeAttributes == null)
            {
                string content = RenderAsString();
                content = ApplyTheme(content);
                if (IsKeyframes)
                {
                    classname = styled.Keyframes(content);
                }
                else if (Classname != null && MediaQuery == MediaQueries.None && _previousClassname == null)
                {
                    //html elements
                    styled.Css(ApplyPseudoClass(Classname), content);
                }
                else if (MediaQuery != MediaQueries.None && ClassnameChanged.HasDelegate)
                {
                    //If ClassnameChanged has a delegate then @bind-Classname was used and this is a "new" style
                    //Otherwise Classname was used and this an existing style which will be handled in OnParametersSet
                    content   = WrapWithMediaQuery(content);
                    classname = styled.Css(content);
                }
                else if (Classname != null && MediaQuery != MediaQueries.None && !ClassnameChanged.HasDelegate && _previousClassname == null)
                {
                    //Media query support for classes where an existing Classname already exists
                    content = WrapWithMediaQuery(ApplyPseudoClass(Classname), content);
                    styled.Css(GetMediaQuery(), content);
                }
                else if (Classname == null && PseudoClass == PseudoClasses.None && MediaQuery != MediaQueries.None && _previousClassname == null)
                {
                    //Media queries for html elements
                    styled.Css(GetMediaQuery(), content);
                }
                else if (Classname != null && PseudoClass != PseudoClasses.None && MediaQuery == MediaQueries.None && _previousClassname == null)
                {
                    content = WrapWithMediaQuery(ApplyPseudoClass(Classname), content);
                    styled.Css(content);
                }
                else
                {
                    classname = styled.Css(content);

                    /*if (_previousClassname == null)
                     * {
                     *  classname = styled.Css(content);
                     * }*/
                }
                await NotifyChanged(classname);
            }
            else
            {
                if (ClassnameChanged.HasDelegate)
                {
                    StringBuilder  sb             = new StringBuilder();
                    IList <string> labels         = new List <string>();
                    IList <string> composeClasses = GetComposeClasses();
                    foreach (string cls in composeClasses)
                    {
                        string        selector = ComposeAttributes[cls].ToString();
                        IList <IRule> rules    = StyleSheet.GetRules(Id, selector);
                        if (rules != null)
                        {
                            foreach (IRule rule in rules)
                            {
                                if (rule.Selector != selector)
                                {
                                    string pseudo = rule.Selector.Replace("." + selector, "");
                                    sb.Append('&').Append(pseudo).Append('{');
                                }
                                foreach (Declaration decleration in rule.Declarations)
                                {
                                    sb.Append(decleration.ToString());
                                }
                                if (rule.Label != null)
                                {
                                    labels.Add(rule.Label);
                                }
                                if (rule.Selector != selector)
                                {
                                    sb.Append('}');
                                }
                            }
                        }
                    }
                    if (sb.Length != 0)
                    {
                        string css = sb.ToString();
                        if (labels.Count != 0)
                        {
                            string labelStr = string.Join("-", labels);
                            css = $"label:{labelStr};{css}";
                        }
                        classname = styled.Css(css);
                        await NotifyChanged(classname);
                    }
                }
            }
        }
Esempio n. 2
0
        private async Task ProcessParameters()
        {
            IStyled styled = Id == null ? StyledService : Priority.HasValue ? StyledService.WithId(Id, Priority.Value) : StyledService.WithId(Id);

            string classname = null;

            string content      = ChildContent.RenderAsSimpleString();
            int    _currentHash = CalculateHash(content);

            if (content != null && content.Length > 0 && (_currentHash != _previousHash || _currentHash == _previousHash && ComposeAttributes != null))
            {
                if (IsKeyframes)
                {
                    classname = styled.Keyframes(content);
                }
                else if (Classname != null && MediaQuery == MediaQueries.None && _previousClassname == null)
                {
                    //html elements
                    styled.Css(ApplyPseudoClass(Classname), content);
                }
                else if (MediaQuery != MediaQueries.None && ClassnameChanged.HasDelegate)
                {
                    //If ClassnameChanged has a delegate then @bind-Classname was used and this is a "new" style
                    //Otherwise Classname was used and this an existing style which will be handled below
                    content   = WrapWithMediaQuery("&{" + content + "}");
                    classname = styled.Css(content);
                }
                else if (Classname != null && MediaQuery != MediaQueries.None && !ClassnameChanged.HasDelegate && _previousClassname == null)
                {
                    //Media query support for classes where an existing Classname already exists
                    content = WrapClass(ApplyPseudoClass(Classname), content);
                    styled.Css(WrapWithMediaQuery(content));
                }
                else if (Classname == null && PseudoClass == PseudoClasses.None && MediaQuery != MediaQueries.None && _previousClassname == null)
                {
                    //Media queries for html elements
                    styled.Css(GetMediaQuery(), content);
                }
                else if (Classname != null && PseudoClass != PseudoClasses.None && MediaQuery == MediaQueries.None && _previousClassname == null)
                {
                    content = WrapClass(ApplyPseudoClass(Classname), content);
                    styled.Css(content);
                }
                else
                {
                    if (PseudoClass == PseudoClasses.None && MediaQuery == MediaQueries.None)
                    {
                        classname = styled.Css(content);
                    }
                }
                if (ComposeAttributes == null || !ClassnameChanged.HasDelegate)
                {
                    await NotifyChanged(classname);
                }
            }
            if (ComposeAttributes != null && ClassnameChanged.HasDelegate)
            {
                StringBuilder sb = new StringBuilder();
                if (classname != null)
                {
                    sb.Append(classname).Append(" ");
                }
                IList <string> composeClasses = GetComposeClasses();
                foreach (string cls in composeClasses)
                {
                    string selector = ComposeAttributes[cls].ToString();
                    sb.Append(selector).Append(" ");
                }
                if (sb.Length != 0)
                {
                    classname = sb.ToString().Trim();
                    await NotifyChanged(classname);
                }
            }
            if (GlobalStyle != null & classname != null && _currentHash != _previousHash)
            {
                _previousHash = _currentHash; // This needs to be done here even though it is also two lines down. Do not remove!
                StyledService.SetGlobalStyle(GlobalStyle, classname);
            }
            _previousHash = _currentHash;
        }