using System.Web.UI; using System; using CMS.CMSHelper; using CMS.GlobalHelper; using CMS.PortalControls; using CMS.PortalEngine; using CMS.SettingsProvider; public partial class CMSWebParts_General_javascript : CMSAbstractWebPart { #region "Public properties" /// /// Gets or sets the inline JavaScript code. /// public string InlineScript { get { return ValidationHelper.GetString(GetValue("InlineScript"), string.Empty); } set { SetValue("InlineScript", value); } } /// /// Gets or sets the inline JavaScript code page location. /// public string InlineScriptPageLocation { get { return ValidationHelper.GetString(GetValue("InlineScriptPageLocation"), string.Empty); } set { SetValue("InlineScriptPageLocation", value); } } /// /// Indicates whether the script tags are generated or not. /// public bool GenerateScriptTags { get { return ValidationHelper.GetBoolean(GetValue("GenerateScriptTags"), true); } set { SetValue("GenerateScriptTags", value); } } /// /// Include jQuery /// public bool IncludeJQuery { get { return ValidationHelper.GetBoolean(this.GetValue("IncludeJQuery"), false); } set { this.SetValue("IncludeJQuery", value); } } /// /// Gets or sets the linked file url. /// public string LinkedFile { get { return ValidationHelper.GetString(GetValue("LinkedFile"), string.Empty); } set { SetValue("LinkedFile", value); } } /// /// Gets or sets the linked file url page location. /// public string LinkedFilePageLocation { get { return ValidationHelper.GetString(GetValue("LinkedFilePageLocation"), string.Empty); } set { SetValue("LinkedFilePageLocation", value); } } #endregion #region "Page events" /// /// Content loaded event handler. /// public override void OnContentLoaded() { base.OnContentLoaded(); SetupControl(); } /// /// Reloads data for partial caching. /// public override void ReloadData() { base.ReloadData(); SetupControl(); } #endregion #region "Methods" /// /// Initializes the control properties. /// protected void SetupControl() { if (StopProcessing) { // Do nothing } else { } } /// /// Registers the control scripts /// protected void RegisterScripts() { // Include javascript only in live site or preview mode ViewModeEnum viewMode = CMSContext.ViewMode; if (viewMode != ViewModeEnum.Design) { // Register jQuery script if (IncludeJQuery) { ScriptHelper.RegisterJQuery(this.Page); } // Register linked files RegisterLinkedFiles(); // Register inline script RegisterInlineScript(); } } /// /// Registers the inline script /// private void RegisterInlineScript() { // Render the inline script if (InlineScript.Trim() != string.Empty) { string inlineScript = InlineScript; // Check if script tags must be generated if (GenerateScriptTags && (InlineScriptPageLocation.ToLowerCSafe() != "submit")) { inlineScript = ScriptHelper.GetScript(InlineScript); } // Switch for script position on the page switch (InlineScriptPageLocation.ToLowerCSafe()) { case "header": Page.Header.Controls.Add(new LiteralControl(inlineScript)); break; case "beginning": ScriptHelper.RegisterClientScriptBlock(Page, typeof(string), ClientID + "inlinescript", inlineScript); break; case "startup": ScriptHelper.RegisterStartupScript(Page, typeof(string), ClientID + "inlinescript", inlineScript); break; case "submit": ScriptHelper.RegisterOnSubmitStatement(Page, typeof(string), ClientID + "inlinescript", inlineScript); break; case "inline": default: ltlInlineScript.Text = inlineScript; break; } } } /// /// Registers the linked files /// private void RegisterLinkedFiles() { // Create linked JS file if (LinkedFile.Trim() != string.Empty) { string linkedFile = String.Empty; // Register some script files manually, to prevent multiple registration switch (LinkedFile.Trim().ToLowerCSafe()) { case "jquery": if (ScriptHelper.RequestScriptRegistration(ScriptHelper.JQUERY_KEY)) { linkedFile = ScriptHelper.JQUERY_FILENAME; } break; case "prototype": if (ScriptHelper.RequestScriptRegistration(ScriptHelper.PROTOTYPE_KEY)) { linkedFile = ScriptHelper.PROTOTYPE_FILENAME; } break; case "mootools": if (ScriptHelper.RequestScriptRegistration(ScriptHelper.MOOTOOLS_KEY)) { linkedFile = ScriptHelper.MOOTOOLS_FILENAME; } break; case "silverlight": if (ScriptHelper.RequestScriptRegistration(ScriptHelper.SILVERLIGHT_KEY)) { linkedFile = ScriptHelper.SILVERLIGHT_FILENAME; } break; default: { // File URL string file = LinkedFile; string key = ScriptHelper.SCRIPTFILE_PREFIX_KEY + file; if (ScriptHelper.RequestScriptRegistration(key)) { linkedFile = ScriptHelper.GetScriptUrl(file, false); linkedFile = ResolveUrl(linkedFile); } } break; } if (!String.IsNullOrEmpty(linkedFile)) { string script = ScriptHelper.GetIncludeScript(linkedFile); // Switch for script position on the page switch (LinkedFilePageLocation.ToLowerCSafe()) { case "beginning": ScriptHelper.RegisterClientScriptBlock(Page, typeof(string), ClientID + "script", script); break; case "startup": ScriptHelper.RegisterStartupScript(Page, typeof(string), ClientID + "script", script); break; case "header": default: Page.Header.Controls.Add(new LiteralControl(script)); break; } } } } /// /// PreRender event handler /// protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (!StopProcessing) { RegisterScripts(); } } #endregion }