root / cloudcms / static / cloudcms / js / forms.js @ 4b5adcc3
History | View | Annotate | Download (2.9 kB)
1 |
(function($){ |
---|---|
2 |
|
3 |
$.fn.formCheckBoxes = function(options) { |
4 |
|
5 |
return this.each(function() { |
6 |
// process checkboxes
|
7 |
var $this = $(this); |
8 |
var el = $('<a class="checkbox-widget" href="javascript:void(0)"/>'); |
9 |
var form = $this.closest(".form-row"); |
10 |
|
11 |
// add class to identify form rows which contain a checkbox
|
12 |
form.addClass("with-checkbox");
|
13 |
|
14 |
if ($this.prev().length > 0) { |
15 |
var lbl = $this.prev()[0]; |
16 |
if (lbl.nodeName == "LABEL" || lbl.nodeName == "label") { |
17 |
$(lbl).addClass("checkbox-label"); |
18 |
|
19 |
$(lbl).click(function(e){ |
20 |
var src = e.srcElement.nodeName;
|
21 |
if (src == "LABEL" || src == "label") { |
22 |
el.toggleClass("checked");
|
23 |
}; |
24 |
}) |
25 |
} |
26 |
} |
27 |
$this.hide();
|
28 |
|
29 |
if ($this.attr('checked')) { |
30 |
el.addClass("checked");
|
31 |
} |
32 |
|
33 |
el.click(function() {
|
34 |
el.toggleClass("checked");
|
35 |
$this.attr('checked', el.hasClass("checked")); |
36 |
}); |
37 |
|
38 |
el.keypress(function(e){
|
39 |
|
40 |
if (e.keyCode == 0 || e.keyCode == 32){ |
41 |
e.preventDefault(); |
42 |
el.toggleClass("checked");
|
43 |
$this.attr('checked', el.hasClass("checked")); |
44 |
} |
45 |
}) |
46 |
|
47 |
$this.prev('label').before(el); |
48 |
}); |
49 |
|
50 |
|
51 |
} |
52 |
|
53 |
$.fn.formErrors = function(options) { |
54 |
return this.each(function() { |
55 |
var $this = $(this); |
56 |
|
57 |
// does the field has any errors ?
|
58 |
var errors = $this.find(".errorlist"); |
59 |
if (errors.length == 0) { |
60 |
return;
|
61 |
} |
62 |
|
63 |
// create the custom error message block
|
64 |
// and copy the contents of the original
|
65 |
// error list
|
66 |
var el = $('<div class="form-error" />'); |
67 |
errors.find("li").each(function(){ |
68 |
el.html(el.html() + $(this).text() + "<br />"); |
69 |
}) |
70 |
|
71 |
var formel = $this.find("input, select"); |
72 |
var lbl = $this.find("label"); |
73 |
var form = $this.closest("form"); |
74 |
|
75 |
|
76 |
// append element on form row
|
77 |
// and apply the appropriate styles
|
78 |
formel.closest(".form-row").append(el);
|
79 |
errors.remove(); |
80 |
var left = formel.width();
|
81 |
var top = formel.height();
|
82 |
var marginleft = lbl.width();
|
83 |
|
84 |
// identify the position
|
85 |
// forms with innerlbales class
|
86 |
// display the label within the input fields
|
87 |
if ($(form).hasClass("innerlabels")) { |
88 |
marginleft = 0;
|
89 |
} |
90 |
|
91 |
var styles = {
|
92 |
left: left + "px", |
93 |
top: top + "px", |
94 |
width: formel.outerWidth() - 10, |
95 |
marginLeft: marginleft,
|
96 |
marginBottom: 5 |
97 |
} |
98 |
|
99 |
if (formel.attr("type") != "checkbox") { |
100 |
el.css(styles); |
101 |
} else {
|
102 |
el.css("margin-top", "5px"); |
103 |
} |
104 |
}); |
105 |
|
106 |
}; |
107 |
})( jQuery ); |