using System; using System.Web.UI; using System.Web.UI.WebControls; // using uComponents.DataTypes.Shared.Extensions; // using uComponents.DataTypes.Shared.PrevalueEditors; using umbraco.cms.businesslogic.datatype; namespace umbraco.editorControls.Slider { /// /// The PreValue Editor for the Slider data-type. /// [ClientDependency.Core.ClientDependency(ClientDependency.Core.ClientDependencyType.Javascript, "ui/jqueryui.js", "UmbracoClient")] [ClientDependency.Core.ClientDependency(ClientDependency.Core.ClientDependencyType.Css, "DateTimePicker/datetimepicker.css", "UmbracoClient")] public class SliderPrevalueEditor : AbstractJsonPrevalueEditor { /// /// The DropDownList for the database data-type. /// private DropDownList DatabaseDataType; /// /// The CheckBox control to enable the range for the slider. /// private CheckBox EnableRange; /// /// The CheckBox control to enable incremental steps for the slider. /// private CheckBox EnableStep; /// /// The TextBox control for the minimum value of the slider. /// private TextBox MinValue; /// /// The TextBox control for the maximum value of the slider. /// private TextBox MaxValue; /// /// The DropDownList control for the orientation of the slider. /// private DropDownList Orientation; /// /// The DropDownList control for the range value. /// private DropDownList RangeValue; /// /// The TextBox control for the incremental step value. /// private TextBox StepValue; /// /// The TextBox control for the first value input. /// private TextBox Value; /// /// The TextBox control for the second value input. /// private TextBox Value2; ////private SliderControl PreviewSlider; /// /// Initializes a new instance of the class. /// /// Type of the data. public SliderPrevalueEditor(umbraco.cms.businesslogic.datatype.BaseDataType dataType) : base(dataType) { } /// /// Saves the data-type PreValue options. /// public override void Save() { // set the database data-type this.m_DataType.DBType = (umbraco.cms.businesslogic.datatype.DBTypes)Enum.Parse(typeof(umbraco.cms.businesslogic.datatype.DBTypes), this.DatabaseDataType.SelectedValue); // parse the integers & doubles double maxValue, minValue, value, value2, stepValue; double.TryParse(this.MaxValue.Text, out maxValue); double.TryParse(this.MinValue.Text, out minValue); double.TryParse(this.Value.Text, out value); double.TryParse(this.Value2.Text, out value2); double.TryParse(this.StepValue.Text, out stepValue); // set the options var options = new SliderOptions() { DBType = (DBTypes)Enum.Parse(typeof(DBTypes), this.DatabaseDataType.SelectedValue), EnableRange = this.EnableRange.Checked, EnableStep = this.EnableStep.Checked, MaxValue = maxValue, MinValue = minValue, Orientation = this.Orientation.SelectedValue, RangeValue = this.RangeValue.SelectedValue, StepValue = stepValue, Value = value, Value2 = value2 }; // save the options as JSON this.SaveAsJson(options); // toggle the non-default fields this.ToggleFields(); } /// /// Raises the event. /// /// An object that contains the event data. protected override void OnInit(EventArgs e) { base.OnInit(e); this.EnsureChildControls(); } /// /// Creates child controls for this control /// protected override void CreateChildControls() { base.CreateChildControls(); // set-up child controls this.DatabaseDataType = new DropDownList() { ID = "DatabaseDataType" }; this.EnableRange = new CheckBox() { ID = "EnableRange" }; this.EnableStep = new CheckBox() { ID = "EnableStep" }; this.MinValue = new TextBox() { ID = "MinValue", CssClass = "guiInputText slider-numeric slider-decimal" }; this.MaxValue = new TextBox() { ID = "MaxValue", CssClass = "guiInputText slider-numeric slider-decimal" }; this.Orientation = new DropDownList() { ID = "Orientation" }; this.RangeValue = new DropDownList() { ID = "RangeValue" }; this.StepValue = new TextBox() { ID = "StepValue", CssClass = "guiInputText slider-numeric slider-decimal" }; this.Value = new TextBox() { ID = "Value", CssClass = "guiInputText slider-numeric slider-decimal" }; this.Value2 = new TextBox() { ID = "Value2", CssClass = "guiInputText slider-numeric slider-decimal" }; ////this.PreviewSlider = new SliderControl() { ID = "PreviewSlider", Options = new SliderOptions(true) }; // add the database data-type options this.DatabaseDataType.Items.Clear(); this.DatabaseDataType.Items.Add(DBTypes.Integer.ToString()); //this.DatabaseDataType.Items.Add(DBTypes.Ntext.ToString()); this.DatabaseDataType.Items.Add(DBTypes.Nvarchar.ToString()); // add range options this.RangeValue.Items.Clear(); this.RangeValue.Items.Add(string.Empty); this.RangeValue.Items.Add(new ListItem("Minimum Value", "min")); this.RangeValue.Items.Add(new ListItem("Maximum Value", "max")); // add orientation options this.Orientation.Items.Clear(); this.Orientation.Items.Add(new ListItem("Horizontal (default)", "horizontal")); this.Orientation.Items.Add(new ListItem("Vertical", "vertical")); // add the child controls this.Controls.AddPrevalueControls(this.DatabaseDataType, this.EnableRange, this.EnableStep, this.MaxValue, this.MinValue, this.Orientation, this.RangeValue, this.StepValue, this.Value, this.Value2); ////this.Controls.Add(this.PreviewSlider); } /// /// Raises the event. /// /// The object that contains the event data. protected override void OnLoad(EventArgs e) { base.OnLoad(e); // get PreValues, load them into the controls. var options = this.GetPreValueOptions(); // if the options are null, then load the defaults if (options == null) { options = new SliderOptions(true); } // set the values this.DatabaseDataType.SelectedValue = this.m_DataType.DBType.ToString(); this.EnableRange.Checked = options.EnableRange; this.EnableStep.Checked = options.EnableStep; this.MinValue.Text = options.MinValue.ToString(); this.MaxValue.Text = options.MaxValue.ToString(); this.Orientation.SelectedValue = options.Orientation; this.RangeValue.SelectedValue = options.RangeValue; this.StepValue.Text = options.StepValue.ToString(); this.Value.Text = options.Value.ToString(); this.Value2.Text = options.Value2.ToString(); // toggle the non-default fields this.ToggleFields(); } /// /// Renders the contents of the control to the specified writer. This method is used primarily by control developers. /// /// A that represents the output stream to render HTML content on the client. protected override void RenderContents(HtmlTextWriter writer) { // add property fields writer.AddPrevalueRow("Database Type:", this.DatabaseDataType); writer.AddPrevalueRow("Enable Range:", this.EnableRange); writer.AddPrevalueRow("Range:", this.RangeValue); writer.AddPrevalueRow("Initial Value:", this.Value); writer.AddPrevalueRow("Initial Value 2:", this.Value2); writer.AddPrevalueRow("Minimum Value:", this.MinValue); writer.AddPrevalueRow("Maximum Value:", this.MaxValue); writer.AddPrevalueRow("Enable Step Increments:", this.EnableStep); writer.AddPrevalueRow("Step Increments:", this.StepValue); writer.AddPrevalueRow("Orientation:", this.Orientation); ////writer.AddPrevalueRow(" ", new LiteralControl("

Preview:


"), this.PreviewSlider); // add jquery window load event for toggling fields. var javascriptMethod = string.Format( @" $('#{0}').click(function(){{ var disable = !$(this).attr('checked'); $('#{1},#{3}').attr('disabled', disable); $('#{6}').val(disable && !checkDecimals() ? 'Integer' : 'Nvarchar'); if(!disable) disable = $('#{1}').val() != ''; }}); $('#{1}').change(function(){{ var disable = $(this).val() != ''; $('#{3}').attr('disabled', disable); }}); $('#{4}').click(function(){{ var disable = !$(this).attr('checked'); $('#{5}').attr('disabled', disable); }}); $('#{6}').change(function(){{ var disable = $(this).val() == 'Integer'; if (checkDecimals() && disable) {{ $('#{6}').val('Nvarchar'); alert('Please remove decimal points below if you wish to use the Integer datatype'); }} else {{ $('#{0}').removeAttr('checked'); $('#{1},#{3}').attr('disabled', disable); }} }}); $('.slider-numeric').keydown(function(event) {{ // Allow only backspace and delete if ( event.keyCode == 46 || event.keyCode == 8 || ($(this).hasClass('slider-decimal') && (event.keyCode == 110 || event.keyCode == 190))) {{ // let it happen, don't do anything }} else {{ // Ensure that it is a number and stop the keypress if ( (event.keyCode < 48 || event.keyCode > 57 ) && (event.keyCode < 96 || event.keyCode > 105 ) ) {{ event.preventDefault(); }} }} }}); $('.slider-numeric').keyup(function(event) {{ if ($('#{6}').val() != 'Nvarchar' && checkDecimals()) {{ $('#{6}').val('Nvarchar'); }} }}); function checkDecimals() {{ foundDecimals = false; $('.slider-numeric').each(function() {{ if ($(this).val().indexOf('.') >= 0) {{ foundDecimals = true; return false; }} }}); return foundDecimals; }} ", this.EnableRange.ClientID, this.RangeValue.ClientID, this.Value.ClientID, this.Value2.ClientID, this.EnableStep.ClientID, this.StepValue.ClientID, this.DatabaseDataType.ClientID); var javascript = string.Concat(""); writer.WriteLine(javascript); } /// /// Toggles the fields. /// private void ToggleFields() { if (this.DatabaseDataType.SelectedIndex == 0) { this.EnableRange.Checked = false; } this.RangeValue.Enabled = this.EnableRange.Checked; this.Value2.Enabled = this.EnableRange.Checked && this.RangeValue.SelectedValue == string.Empty; this.StepValue.Enabled = this.EnableStep.Checked; } } }