/** * YvanUI * * Copyright (c) 2018 www.yvanui.com. All rights reserved. * @author luoyifan@qq.com * @time 2018-11-27 11:16:00 */ "use strict"; (function ($) { var idx = 1; function createBox(target) { //_2 var $dom = $( '
\n' + '\n' + '\n' + '
' ).insertAfter(target); var t = $(target); t.addClass("checkbox-f").hide(); var name = t.attr("name"); if (name) { //安排新的 input 框,取代他的名字 t.removeAttr("name").attr("checkboxName", name); $dom.find(".checkbox-value").attr("name", name); } return $dom; } function init(target) { //_6 var state = $.data(target, "checkbox"); var opts = state.options; var $dom = state.checkbox; var id = "_yvan_checkbox_" + (++idx); $dom.find(".checkbox-value").attr("id", id); if (opts.label) { $dom.find('span').html(opts.label); } if (opts.primary) { $dom.attr('lay-skin', 'primary'); } $(target).checkbox("setValue", opts.value); setValue(target, opts.checked); setDisable(target, opts.disabled); } function bindEvent(target) { //_e var state = $.data(target, "checkbox"); var opts = state.options; var $dom = state.checkbox; $dom.unbind(".checkbox").bind("click.checkbox", function () { if (!opts.disabled) { setValue(target, !opts.checked); } }); } function setValue(target, value, isInit) { //_c var state = $.data(target, "checkbox"); var opts = state.options; var $dom = state.checkbox; $dom.find(".checkbox-value")._propAttr("checked", value); if (value) { $dom.addClass('yvan-checked'); } else { $dom.removeClass('yvan-checked'); } if (isInit) { opts.checked = value; return; } if (opts.checked !== value) { opts.checked = value; opts.onChange.call(target, value); } } function setDisable(target, value) { //_d var state = $.data(target, "checkbox"); var opts = state.options; var $dom = state.checkbox; var rv = $dom.find(".checkbox-value"); opts.disabled = value; if (value) { $(target).add(rv)._propAttr("disabled", true); $dom.addClass('yvan-checkbox-disabled').addClass('yvan-disabled'); } else { $(target).add(rv)._propAttr("disabled", false); $dom.removeClass('yvan-checkbox-disabled').removeClass('yvan-disabled'); } } $.fn.checkbox = function (options, param) { if (typeof options == 'string') { return $.fn.checkbox.methods[options](this, param); } options = options || {}; return this.each(function () { var state = $.data(this, 'checkbox'); if (state) { $.extend(state.options, options); } else { state = $.data(this, 'checkbox', { options: $.extend({}, $.fn.checkbox.defaults, $.fn.checkbox.parseOptions(this), options), checkbox: createBox(this) }); } state.options.originalChecked = state.options.checked; init(this); //_6 bindEvent(this); //_e //_13 }); }; $.fn.checkbox.methods = { options: function (jq) { var opts = jq.data("checkbox"); return $.extend(opts.options, { value: opts.checkbox.find(".checkbox-value").val() }); }, checkbox: function (jq, value) { var state = $.data(jq[0], "checkbox"); return state.checkbox; }, initValue: function (jq, value) { return jq.each(function () { setValue(this, value, true); $.data(this, "checkbox").checkbox.find(".checkbox-value").val(value); }); }, setValue: function (jq, value) { return jq.each(function () { $(this).val(value); $.data(this, "checkbox").checkbox.find(".checkbox-value").val(value); }); }, isChecked: function (jq, value) { return $.data(jq[0], "checkbox").checkbox.find(".checkbox-value").is(':checked'); }, enable: function (jq) { return jq.each(function () { setDisable(this, false); }); }, disable: function (jq) { return jq.each(function () { setDisable(this, true); }); }, check: function (jq) { return jq.each(function () { setValue(this, true); }); }, uncheck: function (jq) { return jq.each(function () { setValue(this, false); }); }, clear: function (jq) { return jq.each(function () { setValue(this, false); }); }, reset: function (jq) { return jq.each(function () { var _28 = $(this).checkbox("options"); setValue(this, _28.originalChecked); }); } }; $.fn.checkbox.parseOptions = function (html) { var t = $(html); return $.extend({}, $.parser.parseOptions(html, ["label", { labelWidth: "number" }]), { value: (t.val() || undefined), checked: (t.attr("checked") ? true : undefined), disabled: (t.attr("disabled") ? true : undefined) }); }; $.fn.checkbox.defaults = { width: 20, height: 20, value: null, primary: true, disabled: false, checked: false, label: null, labelWidth: "auto", onChange: function (value) { } }; })(jQuery);