/// <summary> /// Called by the framework once the component instance /// is initialized /// </summary> public override void Initialize() { base.Initialize(); faqList = Context.ComponentParameters["elements"] as IEnumerable<QnA>; if (faqList == null) throw new ViewComponentException("FaqListComponent: required 'elements' parameter missing or empty."); param = new FaqParameters(); param.QuestionCssClass = Context.ComponentParameters["QuestionCssClass"] as string ?? "Question"; param.AnswerCssClass = Context.ComponentParameters["AnswerCssClass"] as string ?? "Answer"; param.helper = new JavascriptHelper(Context, EngineContext,"FaqListComponent"); param.helper.IncludeStandardScripts("Ajax"); string listType = Context.ComponentParameters["ListType"] as string ?? "none"; switch (listType.ToLower()) { case "none": param.WrapItems = false; ListTag = ""; break; case "ordered": case "numbered": case "ol": param.WrapItems = true; ListTag = "ol"; break; case "unordered": case "ul": case "bullet": param.WrapItems = true; ListTag = "ul"; break; default: throw new ViewComponentException("FaqListComponent: '"+listType+"' is not a acceptable ListType"); } }
/// <summary> /// Builds the item. /// </summary> /// <remarks> This attempts to always "do the right thing", hence, if javascript is enabled, /// the answer is hidden, and clicking the question reveals it. However, if Javascript is disabled, /// that won't work, so the answer must always be display. But, at rendering time, we have no way of knowing /// if Javascript is enabled or not, and, when displayed in the browser, we cannot define a "no scripting" /// contingency option in Javascript, since it won't be run. /// To solve this, the answer is initially displayed, /// and then immediately hidden (via Javascript). If JS is enabled, the text is hidden before it is displayed, /// so the user never sees it. /// </remarks> /// <param name="question">The question.</param> /// <param name="answer">The answer.</param> /// <param name="param">The param.</param> /// <returns>The Html generated for this FAQ item.</returns> public static string BuildItem(string question, string answer, FaqParameters param) { string divFormat; string hideFormat; switch (param.jsLibrary) { case Library.proto: default: param.helper.IncludeStandardScripts("Ajax"); divFormat = @"<div id=""Faq_Q{0}"" onclick=""Element.toggle('Faq_A{0}')"" class=""{1}"">"; // hideFormat = @"<script>$(Faq_A{0}).style.display='none';</script>"; hideFormat = @"$(Faq_A{0}).style.display='none';"; break; case Library.jquery: param.helper.IncludeStandardScripts("jQuery"); divFormat = @"<div id=""Faq_Q{0}"" onclick=""jQuery('#Faq_A{0}').slideToggle()"" class=""{1}"">"; // hideFormat = @"<script>jQuery('#Faq_A{0}').hide();</script>"; hideFormat = "jQuery('#Faq_A{0}').hide();"; break; } StringBuilder sb = new StringBuilder(250); if (param.WrapItems) sb.Append("<li>"); sb.AppendFormat(divFormat, param.Number, param.QuestionCssClass); sb.Append(Environment.NewLine); sb.Append(question); sb.Append("</div>"); sb.Append(Environment.NewLine); sb.AppendFormat(@"<div id=""Faq_A{0}"" class=""{1}"">", param.Number, param.AnswerCssClass); sb.Append(Environment.NewLine); // sb.Append("<br/>"); sb.Append(answer); sb.Append("<hr/>"); sb.Append("</div>"); sb.Append(Environment.NewLine); if (param.WrapItems) sb.Append("</li>"); sb.Append(Environment.NewLine); // sb.AppendFormat(hideFormat, param.Number); // sb.Append(Environment.NewLine); param.helper.InsertSeparateText(string.Format(hideFormat, param.Number)); return sb.ToString(); }
/// <summary> /// Renders this instance. /// </summary> public override void Render() { ConfirmSectionPresent("question"); ConfirmSectionPresent("answer"); string question = GetSectionText("question"); string answer = GetSectionText("answer"); FaqParameters param = new FaqParameters(); param.AnswerCssClass = answerCssClass; param.QuestionCssClass = questionCssClass; param.Number = faqNum.Value; param.WrapItems = wrapItems; param.jsLibrary = Enum.IsDefined(typeof(Library), jsLibrary.ToLowerInvariant()) ? (Library) Enum.Parse(typeof(Library), jsLibrary.ToLowerInvariant()) : Library.proto; param.helper = new JavascriptHelper(Context, EngineContext, "FaqItemComponent"); switch (param.jsLibrary) { case Library.proto: param.helper.IncludeStandardScripts("Ajax"); break; case Library.jquery: param.helper.IncludeStandardScripts("jQuery"); break; } RenderText(FaqItemHelper.BuildItem(question, answer, param)); CancelView(); }