BaseMenuRenderer.cs 16 KB

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