| 1 | /* |
|---|
| 2 | Development Library |
|---|
| 3 | $Id$ |
|---|
| 4 | |
|---|
| 5 | Copyright (c) 2006, Six Apart, Ltd. |
|---|
| 6 | All rights reserved. |
|---|
| 7 | |
|---|
| 8 | Redistribution and use in source and binary forms, with or without |
|---|
| 9 | modification, are permitted provided that the following conditions are |
|---|
| 10 | met: |
|---|
| 11 | |
|---|
| 12 | * Redistributions of source code must retain the above copyright |
|---|
| 13 | notice, this list of conditions and the following disclaimer. |
|---|
| 14 | |
|---|
| 15 | * Redistributions in binary form must reproduce the above |
|---|
| 16 | copyright notice, this list of conditions and the following disclaimer |
|---|
| 17 | in the documentation and/or other materials provided with the |
|---|
| 18 | distribution. |
|---|
| 19 | |
|---|
| 20 | * Neither the name of "Six Apart" nor the names of its |
|---|
| 21 | contributors may be used to endorse or promote products derived from |
|---|
| 22 | this software without specific prior written permission. |
|---|
| 23 | |
|---|
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 25 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 26 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 27 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 28 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 29 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 30 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 31 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 32 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 33 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 34 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 35 | */ |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | /* benchmarking */ |
|---|
| 39 | |
|---|
| 40 | benchmark = function( callback, iterations ) { |
|---|
| 41 | var start = new Date(); |
|---|
| 42 | for( var i = 0; i < iterations; i++ ) |
|---|
| 43 | callback(); |
|---|
| 44 | var end = new Date(); |
|---|
| 45 | return (end.getSeconds() - start.getSeconds()) + |
|---|
| 46 | (end.getMilliseconds() - start.getMilliseconds()) / 1000; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | inspect = function( object, allProperties, noBreaks ) { |
|---|
| 51 | var out = ""; |
|---|
| 52 | for( var property in object ) { |
|---|
| 53 | try { |
|---|
| 54 | if( !allProperties && !object.hasOwnProperty( property ) ) |
|---|
| 55 | continue; |
|---|
| 56 | out += property + ": " + object[ property ] + |
|---|
| 57 | (noBreaks ? "\n" : "<br />"); |
|---|
| 58 | } catch( e ) {} |
|---|
| 59 | } |
|---|
| 60 | return out; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | /* logging, alert override */ |
|---|
| 65 | |
|---|
| 66 | Logger = new Class( Object, { |
|---|
| 67 | width: 320, |
|---|
| 68 | height: 240, |
|---|
| 69 | windowName: "log", |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | log: function() { |
|---|
| 73 | try { |
|---|
| 74 | // concat arguments |
|---|
| 75 | var args = []; |
|---|
| 76 | for( var i = 0; i < arguments.length; i++ ) |
|---|
| 77 | args[ i ] = arguments[ i ]; |
|---|
| 78 | var msg = args.join( "" ); |
|---|
| 79 | |
|---|
| 80 | // create window |
|---|
| 81 | this.createWindow(); |
|---|
| 82 | |
|---|
| 83 | // check for no window |
|---|
| 84 | if( !this.window ) { |
|---|
| 85 | confirm( "Logger popup window blocked. Using confirm() instead.\n\n" + msg ); |
|---|
| 86 | return true; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | // create div |
|---|
| 90 | var div = this.window.document.createElement( "div" ); |
|---|
| 91 | div.style.backgroundColor = (this.count % 2) ? "#eee" : "#fff"; |
|---|
| 92 | div.style.width = "auto"; |
|---|
| 93 | div.style.padding = "3px"; |
|---|
| 94 | div.innerHTML = msg; |
|---|
| 95 | |
|---|
| 96 | // append to window |
|---|
| 97 | this.window.document.body.appendChild( div ); |
|---|
| 98 | this.window.scroll( 0, this.window.document.body.scrollHeight ); |
|---|
| 99 | this.count++; |
|---|
| 100 | return true; |
|---|
| 101 | } catch( e ) {} |
|---|
| 102 | }, |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | createWindow: function() { |
|---|
| 106 | if( this.window && this.window.document ) |
|---|
| 107 | return; |
|---|
| 108 | |
|---|
| 109 | // create window |
|---|
| 110 | var x = "auto"; |
|---|
| 111 | var y = "auto"; |
|---|
| 112 | var attr = "resizable=yes, menubar=no, location=no, directories=no, scrollbars=yes, status=no, " + |
|---|
| 113 | "width=" + this.width + ", height=" + this.height + |
|---|
| 114 | "screenX=" + x + ", screenY=" + y + ", " + |
|---|
| 115 | "left=" + x + ", top=" + y + ", "; |
|---|
| 116 | this.window = window.open( "", this.windowName, attr ); |
|---|
| 117 | |
|---|
| 118 | // check for blocked popup |
|---|
| 119 | if( !this.window ) |
|---|
| 120 | return; |
|---|
| 121 | |
|---|
| 122 | var instance; |
|---|
| 123 | try { |
|---|
| 124 | instance = this.window.__Logger; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | catch( e ) { |
|---|
| 128 | this.window.location.replace( "about:blank" ); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | // check for pre-existing instance |
|---|
| 132 | if( instance ) { |
|---|
| 133 | // create divider div |
|---|
| 134 | var div = this.window.document.createElement( "div" ); |
|---|
| 135 | div.style.backgroundColor = "#f00"; |
|---|
| 136 | div.style.width = "auto"; |
|---|
| 137 | div.style.height = "2px"; |
|---|
| 138 | div.style.fontSize = "0.1px"; |
|---|
| 139 | div.style.lineHeight = "0.1px"; |
|---|
| 140 | this.window.document.body.appendChild( div ); |
|---|
| 141 | } |
|---|
| 142 | else { |
|---|
| 143 | // write body |
|---|
| 144 | this.window.document.open( "text/html", "replace" ); |
|---|
| 145 | this.window.document.write( "<html><head><title>JavaScript Loggers</title></head><body></body></html>" ); |
|---|
| 146 | this.window.document.close(); |
|---|
| 147 | |
|---|
| 148 | // setup style |
|---|
| 149 | this.window.title = "JavaScript Loggers"; |
|---|
| 150 | this.window.document.body.style.margin = "0"; |
|---|
| 151 | this.window.document.body.style.padding = "0"; |
|---|
| 152 | this.window.document.body.style.fontFamily = "verdana, 'lucida grande', geneva, arial, helvetica, sans-serif"; |
|---|
| 153 | this.window.document.body.style.fontSize = "10px"; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | // get previous instance and attach new instance |
|---|
| 157 | this.prev = instance; |
|---|
| 158 | this.window.__Logger = this; |
|---|
| 159 | |
|---|
| 160 | // dereference previous previous |
|---|
| 161 | if( this.prev ) |
|---|
| 162 | this.prev.prev = null; |
|---|
| 163 | |
|---|
| 164 | // copy message count |
|---|
| 165 | this.count = this.prev ? this.prev.count : 0; |
|---|
| 166 | } |
|---|
| 167 | } ); |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | log = |
|---|
| 171 | Logger.log = function() { |
|---|
| 172 | if( !Logger.singleton ) |
|---|
| 173 | Logger.singleton = new Logger(); |
|---|
| 174 | if( Logger.singleton ) |
|---|
| 175 | return Logger.singleton.log.apply( Logger.singleton, arguments ); |
|---|
| 176 | |
|---|
| 177 | return true; |
|---|
| 178 | } |
|---|