aboutsummaryrefslogtreecommitdiffstats
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/TUI.js208
-rw-r--r--js/util.js18
-rw-r--r--js/yui/cookie.js7
3 files changed, 99 insertions, 134 deletions
diff --git a/js/TUI.js b/js/TUI.js
index 6ebc7c717..aa7f2fff4 100644
--- a/js/TUI.js
+++ b/js/TUI.js
@@ -16,153 +16,93 @@
* Rights Reserved.
*
* Contributor(s): Dennis Melentyev <dennis.melentyev@infopulse.com.ua>
+ * Max Kanat-Alexander <mkanat@bugzilla.org>
*/
- /* This file provides JavaScript functions to be included once one wish
- * to add a hide/reveal/collapse per-class functionality
- *
- *
- * This file contains hide/reveal API for customizable page views
- * TUI stands for Tweak UI.
- *
- * See bug 262592 for usage examples.
- *
- * Note: this interface is experimental and under development.
- * We may and probably will make breaking changes to it in the future.
- */
-
- var TUIClasses = new Array;
- var TUICookiesEnabled = -1;
+/* This file provides JavaScript functions to be included when one wishes
+ * to show/hide certain UI elements, and have the state of them being
+ * shown/hidden stored in a cookie.
+ *
+ * TUI stands for Tweak UI.
+ *
+ * Requires js/util.js and the YUI Dom and Cookie libraries.
+ *
+ * See template/en/default/bug/create/create.html.tmpl for a usage example.
+ */
- // Internal function to demangle cookies
- function TUI_demangle(value) {
- var pair;
- var pairs = value.split(",");
- for (i = 0; i < pairs.length; i++) {
- pair = pairs[i].split(":");
- if (pair[0] != null && pair[1] != null)
- TUIClasses[pair[0]] = pair[1];
- }
- }
+var TUI_HIDDEN_CLASS = 'bz_tui_hidden';
+var TUI_COOKIE_NAME = 'TUI';
- /* TUI_tweak: Function to redraw whole document.
- * Also, initialize TUIClasses array with defaults, then override it
- * with values from cookie
- */
+var TUI_alternates = new Array();
- function TUI_tweak( cookiesuffix, classes ) {
- var dc = document.cookie;
- var begin = -1;
- var end = 0;
+/**
+ * Hides a particular class of elements if they are shown,
+ * or shows them if they are hidden. Then it stores whether that
+ * class is now hidden or shown.
+ *
+ * @param className The name of the CSS class to hide.
+ */
+function TUI_toggle_class(className) {
+ var elements = YAHOO.util.Dom.getElementsByClassName(className);
+ for (var i = 0; i < elements.length; i++) {
+ bz_toggleClass(elements[i], TUI_HIDDEN_CLASS);
+ }
+ _TUI_save_class_state(elements, className);
+ _TUI_toggle_control_link(className);
+}
- // Register classes and their defaults
- TUI_demangle(classes);
- if (TUICookiesEnabled > 0) {
- // If cookies enabled, process them
- TUI_demangle(TUI_getCookie(cookiesuffix));
- }
- else if (TUICookiesEnabled == -1) {
- // If cookies availability not checked yet since browser does
- // not has navigator.cookieEnabled property, let's check it manualy
- var cookie = TUI_getCookie(cookiesuffix);
- if (cookie.length == 0)
- {
- TUI_setCookie(cookiesuffix);
- // Cookies are definitely disabled for JS.
- if (TUI_getCookie(cookiesuffix).length == 0)
- TUICookiesEnabled = 0;
- else
- TUICookiesEnabled = 1;
- }
- else {
- // Have cookie set, pretend to be able to reset them later on
- TUI_demangle(cookie);
- TUICookiesEnabled = 1;
- }
- }
-
- if (TUICookiesEnabled > 0) {
- var els = document.getElementsByTagName('*');
- for (i = 0; i < els.length; i++) {
- if (null != TUIClasses[els[i].className]) {
- TUI_apply(els[i], TUIClasses[els[i].className]);
+/**
+ * Specifies that a certain class of items should be hidden by default,
+ * if the user doesn't have a TUI cookie.
+ *
+ * @param className The class to hide by default.
+ */
+function TUI_hide_default(className) {
+ YAHOO.util.Event.onDOMReady(function () {
+ if (!YAHOO.util.Cookie.getSub('TUI', className)) {
+ TUI_toggle_class(className);
}
- }
- }
- return;
- }
+ });
+}
- // TUI_apply: Function to draw certain element.
- // Receives element itself and style value: hide, reveal or collapse
+function _TUI_toggle_control_link(className) {
+ var link = document.getElementById(className + "_controller");
+ var original_text = link.innerHTML;
+ link.innerHTML = TUI_alternates[className];
+ TUI_alternates[className] = original_text;
+}
- function TUI_apply(element, value) {
- if (TUICookiesEnabled > 0 && element != null) {
- switch (value)
- {
- case 'hide':
- element.style.visibility="hidden";
- break;
- case 'collapse':
- element.style.visibility="hidden";
- element.style.display="none";
- break;
- case 'reveal': // Shown item must expand
- default: // The default is to show & expand
- element.style.visibility="visible";
- element.style.display="";
- break;
- }
+function _TUI_save_class_state(elements, aClass) {
+ // We just check the first element to see if it's hidden or not, and
+ // consider that all elements are the same.
+ if (YAHOO.util.Dom.hasClass(elements[0], TUI_HIDDEN_CLASS)) {
+ _TUI_store(aClass, 0);
+ }
+ else {
+ _TUI_store(aClass, 1);
}
- }
+}
- // TUI_change: Function to process class.
- // Usualy called from onclick event of button
+function _TUI_store(aClass, state) {
+ YAHOO.util.Cookie.setSub(TUI_COOKIE_NAME, aClass, state,
+ {
+ expires: new Date('January 1, 2038'),
+ path: BUGZILLA.param.cookie_path
+ });
+}
- function TUI_change(cookiesuffix, clsname, action) {
- if (TUICookiesEnabled > 0) {
- var els, i;
- els = document.getElementsByTagName('*');
- for (i=0; i<els.length; i++) {
- if (els[i].className.match(clsname)) {
- TUI_apply(els[i], action);
+function _TUI_restore() {
+ var classes = YAHOO.util.Cookie.getSubs(TUI_COOKIE_NAME);
+ for (item in classes) {
+ if (classes[item] == 0) {
+ var elements = YAHOO.util.Dom.getElementsByClassName(item);
+ for (var i = 0; i < elements.length; i++) {
+ YAHOO.util.Dom.addClass(elements[i], 'bz_tui_hidden');
+ }
+ _TUI_toggle_control_link(item);
}
- }
- TUIClasses[clsname]=action;
- TUI_setCookie(cookiesuffix);
}
- }
-
- // TUI_setCookie: Function to set TUI cookie.
- // Used internally
+}
- function TUI_setCookie(cookiesuffix) {
- var cookieval = "";
- var expireOn = new Date();
- expireOn.setYear(expireOn.getFullYear() + 25);
- for (clsname in TUIClasses) {
- if (cookieval.length > 0)
- cookieval += ",";
- cookieval += clsname+":"+TUIClasses[clsname];
- }
- document.cookie="Bugzilla_TUI_"+cookiesuffix+"="+cookieval+"; expires="+expireOn.toString();
- }
-
- // TUI_getCookie: Function to get TUI cookie.
- // Used internally
-
- function TUI_getCookie(cookiesuffix) {
- var dc = document.cookie;
- var begin, end;
- var cookiePrefix = "Bugzilla_TUI_"+cookiesuffix+"=";
- begin = dc.indexOf(cookiePrefix, end);
- if (begin != -1) {
- begin += cookiePrefix.length;
- end = dc.indexOf(";", begin);
- if (end == -1) {
- end = dc.length;
- }
- return unescape(dc.substring(begin, end));
- }
- return "";
- }
+YAHOO.util.Event.onDOMReady(_TUI_restore);
diff --git a/js/util.js b/js/util.js
index 8e787847a..666f2666b 100644
--- a/js/util.js
+++ b/js/util.js
@@ -253,3 +253,21 @@ function bz_fireEvent(anElement, anEvent) {
evt.initEvent(anEvent, true, true); // event type, bubbling, cancelable
return !anElement.dispatchEvent(evt);
}
+
+/**
+ * Adds a CSS class to an element if it doesn't have it. Removes the
+ * CSS class from the element if the element does have the class.
+ *
+ * Requires YUI's Dom library.
+ *
+ * @param anElement The element to toggle the class on
+ * @param aClass The name of the CSS class to toggle.
+ */
+function bz_toggleClass(anElement, aClass) {
+ if (YAHOO.util.Dom.hasClass(anElement, aClass)) {
+ YAHOO.util.Dom.removeClass(anElement, aClass);
+ }
+ else {
+ YAHOO.util.Dom.addClass(anElement, aClass);
+ }
+}
diff --git a/js/yui/cookie.js b/js/yui/cookie.js
new file mode 100644
index 000000000..0e020ebdb
--- /dev/null
+++ b/js/yui/cookie.js
@@ -0,0 +1,7 @@
+/*
+Copyright (c) 2008, Yahoo! Inc. All rights reserved.
+Code licensed under the BSD License:
+http://developer.yahoo.net/yui/license.txt
+version: 2.6.0
+*/
+YAHOO.namespace("util");YAHOO.util.Cookie={_createCookieString:function(B,D,C,A){var F=YAHOO.lang;var E=encodeURIComponent(B)+"="+(C?encodeURIComponent(D):D);if(F.isObject(A)){if(A.expires instanceof Date){E+="; expires="+A.expires.toGMTString();}if(F.isString(A.path)&&A.path!=""){E+="; path="+A.path;}if(F.isString(A.domain)&&A.domain!=""){E+="; domain="+A.domain;}if(A.secure===true){E+="; secure";}}return E;},_createCookieHashString:function(B){var D=YAHOO.lang;if(!D.isObject(B)){throw new TypeError("Cookie._createCookieHashString(): Argument must be an object.");}var C=new Array();for(var A in B){if(D.hasOwnProperty(B,A)&&!D.isFunction(B[A])&&!D.isUndefined(B[A])){C.push(encodeURIComponent(A)+"="+encodeURIComponent(String(B[A])));}}return C.join("&");},_parseCookieHash:function(E){var D=E.split("&"),F=null,C=new Object();if(E.length>0){for(var B=0,A=D.length;B<A;B++){F=D[B].split("=");C[decodeURIComponent(F[0])]=decodeURIComponent(F[1]);}}return C;},_parseCookieString:function(I,A){var J=new Object();if(YAHOO.lang.isString(I)&&I.length>0){var B=(A===false?function(K){return K;}:decodeURIComponent);if(/[^=]+=[^=;]?(?:; [^=]+=[^=]?)?/.test(I)){var G=I.split(/;\s/g);var H=null;var C=null;var E=null;for(var D=0,F=G.length;D<F;D++){E=G[D].match(/([^=]+)=/i);if(E instanceof Array){H=decodeURIComponent(E[1]);C=B(G[D].substring(E[1].length+1));}else{H=decodeURIComponent(G[D]);C=H;}J[H]=C;}}}return J;},get:function(A,B){var D=YAHOO.lang;var C=this._parseCookieString(document.cookie);if(!D.isString(A)||A===""){throw new TypeError("Cookie.get(): Cookie name must be a non-empty string.");}if(D.isUndefined(C[A])){return null;}if(!D.isFunction(B)){return C[A];}else{return B(C[A]);}},getSub:function(A,C,B){var E=YAHOO.lang;var D=this.getSubs(A);if(D!==null){if(!E.isString(C)||C===""){throw new TypeError("Cookie.getSub(): Subcookie name must be a non-empty string.");}if(E.isUndefined(D[C])){return null;}if(!E.isFunction(B)){return D[C];}else{return B(D[C]);}}else{return null;}},getSubs:function(A){if(!YAHOO.lang.isString(A)||A===""){throw new TypeError("Cookie.getSubs(): Cookie name must be a non-empty string.");}var B=this._parseCookieString(document.cookie,false);if(YAHOO.lang.isString(B[A])){return this._parseCookieHash(B[A]);}return null;},remove:function(B,A){if(!YAHOO.lang.isString(B)||B===""){throw new TypeError("Cookie.remove(): Cookie name must be a non-empty string.");}A=A||{};A.expires=new Date(0);return this.set(B,"",A);},removeSub:function(B,D,A){if(!YAHOO.lang.isString(B)||B===""){throw new TypeError("Cookie.removeSub(): Cookie name must be a non-empty string.");}if(!YAHOO.lang.isString(D)||D===""){throw new TypeError("Cookie.removeSub(): Subcookie name must be a non-empty string.");}var C=this.getSubs(B);if(YAHOO.lang.isObject(C)&&YAHOO.lang.hasOwnProperty(C,D)){delete C[D];return this.setSubs(B,C,A);}else{return"";}},set:function(B,C,A){var E=YAHOO.lang;if(!E.isString(B)){throw new TypeError("Cookie.set(): Cookie name must be a string.");}if(E.isUndefined(C)){throw new TypeError("Cookie.set(): Value cannot be undefined.");}var D=this._createCookieString(B,C,true,A);document.cookie=D;return D;},setSub:function(B,D,C,A){var F=YAHOO.lang;if(!F.isString(B)||B===""){throw new TypeError("Cookie.setSub(): Cookie name must be a non-empty string.");}if(!F.isString(D)||D===""){throw new TypeError("Cookie.setSub(): Subcookie name must be a non-empty string.");}if(F.isUndefined(C)){throw new TypeError("Cookie.setSub(): Subcookie value cannot be undefined.");}var E=this.getSubs(B);if(!F.isObject(E)){E=new Object();}E[D]=C;return this.setSubs(B,E,A);},setSubs:function(B,C,A){var E=YAHOO.lang;if(!E.isString(B)){throw new TypeError("Cookie.setSubs(): Cookie name must be a string.");}if(!E.isObject(C)){throw new TypeError("Cookie.setSubs(): Cookie value must be an object.");}var D=this._createCookieString(B,this._createCookieHashString(C),false,A);document.cookie=D;return D;}};YAHOO.register("cookie",YAHOO.util.Cookie,{version:"2.6.0",build:"1321"}); \ No newline at end of file