www

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

extensions-jQuery.js (5648B)


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