Exemple #1
0
        private void InitializeProperties()
        {
            //if the value is already created, we need to be created and update the collection according to
            //what is now in the content
            if (_properties != null && _properties.IsValueCreated)
            {
                //re-parse it so we can check what properties are different and adjust the event handlers
                var parsed   = StylesheetHelper.ParseRules(Content).ToArray();
                var names    = parsed.Select(x => x.Name).ToArray();
                var existing = _properties.Value.Where(x => names.InvariantContains(x.Name)).ToArray();
                //update existing
                foreach (var stylesheetProperty in existing)
                {
                    var updateFrom = parsed.Single(x => x.Name.InvariantEquals(stylesheetProperty.Name));
                    //remove current event handler while we update, we'll reset it after
                    stylesheetProperty.PropertyChanged -= Property_PropertyChanged;
                    stylesheetProperty.Alias            = updateFrom.Selector;
                    stylesheetProperty.Value            = updateFrom.Styles;
                    //re-add
                    stylesheetProperty.PropertyChanged += Property_PropertyChanged;
                }
                //remove no longer existing
                var nonExisting = _properties.Value.Where(x => names.InvariantContains(x.Name) == false).ToArray();
                foreach (var stylesheetProperty in nonExisting)
                {
                    stylesheetProperty.PropertyChanged -= Property_PropertyChanged;
                    _properties.Value.Remove(stylesheetProperty);
                }
                //add new ones
                var newItems = parsed.Where(x => _properties.Value.Select(p => p.Name).InvariantContains(x.Name) == false);
                foreach (var stylesheetRule in newItems)
                {
                    var prop = new StylesheetProperty(stylesheetRule.Name, stylesheetRule.Selector, stylesheetRule.Styles);
                    prop.PropertyChanged += Property_PropertyChanged;
                    _properties.Value.Add(prop);
                }
            }

            //we haven't read the properties yet so create the lazy delegate
            _properties = new Lazy <List <StylesheetProperty> >(() =>
            {
                var parsed = StylesheetHelper.ParseRules(Content);
                return(parsed.Select(statement =>
                {
                    var property = new StylesheetProperty(statement.Name, statement.Selector, statement.Styles);
                    property.PropertyChanged += Property_PropertyChanged;
                    return property;
                }).ToList());
            });
        }
Exemple #2
0
        /// <summary>
        /// Adds an Umbraco stylesheet property for use in the back office
        /// </summary>
        /// <param name="property"></param>
        public void AddProperty(StylesheetProperty property)
        {
            if (Properties.Any(x => x.Name.InvariantEquals(property.Name)))
            {
                throw new DuplicateNameException("The property with the name " + property.Name + " already exists in the collection");
            }

            //now we need to serialize out the new property collection over-top of the string Content.
            Content = StylesheetHelper.AppendRule(Content, new StylesheetRule
            {
                Name     = property.Name,
                Selector = property.Alias,
                Styles   = property.Value
            });

            //re-set lazy collection
            InitializeProperties();
        }