[ Index ] |
|
Code source de Kupu-1.3.5 |
1 // turn this into a nice module-like namespace to avoid messing up the global 2 // (window) namespace 3 this.kuputoolcollapser = new function() { 4 var ToolCollapser = function(toolboxesparentid) { 5 this.parent = document.getElementById(toolboxesparentid); 6 }; 7 8 // make the collapser available in the namespace 9 this.Collapser = ToolCollapser; 10 11 ToolCollapser.prototype.initialize = function() { 12 var initial_state = {}; 13 if (navigator.cookieEnabled) { 14 var cookie = document.cookie; 15 var reg = /initial_state=([^;]+);?/; 16 var match = cookie.match(reg); 17 if (match) { 18 eval(unescape(match[0])); 19 }; 20 }; 21 for (var i=0; i < this.parent.childNodes.length; i++) { 22 var child = this.parent.childNodes[i]; 23 if (child.className == 'kupu-toolbox') { 24 var heading = child.getElementsByTagName('h1')[0]; 25 if (!heading) { 26 throw('heading not found by collapser for toolbox ' + 27 child.id); 28 }; 29 heading.setAttribute('title', _('click to unfold')); 30 // find the toolbox's body 31 var body = this.getToolBody(child); 32 // now set a handler that makes the body display and hide 33 // on click, and register it to the heading 34 // WAAAAAHHHH!!! since there's some weird shit happening when 35 // I just use closures to refer to the body (somehow only the 36 // *last* value body is set to in this loop is used?!?) I 37 // used a reference to the body as 'this' in the handler 38 var handler = function(heading) { 39 if (this.style.display == 'none') { 40 // assume we have a block-level element here... 41 this.style.display = 'block'; 42 heading.className = 'kupu-toolbox-heading-opened'; 43 heading.setAttribute('title', _('click to fold')); 44 } else { 45 this.style.display = 'none'; 46 heading.className = 'kupu-toolbox-heading-closed'; 47 heading.setAttribute('title', _('click to unfold')); 48 }; 49 }; 50 var wrap_openhandler = function(body, heading) { 51 return function() { 52 body.style.display = 'block'; 53 heading.className = 'kupu-toolbox-heading-opened'; 54 }; 55 }; 56 addEventHandler(heading, 'click', handler, body, heading); 57 if (initial_state[child.id] === undefined || 58 initial_state[child.id] == '0') { 59 body.style.display = 'none'; 60 } else { 61 heading.className = 'kupu-toolbox-heading-opened'; 62 heading.setAttribute('title', _('click to fold')); 63 }; 64 // add a reference to the openhandler on the toolbox div 65 // so any toolbox code can use that to open the toolbox if 66 // it so desires 67 child.open_handler = wrap_openhandler(body, heading); 68 }; 69 }; 70 71 addEventHandler(window, 'beforeunload', this.saveState, this); 72 }; 73 74 ToolCollapser.prototype.getToolBody = function(tool) { 75 var heading = tool.getElementsByTagName('h1')[0]; 76 var currchild = heading.nextSibling; 77 while (currchild.nodeType != 1) { 78 currchild = currchild.nextSibling; 79 if (!currchild) { 80 throw('body not found by collapser for toolbox ' + 81 child.id); 82 }; 83 }; 84 return currchild; 85 }; 86 87 ToolCollapser.prototype.saveState = function() { 88 /* save collapse state of the toolboxes in a cookie */ 89 if (!navigator.cookieEnabled) { 90 return; 91 }; 92 var current_state = {}; 93 for (var i=0; i < this.parent.childNodes.length; i++) { 94 var child = this.parent.childNodes[i]; 95 if (child.nodeType != 1) { 96 continue; 97 }; 98 var body = this.getToolBody(child); 99 current_state[child.id] = body.style.display == 'none' ? '0' : '1'; 100 }; 101 102 var exp = new Date(); 103 // 100 years before state is lost... should be enough ;) 104 exp.setTime(exp.getTime() + (100 * 365 * 24 * 60 * 60 * 1000)); 105 var cookie = 'initial_state=' + 106 escape(this.serializeMapping(current_state)) + 107 ';' + 108 'expires=' + exp.toGMTString() + ';' + 109 'path=/'; 110 document.cookie = cookie; 111 }; 112 113 ToolCollapser.prototype.serializeMapping = function(mapping) { 114 /* serializes the config dict into a string that can be evalled 115 116 works only for dicts with string values 117 */ 118 if (typeof(mapping) == 'string') { 119 return "'" + mapping + "'"; 120 }; 121 var ret = '{'; 122 var first = true; 123 for (var key in mapping) { 124 if (!first) { 125 ret += ', '; 126 }; 127 ret += "'" + key + "': " + 128 this.serializeMapping(mapping[key]); 129 first = false; 130 }; 131 return ret + '}'; 132 }; 133 }();
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 15:30:41 2007 | par Balluche grâce à PHPXref 0.7 |