1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
|
//
// Authors:
// Marek Habersack <grendel@twistedcode.net>
//
// (C) 2010 Novell, Inc (http://novell.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
namespace System.Web.UI.WebControls
{
abstract class BaseMenuRenderer : IMenuRenderer
{
protected sealed class OwnerContext
{
BaseMenuRenderer container;
string staticPopOutImageTextFormatString;
string dynamicPopOutImageTextFormatString;
string dynamicTopSeparatorImageUrl;
string dynamicBottomSeparatorImageUrl;
string staticTopSeparatorImageUrl;
string staticBottomSeparatorImageUrl;
List <Style> levelMenuItemLinkStyles;
List<Style> levelSelectedLinkStyles;
Style staticMenuItemLinkStyle;
Style dynamicMenuItemLinkStyle;
MenuItemStyle staticSelectedStyle;
Style staticSelectedLinkStyle;
MenuItemStyle dynamicSelectedStyle;
Style dynamicSelectedLinkStyle;
MenuItemStyleCollection levelSelectedStyles;
ITemplate dynamicItemTemplate;
bool dynamicItemTemplateQueried;
public readonly MenuItemStyle StaticMenuItemStyle;
public readonly MenuItemStyle DynamicMenuItemStyle;
public readonly MenuItemStyleCollection LevelMenuItemStyles;
public readonly Style ControlLinkStyle;
public readonly HtmlHead Header;
public readonly string ClientID;
public readonly int StaticDisplayLevels;
public readonly bool IsVertical;
public readonly MenuItem SelectedItem;
public readonly Unit StaticSubMenuIndent;
public string StaticPopOutImageTextFormatString {
get {
if (staticPopOutImageTextFormatString == null)
staticPopOutImageTextFormatString = container.Owner.StaticPopOutImageTextFormatString;
return staticPopOutImageTextFormatString;
}
}
public string DynamicPopOutImageTextFormatString {
get {
if (dynamicPopOutImageTextFormatString == null)
dynamicPopOutImageTextFormatString = container.Owner.DynamicPopOutImageTextFormatString;
return dynamicPopOutImageTextFormatString;
}
}
public string DynamicTopSeparatorImageUrl {
get {
if (dynamicTopSeparatorImageUrl == null)
dynamicTopSeparatorImageUrl = container.Owner.DynamicTopSeparatorImageUrl;
return dynamicTopSeparatorImageUrl;
}
}
public string DynamicBottomSeparatorImageUrl {
get {
if (dynamicBottomSeparatorImageUrl == null)
dynamicBottomSeparatorImageUrl = container.Owner.DynamicBottomSeparatorImageUrl;
return dynamicBottomSeparatorImageUrl;
}
}
public string StaticTopSeparatorImageUrl {
get {
if (staticTopSeparatorImageUrl == null)
staticTopSeparatorImageUrl = container.Owner.StaticTopSeparatorImageUrl;
return staticBottomSeparatorImageUrl;
}
}
public string StaticBottomSeparatorImageUrl {
get {
if (staticBottomSeparatorImageUrl == null)
staticBottomSeparatorImageUrl = container.Owner.StaticBottomSeparatorImageUrl;
return staticBottomSeparatorImageUrl;
}
}
public List <Style> LevelMenuItemLinkStyles {
get {
if (levelMenuItemLinkStyles == null)
levelMenuItemLinkStyles = container.Owner.LevelMenuItemLinkStyles;
return levelMenuItemLinkStyles;
}
}
public List<Style> LevelSelectedLinkStyles {
get {
if (levelSelectedLinkStyles == null)
levelSelectedLinkStyles = container.Owner.LevelSelectedLinkStyles;
return levelSelectedLinkStyles;
}
}
public Style StaticMenuItemLinkStyle {
get {
if (staticMenuItemLinkStyle == null)
staticMenuItemLinkStyle = container.Owner.StaticMenuItemLinkStyle;
return staticMenuItemLinkStyle;
}
}
public Style DynamicMenuItemLinkStyle {
get {
if (dynamicMenuItemLinkStyle == null)
dynamicMenuItemLinkStyle = container.Owner.DynamicMenuItemLinkStyle;
return dynamicMenuItemLinkStyle;
}
}
public MenuItemStyle StaticSelectedStyle {
get {
if (staticSelectedStyle == null)
staticSelectedStyle = container.Owner.StaticSelectedStyle;
return staticSelectedStyle;
}
}
public MenuItemStyle DynamicSelectedStyle {
get {
if (dynamicSelectedStyle == null)
dynamicSelectedStyle = container.Owner.DynamicSelectedStyle;
return dynamicSelectedStyle;
}
}
public Style StaticSelectedLinkStyle {
get {
if (staticSelectedLinkStyle == null)
staticSelectedLinkStyle = container.Owner.StaticSelectedLinkStyle;
return staticSelectedLinkStyle;
}
}
public Style DynamicSelectedLinkStyle {
get {
if (dynamicSelectedLinkStyle == null)
dynamicSelectedLinkStyle = container.Owner.DynamicSelectedLinkStyle;
return dynamicSelectedLinkStyle;
}
}
public MenuItemStyleCollection LevelSelectedStyles {
get {
if (levelSelectedStyles == null)
levelSelectedStyles = container.Owner.LevelSelectedStyles;
return levelSelectedStyles;
}
}
public ITemplate DynamicItemTemplate {
get {
if (!dynamicItemTemplateQueried && dynamicItemTemplate == null) {
dynamicItemTemplate = container.Owner.DynamicItemTemplate;
dynamicItemTemplateQueried = true;
}
return dynamicItemTemplate;
}
}
public OwnerContext (BaseMenuRenderer container)
{
if (container == null)
throw new ArgumentNullException ("container");
this.container = container;
Menu owner = container.Owner;
Page page = owner.Page;
Header = page != null ? page.Header : null;
ClientID = owner.ClientID;
IsVertical = owner.Orientation == Orientation.Vertical;
StaticSubMenuIndent = owner.StaticSubMenuIndent;
SelectedItem = owner.SelectedItem;
ControlLinkStyle = owner.ControlLinkStyle;
StaticDisplayLevels = owner.StaticDisplayLevels;
StaticMenuItemStyle = owner.StaticMenuItemStyleInternal;
DynamicMenuItemStyle = owner.DynamicMenuItemStyleInternal;
LevelMenuItemStyles = owner.LevelMenuItemStyles;
}
}
int registeredStylesCounter = -1;
public abstract HtmlTextWriterTag Tag { get; }
protected Menu Owner {
get;
private set;
}
public BaseMenuRenderer (Menu owner)
{
if (owner == null)
throw new ArgumentNullException ("owner");
this.Owner = owner;
}
public virtual void AddAttributesToRender (HtmlTextWriter writer)
{
Menu owner = Owner;
Page page = owner.Page;
SubMenuStyle staticMenuStyle = owner.StaticMenuStyleInternal;
SubMenuStyleCollection levelSubMenuStyles = owner.LevelSubMenuStylesInternal;
bool haveSubStyles = levelSubMenuStyles != null && levelSubMenuStyles.Count > 0;
Style controlStyle = haveSubStyles || staticMenuStyle != null ? owner.ControlStyle : null;
if (page != null && page.Header != null) {
// styles are registered
if (staticMenuStyle != null) {
AddCssClass (controlStyle, staticMenuStyle.CssClass);
AddCssClass (controlStyle, staticMenuStyle.RegisteredCssClass);
}
if (haveSubStyles) {
AddCssClass (controlStyle, levelSubMenuStyles [0].CssClass);
AddCssClass (controlStyle, levelSubMenuStyles [0].RegisteredCssClass);
}
} else {
// styles are not registered
if (staticMenuStyle != null)
controlStyle.CopyFrom (staticMenuStyle);
if (haveSubStyles)
controlStyle.CopyFrom (levelSubMenuStyles [0]);
}
}
public abstract void PreRender (Page page, HtmlHead head, ClientScriptManager csm, string cmenu, StringBuilder script);
public abstract void RenderMenuBeginTag (HtmlTextWriter writer, bool dynamic, int menuLevel);
public abstract void RenderMenuBody (HtmlTextWriter writer, MenuItemCollection items, bool vertical, bool dynamic, bool notLast);
public abstract void RenderBeginTag (HtmlTextWriter writer, string skipLinkText);
public abstract void RenderEndTag (HtmlTextWriter writer);
public abstract void RenderContents (HtmlTextWriter writer);
public abstract bool IsDynamicItem (Menu owner, MenuItem item);
protected abstract void RenderMenuItem (HtmlTextWriter writer, MenuItem item, bool vertical, bool notLast, bool isFirst, OwnerContext oc);
public virtual void RenderMenuItem (HtmlTextWriter writer, MenuItem item, bool notLast, bool isFirst)
{
var oc = new OwnerContext (this);
RenderMenuItem (writer, item, oc.IsVertical, notLast, isFirst, oc);
}
public virtual void RenderMenuEndTag (HtmlTextWriter writer, bool dynamic, int menuLevel)
{
writer.RenderEndTag ();
}
public virtual void RenderItemContent (HtmlTextWriter writer, MenuItem item, bool isDynamicItem)
{
Menu owner = Owner;
if (!String.IsNullOrEmpty (item.ImageUrl)) {
writer.AddAttribute (HtmlTextWriterAttribute.Src, owner.ResolveClientUrl (item.ImageUrl));
writer.AddAttribute (HtmlTextWriterAttribute.Alt, item.ToolTip);
writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, "none");
writer.AddStyleAttribute (HtmlTextWriterStyle.VerticalAlign, "middle");
writer.RenderBeginTag (HtmlTextWriterTag.Img);
writer.RenderEndTag (); // IMG
}
string format;
if (isDynamicItem && (format = owner.DynamicItemFormatString).Length > 0)
writer.Write (String.Format (format, item.Text));
else if (!isDynamicItem && (format = owner.StaticItemFormatString).Length > 0)
writer.Write (String.Format (format, item.Text));
else
writer.Write (item.Text);
}
public void AddCssClass (Style style, string cssClass)
{
style.AddCssClass (cssClass);
}
public string GetItemClientId (string ownerClientID, MenuItem item, string suffix)
{
return ownerClientID + "_" + item.Path + suffix;
}
public virtual void RenderItemHref (Menu owner, HtmlTextWriter writer, MenuItem item)
{
if (!item.BranchEnabled)
writer.AddAttribute ("disabled", "true", false);
else if (!item.Selectable) {
writer.AddAttribute ("href", "#", false);
writer.AddStyleAttribute ("cursor", "text");
} else if (item.NavigateUrl != String.Empty) {
string target = item.Target != String.Empty ? item.Target : owner.Target;
string navUrl = owner.ResolveClientUrl (item.NavigateUrl);
writer.AddAttribute ("href", navUrl);
if (target != String.Empty)
writer.AddAttribute ("target", target);
} else
writer.AddAttribute ("href", GetClientEvent (owner, item));
}
public string GetPopOutImage (Menu owner, MenuItem item, bool isDynamicItem)
{
if (owner == null)
owner = Owner;
if (item.PopOutImageUrl != String.Empty)
return item.PopOutImageUrl;
bool needArrowResource = false;
if (isDynamicItem) {
if (owner.DynamicPopOutImageUrl != String.Empty)
return owner.DynamicPopOutImageUrl;
if (owner.DynamicEnableDefaultPopOutImage)
needArrowResource = true;
} else {
if (owner.StaticPopOutImageUrl != String.Empty)
return owner.StaticPopOutImageUrl;
if (owner.StaticEnableDefaultPopOutImage)
needArrowResource = true;
}
if (needArrowResource)
return GetArrowResourceUrl (owner);
return null;
}
public string GetArrowResourceUrl (Menu owner)
{
Page page = owner.Page;
ClientScriptManager csm = page != null ? page.ClientScript : null;
if (csm != null)
return csm.GetWebResourceUrl (typeof (Menu), "arrow_plus.gif");
return null;
}
public void FillMenuStyle (HtmlHead header, bool dynamic, int menuLevel, SubMenuStyle style)
{
Menu owner = Owner;
if (header == null) {
Page page = owner.Page;
header = page != null ? page.Header : null;
}
SubMenuStyle staticMenuStyle = owner.StaticMenuStyleInternal;
// MenuItemStyle dynamicMenuItemStyle = owner.DynamicMenuItemStyleInternal;
SubMenuStyle dynamicMenuStyle = owner.DynamicMenuStyleInternal;
SubMenuStyleCollection levelSubMenuStyles = owner.LevelSubMenuStylesInternal;
if (header != null) {
// styles are registered
if (!dynamic && staticMenuStyle != null) {
AddCssClass (style, staticMenuStyle.CssClass);
AddCssClass (style, staticMenuStyle.RegisteredCssClass);
}
if (dynamic && dynamicMenuStyle != null) {
AddCssClass (style, dynamicMenuStyle.CssClass);
AddCssClass (style, dynamicMenuStyle.RegisteredCssClass);
}
if (levelSubMenuStyles != null && levelSubMenuStyles.Count > menuLevel) {
AddCssClass (style, levelSubMenuStyles [menuLevel].CssClass);
AddCssClass (style, levelSubMenuStyles [menuLevel].RegisteredCssClass);
}
} else {
// styles are not registered
if (!dynamic && staticMenuStyle != null)
style.CopyFrom (staticMenuStyle);
if (dynamic && dynamicMenuStyle != null)
style.CopyFrom (dynamicMenuStyle);
if (levelSubMenuStyles != null && levelSubMenuStyles.Count > menuLevel)
style.CopyFrom (levelSubMenuStyles [menuLevel]);
}
}
public void RegisterStyle (Style baseStyle, Style linkStyle, HtmlHead head)
{
RegisterStyle (baseStyle, linkStyle, null, head);
}
public void RegisterStyle (Style baseStyle, Style linkStyle, string className, HtmlHead head)
{
if (head == null)
return;
linkStyle.CopyTextStylesFrom (baseStyle);
linkStyle.BorderStyle = BorderStyle.None;
RegisterStyle (linkStyle, className, head);
RegisterStyle (baseStyle, className, head);
}
public void RegisterStyle (Style baseStyle, HtmlHead head)
{
RegisterStyle (baseStyle, (string)null, head);
}
public void RegisterStyle (Style baseStyle, string className, HtmlHead head)
{
if (head == null)
return;
if (String.IsNullOrEmpty (className))
className = IncrementStyleClassName ();
baseStyle.SetRegisteredCssClass (className);
head.StyleSheet.CreateStyleRule (baseStyle, Owner, "." + className);
}
public void RenderSeparatorImage (Menu owner, HtmlTextWriter writer, string url, bool standardsCompliant)
{
if (String.IsNullOrEmpty (url))
return;
writer.AddAttribute (HtmlTextWriterAttribute.Src, owner.ResolveClientUrl (url));
if (standardsCompliant) {
writer.AddAttribute (HtmlTextWriterAttribute.Alt, String.Empty);
writer.AddAttribute (HtmlTextWriterAttribute.Class, "separator");
}
writer.RenderBeginTag (HtmlTextWriterTag.Img);
writer.RenderEndTag ();
}
public bool IsDynamicItem (MenuItem item)
{
return IsDynamicItem (Owner, item);
}
string GetClientEvent (Menu owner, MenuItem item)
{
if (owner == null)
owner = Owner;
Page page = owner.Page;
ClientScriptManager csm = page != null ? page.ClientScript : null;
if (csm == null)
return String.Empty;
return csm.GetPostBackClientHyperlink (owner, item.Path, true);
}
string IncrementStyleClassName ()
{
registeredStylesCounter++;
return Owner.ClientID + "_" + registeredStylesCounter;
}
}
}
|