/// <summary>
        /// Initializes a new instance of the <see cref="ScriptRegistrar"/> class.
        /// </summary>
        /// <param name="scripts">The scripts.</param>
        /// <param name="scriptableComponents">The scriptable components.</param>
        /// <param name="viewContext">The view context.</param>
        /// <param name="assetItemMerger">The asset merger.</param>
        /// <param name="scriptWrapper">The script wrapper.</param>
        public ScriptRegistrar(WebAssetItemCollection scripts, IList<IScriptableComponent> scriptableComponents, ViewContext viewContext, IWebAssetItemMerger assetItemMerger, ScriptWrapperBase scriptWrapper)
        {
            Guard.IsNotNull(scripts, "scripts");
            Guard.IsNotNull(scriptableComponents, "scriptableComponents");
            Guard.IsNotNull(viewContext, "viewContext");
            Guard.IsNotNull(assetItemMerger, "assetItemMerger");
            Guard.IsNotNull(scriptWrapper, "scriptWrapper");

            if (viewContext.HttpContext.Items[Key] != null)
            {
                throw new InvalidOperationException(Resources.TextResource.OnlyOneScriptRegistrarIsAllowedInASingleRequest);
            }

            viewContext.HttpContext.Items[Key] = this;

            DefaultGroup = new WebAssetItemGroup("default") { DefaultPath = FrameworkScriptPath };
            Scripts = scripts;
            this.scriptableComponents = scriptableComponents;
            ViewContext = viewContext;
            AssetMerger = assetItemMerger;
            ScriptWrapper = scriptWrapper;
            AssetHandlerPath = WebAssetHttpHandler.DefaultPath;

            OnDocumentReadyActions = new List<Action>();
            OnWindowUnloadActions = new List<Action>();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="StyleSheetRegistrar"/> class.
        /// </summary>
        /// <param name="styleSheets">The style sheets.</param>
        /// <param name="viewContext">The view context.</param>
        /// <param name="assetItemMerger">The asset merger.</param>
        public StyleSheetRegistrar(WebAssetItemCollection styleSheets, ViewContext viewContext, IWebAssetItemMerger assetItemMerger)
        {
            // If the instance object is null.
            if (styleSheets == null)
            {
                throw new System.ArgumentNullException("styleSheets");
            }
            if (viewContext == null)
            {
                throw new System.ArgumentNullException("viewContext");
            }
            if (assetItemMerger == null)
            {
                throw new System.ArgumentNullException("assetItemMerger");
            }

            if (viewContext.HttpContext.Items[Key] != null)
            {
                throw new InvalidOperationException("Only one style sheet registrar is allowed in a single request");
            }

            viewContext.HttpContext.Items[Key] = this;

            DefaultGroup = new WebAssetItemGroup("default", false)
            {
                DefaultPath = WebAssetDefaultSettings.StyleSheetFilesPath
            };
            StyleSheets = styleSheets;
            ViewContext = viewContext;
            AssetMerger = assetItemMerger;

            AssetHandlerPath = WebAssetHttpHandler.DefaultPath;
        }
        public void Setting_duplicate_group_should_throw_exception()
        {
            _collection.Add(new WebAssetItemGroup("group1") { DefaultPath = WebAssetDefaultSettings.ScriptFilesPath });
            _collection.Add(new WebAssetItemGroup("group2") { DefaultPath = WebAssetDefaultSettings.ScriptFilesPath });

            Assert.Throws<ArgumentException>(() => _collection[1] = new WebAssetItemGroup("group1") { DefaultPath = WebAssetDefaultSettings.ScriptFilesPath });
        }
Exemple #4
0
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="viewContext">The current Mvc view context.</param>
        /// <param name="clientSideObjectWriterFactory">The client side object writer factory.</param>
        public StyleSheet(ViewContext viewContext, IClientSideObjectWriterFactory clientSideObjectWriterFactory)
        {
            // Get the current context.
            ViewContext = viewContext;
            ClientSideObjectWriterFactory = clientSideObjectWriterFactory;

            // Create a new instance of the web asset item group manager.
            StyleSheets = new WebAssetItemGroup("default")
            {
                DefaultPath = WebAssetDefaultSettings.StyleSheetFilesPath
            };
        }
        /// <summary>
        /// Stores the specified asset group.
        /// </summary>
        /// <param name="contentType">Type of the content.</param>
        /// <param name="assetGroup">The asset group.</param>
        /// <returns></returns>
        public string Store(string contentType, WebAssetItemGroup assetGroup)
        {
            Guard.IsNotNullOrEmpty(contentType, "contentType");
            Guard.IsNotNull(assetGroup, "assetGroup");

            MergedAsset mergedAsset = CreateMergedAssetWith(contentType, assetGroup);
            string id = assetGroup.IsShared ? assetGroup.Name : CreateIdFrom(mergedAsset);

            EnsureAsset(mergedAsset, id);

            return id;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ScriptRegistrar"/> class.
        /// </summary>
        /// <param name="scripts">The scripts.</param>
        /// <param name="scriptableComponents">The scriptable components.</param>
        /// <param name="viewContext">The view context.</param>
        /// <param name="assetItemMerger">The asset merger.</param>
        /// <param name="scriptWrapper">The script wrapper.</param>
        public ScriptRegistrar(WebAssetItemCollection scripts, IList <IScriptableComponent> scriptableComponents,
                               ViewContext viewContext, IWebAssetItemMerger assetItemMerger, ScriptWrapperBase scriptWrapper)
        {
            // If the instance object is null.
            if (scripts == null)
            {
                throw new System.ArgumentNullException("scripts");
            }
            if (scriptableComponents == null)
            {
                throw new System.ArgumentNullException("scriptableComponents");
            }
            if (viewContext == null)
            {
                throw new System.ArgumentNullException("viewContext");
            }
            if (assetItemMerger == null)
            {
                throw new System.ArgumentNullException("assetItemMerger");
            }
            if (scriptWrapper == null)
            {
                throw new System.ArgumentNullException("scriptWrapper");
            }

            if (viewContext.HttpContext.Items[Key] != null)
            {
                throw new InvalidOperationException("Only one script registrar is allowed in a single request");
            }

            viewContext.HttpContext.Items[Key] = this;

            DefaultGroup = new WebAssetItemGroup("default", false)
            {
                DefaultPath = WebAssetDefaultSettings.ScriptFilesPath
            };
            Scripts = scripts;
            Scripts.Insert(0, DefaultGroup);

            this.scriptableComponents = scriptableComponents;
            ViewContext      = viewContext;
            AssetMerger      = assetItemMerger;
            ScriptWrapper    = scriptWrapper;
            AssetHandlerPath = WebAssetHttpHandler.DefaultPath;

            OnDocumentReadyActions    = new List <Action>();
            OnDocumentReadyStatements = new List <string>();
            OnWindowUnloadActions     = new List <Action>();
            OnWindowUnloadStatements  = new List <string>();
        }
        /// <summary>
        /// Executes the provided delegate that is used to add the group fluently with the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="configureAction">The configure action.</param>
        /// <returns></returns>
        public virtual WebAssetItemCollectionBuilder AddGroup(string name, Action<WebAssetItemGroupBuilder> configureAction)
        {
            Guard.IsNotNullOrEmpty(name, "name");
            Guard.IsNotNull(configureAction, "configureAction");

            WebAssetItemGroup itemGroup = assets.FindGroupByName(name);

            if (itemGroup != null)
            {
                throw new ArgumentException(Resources.TextResource.GroupWithSpecifiedNameAlreadyExistsPleaseSpecifyADifferentName.FormatWith(name));
            }

            itemGroup = new WebAssetItemGroup(name) { DefaultPath = assets.DefaultPath };
            assets.Add(itemGroup);

            WebAssetItemGroupBuilder builder = new WebAssetItemGroupBuilder(itemGroup);
            configureAction(builder);

            return this;
        }
        /// <summary>
        /// Adds the group.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="configureAction">The configure action.</param>
        /// <returns></returns>
        public virtual SharedWebAssetGroupBuilder AddGroup(string name, Action<WebAssetItemGroupBuilder> configureAction)
        {
            Guard.IsNotNullOrEmpty(name, "name");
            Guard.IsNotNull(configureAction, "configureAction");

            WebAssetItemGroup group;

            if (assets.TryGetValue(name, out group))
            {
                throw new ArgumentException(TextResource.GroupWithSpecifiedNameAlreadyExistsPleaseSpecifyADifferentName.FormatWith(name));
            }

            group = new WebAssetItemGroup(name, true) { DefaultPath = defaultPath };
            assets.Add(name, group);

            WebAssetItemGroupBuilder builder = new WebAssetItemGroupBuilder(group);
            configureAction(builder);

            return this;
        }
Exemple #9
0
        public Editor(ViewContext viewContext, IClientSideObjectWriterFactory clientSideObjectWriterFactory)
            : base(viewContext, clientSideObjectWriterFactory)
        {
            ScriptFileNames.AddRange(new[] {
                "telerik.common.js",
                "telerik.list.js",
                "telerik.combobox.js",
                "telerik.draganddrop.js",
                "telerik.window.js",
                "telerik.editor.js"
            });

            DefaultToolGroup = new EditorToolGroup();

            ClientEvents = new EditorClientEvents();

            StyleSheets = new WebAssetItemGroup("default", false) { DefaultPath = WebAssetDefaultSettings.StyleSheetFilesPath };

            Localization = new EditorLocalization();

            Template = new HtmlTemplate();

            new EditorToolFactory(DefaultToolGroup)
                .Bold().Italic().Underline().Strikethrough()
                .Separator()
                .FontName()
                .FontSize()
                .FontColor().BackColor()
                .Separator()
                .JustifyLeft().JustifyCenter().JustifyRight().JustifyFull()
                .Separator()
                .InsertUnorderedList().InsertOrderedList()
                .Separator()
                .Outdent().Indent()
                .Separator()
                .FormatBlock()
                .Separator()
                .CreateLink().Unlink()
                .Separator()
                .InsertImage();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="StyleSheetRegistrar"/> class.
        /// </summary>
        /// <param name="styleSheets">The style sheets.</param>
        /// <param name="viewContext">The view context.</param>
        /// <param name="assetItemMerger">The asset merger.</param>
        public StyleSheetRegistrar(WebAssetItemCollection styleSheets, ViewContext viewContext, IWebAssetItemMerger assetItemMerger)
        {
            Guard.IsNotNull(styleSheets, "styleSheets");
            Guard.IsNotNull(viewContext, "viewContext");
            Guard.IsNotNull(assetItemMerger, "assetItemMerger");

            if (viewContext.HttpContext.Items[Key] != null)
            {
                throw new InvalidOperationException(Resources.TextResource.OnlyOneStyleSheetRegistrarIsAllowedInASingleRequest);
            }

            viewContext.HttpContext.Items[Key] = this;

            DefaultGroup = new WebAssetItemGroup("default", false) { DefaultPath = WebAssetDefaultSettings.StyleSheetFilesPath };
            StyleSheets = styleSheets;
            styleSheets.Insert(0, DefaultGroup);

            ViewContext = viewContext;
            AssetMerger = assetItemMerger;

            AssetHandlerPath = WebAssetHttpHandler.DefaultPath;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="WebAssetItemGroupBuilder"/> class.
        /// </summary>
        /// <param name="assetItemGroup">The asset item group.</param>
        public WebAssetItemGroupBuilder(WebAssetItemGroup assetItemGroup)
        {
            Guard.IsNotNull(assetItemGroup, "assetItemGroup");

            this.assetItemGroup = assetItemGroup;
        }
 public WebAssetItemGroupTests()
 {
     _assetItemGroup = new WebAssetItemGroup("Dummy") { DefaultPath = WebAssetDefaultSettings.ScriptFilesPath };
 }
        private void ProcessGroup(WebAssetItemGroup group, string contentType, string assetHandlerPath, IList<string> urls)
        {
            if (group.Combined)
            {
                var fullUrls = FilterFullUrls(group);
                foreach (string fullUrl in fullUrls)
                {
                    if (!urls.Contains(fullUrl, StringComparer.OrdinalIgnoreCase))
                    {
                        urls.Add(fullUrl);
                    }
                }

                string id = assetRegistry.Store(contentType, group);
                string virtualPath = "{0}?{1}={2}".FormatWith(assetHandlerPath, urlEncoder.Encode(WebAssetHttpHandler.IdParameterName), urlEncoder.Encode(id));
                string relativePath = urlResolver.Resolve(virtualPath);

                if (!urls.Contains(relativePath, StringComparer.OrdinalIgnoreCase))
                {
                    urls.Add(relativePath);
                }
            }
            else
            {
                group.Items.Each(i =>
                {
                    if (!urls.Contains(i.Source, StringComparer.OrdinalIgnoreCase))
                    {
                        urls.Add(Resolve(i.Source, group.Version));
                    }
                });
            }
        }
        public IList<string> MergeGroup(string contentType, string assetHandlerPath, bool isSecured, bool canCompress, WebAssetItemGroup group)
        {
            Guard.IsNotNullOrEmpty(contentType, "contentType");
            Guard.IsNotNullOrEmpty(assetHandlerPath, "assetHandlerPath");
            Guard.IsNotNull(group, "group");

            IList<string> mergedList = new List<string>();

            WebAssetItemGroup itemGroup = group;

            if (itemGroup != null)
            {
                if (!itemGroup.Enabled)
                {
                    return null;
                }

                if (!string.IsNullOrEmpty(itemGroup.ContentDeliveryNetworkUrl))
                {
                    mergedList.Add(itemGroup.ContentDeliveryNetworkUrl);
                }
                else
                {
                    WebAssetItemGroup frameworkGroup = null;

                    if (itemGroup.UseTelerikContentDeliveryNetwork)
                    {
                        frameworkGroup = FilterFrameworkGroup(itemGroup);
                    }

                    if ((frameworkGroup != null) && !frameworkGroup.Items.IsEmpty())
                    {
                        ProcessGroup(frameworkGroup, contentType, assetHandlerPath, mergedList);
                    }

                    if (itemGroup.UseTelerikContentDeliveryNetwork)
                    {
                        var nativeFiles = FilterNativeFiles(itemGroup);
                        foreach (string nativefile in nativeFiles)
                        {
                            string fullUrl = GetNativeFileCdnUrl(nativefile, isSecured, canCompress);

                            if (!mergedList.Contains(fullUrl, StringComparer.OrdinalIgnoreCase))
                            {
                                mergedList.Add(fullUrl);
                            }
                        }
                    }

                    if (!itemGroup.Items.IsEmpty())
                    {
                        ProcessGroup(itemGroup, contentType, assetHandlerPath, mergedList);
                    }
                }
            }

            return mergedList.ToList();
        }
        private static IEnumerable<string> FilterFullUrls(WebAssetItemGroup itemGroup)
        {
            List<string> fullUrls = new List<string>();

            for (int i = itemGroup.Items.Count - 1; i >= 0; i--)
            {
                WebAssetItem item = itemGroup.Items[i];

                if (item.Source.IndexOf("://") > -1)
                {
                    fullUrls.Add(item.Source);
                    itemGroup.Items.RemoveAt(i);
                }
            }

            fullUrls.Reverse();

            return fullUrls;
        }
        private static IList<string> FilterNativeFiles(WebAssetItemGroup itemGroup)
        {
            List<string> nativeFiles = new List<string>();

            for (int i = itemGroup.Items.Count - 1; i >= 0; i--)
            {
                WebAssetItem item = itemGroup.Items[i];

                if (IsNativeFile(item))
                {
                    nativeFiles.Add(Path.GetFileName(item.Source));
                    itemGroup.Items.RemoveAt(i);
                }
            }

            nativeFiles.Reverse();

            return nativeFiles;
        }
        private static WebAssetItemGroup FilterFrameworkGroup(WebAssetItemGroup itemGroup)
        {
            WebAssetItemGroup frameworkGroup = new WebAssetItemGroup("framework", false) { Combined = itemGroup.Combined, Compress = itemGroup.Compress, CacheDurationInDays = itemGroup.CacheDurationInDays, DefaultPath = itemGroup.DefaultPath, Version = itemGroup.Version, UseTelerikContentDeliveryNetwork = itemGroup.UseTelerikContentDeliveryNetwork, Enabled = itemGroup.Enabled };

            for (int i = itemGroup.Items.Count - 1; i >= 0; i--)
            {
                WebAssetItem item = itemGroup.Items[i];

                string fileName = Path.GetFileName(item.Source);

                if ((!fileName.Equals(ScriptRegistrar.jQuery, StringComparison.OrdinalIgnoreCase)) && (ScriptRegistrar.FrameworkScriptFileNames.Contains(fileName, StringComparer.OrdinalIgnoreCase)))
                {
                    frameworkGroup.Items.Add(new WebAssetItem(item.Source));
                    itemGroup.Items.RemoveAt(i);
                }
            }

            frameworkGroup.Items.Reverse();

            return frameworkGroup;
        }
        /// <summary>
        /// Adds the specified shared group.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <example>
        /// <code lang="CS">
        /// &lt;%= Html.Telerik().ScriptRegistrar()
        ///            .Scripts(scripts => scripts.AddShareGroup("SharedGroup1"))
        /// %&gt;
        /// </code>
        /// </example>
        public virtual WebAssetItemCollectionBuilder AddSharedGroup(string name)
        {
            Guard.IsNotNullOrEmpty(name, "name");

            WebAssetItemGroup group = (assetType == WebAssetType.StyleSheet) ?
                                      SharedWebAssets.FindStyleSheetGroup(name) :
                                      SharedWebAssets.FindScriptGroup(name);

            if (group == null)
            {
                throw new ArgumentException(TextResource.GroupWithSpecifiedNameDoesNotExistInAssetTypeOfSharedWebAssets.FormatWith(name, assetType), "name");
            }

            if (assets.FindGroupByName(name) == null)
            {
                // People might have the same group reference in multiple place.
                // So we will skip it once it is added.

                // throw new ArgumentException(TextResource.LocalGroupWithSpecifiedNameAlreadyExists.FormatWith(name));

                // Add a copy of the shared asset
                WebAssetItemGroup localGroup = new WebAssetItemGroup(group.Name, true)
                                                   {
                                                       DefaultPath = group.DefaultPath,
                                                       UseTelerikContentDeliveryNetwork = group.UseTelerikContentDeliveryNetwork,
                                                       ContentDeliveryNetworkUrl = group.ContentDeliveryNetworkUrl,
                                                       Enabled = group.Enabled,
                                                       Version = group.Version,
                                                       Compress = group.Compress,
                                                       CacheDurationInDays = group.CacheDurationInDays,
                                                       Combined = group.Combined
                                                   };

                foreach (WebAssetItem item in group.Items)
                {
                    localGroup.Items.Add(new WebAssetItem(item.Source));
                }

                assets.Add(localGroup);
            }

            return this;
        }
        private static void LoadGroups(WebAssetItemGroupConfigurationElementCollection source, IDictionary<string, WebAssetItemGroup> destination, string defaultPath, string defaultVersion)
        {
            foreach (WebAssetItemGroupConfigurationElement configurationGroup in source)
            {
                WebAssetItemGroup group = new WebAssetItemGroup(configurationGroup.Name, true)
                                              {
                                                  DefaultPath = !string.IsNullOrEmpty(configurationGroup.DefaultPath) ? configurationGroup.DefaultPath : defaultPath,
                                                  ContentDeliveryNetworkUrl = configurationGroup.ContentDeliveryNetworkUrl,
                                                  Enabled = configurationGroup.Enabled,
                                                  Version = !string.IsNullOrEmpty(configurationGroup.Version) ? configurationGroup.Version : defaultVersion,
                                                  Compress = configurationGroup.Compress,
                                                  CacheDurationInDays = configurationGroup.CacheDurationInDays,

                                                  Combined = configurationGroup.Combined
                                              };

                if (configurationGroup.UseTelerikContentDeliveryNetwork.HasValue)
                {
                    group.UseTelerikContentDeliveryNetwork = configurationGroup.UseTelerikContentDeliveryNetwork.Value;
                }

                foreach (WebAssetItemConfigurationElement configurationItem in configurationGroup.Items)
                {
                    string itemSource = configurationItem.Source.StartsWith("~/", StringComparison.OrdinalIgnoreCase) ?
                                        configurationItem.Source :
                                        PathHelper.CombinePath(group.DefaultPath, configurationItem.Source);

                    group.Items.Add(new WebAssetItem(itemSource));
                }

                destination.Add(group.Name, group);
            }
        }
        private static MergedAsset CreateMergedAssetWith(string contentType, WebAssetItemGroup assetGroup)
        {
            Func<string, string> getDirectory = path => path.Substring(2, path.LastIndexOf("/", StringComparison.Ordinal) - 2);
            Func<string, string> getFile = path => path.Substring(path.LastIndexOf("/", StringComparison.Ordinal) + 1);

            MergedAsset asset = new MergedAsset
                                    {
                                        ContentType = contentType,
                                        Version = assetGroup.Version,
                                        Compress = assetGroup.Compress,
                                        CacheDurationInDays = assetGroup.CacheDurationInDays
                                    };

            IEnumerable<string> directories = assetGroup.Items.Select(item => getDirectory(item.Source)).Distinct(StringComparer.OrdinalIgnoreCase);

            directories.Each(directory => asset.Directories.Add(new MergedAssetDirectory { Path = directory }));

            for (int i = 0; i < assetGroup.Items.Count; i++)
            {
                string item = assetGroup.Items[i].Source;
                string directory = getDirectory(item);
                string file = getFile(item);

                MergedAssetDirectory assetDirectory = asset.Directories.Single(d => d.Path.IsCaseInsensitiveEqual(directory));

                assetDirectory.Files.Add(new MergedAssetFile { Order = i, Name = file });
            }

            return asset;
        }
 public WebAssetItemGroupBuilderTests()
 {
     _itemGroup = new WebAssetItemGroup("foo") { DefaultPath = WebAssetDefaultSettings.ScriptFilesPath };
     _builder = new WebAssetItemGroupBuilder(_itemGroup);
 }