Select2

The jQuery replacement for select boxes

Από την 09/10/2017. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Select2
// @namespace    https://select2.github.io/
// @version      0.8
// @description  The jQuery replacement for select boxes
// @author       t_liang
// @include      *:*
// @exclude      *://select2.github.io*
// @exclude      *://app.yinxiang.com*
// @exclude      *://www.instagram.com*
// @exclude      *://www.google.co*
// @exclude      *://*.tmall.com*
// @exclude      *://*.taobao.com*
// @grant        none
// ==/UserScript==

/*
History
    0.1 初始版本
    0.2 修复版本比较错误,versionMoreThan
    0.3 jQuery.noConflict(true)
    0.4 支持jQuery.ajax
    0.5 exclude app.yinxiang.com
    0.6 use setInterval
    0.7 config, setTimeout, zIndex
    0.8 templateResult matcher
*/

/*
(function(factory) {
    debugger;
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define([ 'jquery' ], factory);
    } else if (typeof exports === 'object') {
        // Node/CommonJS
        factory(require('jquery'));
    } else {
        // Browser globals
        factory(jQuery);
    }
})(console.log);
*/

// console.log(arguments);
setTimeout(function() {
    // config
    var config = {
        ignore : [ 'typeof Vue == "function"' ], //, 'typeof angular == "object"'
        jquery : {
            js : '//cdn.bootcss.com/jquery/3.2.1/jquery.min.js',
            moreThan : '1.6.2'
        },
        select2 : {
            js : '//cdn.bootcss.com/select2/4.0.3/js/select2.min.js',
            css : '//cdn.bootcss.com/select2/4.0.3/css/select2.min.css'
        },
        timeout : 3000,
        interval : 3000,
        minWidth : 147
    };
    // ignore
    for (var i = config.ignore.length - 1; i >= 0; i--) {
        if (eval(config.ignore[i])) {
            return;
        }
    }

    var noConflict,
        protocol = location.protocol == 'https:' ? 'https:' : 'http:',
        jQueryOnload = function() {
            jQuery(function($) {
                $.ajaxSetup({
                    cache : true
                });
                var select2Onload = function() {
                    noConflict && $.noConflict(true);
                    // console.log($.fn.select2, location.href);
                    if ($.isFunction($.fn.select2)) {
                        $(document.head).append('<link href="' + (protocol + config.select2.css) + '" rel="stylesheet">')
                            .append('<style>span.select2-dropdown{z-index:99999 !important;}</style>');
                        setInterval(function() {
                            // :enabled:not(.select2-hidden-accessible,[readonly])
                            $('select:not(.select2-hidden-accessible,[multiple])').filter(':visible').each(function() {
                                if (this.style.opacity === 0) {
                                    return true;
                                }
                                var $this = $(this),
                                    $options = $this.find('option');
                                $options.length > 1 && $this.select2({
                                    width : Math.max($this.width(), config.minWidth),
                                    dropdownAutoWidth : true,
                                    placeholder : $options.filter(':selected').text(), //for allowClear
                                    allowClear : true,
                                    templateResult : function(option, Eoption) {
                                        if (option.loading || !option.id) {
                                            return option.text;
                                        }
                                        return '[' + option.id + ']' + option.text;
                                    },
                                    matcher : function(params, option) {
                                        var term = $.trim(params.term).toUpperCase();
                                        if (!term || option.text.toUpperCase().indexOf(term) > -1 || option.id.toUpperCase().indexOf(term) > -1) {
                                            return option;
                                        }
                                        return null;
                                    }
                                });
                            });
                        }, config.interval);
                    }
                };
                $.isFunction($.fn.select2) ? select2Onload() : $.getScript(protocol + config.select2.js, select2Onload);
            });
        },
        /** 版本比较: 大于 */
        versionMoreThan = function(version, moreThan) {
            version = version.split('.');
            moreThan = moreThan.split('.');
            for (var i = 0; i < version.length; i++) {
                var moreThan_i = i < moreThan.length ? Number(moreThan[i]) : 0;
                if (version[i] > moreThan_i) {
                    return true;
                } else if (version[i] < moreThan_i) {
                    return false;
                }
            }
            return false;
        };

    // TODO enter
    if (typeof jQuery == 'function') {
        if (versionMoreThan(jQuery.fn.jquery, config.jquery.moreThan)) {
            jQueryOnload();
            return;
        }
    }
    // append jQuery
    noConflict = true;
    setTimeout(function() {
        var jQueryScript = document.createElement('SCRIPT');
        jQueryScript.src = protocol + config.jquery.js;
        jQueryScript.onload = jQueryOnload;
        document.head.appendChild(jQueryScript);
    }, config.timeout);
}, 3000);