Esempio n. 1
0
 private void SetProperties(Control control, SelectorBlock selector)
 {
     foreach (var property in selector.Properties)
     {
         var reflectedProperty = StyledReflectedProperty.Create(control, property.Identifier);
         reflectedProperty.SetValue(property.GetValueAndApplyVariables(this.Variables));
     }
 }
Esempio n. 2
0
        protected string HandleJsonSelector(SelectorBlock selector, JToken parentObj, Dictionary <string, object> variables = null, bool required = true)
        {
            if (selector.Text != null)
            {
                return(ApplyFilters(ApplyGoTemplateText(selector.Text, variables), selector.Filters, variables));
            }

            string value = null;

            if (selector.Selector != null)
            {
                var selectorSelector = ApplyGoTemplateText(selector.Selector.TrimStart('.'), variables);
                selectorSelector = JsonParseFieldSelector(parentObj, selectorSelector);

                JToken selection = null;
                if (selectorSelector != null)
                {
                    selection = parentObj.SelectToken(selectorSelector);
                }

                if (selection == null)
                {
                    if (required)
                    {
                        throw new Exception(string.Format("Selector \"{0}\" didn't match {1}", selectorSelector, parentObj.ToString()));
                    }

                    return(null);
                }

                value = selection.Value <string>();
            }

            if (selector.Case != null)
            {
                foreach (var jcase in selector.Case)
                {
                    if (value.Equals(jcase.Key) || jcase.Key.Equals("*"))
                    {
                        value = jcase.Value;
                        break;
                    }
                }

                if (value == null)
                {
                    if (required)
                    {
                        throw new Exception(string.Format("None of the case selectors \"{0}\" matched {1}", string.Join(",", selector.Case), parentObj.ToString()));
                    }

                    return(null);
                }
            }

            return(ApplyFilters(value?.Trim(), selector.Filters, variables) ?? null);
        }
Esempio n. 3
0
        public void SelectWithVariables()
        {
            IWinformThemeElement variables = new VariableBlock(new[] { new Property("colorOfSky", "Blue") });
            IWinformThemeElement selector  = new SelectorBlock("Form", new[] { new Property("BackColor", "@colorOfSky") });
            var theme = new Theme(new[] { variables, selector });
            var form  = new TestForm {
                BackColor = Color.Black
            };

            theme.ApplyTheme(form);

            Assert.Equal(Color.Blue, form.BackColor);
        }
Esempio n. 4
0
        public void NestedSelectors()
        {
            var form = new TestForm();

            form.button1.Tag = form.button2.Tag = "yellow";
            form.BackColor   = form.button1.BackColor = form.button2.BackColor = Color.Black;

            IWinformThemeElement yellowClassSelector = new SelectorBlock(".yellow", new[] { new Property("BackColor", "Yellow") });
            IWinformThemeElement panel1Selector      = new SelectorBlock("#panel1", new[] { new Property("BackColor", "Blue"), yellowClassSelector });
            var theme = new Theme(new[] { panel1Selector });

            theme.ApplyTheme(form);
            Assert.Equal(Color.Black, form.BackColor);
            Assert.Equal(Color.Black, form.button1.BackColor);
            Assert.Equal(Color.Yellow, form.button2.BackColor);
            Assert.Equal(Color.Blue, form.panel1.BackColor);
        }
Esempio n. 5
0
        public void ThemeWithInlude()
        {
            IWinformThemeElement variables = new VariableBlock(new[] { new Property("colorOfSky", "Blue") });
            IWinformThemeElement selector  = new SelectorBlock("Form", new[] { new Property("BackColor", "@colorOfSky") });
            var theme  = new Theme(new[] { variables, selector });
            var theme2 = new Theme(new[] { new SelectorBlock("Button", new[] { new Property("BackColor", "Yellow") }) });

            theme.IncludeThemes.Add(theme2);
            var form = new TestForm {
                BackColor = Color.Black
            };

            form.button1.BackColor = Color.Black;
            form.button2.BackColor = Color.Black;

            theme.ApplyTheme(form);

            Assert.Equal(Color.Blue, form.BackColor);
            Assert.Equal(Color.Yellow, form.button1.BackColor);
            Assert.Equal(Color.Yellow, form.button2.BackColor);
        }
Esempio n. 6
0
        protected string HandleSelector(SelectorBlock selector, IElement dom, Dictionary <string, object> variables = null, bool required = true)
        {
            if (selector.Text != null)
            {
                return(ApplyFilters(ApplyGoTemplateText(selector.Text, variables), selector.Filters, variables));
            }

            var    selection = dom;
            string value     = null;

            if (selector.Selector != null)
            {
                if (dom.Matches(selector.Selector))
                {
                    selection = dom;
                }
                else
                {
                    selection = QuerySelector(dom, selector.Selector);
                }

                if (selection == null)
                {
                    if (required)
                    {
                        throw new Exception(string.Format("Selector \"{0}\" didn't match {1}", selector.Selector, dom.ToHtmlPretty()));
                    }

                    return(null);
                }
            }

            if (selector.Remove != null)
            {
                foreach (var i in selection.QuerySelectorAll(selector.Remove))
                {
                    i.Remove();
                }
            }

            if (selector.Case != null)
            {
                foreach (var @case in selector.Case)
                {
                    if (selection.Matches(@case.Key) || QuerySelector(selection, @case.Key) != null)
                    {
                        value = @case.Value;
                        break;
                    }
                }

                if (value == null)
                {
                    if (required)
                    {
                        throw new Exception(string.Format("None of the case selectors \"{0}\" matched {1}", string.Join(",", selector.Case), selection.ToHtmlPretty()));
                    }

                    return(null);
                }
            }
            else if (selector.Attribute != null)
            {
                value = selection.GetAttribute(selector.Attribute);
                if (value == null)
                {
                    if (required)
                    {
                        throw new Exception(string.Format("Attribute \"{0}\" is not set for element {1}", selector.Attribute, selection.ToHtmlPretty()));
                    }

                    return(null);
                }
            }
            else
            {
                value = selection.TextContent;
            }

            return(ApplyFilters(value.Trim(), selector.Filters, variables));
        }