Changeset 85 for trunk/jsolait/lib
- Timestamp:
- 12/21/06 12:17:13 (2 years ago)
- Files:
-
- trunk (modified) (1 prop)
- trunk/jsolait/lib/codecs.js (modified) (1 diff)
- trunk/jsolait/lib/crypto.js (modified) (1 diff)
- trunk/jsolait/lib/dom.js (modified) (1 diff)
- trunk/jsolait/lib/forms.js (modified) (1 diff)
- trunk/jsolait/lib/iter.js (deleted)
- trunk/jsolait/lib/itertools.js (copied) (copied from branches/experimental/jsolait/lib/itertools.js)
- trunk/jsolait/lib/jsonrpc.js (modified) (1 diff)
- trunk/jsolait/lib/lang.js (modified) (1 diff)
- trunk/jsolait/lib/logging.js (copied) (copied from branches/experimental/jsolait/lib/logging.js)
- trunk/jsolait/lib/net/sockets.js (modified) (1 diff)
- trunk/jsolait/lib/operators.js (modified) (1 diff)
- trunk/jsolait/lib/sets.js (modified) (1 diff)
- trunk/jsolait/lib/strings.js (modified) (1 diff)
- trunk/jsolait/lib/testing.js (modified) (1 diff)
- trunk/jsolait/lib/urllib.js (modified) (1 diff)
- trunk/jsolait/lib/wscript.js (copied) (copied from branches/experimental/jsolait/lib/wscript.js)
- trunk/jsolait/lib/xml.js (modified) (1 diff)
- trunk/jsolait/lib/xmlrpc.js (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk
- Property svn:ignore set to
build
- Property svn:ignore set to
trunk/jsolait/lib/codecs.js
r44 r85 28 28 @lastchangeddate $Date$ 29 29 */ 30 Module("codecs", "$Revision$", function(mod){ 31 32 /** 33 Returns all all available encoders. 34 @return An array of encoder names. 35 **/ 36 mod.listEncoders=function(){ 37 var c=[]; 38 for(var attr in String.prototype){ 39 if(attr.slice(0, 7) == "encode_"){ 40 c.push(attr.slice(7)); 30 __version__ = "$Revision$"; 31 /** 32 Returns all all available encoders. 33 @return An array of encoder names. 34 **/ 35 publ listEncoders(){ 36 var c=[]; 37 for(var attr in String.prototype){ 38 if(attr.slice(0, 7) == "encode_"){ 39 c.push(attr.slice(7)); 40 } 41 } 42 return c; 43 }; 44 /** 45 Returns all all available decoders. 46 @return An array of decoder names. 47 **/ 48 publ listDecoders(){ 49 var c=[]; 50 for(var attr in String.prototype){ 51 if(attr.slice(0, 7) == "decode_"){ 52 c.push(attr.slice(7)); 53 } 54 } 55 return c; 56 }; 57 58 /** 59 Decodes an encoded string. 60 Parameters but the codec parameter are forwardet to the codec. 61 @param codec The codec to use. 62 **/ 63 String.prototype.decode = function(codec){ 64 var n ="decode_" + codec; 65 if(String.prototype[n]){ 66 var args=[]; 67 for(var i=1;i<arguments.length;i++){ 68 args[i-1] = arguments[i]; 69 } 70 return String.prototype[n].apply(this, args); 71 }else{ 72 throw new mod.Exception("Decoder '%s' not found.".format(codec)); 73 } 74 }; 75 76 /** 77 Encodes a string. 78 Parameters but the codec parameter are forwardet to the codec. 79 @param codec The codec to use. 80 **/ 81 String.prototype.encode = function(codec){ 82 var n ="encode_" + codec; 83 if(String.prototype[n]){ 84 var args=[]; 85 for(var i=1;i<arguments.length;i++){ 86 args[i-1] = arguments[i]; 87 } 88 return String.prototype[n].apply(this, args); 89 }else{ 90 throw new mod.Exception("Ecnoder '%s' not found.".format(codec)); 91 } 92 }; 93 94 /** 95 Decodes a Base64 encoded string to a byte string. 96 **/ 97 String.prototype.decode_base64=function(){ 98 if((this.length % 4) == 0){ 99 if(typeof(atob) != "undefined"){//try using mozillas builtin codec 100 return atob(this); 101 }else{ 102 var nBits; 103 //create a result buffer, this is much faster than having strings concatinated. 104 var sDecoded = new Array(this.length / 4); 105 var base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 106 for(var i=0; i < this.length; i+=4){ 107 nBits = (base64.indexOf(this.charAt(i)) & 0xff) << 18 | 108 (base64.indexOf(this.charAt(i+1)) & 0xff) << 12 | 109 (base64.indexOf(this.charAt(i+2)) & 0xff) << 6 | 110 base64.indexOf(this.charAt(i+3)) & 0xff; 111 sDecoded[i] = String.fromCharCode((nBits & 0xff0000) >> 16, (nBits & 0xff00) >> 8, nBits & 0xff); 41 112 } 42 } 43 return c; 44 }; 45 /** 46 Returns all all available decoders. 47 @return An array of decoder names. 48 **/ 49 mod.listDecoders=function(){ 50 var c=[]; 51 for(var attr in String.prototype){ 52 if(attr.slice(0, 7) == "decode_"){ 53 c.push(attr.slice(7)); 54 } 55 } 56 return c; 57 }; 58 59 /** 60 Decodes an encoded string. 61 Parameters but the codec parameter are forwardet to the codec. 62 @param codec The codec to use. 63 **/ 64 String.prototype.decode = function(codec){ 65 var n ="decode_" + codec; 66 if(String.prototype[n]){ 67 var args=[]; 68 for(var i=1;i<arguments.length;i++){ 69 args[i-1] = arguments[i]; 70 } 71 return String.prototype[n].apply(this, args); 113 //make sure padding chars are left out. 114 sDecoded[sDecoded.length-1] = sDecoded[sDecoded.length-1].substring(0, 3 - ((this.charCodeAt(i - 2) == 61) ? 2 : (this.charCodeAt(i - 1) == 61 ? 1 : 0))); 115 return sDecoded.join(""); 116 } 117 }else{ 118 throw new mod.Exception("String length must be divisible by 4."); 119 } 120 }; 121 122 /** 123 Encodes a string using Base64. 124 **/ 125 String.prototype.encode_base64=function(){ 126 if(typeof(btoa) != "undefined"){//try using mozillas builtin codec 127 return btoa(this); 128 }else{ 129 var base64 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 130 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', 131 '0','1','2','3','4','5','6','7','8','9','+','/']; 132 var sbin; 133 var pad=0; 134 var s="" + this; 135 if((s.length % 3) == 1){ 136 s+=String.fromCharCode(0); 137 s+=String.fromCharCode(0); 138 pad=2; 139 }else if((s.length % 3) == 2){ 140 s+=String.fromCharCode(0); 141 pad=1; 142 } 143 //create a result buffer, this is much faster than having strings concatinated. 144 var rslt=new Array(s.length / 3); 145 var ri=0; 146 for(var i=0;i<s.length; i+=3){ 147 sbin=((s.charCodeAt(i) & 0xff) << 16) | ((s.charCodeAt(i+1) & 0xff ) << 8) | (s.charCodeAt(i+2) & 0xff); 148 rslt[ri] = (base64[(sbin >> 18) & 0x3f] + base64[(sbin >> 12) & 0x3f] + base64[(sbin >>6) & 0x3f] + base64[sbin & 0x3f]); 149 ri++; 150 } 151 if(pad>0){ 152 rslt[rslt.length-1] = rslt[rslt.length-1].substr(0, 4 - pad) + ((pad==2) ? "==" : (pad==1) ? "=" : ""); 153 } 154 return rslt.join(""); 155 } 156 }; 157 158 /** 159 Decodes a URI using decodeURIComponent. 160 **/ 161 String.prototype.decode_uri=function(){ 162 return decodeURIComponent(this); 163 }; 164 165 /** 166 Encodes a URI using encodeURIComponent. 167 **/ 168 String.prototype.encode_uri=function(){ 169 return encodeURIComponent(this); 170 }; 171 172 173 String.prototype.encode_lzw=function(){ 174 var dict = {}; 175 var data = (this + "").split(""); 176 var out=[]; 177 var currChar; 178 var phrase = data[0]; 179 var code = 256; 180 181 for(var i=1;i<data.length;i++){ 182 currChar = data[i]; 183 if(dict[phrase + currChar] != null){ 184 phrase += currChar; 72 185 }else{ 73 throw new mod.Exception("Decoder '%s' not found.".format(codec)); 74 } 75 }; 76 77 /** 78 Encodes a string. 79 Parameters but the codec parameter are forwardet to the codec. 80 @param codec The codec to use. 81 **/ 82 String.prototype.encode = function(codec){ 83 var n ="encode_" + codec; 84 if(String.prototype[n]){ 85 var args=[]; 86 for(var i=1;i<arguments.length;i++){ 87 args[i-1] = arguments[i]; 88 } 89 return String.prototype[n].apply(this, args); 186 out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); 187 dict[phrase + currChar] = code; 188 code++; 189 phrase=currChar; 190 } 191 } 192 out.push( phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); 193 for(var i=0;i<out.length;i++){ 194 195 out[i] = String.fromCharCode(out[i]); 196 } 197 return out.join(""); 198 }; 199 200 String.prototype.decode_lzw=function(){ 201 var dict={}; 202 var data = (this + "").split(""); 203 var currChar = data[0]; 204 var oldPhrase = currChar; 205 var out = [currChar]; 206 var code = 256; 207 var phrase; 208 for(var i=1;i<data.length;i++){ 209 var currCode = data[i].charCodeAt(0); 210 if(currCode < 256){ 211 phrase = data[i]; 90 212 }else{ 91 throw new mod.Exception("Ecnoder '%s' not found.".format(codec)); 92 } 93 }; 94 95 /** 96 Decodes a Base64 encoded string to a byte string. 97 **/ 98 String.prototype.decode_base64=function(){ 99 if((this.length % 4) == 0){ 100 if(typeof(atob) != "undefined"){//try using mozillas builtin codec 101 return atob(this); 102 }else{ 103 var nBits; 104 //create a result buffer, this is much faster than having strings concatinated. 105 var sDecoded = new Array(this.length / 4); 106 var base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 107 for(var i=0; i < this.length; i+=4){ 108 nBits = (base64.indexOf(this.charAt(i)) & 0xff) << 18 | 109 (base64.indexOf(this.charAt(i+1)) & 0xff) << 12 | 110 (base64.indexOf(this.charAt(i+2)) & 0xff) << 6 | 111 base64.indexOf(this.charAt(i+3)) & 0xff; 112 sDecoded[i] = String.fromCharCode((nBits & 0xff0000) >> 16, (nBits & 0xff00) >> 8, nBits & 0xff); 113 } 114 //make sure padding chars are left out. 115 sDecoded[sDecoded.length-1] = sDecoded[sDecoded.length-1].substring(0, 3 - ((this.charCodeAt(i - 2) == 61) ? 2 : (this.charCodeAt(i - 1) == 61 ? 1 : 0))); 116 return sDecoded.join(""); 117 } 118 }else{ 119 throw new mod.Exception("String length must be divisible by 4."); 120 } 121 }; 122 123 /** 124 Encodes a string using Base64. 125 **/ 126 String.prototype.encode_base64=function(){ 127 if(typeof(btoa) != "undefined"){//try using mozillas builtin codec 128 return btoa(this); 129 }else{ 130 var base64 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 131 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', 132 '0','1','2','3','4','5','6','7','8','9','+','/']; 133 var sbin; 134 var pad=0; 135 var s="" + this; 136 if((s.length % 3) == 1){ 137 s+=String.fromCharCode(0); 138 s+=String.fromCharCode(0); 139 pad=2; 140 }else if((s.length % 3) == 2){ 141 s+=String.fromCharCode(0); 142 pad=1; 143 } 144 //create a result buffer, this is much faster than having strings concatinated. 145 var rslt=new Array(s.length / 3); 146 var ri=0; 147 for(var i=0;i<s.length; i+=3){ 148 sbin=((s.charCodeAt(i) & 0xff) << 16) | ((s.charCodeAt(i+1) & 0xff ) << 8) | (s.charCodeAt(i+2) & 0xff); 149 rslt[ri] = (base64[(sbin >> 18) & 0x3f] + base64[(sbin >> 12) & 0x3f] + base64[(sbin >>6) & 0x3f] + base64[sbin & 0x3f]); 150 ri++; 151 } 152 if(pad>0){ 153 rslt[rslt.length-1] = rslt[rslt.length-1].substr(0, 4 - pad) + ((pad==2) ? "==" : (pad==1) ? "=" : ""); 154 } 155 return rslt.join(""); 156 } 157 }; 158 159 /** 160 Decodes a URI using decodeURIComponent. 161 **/ 162 String.prototype.decode_uri=function(){ 163 return decodeURIComponent(this); 164 }; 165 166 /** 167 Encodes a URI using encodeURIComponent. 168 **/ 169 String.prototype.encode_uri=function(){ 170 return encodeURIComponent(this); 171 }; 172 173 174 String.prototype.encode_lzw=function(){ 175 var dict = {}; 176 var data = (this + "").split(""); 177 var out=[]; 178 var currChar; 179 var phrase = data[0]; 180 var code = 256; 181 182 for(var i=1;i<data.length;i++){ 183 currChar = data[i]; 184 if(dict[phrase + currChar] != null){ 185 phrase += currChar; 186 }else{ 187 out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); 188 dict[phrase + currChar] = code; 189 code++; 190 phrase=currChar; 191 } 192 } 193 out.push( phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); 194 for(var i=0;i<out.length;i++){ 195 196 out[i] = String.fromCharCode(out[i]); 197 } 198 return out.join(""); 199 }; 200 201 String.prototype.decode_lzw=function(){ 202 var dict={}; 203 var data = (this + "").split(""); 204 var currChar = data[0]; 205 var oldPhrase = currChar; 206 var out = [currChar]; 207 var code = 256; 208 var phrase; 209 for(var i=1;i<data.length;i++){ 210 var currCode = data[i].charCodeAt(0); 211 if(currCode < 256){ 212 phrase = data[i]; 213 }else{ 214 phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar); 215 } 216 out.push(phrase); 217 currChar = phrase.charAt(0); 218 dict[code] = oldPhrase + currChar; 219 code ++; 220 oldPhrase = phrase; 221 } 222 return out.join(""); 223 }; 224 225 }); 213 phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar); 214 } 215 out.push(phrase); 216 currChar = phrase.charAt(0); 217 dict[code] = oldPhrase + currChar; 218 code ++; 219 oldPhrase = phrase; 220 } 221 return out.join(""); 222 }; trunk/jsolait/lib/crypto.js
r5 r85 27 27 @lastchangeddate $Date$ 28 28 */ 29 Module("crypto", "$Revision$", function(mod){ 30 /** 31 Returns all all available encrypters. 32 @return An array of encrypters names. 33 **/ 34 mod.listEncrypters=function(){ 35 var c=[]; 36 for(var attr in String.prototype){ 37 if(attr.slice(0, 8) == "encrypt_"){ 38 c.push(attr.slice(8)); 39 } 29 __version__="$Revision$"; 30 /** 31 Returns all all available encrypters. 32 @return An array of encrypters names. 33 **/ 34 publ listEncrypters(){ 35 var c=[]; 36 for(var attr in String.prototype){ 37 if(attr.slice(0, 8) == "encrypt_"){ 38 c.push(attr.slice(8)); 40 39 } 41 return c;42 };43 /** 44 Returns all all available decrypters. 45 @return An array of decrypters names.46 **/47 mod.listDecrypters=function(){ 48 var c=[]; 49 for(var attr in String.prototype){50 if(attr.slice(0, 8) == "decrypt_"){51 c.push(attr.slice(8));52 }40 } 41 return c; 42 }; 43 /** 44 Returns all all available decrypters. 45 @return An array of decrypters names. 46 **/ 47 publ listDecrypters(){ 48 var c=[]; 49 for(var attr in String.prototype){ 50 if(attr.slice(0, 8) == "decrypt_"){ 51 c.push(attr.slice(8)); 53 52 } 54 return c; 55 }; 53 } 54 return c; 55 }; 56 57 /** 58 Encrypts a string. 59 Parameters but the crypdec parameter are forwardet to the crypdec. 60 @param codec The codec to use. 61 **/ 62 String.prototype.encrypt=function(crydec){ 63 var n = "encrypt_" + crydec; 64 if(String.prototype[n]){ 65 var args=[]; 66 for(var i=1;i<arguments.length;i++){ 67 args[i-1] = arguments[i]; 68 } 69 return String.prototype[n].apply(this, args); 70 }else{ 71 throw new mod.Exception("Decrypter '%s' not found.".format(crydec)); 72 } 73 }; 74 /** 75 Decrypts a string. 76 Parameters but the crypdec parameter are forwardet to the crypdec. 77 @param codec The codec to use. 78 **/ 79 String.prototype.decrypt=function(crydec){ 80 var n = "decrypt_" + crydec; 81 if(String.prototype[n]){ 82 var args=[]; 83 for(var i=1;i<arguments.length;i++){ 84 args[i-1] = arguments[i]; 85 } 86 return String.prototype[n].apply(this, args); 87 }else{ 88 throw new mod.Exception("Encrypter '%s' not found.".format(crydec)); 89 } 90 }; 91 92 /** 93 Encrypts a string using XOR. 94 The whole String will be XORed with the key. 95 If the key is shorter than the String then it will be multiplied to fit the length of the String. 96 @param key The key to use for encryption. 97 **/ 98 String.prototype.encrypt_xor=function(key){ 99 var e=new Array(this.length); 100 var l=key.length; 101 for(var i=0;i<this.length;i++){ 102 e[i] = String.fromCharCode(this.charCodeAt(i) ^ key.charCodeAt(i % l)); 103 } 104 return e.join(""); 105 }; 106 /** 107 Decrypts a string using XOR. 108 Since XORing is symetric it is the same as the encrypter. 109 @param key The key to use for decryption. 110 **/ 111 String.prototype.decrypt_xor=String.prototype.encrypt_xor; 112 /** 113 Encrypts a string using the ARC4 algorithm. 114 @param key The key to use for encryption. 115 **/ 116 String.prototype.encrypt_rc4=function(key){ 117 //generate substitution box 118 var sbox = new Array(256); 119 for (var i=0; i<256; i++){ 120 sbox[i]=i; 121 } 56 122 57 /** 58 Encrypts a string. 59 Parameters but the crypdec parameter are forwardet to the crypdec. 60 @param codec The codec to use. 61 **/ 62 String.prototype.encrypt=function(crydec){ 63 var n = "encrypt_" + crydec; 64 if(String.prototype[n]){ 65 var args=[]; 66 for(var i=1;i<arguments.length;i++){ 67 args[i-1] = arguments[i]; 68 } 69 return String.prototype[n].apply(this, args); 70 }else{ 71 throw new mod.Exception("Decrypter '%s' not found.".format(crydec)); 72 } 73 }; 74 /** 75 Decrypts a string. 76 Parameters but the crypdec parameter are forwardet to the crypdec. 77 @param codec The codec to use. 78 **/ 79 String.prototype.decrypt=function(crydec){ 80 var n = "decrypt_" + crydec; 81 if(String.prototype[n]){ 82 var args=[]; 83 for(var i=1;i<arguments.length;i++){ 84 args[i-1] = arguments[i]; 85 } 86 return String.prototype[n].apply(this, args); 87 }else{ 88 throw new mod.Exception("Encrypter '%s' not found.".format(crydec)); 89 } 90 }; 123 //swap things around 124 var j=0; 125 for (var i=0; i < 256; i++) { 126 j = (j + sbox[i] + key.charCodeAt(i % key.length)) % 256; 127 var tmp = sbox[i]; 128 sbox[i] = sbox[j]; 129 sbox[j] = tmp; 130 } 91 131 92 /** 93 Encrypts a string using XOR. 94 The whole String will be XORed with the key. 95 If the key is shorter than the String then it will be multiplied to fit the length of the String. 96 @param key The key to use for encryption. 97 **/ 98 String.prototype.encrypt_xor=function(key){ 99 var e=new Array(this.length); 100 var l=key.length; 101 for(var i=0;i<this.length;i++){ 102 e[i] = String.fromCharCode(this.charCodeAt(i) ^ key.charCodeAt(i % l)); 103 } 104 return e.join(""); 105 }; 106 /** 107 Decrypts a string using XOR. 108 Since XORing is symetric it is the same as the encrypter. 109 @param key The key to use for decryption. 110 **/ 111 String.prototype.decrypt_xor=String.prototype.encrypt_xor; 112 /** 113 Encrypts a string using the ARC4 algorithm. 114 @param key The key to use for encryption. 115 **/ 116 String.prototype.encrypt_rc4=function(key){ 117 //generate substitution box 118 var sbox = new Array(256); 119 for (var i=0; i<256; i++){ 120 sbox[i]=i; 121 } 122 123 //swap things around 124 var j=0; 125 for (var i=0; i < 256; i++) { 126 j = (j + sbox[i] + key.charCodeAt(i % key.length)) % 256; 127 var tmp = sbox[i]; 128 sbox[i] = sbox[j]; 129 sbox[j] = tmp; 130 } 131 132 //calculate the result 133 var i=256; 134 var j=256; 135 var rslt=new Array(this.length); 136 for (var k=0; k < this.length; k++) { 137 i = (i + 1) % 256; 138 j = (j + sbox[i]) % 256; 139 var tmp = sbox[i]; 140 sbox[i] = sbox[j]; 141 sbox[j] = tmp; 142 t = (sbox[i] + sbox[j]) % 256; 143 rslt[k] = String.fromCharCode(this.charCodeAt(k) ^ sbox[t]); 144 } 145 return rslt.join(""); 146 }; 147 /** 148 Decrypts a string using the ARC4 algorithm. 149 Since it is symetric it is the same as the encrypter. 150 @param key The key to use for decryption. 151 **/ 152 String.prototype.decrypt_rc4=String.prototype.encrypt_rc4; 153 }); 132 //calculate the result 133 var i=256; 134 var j=256; 135 var rslt=new Array(this.length); 136 for (var k=0; k < this.length; k++) { 137 i = (i + 1) % 256; 138 j = (j + sbox[i]) % 256; 139 var tmp = sbox[i]; 140 sbox[i] = sbox[j]; 141 sbox[j] = tmp; 142 t = (sbox[i] + sbox[j]) % 256; 143 rslt[k] = String.fromCharCode(this.charCodeAt(k) ^ sbox[t]); 144 } 145 return rslt.join(""); 146 }; 147 /** 148 Decrypts a string using the ARC4 algorithm. 149 Since it is symetric it is the same as the encrypter. 150 @param key The key to use for decryption. 151 **/ 152 String.prototype.decrypt_rc4=String.prototype.encrypt_rc4; 154 153 trunk/jsolait/lib/dom.js
r62 r85 25 25 @lastchangeddate $Date$ 26 26 **/ 27 Module("dom", "$Revision$", function(mod){ 28 var sets=imprt("sets"); 27 __version__="$Revision$"; 29 28 30 /** 31 Event class. 32 **/ 33 mod.Event=Class(function(publ, supr){ 34 publ.__init__=function(type, target){ 35 this.type = type; 36 this.target = target; 37 }; 38 ///The event type. 39 publ.type=null; 40 ///The target of the event 41 publ.target=null; 42 }); 29 import sets; 30 31 /** 32 Event class. 33 **/ 34 class Event({ 35 publ __init__(target){ 36 this.type = this.__class__.__name__; 37 this.target = target; 38 }; 39 ///The event type. 40 publ type=null; 41 ///The target of the event 42 publ target=null; 43 44 publ __str__(){ 45 return '[Event %s]'.format(this.type); 46 47 }; 48 }); 43 49 44 50 51 /** 52 An EventTarget implementation. 53 **/ 54 class EventTarget({ 55 publ __init__(){ 56 this.eventListeners={}; 57 }; 45 58 /** 46 An EventTarget implementation. 59 Dispatches an event to it's listeners. 60 @param evt The event to dispatch. 47 61 **/ 48 mod.EventTarget =Class(function(publ, supr){ 49 publ.__init__=function(){ 50 this.eventListeners={}; 51 }; 52 /** 53 Dispatches an event to it's listeners. 54 @param evt The event to dispatch. 55 **/ 56 publ.dispatchEvent = function(evt){ 57 if(this.eventListeners[evt.type]){ 58 var l = this.eventListeners[evt.type].items; 59 for(var h in l){ 60 if(typeof l == 'function'){ 61 l(evt); 62 }else{ 63 l[h].handleEvent(evt); 64 } 62 publ dispatchEvent(evt){ 63 if(this.eventListeners[evt.type]){ 64 var l = this.eventListeners[evt.type].items; 65 for(var h in l){ 66 if(typeof l == 'function'){ 67 l(evt); 68 }else{ 69 l[h].handleEvent(evt); 65 70 } 66 71 } 67 }; 68 /** 69 Adds an EventListener. 70 @param evtType The event type to register the listener for. 71 @param listener The EventListener object. 72 @param useCapture todo: Not used yet. 73 **/ 74 publ.addEventListener=function(evtType, listener, useCapture){ 75 if(this.eventListeners[evtType]===undefined){ 76 this.eventListeners[evtType] = new sets.Set(); 77 } 78 //make sure the listener has an id that the set can use 79 id(listener, true); 80 this.eventListeners[evtType].add(listener); 81 }; 82 /** 83 Removes a registered EventListener. 84 @param evtType The event type the listener was registered for. 85 @param listener The EventListener object. 86 @param useCapture todo: Not used yet. 87 **/ 88 publ.removeEventListener=function(evtType, listener, useCapture){ 89 if(this.eventListeners[evtType]){ 90 this.eventListeners[evtType].discard(listener); 91 } 92 }; 93 }); 72 } 73 }; 74 /** 75 Adds an EventListener. 76 @param evtType The event type to register the listener for. 77 @param listener The EventListener object. 78 @param useCapture todo: Not used yet. 79 **/ 80 publ addEventListener(evtType, listener, useCapture){ 81 if(this.eventListeners[evtType]===undefined){ 82 this.eventListeners[evtType] = new sets.Set(); 83 } 84 //make sure the listener has an id that the set can use 85 id(listener, true); 86 this.eventListeners[evtType].add(listener); 87 }; 88 /** 89 Removes a registered EventListener. 90 @param evtType The event type the listener was registered for. 91 @param listener The EventListener object. 92 @param useCapture todo: Not used yet. 93 **/ 94 publ removeEventListener(evtType, listener, useCapture){ 95 if(this.eventListeners[evtType]){ 96 this.eventListeners[evtType].discard(listener); 97 } 98 }; 99 }); 94 100 101 /** 102 An EventListener wrapper. 103 It forwards all events to handler methods using the evt.type as the name for the method. 104 **/ 105 class EventListener({ 95 106 /** 96 An EventListener wrapper. 97 It forwards all events to handler methods using the evt.type as the name for the method. 107 Handles events dispatched by an EventTarget. 108 It forwards the evt to a handler method with the name == evt.type. 109 @param evt The event to handle. 98 110 **/ 99 mod.EventListener=Class(function(publ){ 100 /** 101 Handles events dispatched by an EventTarget. 102 It forwards the evt to a handler method with the name == evt.type. 103 @param evt The event to handle. 104 **/ 105 publ.handleEvent=function(evt){ 106 if(this['handleEvent_' +evt.type]){ 107 this['handleEvent_' + evt.type](evt); 108 } 109 }; 110 }); 111 publ handleEvent(evt){ 112 if(this['handleEvent_' +evt.type]){ 113 this['handleEvent_' + evt.type](evt); 114 } 115 }; 116 }); 111 117 112 /** 113 A combination of an EventTarget and a EventListener. 114 **/ 115 mod.EventListenerTarget=Class(mod.EventTarget, mod.EventListener, function(publ, supr){ 116 }); 118 /** 119 A combination of an EventTarget and a EventListener. 120 **/ 121 class EventListenerTarget extends EventTarget, EventListener({ 117 122 }); trunk/jsolait/lib/forms.js
r5 r85 28 28 **/ 29 29 30 Module("forms", "$Revision$", function(mod){ 31 /** 32 A class that resembles the functionality of an HTML form. 33 One can access the elements using the elements property of the form or 34 by accessing the element as a named property of the form object(formObj.elemName ...). 35 **/ 36 mod.Form=Class(function(publ, supr){ 37 ///Contains the form elements. 38 publ.elements=[]; 39 ///The URL to submit the form to. 40 publ.action=""; 41 ///The method to use for subitting the form(GET/POST) 42 publ.method="GET"; 43 44 /** 45 Initializes a Form object. 46 @param action="" The URL to submit the form to. 47 @param method="GET" The method to use for submitting(GET, POST). 48 **/ 49 publ.__init__=function(action, method){ 50 this.elements=[]; 51 this.action= (action==null)?"":action; 52 this.method= (method==null)?"GET":method; 53 }; 54 55 /** 56 Adds a new form element to the form or sets the value of an existing element. 57 @param name The name of the form element. 58 @param value The value of the form element. 59 @return The Element set. 60 **/ 61 publ.set=function(name, value){ 62 var f = null; 63 for(var i=0;i<this.elements;i++){ 64 if(name == this.elements[i].name){ 65 f = this.elements[i]; 66 f.value = value; 30 __version__="$Revision$"; 31 32 import urllib; 33 34 /** 35 A class that resembles the functionality of an HTML form. 36 One can access the elements using the elements property of the form or 37 by accessing the element as a named property of the form object(formObj.elemName ...). 38 **/ 39 class Form({ 40 ///Contains the form elements. 41 publ elements=[]; 42 ///The URL to submit the form to. 43 publ action=""; 44 ///The method to use for subitting the form(GET/POST) 45 publ method="GET"; 46 47 /** 48 Initializes a Form object. 49 @param action="" The URL to submit the form to. 50 @param method="GET" The method to use for submitting(GET, POST). 51 **/ 52 publ __init__(action, method){ 53 this.elements=[]; 54 this.action= (action==null)?"":action; 55 this.method= (method==null)?"GET":method; 56 }; 57 58 /** 59 Adds a new form element to the form or sets the value of an existing element. 60 @param name The name of the form element. 61 @param value The value of the form element. 62 @return The Element set. 63 **/ 64 publ set(name, value){ 65 var f = null; 66 for(var i=0;i<this.elements;i++){ 67 if(name == this.elements[i].name){ 68 f = this.elements[i]; 69 f.value = value; 70 } 71 } 72 if(f == null){//add a new element 73 f = new Element(name, value); 74 this.elements.push(f); 75 } 76 //add the element as a properyt to the form object 77 //if that name is not taken yet. 78 if(this[name] == null){ 79 this[name] = f; 80 } 81 return f; 82 }; 83 84 /** 85 Encodes the form to a form data String. 86 @return The encoded form. 87 **/ 88 publ encode(){ 89 var data=[]; 90 for(var i=0;i<this.elements.length;i++){ 91 data.push(this.elements[i].encode()); 92 } 93 return data.join("&"); 94 }; 95 96 /** 97 Creates a query URL using the action property and 98 appending it with a "?" and the URL encoded data. 99 This can be used for GET requests. 100 @return The query string of the form. 101 **/ 102 publ queryString(){ 103 return this.action + "?" + this.encode(); 104 }; 105 106 /** 107 Submits the form to the server, reloading the page. 108 In HTML a hidden form will be generated which is submitted. 109 In SVG only forms with GET method wil be submitted by setting the browsers 110 location to the action URL appended with the data. 111 **/ 112 publ submit(){ 113 if(this.method.toLowerCase() == "get"){ 114 try{//this should work in HTML browsers 115 location.href = this.queryString(); 116 }catch(e){ 117 try{//this is a fallback for SVG 118 var s = 'location="' + this.queryString().replace(/(["\\])/g, '\\$1') + '"'; 119 browserEval(encodeURI(s)); 120 }catch(e){ 121 throw "Cannot set new location."; 67 122 } 68 123 } 69 if(f == null){//add a new element 70 f = new mod.Element(name, value); 71 this.elements.push(f); 72 } 73 //add the element as a properyt to the form object 74 //if that name is not taken yet. 75 if(this[name] == null){ 76 this[name] = f; 77 } 78 return f; 79 }; 80 81 /** 82 Encodes the form to a form data String. 83 @return The encoded form. 84 **/ 85 publ.encode=function(){ 86 var data=[]; 124 }else{//this will only work in HTML 125 var frm = document.createElement("form"); 126 frm.setAttribute("action", this.action); 127 frm.setAttribute("method", this.method); 128 document.getElementsByTagName("body")[0].appendChild(frm); 87 129 for(var i=0;i<this.elements.length;i++){ 88 data.push(this.elements[i].encode()); 89 } 90 return data.join("&"); 91 }; 92 93 /** 94 Creates a query URL using the action property and 95 appending it with a "?" and the URL encoded data. 96 This can be used for GET requests. 97 @return The query string of the form. 98 **/ 99 publ.queryString=function(){ 100 return this.action + "?" + this.encode(); 101 }; 102 103 /** 104 Submits the form to the server, reloading the page. 105 In HTML a hidden form will be generated which is submitted. 106 In SVG only forms with GET method wil be submitted by setting the browsers 107 location to the action URL appended with the data. 108 **/ 109 publ.submit=function(){ 110 if(this.method.toLowerCase() == "get"){ 111 try{//this should work in HTML browsers 112 location.href = this.queryString(); 113 }catch(e){ 114 try{//this is a fallback for SVG 115 var s = 'location="' + this.queryString().replace(/(["\\])/g, '\\$1') + '"'; 116 browserEval(encodeURI(s)); 117 }catch(e){ 118 throw "Cannot set new location."; 119 } 120 } 121 }else{//this will only work in HTML 122 var frm = document.createElement("form"); 123 frm.setAttribute("action", this.action); 124 frm.setAttribute("method", this.method); 125 document.getElementsByTagName("body")[0].appendChild(frm); 126 for(var i=0;i<this.elements.length;i++){ 127 var elem = this.elements[i]; 128 var inp = document.createElement("input"); 129 inp.setAttribute("type", "hidden"); 130 inp.setAttribute("name", elem.name); 131 inp.setAttribute("value", elem.value); 132 frm.appendChild(inp); 133 } 134 frm.submit(); 135 } 136 }; 137 138 /** 139 Submits the form t
