checkbox.js
6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
define("mall/res/js/checkbox", ["mall/res/js/checkbox.html"],
function(t) {
"use strict";
function e(t) {
var e = $(t);
e.each(function() {
var t = $(this),
e = t.prop("checked"),
n = t.parent();
e ? n.addClass("selected") : n.removeClass("selected");
});
}
function n(t) {
var e = $(t);
e.each(function() {
var t = $(this).prop("disabled"),
e = $(this).parent();
t ? e.addClass("disabled") : e.removeClass("disabled");
});
}
function i() {
return "checkbox" + s++;
}
var a = {
container: null,
label: "",
name: "",
type: "checkbox"
},
c = t("mall/res/js/checkbox.html"),
r = null,
s = 2,
o = 1,
p = function(t) {
this.options = $.extend(!0, {},
a, t),
this.options.index = o++,
this.$container = $(this.options.container),
this.$dom = $(r(c, this.options)).appendTo(this.$container),
this.$input = this.$dom.find("input"),
this.$input.checkbox();
};
return p.prototype = {
checked: function(t) {
return "undefined" != typeof t && (this.$input.prop("checked", t), e(this.$input)),
this.$input.prop("checked");
},
disabled: function(t) {
return "undefined" != typeof t && (this.$input.prop("disabled", t), n(this.$input)),
this.$input.prop("disabled");
}
},
$.fn.checkbox = function(t) {
var a, c, r, s, o = !1;
"boolean" == typeof t ? a = t: $.isPlainObject(t) ? (a = t.multi, c = t.onChanged) : "string" == typeof t ? (o = !0, r = t, s = [].slice.call(arguments, 1)) : "undefined" == typeof t && (t = {}),
"undefined" == typeof a && (a = this.is("input[type=checkbox]"));
var p = this,
d = a ? "checkbox": "radio",
h = {
checked: function(t) {
return p.attr("checked", t),
p.prop("checked", t),
e(p),
p;
},
disabled: function(t) {
return p.attr("disabled", t),
p.prop("disabled", t),
n(p),
p;
},
value: function() {
var t = p.eq(0);
return t.prop("checked") ? t.val() : "";
},
values: function() {
var t = [];
return p.each(function() {
$(this).prop("checked") && t.push($(this).val());
}),
t;
},
adjust: function(t) {
var n;
return n = "string" == typeof t ? t.split(",") : t,
n && n.length > 0 && p.each(function() {
var t = $(this);
n.indexOf(t.val()) >= 0 && (t.attr("checked", !0), e(t));
}),
this;
},
disable: function(t) {
var e;
return e = "string" == typeof t ? t.split(",") : t,
e && e.length > 0 && p.each(function() {
var t = $(this);
e.indexOf(t.val()) >= 0 && (t.attr("disabled", !0), n(t));
}),
this;
},
setall: function(t) {
p.each(function() {
var e = $(this);
e.attr("disabled", t ? !1 : !0),
n(e);
});
},
enable: function(t) {
var e;
return e = "string" == typeof t ? t.split(",") : t,
e && e.length > 0 && p.each(function() {
var t = $(this);
e.indexOf(t.val()) >= 0 && (t.attr("disabled", !1), n(t));
}),
this;
},
label: function(t) {
return t && (p.parent().find(".lbl_content").text(t), p.attr("data-label", t)),
p;
}
};
return o && "function" == typeof h[r] ? h[r].apply(h, s) : (this.addClass("frm_" + d).each(function() {
var t = $(this),
e = t.parent();
if (!e.is("label")) {
var n = t.attr("data-label") || "";
e = $('<label class="frm_{type}_label"><i class="icon_{type}"></i></label>'.format({
type: d
})).append("<span class='lbl_content'>{content}</span>".format({
content: n.html(!0)
})),
e.insertBefore(t).prepend(t);
}
if (!this.id) {
var a = i();
this.id = a;
}
e.attr("for", this.id);
}), e(this), n(this), t && t.initOnChanged && "function" == typeof c && p.parent().find("input[type=checkbox],input[type=radio]").each(function() {
c.call(h, $(this));
}), this.parent().delegate("input[type=checkbox],input[type=radio]", "click",
function() {
var t = $(this),
n = t.prop("checked");
a ? (t.attr("checked", n), e(t)) : (p.attr("checked", !1), t.attr("checked", !0).prop("checked", !0), e(p)),
"function" == typeof c && c.call(h, t);
}).addClass("frm_" + d + "_label"), h);
},
p;
});