コード例 #1
0
ファイル: ShapeTracingFactory.cs プロジェクト: Higea/Orchard
        public void Created(ShapeCreatedContext context) {
            if(!IsActivable()) {
                return;
            }

            // prevent reentrance as some methods could create new shapes, and trigger this event
            if(_processing) {
                return;
            }

            _processing = true;

            if (context.ShapeType != "Layout"
                && context.ShapeType != "DocumentZone"
                && context.ShapeType != "PlaceChildContent"
                && context.ShapeType != "ContentZone"
                && context.ShapeType != "ShapeTracingMeta"
                && context.ShapeType != "ShapeTracingTemplates"
                && context.ShapeType != "DateTimeRelative") {

                var shapeMetadata = (ShapeMetadata)context.Shape.Metadata;
                var currentTheme = _workContext.CurrentTheme;
                var shapeTable = _shapeTableManager.GetShapeTable(currentTheme.Id);

                if (!shapeTable.Descriptors.ContainsKey(shapeMetadata.Type)) {
                    _processing = false;
                    return;
                }

                shapeMetadata.Wrappers.Add("ShapeTracingWrapper");
                shapeMetadata.OnDisplaying(OnDisplaying);
            }

            _processing = false;
        }
コード例 #2
0
        public void Created(ShapeCreatedContext context) {
            if(!IsActivable()) {
                return;
            }

            if (context.ShapeType != "Layout"
                && context.ShapeType != "DocumentZone"
                && context.ShapeType != "PlaceChildContent"
                && context.ShapeType != "ContentZone"
                && context.ShapeType != "ShapeTracingMeta"
                && context.ShapeType != "ShapeTracingTemplates"
                && context.ShapeType != "DateTimeRelative") {

                var shapeMetadata = (ShapeMetadata)context.Shape.Metadata;
                var currentTheme = _themeManager.GetRequestTheme(_workContext.HttpContext.Request.RequestContext);
                var shapeTable = _shapeTableManager.GetShapeTable(currentTheme.Id);

                if (!shapeTable.Descriptors.ContainsKey(shapeMetadata.Type)) {
                    return;
                }

                shapeMetadata.Wrappers.Add("ShapeTracingWrapper");
                shapeMetadata.OnDisplaying(OnDisplaying);
            }
        }
コード例 #3
0
        public override void Created(ShapeCreatedContext context) {

            IShape shape = context.Shape;
            var shapeMetaData = shape.Metadata;

            if (_urlAlternates.Value == null || !_urlAlternates.Value.Any()) {
                return;
            }

            // prevent applying alternate again, c.f. http://orchard.codeplex.com/workitem/18298
            if (shapeMetaData.Alternates.Any(x => x.Contains("__url__"))) {
                return;
            }

            // appends Url alternates to current ones
            shapeMetaData.Alternates = shapeMetaData.Alternates.SelectMany(
                alternate => new [] { alternate }.Union(_urlAlternates.Value.Select(a => alternate + "__url__" + a))
                ).ToList();

            // appends [ShapeType]__url__[Url] alternates
            shapeMetaData.Alternates = _urlAlternates.Value.Select(url => shapeMetaData.Type + "__url__" + url)
                .Union(shapeMetaData.Alternates)
                .ToList();
        }
コード例 #4
0
        public IShape Create(string shapeType, INamedEnumerable<object> parameters, Func<dynamic> createShape)
        {
            var defaultShapeTable = _shapeTableManager.GetShapeTable(null);
            ShapeDescriptor shapeDescriptor;
            defaultShapeTable.Descriptors.TryGetValue(shapeType, out shapeDescriptor);

            parameters = parameters ?? Arguments.Empty();

            var creatingContext = new ShapeCreatingContext {
                New = this,
                ShapeFactory = this,
                ShapeType = shapeType,
                OnCreated = new List<Action<ShapeCreatedContext>>()
            };

            IEnumerable<object> positional = parameters.Positional.ToList();
            var baseType = positional.FirstOrDefault() as Type;

            if (baseType == null) {
                // default to common base class
                creatingContext.Create = createShape ?? (() => new Shape());
            }
            else {
                // consume the first argument
                positional = positional.Skip(1);
                creatingContext.Create = () => Activator.CreateInstance(baseType);
            }

            // "creating" events may add behaviors and alter base type)
            foreach (var ev in _events) {
                ev.Creating(creatingContext);
            }
            if (shapeDescriptor != null) {
                foreach (var ev in shapeDescriptor.Creating) {
                    ev(creatingContext);
                }
            }

            // create the new instance
            var createdContext = new ShapeCreatedContext {
                New = creatingContext.New,
                ShapeType = creatingContext.ShapeType,
                Shape = creatingContext.Create()
            };

            if (!(createdContext.Shape is IShape)) {
                throw new InvalidOperationException("Invalid base type for shape: " + createdContext.Shape.GetType().ToString());
            }

            ShapeMetadata shapeMetadata = createdContext.Shape.Metadata;
            createdContext.Shape.Metadata.Type = shapeType;

            if (shapeDescriptor != null)
                shapeMetadata.Wrappers = shapeMetadata.Wrappers.Concat(shapeDescriptor.Wrappers).ToList();

            // "created" events provides default values and new object initialization
            foreach (var ev in _events) {
                ev.Created(createdContext);
            }
            if (shapeDescriptor != null) {
                foreach (var ev in shapeDescriptor.Created) {
                    ev(createdContext);
                }
            }
            foreach (var ev in creatingContext.OnCreated) {
                ev(createdContext);
            }

            // other properties passed with call overlay any defaults, so are after the created events

            // only one non-Type, non-named argument is allowed
            var initializer = positional.SingleOrDefault();
            if (initializer != null) {
                foreach (var prop in initializer.GetType().GetProperties()) {
                    createdContext.Shape[prop.Name] = prop.GetValue(initializer, null);
                }
            }

            foreach (var kv in parameters.Named) {
                createdContext.Shape[kv.Key] = kv.Value;
            }

            return createdContext.Shape;
        }
コード例 #5
0
        public IShape Create(string shapeType, INamedEnumerable <object> parameters, IEnumerable <IClayBehavior> behaviors)
        {
            var             defaultShapeTable = _shapeTableLocator.Value.Lookup(null);
            ShapeDescriptor shapeDescriptor;

            defaultShapeTable.Descriptors.TryGetValue(shapeType, out shapeDescriptor);

            var creatingContext = new ShapeCreatingContext {
                New          = this,
                ShapeFactory = this,
                ShapeType    = shapeType,
                OnCreated    = new List <Action <ShapeCreatedContext> >()
            };
            var positional = parameters.Positional;

            creatingContext.BaseType = positional.Take(1).OfType <Type>().SingleOrDefault();
            if (creatingContext.BaseType == null)
            {
                // default to common base class
                creatingContext.BaseType = typeof(Shape);
            }
            else
            {
                // consume the first argument
                positional = positional.Skip(1);
            }

            if (creatingContext.BaseType == typeof(Array))
            {
                // array is a hint - not an intended base class
                creatingContext.BaseType  = typeof(Shape);
                creatingContext.Behaviors = new List <IClayBehavior> {
                    new ClaySharp.Behaviors.InterfaceProxyBehavior(),
                    new ClaySharp.Behaviors.PropBehavior(),
                    new ClaySharp.Behaviors.ArrayBehavior(),
                    new ClaySharp.Behaviors.NilResultBehavior(),
                };
            }
            else
            {
                creatingContext.Behaviors = new List <IClayBehavior> {
                    new ClaySharp.Behaviors.InterfaceProxyBehavior(),
                    new ClaySharp.Behaviors.PropBehavior(),
                    new ClaySharp.Behaviors.NilResultBehavior(),
                    new Shape.ShapeBehavior(),
                };
            }

            if (behaviors != null && behaviors.Any())
            {
                // include behaviors passed in by caller, if any
                creatingContext.Behaviors = creatingContext.Behaviors.Concat(behaviors).ToList();
            }

            // "creating" events may add behaviors and alter base type)
            foreach (var ev in _events)
            {
                ev.Value.Creating(creatingContext);
            }
            if (shapeDescriptor != null)
            {
                foreach (var ev in shapeDescriptor.Creating)
                {
                    ev(creatingContext);
                }
            }

            // create the new instance
            var createdContext = new ShapeCreatedContext {
                New       = creatingContext.New,
                ShapeType = creatingContext.ShapeType,
                Shape     = ClayActivator.CreateInstance(creatingContext.BaseType, creatingContext.Behaviors)
            };
            var shapeMetadata = new ShapeMetadata {
                Type = shapeType
            };

            createdContext.Shape.Metadata = shapeMetadata;

            if (shapeDescriptor != null)
            {
                shapeMetadata.Wrappers = shapeMetadata.Wrappers.Concat(shapeDescriptor.Wrappers).ToList();
            }

            // "created" events provides default values and new object initialization
            foreach (var ev in _events)
            {
                ev.Value.Created(createdContext);
            }
            if (shapeDescriptor != null)
            {
                foreach (var ev in shapeDescriptor.Created)
                {
                    ev(createdContext);
                }
            }
            foreach (var ev in creatingContext.OnCreated)
            {
                ev(createdContext);
            }


            // other properties passed with call overlay any defaults, so are after the created events

            // only one non-Type, non-named argument is allowed
            var initializer = positional.SingleOrDefault();

            if (initializer != null)
            {
                foreach (var prop in initializer.GetType().GetProperties())
                {
                    createdContext.Shape[prop.Name] = prop.GetValue(initializer, null);
                }
            }

            foreach (var kv in parameters.Named)
            {
                createdContext.Shape[kv.Key] = kv.Value;
            }

            return(createdContext.Shape);
        }
コード例 #6
0
        public IShape Create(string shapeType, Func <dynamic> shapeFactory)
        {
            ShapeDescriptor shapeDescriptor;

            GetShapeTable().Descriptors.TryGetValue(shapeType, out shapeDescriptor);

            var creatingContext = new ShapeCreatingContext
            {
                New          = this,
                ShapeFactory = this,
                ShapeType    = shapeType,
                OnCreated    = new List <Action <ShapeCreatedContext> >()
            };

            creatingContext.Create = shapeFactory;

            // "creating" events may add behaviors and alter base type
            foreach (var ev in _events)
            {
                ev.Creating(creatingContext);
            }

            if (shapeDescriptor != null)
            {
                foreach (var ev in shapeDescriptor.Creating)
                {
                    ev(creatingContext);
                }
            }

            // create the new instance
            var createdContext = new ShapeCreatedContext
            {
                New          = creatingContext.New,
                ShapeFactory = creatingContext.ShapeFactory,
                ShapeType    = creatingContext.ShapeType,
                Shape        = creatingContext.Create()
            };

            var shape = createdContext.Shape as IShape;

            if (shape == null)
            {
                throw new InvalidOperationException("Invalid base type for shape: " + createdContext.Shape.GetType().ToString());
            }

            if (shape.Metadata == null)
            {
                shape.Metadata = new ShapeMetadata();
            }

            ShapeMetadata shapeMetadata = shape.Metadata;

            shape.Metadata.Type = shapeType;

            // Concatenate wrappers if there are any
            if (shapeDescriptor != null && shapeMetadata.Wrappers.Count + shapeDescriptor.Wrappers.Count > 0)
            {
                shapeMetadata.Wrappers = shapeMetadata.Wrappers.Concat(shapeDescriptor.Wrappers).ToList();
            }

            // "created" events provides default values and new object initialization
            foreach (var ev in _events)
            {
                ev.Created(createdContext);
            }

            if (shapeDescriptor != null)
            {
                foreach (var ev in shapeDescriptor.Created)
                {
                    ev(createdContext);
                }
            }

            if (creatingContext.OnCreated != null)
            {
                foreach (var ev in creatingContext.OnCreated)
                {
                    ev(createdContext);
                }
            }

            return(createdContext.Shape);
        }
コード例 #7
0
        public IShape Create(string shapeType, INamedEnumerable <object> parameters, Func <dynamic> createShape)
        {
            var             defaultShapeTable = _shapeTableLocator.Value.Lookup(null);
            ShapeDescriptor shapeDescriptor;

            defaultShapeTable.Descriptors.TryGetValue(shapeType, out shapeDescriptor);

            parameters = parameters ?? Arguments.Empty();

            var creatingContext = new ShapeCreatingContext {
                New          = this,
                ShapeFactory = this,
                ShapeType    = shapeType,
                OnCreated    = new List <Action <ShapeCreatedContext> >()
            };

            IEnumerable <object> positional = parameters.Positional.ToList();
            var baseType = positional.FirstOrDefault() as Type;

            if (baseType == null)
            {
                // default to common base class
                creatingContext.Create = createShape ?? (() => new Shape());
            }
            else
            {
                // consume the first argument
                positional             = positional.Skip(1);
                creatingContext.Create = () => Activator.CreateInstance(baseType);
            }

            // "creating" events may add behaviors and alter base type)
            foreach (var ev in _events)
            {
                ev.Value.Creating(creatingContext);
            }
            if (shapeDescriptor != null)
            {
                foreach (var ev in shapeDescriptor.Creating)
                {
                    ev(creatingContext);
                }
            }

            // create the new instance
            var createdContext = new ShapeCreatedContext {
                New       = creatingContext.New,
                ShapeType = creatingContext.ShapeType,
                Shape     = creatingContext.Create()
            };

            if (!(createdContext.Shape is IShape))
            {
                throw new InvalidOperationException("Invalid base type for shape: " + createdContext.Shape.GetType().ToString());
            }

            ShapeMetadata shapeMetadata = createdContext.Shape.Metadata;

            createdContext.Shape.Metadata.Type = shapeType;

            if (shapeDescriptor != null)
            {
                shapeMetadata.Wrappers = shapeMetadata.Wrappers.Concat(shapeDescriptor.Wrappers).ToList();
            }

            // "created" events provides default values and new object initialization
            foreach (var ev in _events)
            {
                ev.Value.Created(createdContext);
            }
            if (shapeDescriptor != null)
            {
                foreach (var ev in shapeDescriptor.Created)
                {
                    ev(createdContext);
                }
            }
            foreach (var ev in creatingContext.OnCreated)
            {
                ev(createdContext);
            }


            // other properties passed with call overlay any defaults, so are after the created events

            // only one non-Type, non-named argument is allowed
            var initializer = positional.SingleOrDefault();

            if (initializer != null)
            {
                foreach (var prop in initializer.GetType().GetProperties())
                {
                    createdContext.Shape[prop.Name] = prop.GetValue(initializer, null);
                }
            }

            foreach (var kv in parameters.Named)
            {
                createdContext.Shape[kv.Key] = kv.Value;
            }

            return(createdContext.Shape);
        }
コード例 #8
0
 public virtual void Created(ShapeCreatedContext context)
 {
 }
コード例 #9
0
ファイル: IShapeFactoryEvents.cs プロジェクト: cmacri/Orchard
 public virtual void Created(ShapeCreatedContext context) { }
コード例 #10
0
        public IShape Create(string shapeType, INamedEnumerable<object> parameters, IEnumerable<IClayBehavior> behaviors) {
            var defaultShapeTable = _shapeTableLocator.Value.Lookup(null);
            ShapeDescriptor shapeDescriptor;
            defaultShapeTable.Descriptors.TryGetValue(shapeType, out shapeDescriptor);

            var creatingContext = new ShapeCreatingContext {
                New = this,
                ShapeFactory = this,
                ShapeType = shapeType,
                OnCreated = new List<Action<ShapeCreatedContext>>()
            };
            var positional = parameters.Positional;

            creatingContext.BaseType = positional.Take(1).OfType<Type>().SingleOrDefault();
            if (creatingContext.BaseType == null) {
                // default to common base class
                creatingContext.BaseType = typeof(Shape);
            }
            else {
                // consume the first argument
                positional = positional.Skip(1);
            }

            if (creatingContext.BaseType == typeof(Array)) {
                // array is a hint - not an intended base class
                creatingContext.BaseType = typeof(Shape);
                creatingContext.Behaviors = new List<IClayBehavior> {
                    new ClaySharp.Behaviors.InterfaceProxyBehavior(),
                    new ClaySharp.Behaviors.PropBehavior(),
                    new ClaySharp.Behaviors.ArrayBehavior(),
                    new ClaySharp.Behaviors.NilResultBehavior(),
                };
            }
            else {
                creatingContext.Behaviors = new List<IClayBehavior> {
                    new ClaySharp.Behaviors.InterfaceProxyBehavior(),
                    new ClaySharp.Behaviors.PropBehavior(),
                    new ClaySharp.Behaviors.NilResultBehavior(),
                    new Shape.ShapeBehavior(),
                };
            }
            
            if (behaviors != null && behaviors.Any()) {
                // include behaviors passed in by caller, if any
                creatingContext.Behaviors = creatingContext.Behaviors.Concat(behaviors).ToList();
            }

            // "creating" events may add behaviors and alter base type)
            foreach (var ev in _events) {
                ev.Value.Creating(creatingContext);
            }
            if (shapeDescriptor != null) {
                foreach (var ev in shapeDescriptor.Creating) {
                    ev(creatingContext);
                }
            }

            // create the new instance
            var createdContext = new ShapeCreatedContext {
                New = creatingContext.New,
                ShapeType = creatingContext.ShapeType,
                Shape = ClayActivator.CreateInstance(creatingContext.BaseType, creatingContext.Behaviors)
            };
            var shapeMetadata = new ShapeMetadata { Type = shapeType };
            createdContext.Shape.Metadata = shapeMetadata;

            if (shapeDescriptor != null)
                shapeMetadata.Wrappers = shapeMetadata.Wrappers.Concat(shapeDescriptor.Wrappers).ToList();

            // "created" events provides default values and new object initialization
            foreach (var ev in _events) {
                ev.Value.Created(createdContext);
            }
            if (shapeDescriptor != null) {
                foreach (var ev in shapeDescriptor.Created) {
                    ev(createdContext);
                }
            }
            foreach (var ev in creatingContext.OnCreated) {
                ev(createdContext);
            }


            // other properties passed with call overlay any defaults, so are after the created events

            // only one non-Type, non-named argument is allowed
            var initializer = positional.SingleOrDefault();
            if (initializer != null) {
                foreach (var prop in initializer.GetType().GetProperties()) {
                    createdContext.Shape[prop.Name] = prop.GetValue(initializer, null);
                }
            }

            foreach (var kv in parameters.Named) {
                createdContext.Shape[kv.Key] = kv.Value;
            }

            return createdContext.Shape;
        }
コード例 #11
0
 public void Created(ShapeCreatedContext context) {
 }