TUI.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* The contents of this file are subject to the Mozilla Public
  2. * License Version 1.1 (the "License"); you may not use this file
  3. * except in compliance with the License. You may obtain a copy of
  4. * the License at http://www.mozilla.org/MPL/
  5. *
  6. * Software distributed under the License is distributed on an "AS
  7. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  8. * implied. See the License for the specific language governing
  9. * rights and limitations under the License.
  10. *
  11. * The Original Code is the Bugzilla Bug Tracking System.
  12. *
  13. * The Initial Developer of the Original Code is Netscape Communications
  14. * Corporation. Portions created by Netscape are
  15. * Copyright (C) 1998 Netscape Communications Corporation. All
  16. * Rights Reserved.
  17. *
  18. * Contributor(s): Dennis Melentyev <dennis.melentyev@infopulse.com.ua>
  19. */
  20. /* This file provides JavaScript functions to be included once one wish
  21. * to add a hide/reveal/collapse per-class functionality
  22. *
  23. *
  24. * This file contains hide/reveal API for customizable page views
  25. * TUI stands for Tweak UI.
  26. *
  27. * See bug 262592 for usage examples.
  28. *
  29. * Note: this interface is experimental and under development.
  30. * We may and probably will make breaking changes to it in the future.
  31. */
  32. var TUIClasses = new Array;
  33. var TUICookiesEnabled = -1;
  34. // Internal function to demangle cookies
  35. function TUI_demangle(value) {
  36. var pair;
  37. var pairs = value.split(",");
  38. for (i = 0; i < pairs.length; i++) {
  39. pair = pairs[i].split(":");
  40. if (pair[0] != null && pair[1] != null)
  41. TUIClasses[pair[0]] = pair[1];
  42. }
  43. }
  44. /* TUI_tweak: Function to redraw whole document.
  45. * Also, initialize TUIClasses array with defaults, then override it
  46. * with values from cookie
  47. */
  48. function TUI_tweak( cookiesuffix, classes ) {
  49. var dc = document.cookie;
  50. var begin = -1;
  51. var end = 0;
  52. // Register classes and their defaults
  53. TUI_demangle(classes);
  54. if (TUICookiesEnabled > 0) {
  55. // If cookies enabled, process them
  56. TUI_demangle(TUI_getCookie(cookiesuffix));
  57. }
  58. else if (TUICookiesEnabled == -1) {
  59. // If cookies availability not checked yet since browser does
  60. // not has navigator.cookieEnabled property, let's check it manualy
  61. var cookie = TUI_getCookie(cookiesuffix);
  62. if (cookie.length == 0)
  63. {
  64. TUI_setCookie(cookiesuffix);
  65. // Cookies are definitely disabled for JS.
  66. if (TUI_getCookie(cookiesuffix).length == 0)
  67. TUICookiesEnabled = 0;
  68. else
  69. TUICookiesEnabled = 1;
  70. }
  71. else {
  72. // Have cookie set, pretend to be able to reset them later on
  73. TUI_demangle(cookie);
  74. TUICookiesEnabled = 1;
  75. }
  76. }
  77. if (TUICookiesEnabled > 0) {
  78. var els = document.getElementsByTagName('*');
  79. for (i = 0; i < els.length; i++) {
  80. if (null != TUIClasses[els[i].className]) {
  81. TUI_apply(els[i], TUIClasses[els[i].className]);
  82. }
  83. }
  84. }
  85. return;
  86. }
  87. // TUI_apply: Function to draw certain element.
  88. // Receives element itself and style value: hide, reveal or collapse
  89. function TUI_apply(element, value) {
  90. if (TUICookiesEnabled > 0 && element != null) {
  91. switch (value)
  92. {
  93. case 'hide':
  94. element.style.visibility="hidden";
  95. break;
  96. case 'collapse':
  97. element.style.visibility="hidden";
  98. element.style.display="none";
  99. break;
  100. case 'reveal': // Shown item must expand
  101. default: // The default is to show & expand
  102. element.style.visibility="visible";
  103. element.style.display="";
  104. break;
  105. }
  106. }
  107. }
  108. // TUI_change: Function to process class.
  109. // Usualy called from onclick event of button
  110. function TUI_change(cookiesuffix, clsname, action) {
  111. if (TUICookiesEnabled > 0) {
  112. var els, i;
  113. els = document.getElementsByTagName('*');
  114. for (i=0; i<els.length; i++) {
  115. if (els[i].className.match(clsname)) {
  116. TUI_apply(els[i], action);
  117. }
  118. }
  119. TUIClasses[clsname]=action;
  120. TUI_setCookie(cookiesuffix);
  121. }
  122. }
  123. // TUI_setCookie: Function to set TUI cookie.
  124. // Used internally
  125. function TUI_setCookie(cookiesuffix) {
  126. var cookieval = "";
  127. var expireOn = new Date();
  128. expireOn.setYear(expireOn.getFullYear() + 25);
  129. for (clsname in TUIClasses) {
  130. if (cookieval.length > 0)
  131. cookieval += ",";
  132. cookieval += clsname+":"+TUIClasses[clsname];
  133. }
  134. document.cookie="Bugzilla_TUI_"+cookiesuffix+"="+cookieval+"; expires="+expireOn.toString();
  135. }
  136. // TUI_getCookie: Function to get TUI cookie.
  137. // Used internally
  138. function TUI_getCookie(cookiesuffix) {
  139. var dc = document.cookie;
  140. var begin, end;
  141. var cookiePrefix = "Bugzilla_TUI_"+cookiesuffix+"=";
  142. begin = dc.indexOf(cookiePrefix, end);
  143. if (begin != -1) {
  144. begin += cookiePrefix.length;
  145. end = dc.indexOf(";", begin);
  146. if (end == -1) {
  147. end = dc.length;
  148. }
  149. return unescape(dc.substring(begin, end));
  150. }
  151. return "";
  152. }