www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

extensions-jQuery.js (6088B)


      1 // Extensions de String
      2 
      3 String.prototype.escapeXML = function() {
      4     return this
      5     .replace(/&/g, "&")
      6     .replace(/</g, "&lt;")
      7     .replace(/>/g, "&gt;")
      8     .replace(/"/g, "&quot;")
      9     .replace(/'/g, "&apos;");
     10 }
     11 
     12 /* "<div>foo</div>".toDom() ⇔ $("<div>foo</div>") */
     13 String.prototype.toDom = function() {
     14     return $("" + this);
     15 }
     16 
     17 String.prototype.appendTo = function() {
     18     var d = this.toDom();
     19     return d.appendTo.apply(d, arguments);
     20 }
     21 
     22 // Extensions de jQuery
     23 
     24 jQuery.fn.extend({
     25     remap: function(fn) {
     26         var a = $.makeArray(arguments);
     27         a.shift();
     28 
     29         var r = $();
     30         this.each(function(i, e) {
     31             var aa = $.makeArray(a);
     32             aa.push(e);
     33             r = r.add(fn.apply(this, aa));
     34         });
     35         return r;
     36     },
     37     // Sérialise le DOM de l'élément sous forme de HTML.
     38     serializeDOM: function(value) {
     39         /* get the DOM of this node, serialized as HTML.
     40          * with value, set the DOM of this node to the
     41          * serialized HTML value (Not Implemented Yet) */
     42         if ( value === undefined ) {
     43 	    return this.html().escapeXML();
     44 	}
     45     },
     46     // renvoie le map de attr sur chaque élément (lecture seule).
     47     attrs: function(value) {
     48         return this.map(function(idx, elem) {
     49             return $([elem]).attr(value);
     50         });
     51     },
     52     // Active ou désactive resizable(), et rend la hauteur libre.
     53     toggleResizable: function() {
     54         // TODO : devrait enregistrer les options.
     55         
     56         if (this.data('notResizable')) {
     57             this.resizable();
     58             this.height(this.data('oldHeight'));
     59         } else {
     60             this.resizable('destroy');
     61             this.data('oldHeight', this.height());
     62             this.height('auto');
     63         }
     64         
     65         this.data('notResizable', ! this.data('notResizable'))
     66         
     67         return this;
     68     },
     69     
     70     // Alias pour des accesseurs courants sur les positions des éléments
     71     // Top, left, center, bottom, right, en X et en Y.
     72     offX: function(value) {
     73         if (value === undefined) {
     74             return this.offset().left;
     75         } else {
     76             return this.offset({left: value});
     77         }
     78     },
     79     offY: function(value) {
     80         if (value === undefined) {
     81             return this.offset().top;
     82         } else {
     83             return this.offset({top: value});
     84         }
     85     },
     86     leftX: function() {
     87         return this.offX.apply(this, arguments);
     88     },
     89     topY: function() {
     90         return this.offY.apply(this, arguments);
     91     },
     92     centerX: function() {
     93         return this.offX() + (this.width() / 2);
     94     },
     95     centerY: function() {
     96         return this.offY() + (this.height() / 2);
     97     },
     98     rightX: function() {
     99         return this.offX() + this.width();
    100     },
    101     bottomY: function() {
    102         return this.offY() + this.height();
    103     }
    104 });
    105 
    106 // Fonction utilitaire permettant de facilement surcharger un
    107 // accesseur pour un certain type d'éléments.
    108 function surchargeAccesseur(nom, type, get, set) {
    109     var _old = $.fn[nom];
    110     $.fn[nom] = function(options) {
    111         var args = arguments;
    112         if (options !== undefined) {
    113             var that = this;
    114             var ret = this.map(function (i) {
    115                 if (that[i] instanceof type) {
    116                     return set(that[i], options);
    117                 } else {
    118                     return _old.apply($(that[i]), args);
    119                 }
    120             });
    121             return ret[0];
    122         } else {
    123             if (this[0] instanceof type) {
    124                 return get(this);
    125             } else {
    126 	        return _old.call(this);
    127             }
    128         }
    129     };
    130     
    131 }
    132 
    133 function surchargeAccesseurSimple(nom, defaut, type) {
    134     surchargeAccesseur(
    135         nom,
    136         type,
    137         function (obj) { ret = obj[0][nom]; return (ret !== undefined) ? ret : defaut; },
    138         function (obj, val) { obj[nom] = val; }
    139     );
    140 }
    141 
    142 // Permet d'utiliser un évènement comme si c'était un élément du DOM.
    143 
    144 // This is the beauty of JavaScript ♥.
    145 surchargeAccesseurSimple('height', 0, $.Event);
    146 surchargeAccesseurSimple('width', 0, $.Event);
    147 surchargeAccesseurSimple('scrollLeft', 0, $.Event);
    148 surchargeAccesseurSimple('scrollTop', 0, $.Event);
    149 surchargeAccesseurSimple('outerWidth', 0, $.Event);
    150 surchargeAccesseurSimple('outerHeight', 0, $.Event);
    151 surchargeAccesseur(
    152     'offset',
    153     $.Event,
    154     function (obj) {
    155         return {
    156             left: obj[0].pageX,
    157             top:  obj[0].pageY
    158         };
    159     },
    160     function (obj, val) {
    161         if ('left' in val) { that[i].pageX = val.left; }
    162         if ('top'  in val) { that[i].pageY = val.top;  }
    163     }
    164 );
    165 
    166 // Fix firefox bug : when top or left are set to a non-integer value, flicker occurs. 
    167 (function ($) {
    168     var _offset = $.fn.offset;
    169     
    170     $.fn.offset = function(options) {
    171         var args = arguments;
    172         if (options !== undefined) {
    173             if ('left' in options) { args[0].left = Math.floor(options.left); }
    174             if ('top'  in options) { args[0].top  = Math.floor(options.top);  }
    175         }
    176         
    177         return _offset.apply(this, args);
    178     }
    179 }(jQuery));
    180 
    181 
    182 
    183 /* Fioritures graphique */
    184 jQuery.fn.extend({
    185     blink: function (count, speed) {
    186         elem = this;
    187         count = count || 10;
    188         speed = speed || 1000;
    189         
    190         // Mouseover
    191         // Todo : il y a des bugs graphiques ici,
    192         // et il faudrait enlever ce hook "mouseover"
    193         // après la première fois.
    194         elem.mouseover(function () {
    195             elem.clearQueue("blink");
    196             elem.queue("blink", function() {
    197                 elem.removeClass('boutonHover', 1000);
    198             });
    199         });
    200         
    201         // Enqueue blinks
    202         for (; count > 0; count--) {
    203             elem.queue("blink", function () {
    204                 elem.toggleClass('boutonHover', 1000, function() { elem.dequeue("blink"); });
    205             });
    206         }
    207         elem.queue("blink", function() {
    208             elem.removeClass('boutonHover', 1000);
    209         });
    210         
    211         // Start first blink
    212         elem.dequeue("blink");
    213     }
    214 });