using System; using System.Collections; using System.Web.UI; using CMS.ExtendedControls; using CMS.GlobalHelper; using CMS.UIControls; using CMS.SettingsProvider; public partial class CMSModules_Content_Controls_Dialogs_Properties_BBMediaProperties : ItemProperties { #region "Private properties" /// /// Gets or sets the original URL (the one which come when properties are loaded). /// private string OriginalUrl { get { return ValidationHelper.GetString(ViewState["OriginalUrl"], ""); } set { ViewState["OriginalUrl"] = value; } } /// /// Gets or sets the permanent URL. /// private string PermanentUrl { get { return ValidationHelper.GetString(ViewState["PermanentUrl"], ""); } set { ViewState["PermanentUrl"] = value; } } /// /// Gets or sets the value which determines whether to show or hide the controls in WidthHeightSelector. /// private bool ShowSizeControls { get { return ValidationHelper.GetBoolean(ViewState["ShowSizeControls"], false); } set { ViewState["ShowSizeControls"] = value; } } /// /// Gets or sets the value which determines whether the control is displayed on the Web tab. /// public bool IsWeb { get { return ValidationHelper.GetBoolean(ViewState["IsWeb"], false); } set { ViewState["IsWeb"] = value; } } /// /// Returns the default width of the width height selector. /// private int DefaultWidth { get { return ValidationHelper.GetInteger(ViewState["DefaultHeight"], 0); } set { ViewState["DefaultHeight"] = value; } } /// /// Returns the default height of the width height selector. /// private int DefaultHeight { get { return ValidationHelper.GetInteger(ViewState["DefaultWidth"], 0); } set { ViewState["DefaultWidth"] = value; } } #endregion #region "Public properties" /// /// Indicates whether the URL text box should be hidden. /// public bool HideUrlBox { get { return ValidationHelper.GetBoolean(ViewState["HideUrlBox"], false); } set { ViewState["HideUrlBox"] = value; } } #endregion #region "Page events" protected void Page_Load(object sender, EventArgs e) { if (!StopProcessing) { if (!URLHelper.IsPostback() && IsWeb) { pnlEmpty.Visible = false; pnlTabs.CssClass = "Dialog_Tabs"; } // Refresh button imgRefresh.Click += imgRefresh_Click; imgRefresh.ImageUrl = GetImageUrl("Design/Controls/Dialogs/refresh.png"); imgRefresh.ToolTip = GetString("dialogs.web.refresh"); tabImageGeneral.HeaderText = GetString("general.general"); string postBackRef = ControlsHelper.GetPostBackEventReference(btnHidden, ""); string postBackKeyDownRef = "var keynum;if(window.event){keynum = event.keyCode;}else if(event.which){keynum = event.which;}if(keynum == 13){" + postBackRef + "; return false;}"; string postBackRefTxt = ControlsHelper.GetPostBackEventReference(btnTxtHidden, ""); string postBackKeyDownTxtRef = "var keynum;if(window.event){keynum = event.keyCode;}else if(event.which){keynum = event.which;}if(keynum == 13){" + postBackRefTxt + "; return false;}"; // Assign javascript change event to all fields (to refresh the preview) widthHeightElem.TextBoxesClass = "ShortTextBox"; widthHeightElem.HeightTextBox.Attributes["onkeydown"] = postBackKeyDownRef; widthHeightElem.WidthTextBox.Attributes["onkeydown"] = postBackKeyDownRef; widthHeightElem.ScriptAfterChange = postBackRef; txtUrl.Attributes["onchange"] = postBackRefTxt; txtUrl.Attributes["onkeydown"] = postBackKeyDownTxtRef; btnHidden.Click += btnHidden_Click; btnTxtHidden.Click += btnTxtHidden_Click; btnHiddenSize.Click += btnHiddenSize_Click; widthHeightElem.CustomRefreshCode = ControlsHelper.GetPostBackEventReference(btnHiddenSize, "") + ";return false;"; widthHeightElem.ShowActions = true; if (!URLHelper.IsPostback()) { EditorClientID = QueryHelper.GetString("editor_clientid", ""); widthHeightElem.Locked = true; LoadPreview(); } plcUrlBox.Visible = !HideUrlBox; if (!string.IsNullOrEmpty(NoSelectionText)) { lblEmpty.Text = NoSelectionText; } else { pnlEmpty.Visible = false; } LoadPreview(); } } /// /// Update parameters and preview from URL textbox. /// private void UpdateFromUrl() { int width = ValidationHelper.GetInteger(URLHelper.GetUrlParameter(txtUrl.Text, "width"), 0); int height = ValidationHelper.GetInteger(URLHelper.GetUrlParameter(txtUrl.Text, "height"), 0); // Update WIDTH and HEIGHT information according URL if (width > 0) { widthHeightElem.Width = width; } if (height > 0) { widthHeightElem.Height = height; } pnlUpdateWidthHeight.Update(); LoadPreview(); } protected void imgRefresh_Click(object sender, ImageClickEventArgs e) { UpdateFromUrl(); } protected void btnTxtHidden_Click(object sender, EventArgs e) { UpdateFromUrl(); } protected void btnHiddenSize_Click(object sender, EventArgs e) { widthHeightElem.Width = DefaultWidth; widthHeightElem.Height = DefaultHeight; // Remove width & height parameters from url string url = URLHelper.RemoveParameterFromUrl(URLHelper.RemoveParameterFromUrl(OriginalUrl, "width"), "height"); txtUrl.Text = url; LoadPreview(); } protected void btnHidden_Click(object sender, EventArgs e) { // Update item URL bool getPermanent = ((widthHeightElem.Width <= DefaultWidth) || (widthHeightElem.Height <= DefaultHeight)) && (SourceType == MediaSourceEnum.MediaLibraries); txtUrl.Text = UpdateUrl(widthHeightElem.Width, widthHeightElem.Height, (getPermanent ? PermanentUrl : OriginalUrl)); pnlUpdateImgUrl.Update(); LoadPreview(); } #endregion #region "Private methods" /// /// Returns URL updated according specified properties. /// /// Height of the item /// Width of the item /// URL to update private string UpdateUrl(int width, int height, string url) { return CMSDialogHelper.UpdateUrl(width, height, DefaultWidth, DefaultHeight, url, SourceType); } /// /// Configures the preview control. /// private void LoadPreview() { string url = txtUrl.Text; if (!string.IsNullOrEmpty(url)) { int dotIndex = url.LastIndexOfCSafe('.'); string ext = null; if (dotIndex > 0) { ext = url.Substring(dotIndex); } // Only for non-image extensions add chset to url if (!ImageHelper.IsImage(ext)) { url = URLHelper.UpdateParameterInUrl(url, "chset", Guid.NewGuid().ToString()); // Add latest version requirement for live site if (IsLiveSite) { // Add requirement for latest version of files for current document string newparams = "latestforhistoryid=" + HistoryID; newparams += "&hash=" + ValidationHelper.GetHashString("h" + HistoryID); url += "&" + newparams; } } imagePreview.Visible = true; imagePreview.URL = url; imagePreview.SizeToURL = ValidationHelper.GetBoolean(ViewState[DialogParameters.IMG_SIZETOURL], false); imagePreview.Width = widthHeightElem.Width; imagePreview.Height = widthHeightElem.Height; SaveSession(); } else { imagePreview.Visible = false; } } /// /// Save current properties into session. /// private void SaveSession() { Hashtable savedProperties = SessionHelper.GetValue("DialogSelectedParameters") as Hashtable; if (savedProperties == null) { savedProperties = new Hashtable(); } Hashtable properties = GetItemProperties(); foreach (DictionaryEntry entry in properties) { savedProperties[entry.Key] = entry.Value; } SessionHelper.SetValue("DialogSelectedParameters", savedProperties); } #endregion #region "Overridden methods" public override void LoadSelectedItems(MediaItem item, Hashtable properties) { if (!string.IsNullOrEmpty(item.Url)) { // Display the properties pnlEmpty.Visible = false; pnlTabs.CssClass = "Dialog_Tabs"; widthHeightElem.Width = item.Width; widthHeightElem.Height = item.Height; DefaultWidth = item.Width; DefaultHeight = item.Height; HistoryID = item.HistoryID; if (properties == null) { properties = new Hashtable(); } properties[DialogParameters.IMG_WIDTH] = item.Width; properties[DialogParameters.IMG_HEIGHT] = item.Height; properties[DialogParameters.IMG_ORIGINALWIDTH] = item.Width; properties[DialogParameters.IMG_ORIGINALHEIGHT] = item.Height; properties[DialogParameters.IMG_URL] = item.Url; txtUrl.Text = item.Url; OriginalUrl = item.Url; PermanentUrl = item.PermanentUrl; properties[DialogParameters.EDITOR_CLIENTID] = EditorClientID; } LoadProperties(properties); LoadPreview(); } /// /// Loads the properites into control. /// /// Collection with properties public override void LoadItemProperties(Hashtable properties) { LoadProperties(properties); LoadPreview(); } public override void LoadProperties(Hashtable properties) { if (properties != null) { // Display the properties pnlEmpty.Visible = false; pnlTabs.CssClass = "Dialog_Tabs"; #region "Image general tab" if (tabImageGeneral.Visible) { int width = ValidationHelper.GetInteger(properties[DialogParameters.IMG_WIDTH], 0); int height = ValidationHelper.GetInteger(properties[DialogParameters.IMG_HEIGHT], 0); DefaultWidth = ValidationHelper.GetInteger(properties[DialogParameters.IMG_ORIGINALWIDTH], 0); DefaultHeight = ValidationHelper.GetInteger(properties[DialogParameters.IMG_ORIGINALHEIGHT], 0); widthHeightElem.Width = width; widthHeightElem.Height = height; OriginalUrl = ValidationHelper.GetString(properties[DialogParameters.IMG_URL], ""); txtUrl.Text = OriginalUrl; ViewState[DialogParameters.IMG_SIZETOURL] = ValidationHelper.GetBoolean(properties[DialogParameters.IMG_SIZETOURL], false); } #endregion #region "General items" EditorClientID = ValidationHelper.GetString(properties[DialogParameters.EDITOR_CLIENTID], ""); #endregion } } /// /// Returns all parameters of the selected item as name – value collection. /// public override Hashtable GetItemProperties() { Hashtable retval = new Hashtable(); #region "Image general tab" if (tabImageGeneral.Visible) { string url = txtUrl.Text.Trim(); bool sizeToUrl = ValidationHelper.GetBoolean(ViewState[DialogParameters.IMG_SIZETOURL], false); if (widthHeightElem.Width != DefaultWidth) { retval[DialogParameters.IMG_WIDTH] = widthHeightElem.Width; if (sizeToUrl) { url = URLHelper.AddParameterToUrl(url, "width", widthHeightElem.Width.ToString()); } } if (widthHeightElem.Height != DefaultHeight) { retval[DialogParameters.IMG_HEIGHT] = widthHeightElem.Height; if (sizeToUrl) { url = URLHelper.AddParameterToUrl(url, "height", widthHeightElem.Height.ToString()); } } retval[DialogParameters.IMG_URL] = URLHelper.ResolveUrl(url); retval[DialogParameters.IMG_SIZETOURL] = sizeToUrl; } #endregion #region "General items" retval[DialogParameters.EDITOR_CLIENTID] = EditorClientID; #endregion return retval; } /// /// Validates all the user input. /// public override bool Validate() { string errorMessage = ""; if (!widthHeightElem.Validate()) { errorMessage += " " + GetString("dialogs.image.invalidsize"); } errorMessage = errorMessage.Trim(); if (errorMessage != "") { LoadPreview(); ScriptHelper.RegisterStartupScript(Page, typeof(Page), "ImagePropertiesError", ScriptHelper.GetAlertScript(errorMessage)); return false; } return true; } /// /// Clears the properties form. /// public override void ClearProperties(bool hideProperties) { // Hide the properties pnlEmpty.Visible = hideProperties; pnlTabs.CssClass = (hideProperties ? "DialogElementHidden" : "Dialog_Tabs"); widthHeightElem.Height = 0; widthHeightElem.Width = 0; imagePreview.URL = ""; txtUrl.Text = ""; } #endregion }