/// <summary> /// Handle OnAfterValidate event of the UI form. /// </summary> /// <param name="sender">Sender object</param> /// <param name="e">Event argument</param> protected void EditForm_OnAfterValidate(object sender, EventArgs e) { // Validate the dynamic code if (!languageChangeProceeded && !EditForm.IsInsertMode) { // Save button doesn't change style sheet language, reset field value to the data base value EditForm.FieldControls["StylesheetDynamicLanguage"].Value = EditForm.GetDataValue("StylesheetDynamicLanguage"); string lang = ValidationHelper.GetString(EditForm.GetDataValue("StylesheetDynamicLanguage"), CssStylesheetInfo.PLAIN_CSS); if (!lang.EqualsCSafe(CssStylesheetInfo.PLAIN_CSS)) { string output = String.Empty; string dynamicCode = EditForm.GetFieldValue("StylesheetDynamicCode") as string; string error = CSSStylesheetNewControlExtender.ProcessCss(dynamicCode, lang, out output, hidCompiledCss); if (!String.IsNullOrEmpty(error)) { EditForm.AddError(error); EditForm.StopProcessing = true; } // Keep the current value for better performance else { CssPreprocessor.CurrentCompiledValue = output; } } } RegisterHideProgress(); }
/// <summary> /// Detects stylesheet language change and converts CSS code according to the former and the latter language. /// </summary> /// <param name="originalLanguage">The previous language</param> /// <param name="newLanguage">The new language</param> private void ChangeStylesheetLanguage(string originalLanguage, string newLanguage) { // Check whether the stylesheet language has actually changed. if (!String.IsNullOrEmpty(newLanguage) && !String.IsNullOrEmpty(originalLanguage) && !originalLanguage.EqualsCSafe(newLanguage, true)) { string code = String.Empty; string output = String.Empty; string errorMessage = String.Empty; if (newLanguage.EqualsCSafe(CssStylesheetInfo.PLAIN_CSS, true)) { // Changed from any CSS preprocessor language to plain CSS. code = ValidationHelper.GetString(EditForm.GetFieldValue("StylesheetDynamicCode"), String.Empty); // Try to parse the code against the original preprocessor language. errorMessage = CSSStylesheetNewControlExtender.ProcessCss(code, originalLanguage, out output, hidCompiledCss); } else if (originalLanguage.EqualsCSafe(CssStylesheetInfo.PLAIN_CSS, true)) { // Changed from Plain CSS to some CSS preprocessor language. code = ValidationHelper.GetString(EditForm.GetFieldValue("StylesheetText"), String.Empty); // Try to parse the code against the new preprocessor language. errorMessage = CSSStylesheetNewControlExtender.ProcessCss(code, newLanguage, out output, hidCompiledCss); } else { // Changed from CSS preprocessor language to another one. code = ValidationHelper.GetString(EditForm.GetFieldValue("StylesheetDynamicCode"), String.Empty); // Try to parse the code against the original preprocessor language. errorMessage = CSSStylesheetNewControlExtender.ProcessCss(code, originalLanguage, out output, hidCompiledCss); } if (String.IsNullOrEmpty(errorMessage)) { // Success -> Set correct values to form controls. if (newLanguage.EqualsCSafe(CssStylesheetInfo.PLAIN_CSS, true)) { // output -> StylesheetText EditForm.FieldControls["StylesheetText"].Value = output; } else if (originalLanguage.EqualsCSafe(CssStylesheetInfo.PLAIN_CSS, true)) { // Copy the original code to StylesheetDynamicCode field. EditForm.FieldControls["StylesheetDynamicCode"].Value = code; } else { // Copy the original code to StylesheetDynamicCode field. EditForm.FieldControls["StylesheetDynamicCode"].Value = output; } // Prevent to additional change processing. languageChangeProceeded = true; } else { // Failure -> Show error message EditForm.AddError(errorMessage); // Prevent further processing EditForm.StopProcessing = true; // Set drop-down list to its former value FormEngineUserControl langControl = EditForm.FieldControls["StylesheetDynamicLanguage"]; if (langControl != null) { langControl.Value = originalLanguage; CssStylesheetInfo cssInfo = EditForm.EditedObject as CssStylesheetInfo; if (cssInfo != null) { cssInfo.StylesheetDynamicLanguage = originalLanguage; } } } } }
/// <summary> /// Displays and hides plain CSS code preview. /// </summary> private void HandleCssCodePreview() { if (RequestHelper.IsPostBack() && !plainCssOnly) { // Don't process if the CSS preview control is not visible FormEngineUserControl previewControl = EditForm.FieldControls["StylesheetCodePreview"]; if ((previewControl != null) && !previewControl.Visible) { return; } // Get the edited stylesheet CssStylesheetInfo cssInfo = EditForm.EditedObject as CssStylesheetInfo; if (cssInfo == null) { return; } string lang = ValidationHelper.GetString(cssInfo.StylesheetDynamicLanguage, CssStylesheetInfo.PLAIN_CSS); string previewMode = ValidationHelper.GetString(EditForm.GetFieldValue("StylesheetCodePreview"), String.Empty); // Display preview if (!String.IsNullOrEmpty(previewMode) && previewMode.EqualsCSafe("preview", true) && !lang.EqualsCSafe(CssStylesheetInfo.PLAIN_CSS, true)) { // Get the stylesheet dynamic code string code = ValidationHelper.GetString(EditForm.GetFieldValue("StylesheetDynamicCode"), String.Empty); string output = String.Empty; // Run preprocessor string parserError = CSSStylesheetNewControlExtender.ProcessCss(code, lang, out output, hidCompiledCss); if (String.IsNullOrEmpty(parserError)) { // Set the editor value and disable editing FormEngineUserControl plainCssEditor = EditForm.FieldControls["StylesheetText"]; if (plainCssEditor != null) { plainCssEditor.Value = output; codePreviewDisplayed = true; } // Hide dynamic code editor FormEngineUserControl dynamicCodeEditor = EditForm.FieldControls["StylesheetDynamicCode"]; if (dynamicCodeEditor != null) { dynamicCodeEditor.CssClass = "Hidden"; } } else { // Handle the error that occurred during parsing EditForm.ShowError(parserError); FormEngineUserControl previewField = EditForm.FieldControls["StylesheetCodePreview"]; if (previewField != null) { previewField.Value = lang; } } return; } // Hide preview if (!String.IsNullOrEmpty(previewMode) && !previewMode.EqualsCSafe("preview", true)) { // Ensure the visibility of the field if the preview is not active FormEngineUserControl dynamicCodeEditor = EditForm.FieldControls["StylesheetDynamicCode"]; if (dynamicCodeEditor != null) { dynamicCodeEditor.CssClass = ""; } // Enable for editing despite the editor is hidden FormEngineUserControl plainCssEditor = EditForm.FieldControls["StylesheetText"]; if (plainCssEditor != null) { plainCssEditor.Enabled = true; } } } }