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