check.js 4.56 KB
var jQuery = require('yoho-jquery');

((function($) {

    var defaultsHtml = {
        radio: {
            uncheckedHtml: '',
            checkedHtml: '',
            single: true
        },
        checkbox: {
            uncheckedHtml: '',
            checkedHtml: '',
            single: false
        }
    };

    var defaults = {
        type: 'radio',
        single: false
    };

    var check = {
        init: function(options) {
            var type = options.type || defaults.type;
            var ele = this;
            var _checked;

            options = $.extend({}, defaults, $(this).data('options'), defaultsHtml[type], options);

            _checked = options.checked || $('.iconfont', this).hasClass('checked');

            $('.iconfont', this).removeClass('radio').removeClass('checkbox').addClass(options.type);
            $('.iconfont', this).html(_checked ? options.checkedHtml : options.uncheckedHtml);

            if (options.group) {
                $(this).addClass('check-group-' + options.group);
            }
            $(this).data('options', options);
            $(this).off().on('click', function() {
                var icon = $(ele).find('.' + options.type);
                var checked = $(icon).hasClass('checked');

                if (options.group) {
                    if (options.single) {
                        if (!checked) {
                            check.unCheckGroup(ele, options);
                            check.doChange(checked, ele, options);
                        }
                    } else {
                        check.doChange(checked, ele, options);
                    }
                } else {
                    check.doChange(checked, ele, options);
                }


            });
        },
        doChange: function(checked, ele, options) {
            var icon = $(ele).find('.' + options.type);

            if (checked) {
                $(icon).removeClass('checked');
                $(icon).html(options.uncheckedHtml);
            } else {
                $(icon).addClass('checked');
                $(icon).html(options.checkedHtml);
            }

            if (typeof options.onChange === 'function') {
                options.onChange(ele, !checked, $(ele).data('value'));
            }
        },
        unCheckGroup: function(ele, options) {
            var groupClass = 'check-group-' + options.group;

            check.unCheckAll.call($('.' + groupClass));
        },
        checkAll: function() {
            $(this).each(function() {
                var options = $(this).data('options');

                check._check(this, options);
            });
        },
        unCheckAll: function() {
            $(this).each(function() {
                var options = $(this).data('options');

                check._unCheck(this, options);
            });
        },
        getChecked: function() {
            var checkedEle = [];
            var checkedValues = [];

            $(this).each(function() {
                var options = $(this).data('options');

                if ($('.' + options.type, this).hasClass('checked')) {
                    checkedEle.push(this);
                    checkedValues.push($(this).data('value'));
                }
            });
            return {
                documents: checkedEle,
                values: checkedValues
            };
        },
        _check: function(ele, options) {
            var icon = $(ele).find('.' + options.type);
            var checked = $(icon).hasClass('checked');

            if (!checked) {
                $(icon).addClass('checked');
                $(icon).html(options.checkedHtml);

                if (typeof options.onChange === 'function') {
                    options.onChange(ele, !checked, $(ele).data('value'));
                }
            }
        },
        _unCheck: function(ele, options) {
            var icon = $(ele).find('.' + options.type);
            var checked = $(icon).hasClass('checked');

            if (checked) {
                $(icon).removeClass('checked');
                $(icon).html(options.uncheckedHtml);

                if (typeof options.onChange === 'function') {
                    options.onChange(ele, !checked, $(ele).data('value'));
                }
            }
        }

    };

    $.fn.check = function(options, param) {
        if (typeof options === 'string') {
            return check[options].call(this, param);
        } else {
            return $(this).each(function() {
                check.init.call(this, options);
            });
        }
    };
})(jQuery));