Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / static / im / js / forms.js @ a70dacde

History | View | Annotate | Download (2.6 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 = $('<span class="checkbox-widget" />');
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.is("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
      $this.after(el);
39
    });
40

    
41

    
42
  }
43

    
44
  $.fn.formErrors = function(options) {  
45
    return this.each(function() {
46
        var $this = $(this);
47

    
48
        // does the field has any errors ?
49
        var errors = $this.find(".errorlist");
50
        if (errors.length == 0) {
51
            return;
52
        }
53
        
54
        // create the custom error message block
55
        // and copy the contents of the original
56
        // error list
57
        var el = $('<div class="form-error" />');
58
        errors.find("li").each(function(){
59
            el.html(el.html() + $(this).text() + "<br />");
60
        })
61
        
62
        var formel = $this.find("input, select");
63
        var lbl = $this.find("label");
64
        var form = $this.closest("form");
65

    
66

    
67
        // append element on form row 
68
        // and apply the appropriate styles
69
        formel.closest(".form-row").append(el);
70
        errors.remove();
71
        var left = formel.width();
72
        var top = formel.height();
73
        var marginleft = lbl.width();
74
        
75
        // identify the position
76
        // forms with innerlbales class
77
        // display the label within the input fields
78
        if ($(form).hasClass("innerlabels")) {
79
            marginleft = 0;
80
        }
81
        
82
        var styles = {
83
            left: left + "px", 
84
            top: top + "px", 
85
            width: formel.outerWidth() - 10,
86
            marginLeft: marginleft,
87
            marginBottom: 5
88
        }
89
        
90
        if (formel.attr("type") != "checkbox") {
91
            el.css(styles);
92
        } else {
93
            el.css("margin-top", "5px");
94
        }
95
    });
96

    
97
  };
98
})( jQuery );