#140 ✓not_for_core
rockerox

Prototype Extensions

Reported by rockerox | June 3rd, 2008 @ 09:38 PM

Nuevas funcionalidades para PrototypeJS

Comments and changes to this ticket

  • rockerox

    rockerox June 3rd, 2008 @ 09:41 PM

    /*

    • Fecha : Junio 2008
    • Programador : José Miguel Salazar A [rockerox@msn.com]
    • Descripcion : Extensiones Para Prototypejs
    • Notas :

    * Todos los codigos excepto los algoritmos en el objeto Encoding son creados por mi, cualquier comentario pueden hacerlas a mi correo.

    * Soy un fanatico de PrototypeJS y espero que salga alguno de mis codigos en las nuevas versiones.

    */

    // Extendiendo la clase String

    Object.extend(String, {

    PAD : {

    // Enumerador

    RIGHT : 0,

    LEFT : 1,

    BOTH : 2

    },

    trim : function ( string, charlist ) {

    var whitespace;

    if(!charlist){

    whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';

    } else{

    whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');

    }

    for (var i = 0; i < string.length; i++) {

    if (whitespace.indexOf(string.charAt(i)) === -1) {

    string = string.substring(i);

    break;

    }

    }

    for (i = string.length - 1; i >= 0; i--) {

    if (whitespace.indexOf(string.charAt(i)) === -1) {

    string = string.substring(0, i + 1);

    break;

    }

    }

    return (whitespace.indexOf(string.charAt(0)) === -1 ? string : '');

    },

    ltrim : function ( string, charlist ) {

    charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');

    var re = new RegExp('^[' + charlist + ']+', 'g');

    return (string.replace(re, ''));

    },

    rtrim : function ( string, charlist ) {

    charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');

    var re = new RegExp('[' + charlist + ']+$', 'g');

    return string.replace(re, '');

    },

    repeat : function ( string, multiplier ) {

    return (new Array(multiplier+1).join(string));

    },

    reverse : function(string){

    var str = new String();

    var i = new Number(0);

    for(i=(string.length-1);i>=0;i--) str+= string.charAt(i);

    return (str);

    },

    pad : function ( string, length, fill, type ) {

    var half = '', multiplier;

    if (type != String.PAD.LEFT && type != String.PAD.RIGHT && type != String.PAD.BOTH){

    type = String.PAD.RIGHT;

    }

    if ((multiplier = length - string.length) > 0) {

    if (type == String.PAD.LEFT) {

    string = String.repeat(fill, multiplier) + string;

    }else if (type == String.PAD.RIGHT) {

    string = string + String.repeat(fill, multiplier);

    }else if (type == String.PAD.BOTH) {

    half = String.repeat(fill, Math.ceil(multiplier/2));

    string = half + string + half;

    string = string.substr(0, length);

    }

    }

    return (string);

    },

    lpad : function(string, length, fill){

    return(String.pad(string,length,fill,String.PAD.LEFT));

    },

    rpad : function(string, length, fill){

    return(String.pad(string,length,fill,String.PAD.RIGHT));

    },

    startsWith: function(string, pattern) {

    return (string.indexOf(pattern) === 0);

    },

    endsWith: function(string, pattern) {

    var d = new Number(string.length - pattern.length);

    return (d >= 0 && string.lastIndexOf(pattern) === d);

    },

    empty: function(string) {

    return ( string === "" || string === 0 || string === "0" || string === null || string === false || ( is_array(string) && string.length === 0 ) );

    },

    addslashes : function ( string ) {

    return (string.replace('/(["\'\])/g', "\\$1").replace('/\0/g', "\\0"));

    },

    stripslashes : function ( string ){

    return (string.replace('/\0/g', '0').replace('/\(.)/g', '$1'));

    },

    ucfirst : function ( string ){

    return (string.charAt(0).toUpperCase() + string.substr(1, string.length-1));

    },

    ucwords : function ( string ){

    return (string.replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase ( ); } ));

    }

    });

    // Agregamos nuevos prototipos a la clase String

    Object.extend(String.prototype,{

    trim: function(charlist){

    return(String.trim(this,charlist));

    },

    ltrim : function(charlist){

    return (String.ltrim(this,charlist));

    },

    rtrim : function(charlist){

    return (String.rtrim(this,charlist));

    },

    repeat : function(multiplier){

    return(String.repeat(this,multiplier));

    },

    reverse : function(){

    return (String.reverse(this));

    },

    pad : function(length, fill, type){

    return (String.lpad(this, length, fill,type));

    },

    lpad : function(length, fill){

    return (String.lpad(this, length, fill));

    },

    rpad : function(length, fill){

    return (String.rpad(this, length, fill));

    },

    startsWith : function(pattern){

    return (String.startsWith(this,pattern));

    },

    endsWith : function(pattern){

    return (String.endsWith(this,pattern));

    },

    empty : function(){

    return (String.empty(this));

    },

    addslashes : function(){

    return (String.addslashes(this));

    },

    stripslashes : function(){

    return (String.stripslashes(this));

    },

    ucfirst : function(){

    return (String.ucfirst(this));

    },

    ucwords : function(){

    return (String.ucwords(this));

    }

    });

    /*

    Utilizado para cargar script o css en tiempo de ejecucion

    ejemplo:

    Import.script("/js/archivo.js",{callback : function(){

    alert("Archivo cargado");

    }});

    */

    var Import = {

    script : function(source,properties){

    var JScript = document.createElement("script");

    Object.extend(JScript, {

    type : "text/javascript",

    src : source,

    targetNode : document.getElementsByTagName("head")[0], //Target es por referencia

    overwrite : false,

    callback : function(){

    }

    });

    if(properties != undefined)Object.extend(JScript, properties);

    this.load(JScript);

    return (true);

    },

    css : function (source, properties){

    var myLink = document.createElement("link");

    Object.extend(myLink, {

    type : "text/css",

    rel : "stylesheet",

    href : source,

    media : "screen",

    targetNode : document.getElementsByTagName("head")[0], //Target es por referencia

    overwrite : false,

    callback : function(){

    }

    });

    if(properties != undefined)Object.extend(myLink, properties);

    this.load(myLink);

    return (true);

    },

    load : function(obj){

    /*

    • Descripcion : Controla la carga del archivo y detona el CallBack
    • Ambito : Privado

    */

    if(Prototype.Browser.IE){

    obj.onreadystatechange= function () {

    if(this.readyState == "loaded" || this.readyState == 'complete'){

    this.callback();

    }

    }

    }else{

    Event.observe(obj, "load", obj.callback);

    }

    var t = obj.targetNode.getElementsByTagName(obj.nodeName);

    var i = new Number();

    var target = null;

    for(i=0;i

    //alert(t[i].href.toUpperCase() + " == " + obj.href.toUpperCase());

    switch(t[i].nodeName.toUpperCase()){

    case "LINK":

    if(t[i].href.toUpperCase() == obj.href.toUpperCase()){

    target = t[i];

    }

    break;

    case "SCRIPT":

    if(t[i].src.toUpperCase() == obj.src.toUpperCase()){

    target = t[i];

    }

    break;

    }

    }

    if(target != null){

    if(obj.overwrite == true){

    try{target.src = ""}catch(ex){target.href = ""};

    obj.target.removeChild(target);

    obj.target.appendChild(obj);

    }else{

    obj.callback();

    }

    }else{

    obj.targetNode.appendChild(obj);

    }

    }

    }

    /*

    este objeto contiene algunos algoritmos para la codificacion y decodificacion de strings

    */

    var Encoding = {

    url : {

    encode : function (string){

    return (encodeURIComponent(string.toString()).replace(/%20/g, '+'));

    },

    decode : function ( string ){

    return (decodeURIComponent(string.replace(/\+/g, '%20')).toString());

    }

    },

    utf8 : {

    encode : function(string){

    string = string.replace(/\r\n/g,"\n");

    var tmp_arr = [], ac = 0;

    for (var n = 0; n < string.length; n++) {

    var c = string.charCodeAt(n);

    if (c < 128) {

    tmp_arr[ac++] = String.fromCharCode(c);

    } else if((c > 127) && (c < 2048)) {

    tmp_arr[ac++] = String.fromCharCode((c >> 6) | 192);

    tmp_arr[ac++] = String.fromCharCode((c & 63) | 128);

    } else {

    tmp_arr[ac++] = String.fromCharCode((c >> 12) | 224);

    tmp_arr[ac++] = String.fromCharCode(((c >> 6) & 63) | 128);

    tmp_arr[ac++] = String.fromCharCode((c & 63) | 128);

    }

    }

    return tmp_arr.join('');

    },

    decode : function(string){

    var tmp_arr = [], i = ac = c = c1 = c2 = 0;

    while ( i < string.length ) {

    c = string.charCodeAt(i);

    if (c < 128) {

    tmp_arr[ac++] = String.fromCharCode(c);

    i++;

    } else if ((c > 191) && (c < 224)) {

    c2 = string.charCodeAt(i+1);

    tmp_arr[ac++] = String.fromCharCode(((c & 31) << 6) | (c2 & 63));

    i += 2;

    } else {

    c2 = string.charCodeAt(i+1);

    c3 = string.charCodeAt(i+2);

    tmp_arr[ac++] = String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));

    i += 3;

    }

    }

    return tmp_arr.join('');

    }

    },

    base64: {

    keyBASE64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    encode: function(input){

    var output = "";

    var chr1, chr2, chr3;

    var enc1, enc2, enc3, enc4;

    var i = 0;

    do {

    chr1 = input.charCodeAt(i++);

    chr2 = input.charCodeAt(i++);

    chr3 = input.charCodeAt(i++);

    enc1 = chr1 >> 2;

    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);

    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);

    enc4 = chr3 & 63;

    if (isNaN(chr2)) {

    enc3 = enc4 = 64;

    }

    else

    if (isNaN(chr3)) {

    enc4 = 64;

    }

    output = output + this.keyBASE64.charAt(enc1) + this.keyBASE64.charAt(enc2) +

    this.keyBASE64.charAt(enc3) +

    this.keyBASE64.charAt(enc4);

    }

    while (i < input.length);

    return output;

    },

    decode: function(input){

    var output = "";

    var chr1, chr2, chr3;

    var enc1, enc2, enc3, enc4;

    var i = 0;

    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =

    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    do {

    enc1 = this.keyBASE64.indexOf(input.charAt(i++));

    enc2 = this.keyBASE64.indexOf(input.charAt(i++));

    enc3 = this.keyBASE64.indexOf(input.charAt(i++));

    enc4 = this.keyBASE64.indexOf(input.charAt(i++));

    chr1 = (enc1 << 2) | (enc2 >> 4);

    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);

    chr3 = ((enc3 & 3) << 6) | enc4;

    output = output + String.fromCharCode(chr1);

    if (enc3 != 64) {

    output = output + String.fromCharCode(chr2);

    }

    if (enc4 != 64) {

    output = output + String.fromCharCode(chr3);

    }

    }

    while (i < input.length);

    return output;

    }

    },

    md5: {

    /*****************************************************************************

    • md5.js

    *

    *

    • Copyright (C) Paul Johnston 1999 - 2000. Distributed under the LGPL.

    *****************************************************************************/

    sAscii : " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",

    sHex : "0123456789ABCDEF",

    hex : function(i){

    h = "";

    for(j = 0; j <= 3; j++)

    {

    h += this.sHex.charAt((i >> (j * 8 + 4)) & 0x0F) +

    this.sHex.charAt((i >> (j * 8)) & 0x0F);

    }

    return h;

    },

    /* add, handling overflows correctly */

    add : function(x, y){

    return ((x&0x7FFFFFFF) + (y&0x7FFFFFFF)) ^ (x&0x80000000) ^ (y&0x80000000);

    },

    /* MD5 rounds functions */

    R1 : function (A, B, C, D, X, S, T){

    q = this.add(this.add(A, (B & C) | ((~B) & D)), this.add(X, T));

    return this.add((q << S) | (q >>> (32 - S)), B);

    },

    R2 : function (A, B, C, D, X, S, T){

    q = this.add(this.add(A, (B & D) | (C & (~D))), this.add(X, T));

    return this.add((q << S) | (q >>> (32 - S)), B);

    },

    R3 : function (A, B, C, D, X, S, T){

    q = this.add(this.add(A, B ^ C ^ D), this.add(X, T));

    return this.add((q << S) | (q >>> (32 - S)), B);

    },

    R4 : function (A, B, C, D, X, S, T){

    q = this.add(this.add(A, C ^ (B | (~D))), this.add(X, T));

    return this.add((q << S) | (q >>> (32 - S)), B);

    },

    /* main entry point */

    encode : function (sInp) {

    /* Calculate length in machine words, including padding */

    wLen = (((sInp.length + 8) >> 6) + 1) << 4;

    var X = new Array(wLen);

    /* Convert string to array of words */

    j = 4;

    for (i = 0; (i * 4) < sInp.length; i++){

    X[i] = 0;

    for (j = 0; (j < 4) && ((j + i * 4) < sInp.length); j++){

    X[i] += (this.sAscii.indexOf(sInp.charAt((i * 4) + j)) + 32) << (j * 8);

    }

    }

    /* Append padding bits and length */

    if (j == 4){

    X[i++] = 0x80;

    }else{

    X[i - 1] += 0x80 << (j * 8);

    }

    for(; i < wLen; i++){

    X[i] = 0;

    }

    X[wLen - 2] = sInp.length * 8;

    /* hard coded initial values */

    a = 0x67452301;

    b = 0xefcdab89;

    c = 0x98badcfe;

    d = 0x10325476;

    /* Process each 16 word block in turn */

    for (i = 0; i < wLen; i += 16) {

    aO = a;

    bO = b;

    cO = c;

    dO = d;

    a = this.R1(a, b, c, d, X[i+ 0], 7 , 0xd76aa478);

    d = this.R1(d, a, b, c, X[i+ 1], 12, 0xe8c7b756);

    c = this.R1(c, d, a, b, X[i+ 2], 17, 0x242070db);

    b = this.R1(b, c, d, a, X[i+ 3], 22, 0xc1bdceee);

    a = this.R1(a, b, c, d, X[i+ 4], 7 , 0xf57c0faf);

    d = this.R1(d, a, b, c, X[i+ 5], 12, 0x4787c62a);

    c = this.R1(c, d, a, b, X[i+ 6], 17, 0xa8304613);

    b = this.R1(b, c, d, a, X[i+ 7], 22, 0xfd469501);

    a = this.R1(a, b, c, d, X[i+ 8], 7 , 0x698098d8);

    d = this.R1(d, a, b, c, X[i+ 9], 12, 0x8b44f7af);

    c = this.R1(c, d, a, b, X[i+10], 17, 0xffff5bb1);

    b = this.R1(b, c, d, a, X[i+11], 22, 0x895cd7be);

    a = this.R1(a, b, c, d, X[i+12], 7 , 0x6b901122);

    d = this.R1(d, a, b, c, X[i+13], 12, 0xfd987193);

    c = this.R1(c, d, a, b, X[i+14], 17, 0xa679438e);

    b = this.R1(b, c, d, a, X[i+15], 22, 0x49b40821);

    a = this.R2(a, b, c, d, X[i+ 1], 5 , 0xf61e2562);

    d = this.R2(d, a, b, c, X[i+ 6], 9 , 0xc040b340);

    c = this.R2(c, d, a, b, X[i+11], 14, 0x265e5a51);

    b = this.R2(b, c, d, a, X[i+ 0], 20, 0xe9b6c7aa);

    a = this.R2(a, b, c, d, X[i+ 5], 5 , 0xd62f105d);

    d = this.R2(d, a, b, c, X[i+10], 9 , 0x2441453);

    c = this.R2(c, d, a, b, X[i+15], 14, 0xd8a1e681);

    b = this.R2(b, c, d, a, X[i+ 4], 20, 0xe7d3fbc8);

    a = this.R2(a, b, c, d, X[i+ 9], 5 , 0x21e1cde6);

    d = this.R2(d, a, b, c, X[i+14], 9 , 0xc33707d6);

    c = this.R2(c, d, a, b, X[i+ 3], 14, 0xf4d50d87);

    b = this.R2(b, c, d, a, X[i+ 8], 20, 0x455a14ed);

    a = this.R2(a, b, c, d, X[i+13], 5 , 0xa9e3e905);

    d = this.R2(d, a, b, c, X[i+ 2], 9 , 0xfcefa3f8);

    c = this.R2(c, d, a, b, X[i+ 7], 14, 0x676f02d9);

    b = this.R2(b, c, d, a, X[i+12], 20, 0x8d2a4c8a);

    a = this.R3(a, b, c, d, X[i+ 5], 4 , 0xfffa3942);

    d = this.R3(d, a, b, c, X[i+ 8], 11, 0x8771f681);

    c = this.R3(c, d, a, b, X[i+11], 16, 0x6d9d6122);

    b = this.R3(b, c, d, a, X[i+14], 23, 0xfde5380c);

    a = this.R3(a, b, c, d, X[i+ 1], 4 , 0xa4beea44);

    d = this.R3(d, a, b, c, X[i+ 4], 11, 0x4bdecfa9);

    c = this.R3(c, d, a, b, X[i+ 7], 16, 0xf6bb4b60);

    b = this.R3(b, c, d, a, X[i+10], 23, 0xbebfbc70);

    a = this.R3(a, b, c, d, X[i+13], 4 , 0x289b7ec6);

    d = this.R3(d, a, b, c, X[i+ 0], 11, 0xeaa127fa);

    c = this.R3(c, d, a, b, X[i+ 3], 16, 0xd4ef3085);

    b = this.R3(b, c, d, a, X[i+ 6], 23, 0x4881d05);

    a = this.R3(a, b, c, d, X[i+ 9], 4 , 0xd9d4d039);

    d = this.R3(d, a, b, c, X[i+12], 11, 0xe6db99e5);

    c = this.R3(c, d, a, b, X[i+15], 16, 0x1fa27cf8);

    b = this.R3(b, c, d, a, X[i+ 2], 23, 0xc4ac5665);

    a = this.R4(a, b, c, d, X[i+ 0], 6 , 0xf4292244);

    d = this.R4(d, a, b, c, X[i+ 7], 10, 0x432aff97);

    c = this.R4(c, d, a, b, X[i+14], 15, 0xab9423a7);

    b = this.R4(b, c, d, a, X[i+ 5], 21, 0xfc93a039);

    a = this.R4(a, b, c, d, X[i+12], 6 , 0x655b59c3);

    d = this.R4(d, a, b, c, X[i+ 3], 10, 0x8f0ccc92);

    c = this.R4(c, d, a, b, X[i+10], 15, 0xffeff47d);

    b = this.R4(b, c, d, a, X[i+ 1], 21, 0x85845dd1);

    a = this.R4(a, b, c, d, X[i+ 8], 6 , 0x6fa87e4f);

    d = this.R4(d, a, b, c, X[i+15], 10, 0xfe2ce6e0);

    c = this.R4(c, d, a, b, X[i+ 6], 15, 0xa3014314);

    b = this.R4(b, c, d, a, X[i+13], 21, 0x4e0811a1);

    a = this.R4(a, b, c, d, X[i+ 4], 6 , 0xf7537e82);

    d = this.R4(d, a, b, c, X[i+11], 10, 0xbd3af235);

    c = this.R4(c, d, a, b, X[i+ 2], 15, 0x2ad7d2bb);

    b = this.R4(b, c, d, a, X[i+ 9], 21, 0xeb86d391);

    a = this.add(a, aO);

    b = this.add(b, bO);

    c = this.add(c, cO);

    d = this.add(d, dO);

    }

    return this.hex(a) + this.hex(b) + this.hex(c) + this.hex(d);

    }

    }

    }

  • Juriy Zaytsev

    Juriy Zaytsev June 3rd, 2008 @ 11:47 PM

    • State changed from “new” to “not_for_core”

    It seems more appropriate to submit these extensions to http://scripteka.com as a standalone plugin/library.

    Thanks.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket

Attachments

Pages