Select2

The jQuery replacement for select boxes

目前為 2017-10-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Select2
// @namespace    https://select2.github.io/
// @version      0.8.2
// @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 $.fn.selectpicker == "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() {
                            $('select:not(.select2-hidden-accessible,[multiple])').filter(':visible').each(function() {
                                var $this = $(this),
                                    $options = $this.find('option'),
                                    $selected = $options.filter(':selected');
                                $options.length > 2 && $this.select2({
                                    width : Math.max($this.width(), config.minWidth),
                                    dropdownAutoWidth : true,
                                    placeholder : $selected.val() ? '' : $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);