Changeset 1171

Show
Ignore:
Timestamp:
09/03/10 17:28:06 (17 months ago)
Author:
zeank
Message:

when submitting new comment make sure we have them expanded (refs #203)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • HelpIM3/htdocs/functions.js

    r1170 r1171  
    3636    _registeredEvents: {}, 
    3737 
     38    // quirks mode cookies -- http://www.quirksmode.org/js/cookies.html 
     39    createCookie: function(name,value,days) { 
     40        if (days) { 
     41            var date = new Date(); 
     42            date.setTime(date.getTime()+(days*24*60*60*1000)); 
     43            var expires = "; expires="+date.toGMTString(); 
     44        } 
     45        else var expires = ""; 
     46        document.cookie = name+"="+value+expires+"; path=/"; 
     47    }, 
     48 
     49    el: { 
     50        byId: function(id) { 
     51            return document.getElementById(id); 
     52        }, 
     53 
     54        firstChild: function(element, nodeName) { 
     55            if (typeof element == 'string') 
     56                element = HIM.el.byId(element); 
     57            if (!element) 
     58                return; 
     59            for (var i=0; i<element.childNodes.length; i++) { 
     60                if (element.childNodes.item(i).nodeName && 
     61                    element.childNodes.item(i).nodeName.toLowerCase() == nodeName.toLowerCase()) 
     62                    return element.childNodes.item(i); 
     63            } 
     64        }, 
     65 
     66        hide: function(el) { 
     67            if (typeof el == 'string') // asume id given 
     68                el = HIM.el.byId(el); 
     69            el.style.display = 'none'; 
     70        }, 
     71 
     72        next: function(element, nodeName) { 
     73            // searches for next sibling with given nodeName for element 
     74            return this._sibling('next', element, nodeName); 
     75        }, 
     76 
     77        parentNode: function(element, nodeName) { 
     78            if (typeof element == 'string') 
     79                element = HIM.el.byId(element); 
     80            if (!element) 
     81                return; 
     82            var parent = element.parentNode; 
     83            while (parent.nodeName.toLowerCase() != nodeName.toLowerCase() && 
     84                   parent.parentNode) 
     85                parent = parent.parentNode; 
     86            if (parent.nodeName && parent.nodeName.toLowerCase() == nodeName.toLowerCase()) 
     87                return parent; 
     88            return; 
     89        }, 
     90 
     91        prev: function(element, nodeName) { 
     92            // search for previous sibling with given nodeName for element 
     93            return this._sibling('prev', element, nodeName); 
     94        }, 
     95 
     96        show: function(el) { 
     97            if (typeof el == 'string') // asume id given 
     98                el = HIM.el.byId(el); 
     99            el.style.display = ''; 
     100        }, 
     101 
     102        toggle: function(el, activateFun, deactivateFun) { 
     103            if (typeof el == 'string') // asume id given 
     104                el = HIM.el.byId(el); 
     105            if (el.style.display == 'none') { 
     106                this.show(el); 
     107                if (activateFun && typeof activateFun == 'function') 
     108                    activateFun(); 
     109            } else { 
     110                this.hide(el); 
     111                if (deactivateFun && typeof deactivateFun == 'function') 
     112                    deactivateFun(); 
     113            } 
     114        }, 
     115 
     116        _sibling: function(dir, element, nodeName) { 
     117            if (typeof element == 'string') 
     118                element = HIM.el.byId(element); 
     119            if (!element) 
     120                return; 
     121            dir = (dir == 'next')?'nextSibling':'previousSibling'; 
     122            var sibling = element[dir]; 
     123            if (!sibling || !sibling.nodeName) 
     124                return; 
     125            if (nodeName && typeof nodeName == 'string' && nodeName != '') { 
     126                nodeName = nodeName.toLowerCase(); 
     127                while (sibling.nodeName.toLowerCase() != nodeName && sibling[dir]) 
     128                    sibling = sibling[dir]; 
     129                if (sibling.nodeName.toLowerCase() != nodeName) 
     130                    return; 
     131            } 
     132            return sibling; 
     133        } 
     134    }, 
     135 
     136    fromJSON: function (str) { 
     137        try { 
     138            return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test( 
     139                str.replace(/"(\\.|[^"\\])*"/g, ''))) && 
     140                eval('(' + str + ')'); 
     141        } catch (e) { 
     142            return false; 
     143        } 
     144    }, 
     145 
    38146    handleEvent: function(event) { 
    39147 
     
    44152            } 
    45153        } 
     154    }, 
     155 
     156    // quirks mode cookies -- http://www.quirksmode.org/js/cookies.html 
     157    readCookie: function(name) { 
     158        var nameEQ = name + "="; 
     159        var ca = document.cookie.split(';'); 
     160        for(var i=0;i < ca.length;i++) { 
     161            var c = ca[i]; 
     162            while (c.charAt(0)==' ') c = c.substring(1,c.length); 
     163            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 
     164        } 
     165        return null; 
    46166    }, 
    47167 
     
    72192    }, 
    73193 
    74     el: { 
    75         byId: function(id) { 
    76             return document.getElementById(id); 
     194    toJSON: function (obj) { 
     195        var m = { 
     196            '\b': '\\b', 
     197            '\t': '\\t', 
     198            '\n': '\\n', 
     199            '\f': '\\f', 
     200            '\r': '\\r', 
     201            '"' : '\\"', 
     202            '\\': '\\\\' 
    77203        }, 
    78  
    79         hide: function(el) { 
    80             if (typeof el == 'string') // asume id given 
    81                 el = HIM.el.byId(el); 
    82             el.style.display = 'none'; 
    83         }, 
    84  
    85         show: function(el) { 
    86             if (typeof el == 'string') // asume id given 
    87                 el = HIM.el.byId(el); 
    88             el.style.display = ''; 
    89         }, 
    90  
    91         toggle: function(el, activateFun, deactivateFun) { 
    92             if (typeof el == 'string') // asume id given 
    93                 el = HIM.el.byId(el); 
    94             if (el.style.display == 'none') { 
    95                 this.show(el); 
    96                 if (activateFun && typeof activateFun == 'function') 
    97                     activateFun(); 
    98             } else { 
    99                 this.hide(el); 
    100                 if (deactivateFun && typeof deactivateFun == 'function') 
    101                     deactivateFun(); 
     204        s = { 
     205            array: function (x) { 
     206                var a = ['['], b, f, i, l = x.length, v; 
     207                for (i = 0; i < l; i += 1) { 
     208                    v = x[i]; 
     209                    f = s[typeof v]; 
     210                    if (f) { 
     211                        try { 
     212                            v = f(v); 
     213                            if (typeof v == 'string') { 
     214                                if (b) { 
     215                                    a[a.length] = ','; 
     216                                } 
     217                                a[a.length] = v; 
     218                                b = true; 
     219                            } 
     220                        } catch(e) {  
     221                        } 
     222                    } 
     223                } 
     224                a[a.length] = ']'; 
     225                return a.join(''); 
     226            }, 
     227            'boolean': function (x) { 
     228                return String(x); 
     229            }, 
     230            'null': function (x) { 
     231                return "null"; 
     232            }, 
     233            number: function (x) { 
     234                return isFinite(x) ? String(x) : 'null'; 
     235            }, 
     236            object: function (x) { 
     237                if (x) { 
     238                    if (x instanceof Array) { 
     239                        return s.array(x); 
     240                    } 
     241                    var a = ['{'], b, f, i, v; 
     242                    for (i in x) { 
     243                        if (x.hasOwnProperty(i)) { 
     244                            v = x[i]; 
     245                            f = s[typeof v]; 
     246                            if (f) { 
     247                                try { 
     248                                    v = f(v); 
     249                                    if (typeof v == 'string') { 
     250                                        if (b) { 
     251                                            a[a.length] = ','; 
     252                                        } 
     253                                        a.push(s.string(i), ':', v); 
     254                                        b = true; 
     255                                    } 
     256                                } catch(e) { 
     257                                } 
     258                            } 
     259                        } 
     260                    } 
     261                     
     262                    a[a.length] = '}'; 
     263                    return a.join(''); 
     264                } 
     265                return 'null'; 
     266            }, 
     267            string: function (x) { 
     268                if (/["\\\x00-\x1f]/.test(x)) { 
     269                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) { 
     270                        var c = m[b]; 
     271                        if (c) { 
     272                            return c; 
     273                        } 
     274                        c = b.charCodeAt(); 
     275                        return '\\u00' + 
     276                            Math.floor(c / 16).toString(16) + 
     277                            (c % 16).toString(16); 
     278                    }); 
     279                } 
     280                return '"' + x + '"'; 
    102281            } 
    103         }, 
    104  
    105         next: function(element, nodeName) { 
    106             // searches for next sibling with given nodeName for element 
    107             return this._sibling('next', element, nodeName); 
    108         }, 
    109  
    110         prev: function(element, nodeName) { 
    111             // search for previous sibling with given nodeName for element 
    112             return this._sibling('prev', element, nodeName); 
    113         }, 
    114  
    115         _sibling: function(dir, element, nodeName) { 
    116             if (typeof element == 'string') 
    117                 element = HIM.el.byId(element); 
    118             if (!element) 
    119                 return; 
    120             dir = (dir == 'next')?'nextSibling':'previousSibling'; 
    121             var sibling = element[dir]; 
    122             if (!sibling || !sibling.nodeName) 
    123                 return; 
    124             if (nodeName && typeof nodeName == 'string' && nodeName != '') { 
    125                 nodeName = nodeName.toLowerCase(); 
    126                 while (sibling.nodeName.toLowerCase() != nodeName && sibling[dir]) 
    127                     sibling = sibling[dir]; 
    128                 if (sibling.nodeName.toLowerCase() != nodeName) 
    129                     return; 
    130             } 
    131             return sibling; 
    132         }, 
    133  
    134         firstChild: function(element, nodeName) { 
    135             if (typeof element == 'string') 
    136                 element = HIM.el.byId(element); 
    137             if (!element) 
    138                 return; 
    139             for (var i=0; i<element.childNodes.length; i++) { 
    140                 if (element.childNodes.item(i).nodeName && 
    141                     element.childNodes.item(i).nodeName.toLowerCase() == nodeName.toLowerCase()) 
    142                     return element.childNodes.item(i); 
    143             } 
    144         }, 
    145  
    146         parentNode: function(element, nodeName) { 
    147             if (typeof element == 'string') 
    148                 element = HIM.el.byId(element); 
    149             if (!element) 
    150                 return; 
    151             var parent = element.parentNode; 
    152             while (parent.nodeName.toLowerCase() != nodeName.toLowerCase() && 
    153                    parent.parentNode) 
    154                 parent = parent.parentNode; 
    155             if (parent.nodeName && parent.nodeName.toLowerCase() == nodeName.toLowerCase()) 
    156                 return parent; 
    157             return; 
     282        }; 
     283 
     284        switch (typeof(obj)) { 
     285        case 'object': 
     286            return s.object(obj); 
     287        case 'array': 
     288            return s.array(obj); 
     289             
    158290        } 
    159291    } 
     
    544676 
    545677function expandComments(toggleButton) { 
     678    if (typeof toggleButton == 'string') 
     679        toggleButton = HIM.el.byId(toggleButton); 
     680 
    546681    _toggleComments( 
    547682        toggleButton, 
     
    550685            toggleButton.innerHTML = '-'; 
    551686            toggleButton.title = 'collapse comments'; 
     687             
     688            commentsExpanded[toggleButton.id] = true; 
     689            HIM.createCookie('commentsExpanded', HIM.toJSON(commentsExpanded)); 
    552690        }); 
    553691} 
    554692 
    555693function collapseComments(toggleButton) { 
     694    if (typeof toggleButton == 'string') 
     695        toggleButton = HIM.el.byId(toggleButton); 
     696 
    556697    _toggleComments( 
    557698        toggleButton, 
     
    560701            toggleButton.innerHTML = '+'; 
    561702            toggleButton.title = 'expand comments'; 
     703 
     704            delete commentsExpanded[toggleButton.id]; 
     705            HIM.createCookie('commentsExpanded', HIM.toJSON(commentsExpanded)); 
    562706        }); 
    563707} 
     
    587731HIM.registerHandler('onload', function() { 
    588732    // make comments collapsible 
    589  
    590733    var comment_rows = document.getElementsByClassName('comment_row'); 
    591734 
     
    616759 
    617760    comment_rows.forEach( 
    618         function(comment_row) { 
     761        function(comment_row,idx) { 
    619762            var logMessage = HIM.el.prev(HIM.el.prev(comment_row, 'tr'), 'tr'); 
    620763            if (!logMessage) 
     
    635778            var td = logMessage.appendChild(document.createElement('td')); 
    636779            td.className = 'comments_toggleButton'; 
     780            td.id = 'comments_toggleButton_'+idx; 
    637781            var text = td.appendChild(document.createTextNode('+')); 
    638782 
     
    657801    ); 
    658802 
     803    commentsExpanded = HIM.fromJSON(HIM.readCookie('commentsExpanded')) || {}; 
     804    for (i in commentsExpanded) (expandComments(i)); 
     805 
    659806    // don't send empty form 
    660807    document.getElementsByClassName('comment', 'form').forEach( 
     
    663810                if (form['comment_text'].value == '') 
    664811                    return false; 
     812                 
     813                // expand comments - just to make sure 
     814                var logMessage = HIM.el.prev( 
     815                    HIM.el.parentNode(form, 'tr'), 
     816                    'tr' 
     817                ); 
     818 
     819                for (var i=0; i<logMessage.childNodes.length; i++) 
     820                    if (logMessage.childNodes.item(i).className == 'comments_toggleButton') 
     821                        expandComments(logMessage.childNodes.item(i)); 
     822 
     823                return true; 
    665824            } 
    666825        });