dropDown.js 4.51 KB
require('./util/select2');

var $ = require('jquery');

var urlObj = {
    "queryShopPass":"/ShopsRest/selectCheckPassShopsByName",
    "queryBrandByShopId":"/MerchantShopsRest/queryMerchantBrandByShopsId",
    "getjitSup":"/AgreementRest/getJitSupplierByBrandId",
    'shopsRest': '/ShopsRest/selectCheckPassShopsByName', // 店铺列表
    'querySupplierByShopId': '/MerchantShopsRest/queryMerchantSupplierByShopsId', //查询店铺下关联供应商
    'maxSort': '/product/queryMaxSortByName',
    'sortsize2':'/product/querySizeListBySortId4Html'
};

var minimumResultsForSearch = ["sortbybrand", "getjitSup", "sortsize2","querySupplier"];

var dropDown = function(option) {
    new dropDown.prototype.init(option);
}

dropDown.prototype = {
    constructor: dropDown,
    init: function(_option) {
        var option = {
            el: '.select2-offscreen',
            url: urlObj[_option.ajax]
        };
        for (var i in _option) {
            if (_option.hasOwnProperty(i)) {
                option[i] = _option[i];
            }
        }
        if (minimumResultsForSearch.indexOf(_option.ajax) > -1) {
            option.minimumResultsForSearch = Infinity
        }
        if (option.ajax) {
            var defaultName = "请选择";
            if ($(option.el).val() == -1) {
                defaultName = $(option.el).find("option[value='-1']").text()
            }
            if (option.hash) {
                if ($(option.el).val() == null) {

                    var name = option.el.replace(/\.|#/g, '');
                    var arr = location.hash.replace(/^#/, '').split("&&");
                    var map = {};
                    arr.forEach(function(item) {
                        var _a = item.split('=');
                        if (_a && _a[0]) {
                            map[_a[0]] = _a[1];
                        }
                    });

                    var id = map[name];
                    var text = map[name + "Name"];
                    $(option.el).html('');
                    $(option.el).append("<option value=" + id + " >" + text + "</option>");
                }
                $(document).on("change", option.el, function() {
                    var name = option.el.replace(/\.|#/g, '');
                    var id = $(option.el).val();
                    var text = $(option.el).find("option[value='" + id + "']").text();
                    var arr = location.hash.replace(/^#/, '').split("&&");
                    var map = {};
                    arr.forEach(function(item) {
                        var _a = item.split('=');
                        if (_a && _a[0]) {
                            map[_a[0]] = _a[1];
                        }
                    });
                    map[name] = id;
                    map[name + "Name"] = text;
                    var urlHash = '';
                    $.each(map, function(key, value) {
                        if (value || value == '0') {
                            urlHash += key + '=' + value + '&&';
                        }
                    });
                    location.hash = urlHash;
                });
            }

            $(option.el).select2({
                minimumResultsForSearch: option.minimumResultsForSearch || 0,
                ajax: {
                    url: option.url,
                    dataType: 'json',
                    type: 'POST',
                    delay: 250,
                    data: function(params) {

                        var param = {};
                        if (Object.prototype.toString.call(option.params) == "[object Function]") {
                            param = option.params();
                        } else {
                            param = option.params;
                        }
                        var data = $.extend({}, {
                            idName: params.term
                        }, param || {});
                        return data;
                    },
                    processResults: function(data, params) {
                        params.page = params.page || 1;
                        data.data.unshift({
                            id: '-1',
                            text: defaultName
                        });
                        return {
                            results: data.data
                        }
                    },
                    cache: true
                }
            });


        } else {
            $(option.el).select2();
        }
    }
}
dropDown.prototype.init.prototype = dropDown.prototype;

module.exports = dropDown;