Utilisateur:Od1n/Snippets JavaScript

//------------------------------------------------------------------------------
// pour consulter les events jQuery attachés à un élément
// attention : uniquement pour debug, ne pas modifier les données
//------------------------------------------------------------------------------

// jQuery 1.4.2 ~ 1.7.2
jElement.data('events');

// jQuery 1.8+
$._data(domElement, 'events');


//------------------------------------------------------------------------------
// cache key->value avec limite de taille, inspiré de :
// http://geoffray.be/blog/javascript/creer-file-circulaire-fifo-associative-javascript
// https://github.com/gwarnants/fixedsize-fifo-queue
//------------------------------------------------------------------------------

function SimpleCache(size) {
    this.data = {};
    this.length = 0;
    this.maxlength = size;
}
SimpleCache.prototype = {
    get: function (k) {
        return (typeof this.data[k] !== 'undefined') ? this.data[k] : null;
    },
    add: function (k, v) {
        if (typeof this.data[k] === 'undefined') {
            if (this.length < this.maxlength) {
                this.length++;
            } else {
                this._pop();
            }
        }
        this.data[k] = v;
    },
    _pop: function () {
        var i;
        for (i in this.data) {
            delete this.data[i];
            break;
        }
    }
};

var resultsCache = new SimpleCache(50);


//------------------------------------------------------------------------------
// pour la détection des objets localStorage et sessionStorage de HTML5
// code issu de la librairie Modernizr, sous licences MIT et BSD : www.modernizr.com/license/
//------------------------------------------------------------------------------

//----------------------------------------------------------
//  Modernizer 1.7 (20/02/2011)
//  voir notamment commit du 07/12/2010 :
//  https://github.com/Modernizr/Modernizr/commit/c630c3938f704bfb4153bc939dc2eeee012e34b5
//----------------------------------------------------------

// Firefox has made these tests rather unfun.

// In FF4, if disabled, window.localStorage should === null.

// Normally, we could not test that directly and need to do a 
//   `('localStorage' in window) && ` test first because otherwise Firefox will
//   throw http://bugzil.la/365772 if cookies are disabled

// However, in Firefox 4 betas, if dom.storage.enabled == false, just mentioning
//   the property will throw an exception. http://bugzil.la/599479
// This looks to be fixed for FF4 Final.

// Because we are forced to try/catch this, we'll go aggressive.

// FWIW: IE8 Compat mode supports these features completely:
//   http://www.quirksmode.org/dom/html5.html
// But IE8 doesn't support either with local files

tests['localstorage'] = function() {
    try {
        return !!localStorage.getItem;
    } catch(e) {
        return false;
    }
};

tests['sessionstorage'] = function() {
    try {
        return !!sessionStorage.getItem;
    } catch(e){
        return false;
    }
};


//----------------------------------------------------------
//  Modernizer 1.6 (25/10/2010)
//----------------------------------------------------------

// Both localStorage and sessionStorage are
//   tested via the `in` operator because otherwise Firefox will
//   throw an error: https://bugzilla.mozilla.org/show_bug.cgi?id=365772
//   if cookies are disabled

// They require try/catch because of possible firefox configuration:
//   http://github.com/Modernizr/Modernizr/issues#issue/92

// FWIW miller device resolves to [object Storage] in all supporting browsers
//   except for IE who does [object Object]

// IE8 Compat mode supports these features completely:
//   http://www.quirksmode.org/dom/html5.html

tests['localstorage'] = function () {
    try {
        return ('localStorage' in window) && window.localStorage !== null;
    } catch (e) {
        return false;
    }
};

tests['sessionstorage'] = function () {
    try {
        return ('sessionStorage' in window) && window.sessionStorage !== null;
    } catch (e) {
        return false;
    }
};