Beispiel #1
0
        public ImagePickerImage(IPublishedContent content, ImagePickerConfiguration config)
        {
            int width  = content.Value <int>(Constants.Conventions.Media.Width);
            int height = content.Value <int>(Constants.Conventions.Media.Height);

            Media   = content;
            Width   = width;
            Height  = height;
            CropUrl = content.GetCropUrl(width, height, preferFocalPoint: config.PreferFocalPoint, imageCropMode: config.CropMode);
        }
Beispiel #2
0
        /// <summary>
        /// Converts the intermediate value to a corresponding object value.
        /// </summary>
        /// <param name="owner">The element holding the property type.</param>
        /// <param name="propertyType">The property type.</param>
        /// <param name="referenceCacheLevel">The reference cache level.</param>
        /// <param name="inter">The intermediate value.</param>
        /// <param name="preview">Whether preview mode is enabled.</param>
        /// <returns>The results of the conversion.</returns>
        public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
        {
            // Get the data type configuration
            ImagePickerConfiguration config = propertyType.DataType.ConfigurationAs <ImagePickerConfiguration>();

            // Get the UDIs from the intermediate value
            Udi[] udis = inter as Udi[] ?? Array.Empty <Udi>();

            // Initialize a collection for the items
            List <object> items = new List <object>();

            // Determine the item value type
            Type valueType = propertyType.DataType.ConfigurationAs <ImagePickerConfiguration>()?.ValueType;

            foreach (Udi udi in udis)
            {
                // Look up the media
                IPublishedContent media = _publishedSnapshotAccessor.PublishedSnapshot.Media.GetById(udi);
                if (media == null)
                {
                    continue;
                }

                // If the configuration doesn't specify a value type, we just create a new ImagePickerImage
                if (valueType == null)
                {
                    items.Add(new ImagePickerImage(media, config));
                    continue;
                }

                // If the selected type has a constructor with an ImagePickerConfiguration as the second parameter, we choose that constructor
                if (HasConstructor <IPublishedContent, ImagePickerConfiguration>(valueType))
                {
                    items.Add(Current.Factory.CreateInstance(valueType, media, config));
                    continue;
                }

                items.Add(Current.Factory.CreateInstance(valueType, media));
            }

            // Return the item(s) with the correct value type
            valueType ??= typeof(ImagePickerImage);
            return(config.IsMultiPicker ? items.Cast(valueType).ToList(valueType) : items.FirstOrDefault());
        }