private void RegisterStarupScriptIfNeeded(BocBooleanValueRenderingContext renderingContext, BocBooleanValueResourceSet resourceSet)
        {
            string startUpScriptKey = s_startUpScriptKeyPrefix + resourceSet.ResourceKey;

            if (!renderingContext.Control.Page.ClientScript.IsStartupScriptRegistered(typeof(BocBooleanValueRenderer), startUpScriptKey))
            {
                string trueValue  = true.ToString();
                string falseValue = false.ToString();
                string nullValue  = c_nullString;

                string startupScript = string.Format(
                    "BocBooleanValue_InitializeGlobals ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}');",
                    resourceSet.ResourceKey,
                    trueValue,
                    falseValue,
                    nullValue,
                    ScriptUtility.EscapeClientScript(resourceSet.DefaultTrueDescription),
                    ScriptUtility.EscapeClientScript(resourceSet.DefaultFalseDescription),
                    ScriptUtility.EscapeClientScript(resourceSet.DefaultNullDescription),
                    resourceSet.TrueIconUrl,
                    resourceSet.FalseIconUrl,
                    resourceSet.NullIconUrl);
                renderingContext.Control.Page.ClientScript.RegisterStartupScriptBlock(
                    renderingContext.Control,
                    typeof(BocBooleanValueRenderer),
                    startUpScriptKey,
                    startupScript);
            }
        }
        private void PrepareLinkControl(BocBooleanValueRenderingContext renderingContext, HyperLink linkControl, bool isClientScriptEnabled)
        {
            if (!isClientScriptEnabled)
            {
                return;
            }

            linkControl.Attributes.Add("onkeydown", "BocBooleanValue_OnKeyDown (this);");
            linkControl.Attributes.Add("href", "#");
            linkControl.Enabled = renderingContext.Control.Enabled;
        }
        private string GetClickScript(
            BocBooleanValueRenderingContext renderingContext,
            BocBooleanValueResourceSet resourceSet,
            bool isEnabled)
        {
            string script = "return false;";

            if (!isEnabled)
            {
                return(script);
            }

            string requiredFlag = renderingContext.Control.IsRequired ? "true" : "false";

            var scriptBuilder = new StringBuilder(500);

            scriptBuilder.Append("BocBooleanValue_SelectNextCheckboxValue (");
            scriptBuilder.Append("'").Append(resourceSet.ResourceKey).Append("'");
            scriptBuilder.Append(", ");
            scriptBuilder.Append("$(this).parent().children('a').children('img').first()[0]");
            scriptBuilder.Append(", ");
            if (renderingContext.Control.ShowDescription)
            {
                scriptBuilder.Append("$(this).parent().children('span').first()[0]");
            }
            else
            {
                scriptBuilder.Append("null");
            }
            scriptBuilder.Append(", ");
            scriptBuilder.Append("$(this).parent().children('input').first()[0]");
            scriptBuilder.Append(", ");
            scriptBuilder.Append(requiredFlag);
            scriptBuilder.Append(", ");
            AppendStringValueOrNullToScript(scriptBuilder, renderingContext.Control.TrueDescription);
            scriptBuilder.Append(", ");
            AppendStringValueOrNullToScript(scriptBuilder, renderingContext.Control.FalseDescription);
            scriptBuilder.Append(", ");
            AppendStringValueOrNullToScript(scriptBuilder, renderingContext.Control.NullDescription);
            scriptBuilder.Append(");");

            if (renderingContext.Control.IsAutoPostBackEnabled)
            {
                scriptBuilder.Append(renderingContext.Control.Page.ClientScript.GetPostBackEventReference(renderingContext.Control, "")).Append(";");
            }
            scriptBuilder.Append("return false;");
            return(scriptBuilder.ToString());
        }
        private void PrepareVisibleControls(
            BocBooleanValueRenderingContext renderingContext,
            BocBooleanValueResourceSet resourceSet,
            Image imageControl,
            Label labelControl)
        {
            string imageUrl;
            string description;

            if (!renderingContext.Control.Value.HasValue)
            {
                imageUrl    = resourceSet.NullIconUrl;
                description = string.IsNullOrEmpty(renderingContext.Control.NullDescription)
            ? resourceSet.DefaultNullDescription
            : renderingContext.Control.NullDescription;
            }
            else if (renderingContext.Control.Value.Value)
            {
                imageUrl    = resourceSet.TrueIconUrl;
                description = string.IsNullOrEmpty(renderingContext.Control.TrueDescription)
            ? resourceSet.DefaultTrueDescription
            : renderingContext.Control.TrueDescription;
            }
            else
            {
                imageUrl    = resourceSet.FalseIconUrl;
                description = string.IsNullOrEmpty(renderingContext.Control.FalseDescription)
            ? resourceSet.DefaultFalseDescription
            : renderingContext.Control.FalseDescription;
            }

            imageControl.AlternateText = description;

            imageControl.ImageUrl = imageUrl;
            if (renderingContext.Control.ShowDescription)
            {
                labelControl.Text = description;
            }

            labelControl.Width  = Unit.Empty;
            labelControl.Height = Unit.Empty;
            labelControl.ApplyStyle(renderingContext.Control.LabelStyle);
        }
        /// <summary>
        /// Renders an image and a label. In edit mode, the image is wrapped in a hyperlink that is
        /// scripted to respond to clicks and change the "checkbox" state accordingly;
        /// in addition, the state is put into an additional hidden field.
        /// </summary>
        public void Render(BocBooleanValueRenderingContext renderingContext)
        {
            ArgumentUtility.CheckNotNull("renderingContext", renderingContext);

            var resourceSet = _resourceSetFactory.CreateResourceSet(renderingContext.Control);

            AddAttributesToRender(renderingContext);
            renderingContext.Writer.RenderBeginTag(HtmlTextWriterTag.Span);

            var labelControl       = new Label();
            var imageControl       = new Image();
            var hiddenFieldControl = new HiddenField {
                ID = renderingContext.Control.GetValueName(), ClientIDMode = ClientIDMode.Static
            };
            var dataValueReadOnlyControl = new Label {
                ID = renderingContext.Control.GetValueName(), ClientIDMode = ClientIDMode.Static
            };
            var linkControl = new HyperLink {
                ID = renderingContext.Control.GetDisplayValueName(), ClientIDMode = ClientIDMode.Static
            };

            bool isClientScriptEnabled = DetermineClientScriptLevel(renderingContext);

            if (isClientScriptEnabled)
            {
                if (renderingContext.Control.Enabled)
                {
                    RegisterStarupScriptIfNeeded(renderingContext, resourceSet);
                }

                var script = GetClickScript(
                    renderingContext,
                    resourceSet,
                    renderingContext.Control.Enabled);
                labelControl.Attributes.Add("onclick", script);
                linkControl.Attributes.Add("onclick", script);
            }

            PrepareLinkControl(renderingContext, linkControl, isClientScriptEnabled);
            PrepareVisibleControls(renderingContext, resourceSet, imageControl, labelControl);

            if (!renderingContext.Control.IsReadOnly)
            {
                hiddenFieldControl.Value   = renderingContext.Control.Value.HasValue ? renderingContext.Control.Value.ToString() : c_nullString;
                hiddenFieldControl.Visible = true;
                hiddenFieldControl.RenderControl(renderingContext.Writer);
            }
            else
            {
                if (renderingContext.Control.Value.HasValue)
                {
                    dataValueReadOnlyControl.Attributes.Add("data-value", renderingContext.Control.Value.Value.ToString());
                }
                dataValueReadOnlyControl.RenderControl(renderingContext.Writer);
            }
            linkControl.Controls.Add(imageControl);
            linkControl.RenderControl(renderingContext.Writer);
            labelControl.RenderControl(renderingContext.Writer);

            renderingContext.Writer.RenderEndTag();
        }
 private string GetImageName(BocBooleanValueRenderingContext renderingContext)
 {
     return(renderingContext.Control.ClientID + "_Image");
 }
 private string GetLabelName(BocBooleanValueRenderingContext renderingContext)
 {
     return(renderingContext.Control.ClientID + "_LabelValue");
 }
 private bool DetermineClientScriptLevel(BocBooleanValueRenderingContext renderingContext)
 {
     return(!renderingContext.Control.IsDesignMode && !renderingContext.Control.IsReadOnly);
 }