예제 #1
0
        /// <summary>
        /// set up the file watcher for detecting changes
        /// </summary>
        protected void InitSite()
        {
            if (_host != null && _host.WebSite != null && !String.IsNullOrEmpty(_host.WebSite.Path))
            {
                prefUtility = new PrefUtility()
                {
                    ExtensionName = "OrangeBits",
                    SitePath = _host.WebSite.Path,
                    SitePreferences = _host.WebSite.SitePreferences
                };

                // by default, do not do anything to node_modules
                var path = Path.Combine(_host.WebSite.Path, "node_modules");
                var isSet = prefUtility.PathHasValue(path);
                if (!isSet)
                {
                    var props = typeof(OptionViewModel).GetProperties().Where(x => Attribute.IsDefined(x, typeof(DefaultValueAttribute)));
                    OptionViewModel vm = new OptionViewModel()
                    {
                        Paths = new string[] { path }
                    };

                    foreach (var prop in props)
                    {
                        if (prop.PropertyType == typeof(bool?))
                            prop.SetValue(vm, false, null);
                    }
                    prefUtility.SaveOptions(vm);
                }

                _siteFileWatcher.RegisterForSiteNotifications(WatcherChangeTypes.Changed | WatcherChangeTypes.Created, new FileSystemEventHandler(SourceFileChanged), null);
            }
            else
            {
                _siteFileWatcher.DeregisterForSiteNotifications(WatcherChangeTypes.Changed | WatcherChangeTypes.Created, new FileSystemEventHandler(SourceFileChanged), null);
            }
        }
예제 #2
0
        /// <summary>
        /// save the true/false options for the given set of paths, clearing any previous downstream values
        /// </summary>
        /// <param name="prefs"></param>
        public void SaveOptions(OptionViewModel vm)
        {
            // clear any settings for the current path or paths that are downstream
            var props = typeof(OptionViewModel).GetProperties().Where(x => Attribute.IsDefined(x, typeof(DefaultValueAttribute)));
            if (vm.OverwriteChildSettings)
            {
                foreach (var path in vm.Paths)
                {
                    this.ClearPrefs(path, props);
                }
            }

            // save new settings for each path and property
            foreach (var path in vm.Paths)
            {
                foreach (var prop in props)
                {
                    var key = this.getPathKey(path, prop.Name);
                    var value = prop.GetValue(vm, null);
					if (prop.PropertyType == typeof(bool?))
					{
						if ((value as bool?).HasValue)
						{
							this.SitePreferences.SetValue(key, (value as bool?).Value.ToString());
						}
					}
					else
					{
						if (value != null)
						{
							this.SitePreferences.SetValue(key, value.ToString());
						}
					}
                }
            }
            this.SitePreferences.Save();
        }
예제 #3
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected void AddOptions(ContextMenuOpeningEventArgs e)
        {
            // we only show the context menu if every item selected in the tree is valid to be compiled
            var jobs = new List<OrangeJob>();
            var showMenu = e.Items.Count > 0;
            Type itemType = null;

            // determine if we're dealing with folders or files.  If it's mixed, bug out
            foreach (ISiteItem item in e.Items)
            {
                if (itemType == null)
                {
                    itemType = item.GetType();
                }
                else if (itemType != item.GetType())
                {
                    return;
                }
            }

            var menuItem = new ContextMenuItem("OrangeBits Options", null, new DelegateCommand((items) =>
            {
                var selectedItems = items as IEnumerable<ISiteItem>;
                var dialog = new OptionsUI();
                var vm = new OptionViewModel()
                {
                    Paths = selectedItems.Select(x => (x as ISiteFileSystemItem).Path).ToArray()
                };

                prefUtility.LoadOptions(vm);
                dialog.DataContext = vm;
                var result = _host.ShowDialog("OrangeBits Options", dialog);
                if (result.HasValue && result.Value)
                {
                    prefUtility.SaveOptions(vm);
                }

            }), e.Items);
            e.AddMenuItem(menuItem);
        }
예제 #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="paths"></param>
        /// <param name="prefs"></param>
        public void LoadOptions(OptionViewModel vm)
        {
            // get all of the properties on this class with the DefaultValue attribute
            var props = typeof(OptionViewModel).GetProperties().Where(x => Attribute.IsDefined(x, typeof(DefaultValueAttribute)));
            foreach (var prop in props)
            {
                // what's the default value for this property?
                object def = (prop.GetCustomAttributes(typeof(DefaultValueAttribute), false)[0] as DefaultValueAttribute).Value;
                object firstValue = null;

                foreach (var path in vm.Paths)
                {
                    object prefValue = this.GetPref(path, prop.Name, def);
                    if (firstValue == null)
                        firstValue = prefValue;

                    if (prefValue != firstValue)
                        prop.SetValue(vm, null, null);
                }
                prop.SetValue(vm, firstValue == null ? def : firstValue, null);
            }
        }