Statistics
| Branch: | Tag: | Revision:

root / cloudcms / static / cloudcms / js / forms.js @ 322a5bbc

History | View | Annotate | Download (3.2 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
                                        
25
                };
26
                if ($this.attr('checked')=='checked') {
27
                                        ($this.removeAttr('checked'))
28
                                } else {
29
                                        $this.attr('checked','checked')
30
                                }
31
            })
32
        }
33
      }
34
      $this.hide();
35
      
36
      if ($this.attr('checked')) {
37
        el.addClass("checked");  
38
        
39
      }
40

    
41
          el.addClass(className);        
42
                
43
      el.click(function() {
44
        el.toggleClass("checked");
45
        $this.attr('checked', el.hasClass("checked"));
46
      });
47
      
48
      el.keypress(function(e){
49
              
50
              if (e.keyCode == 0 || e.keyCode == 32){
51
                      e.preventDefault();
52
                      el.toggleClass("checked");
53
                $this.attr('checked', el.hasClass("checked"));
54
              }
55
      })
56

    
57
      $this.prev('label').before(el);
58
    });
59

    
60

    
61
  }
62

    
63
  $.fn.formErrors = function(options) {  
64
    return this.each(function() {
65
        var $this = $(this);
66

    
67
        // does the field has any errors ?
68
        var errors = $this.find(".errorlist");
69
        if (errors.length == 0) {
70
            return;
71
        }
72
        
73
        // create the custom error message block
74
        // and copy the contents of the original
75
        // error list
76
        var el = $('<div class="form-error" />');
77
        errors.find("li").each(function(){
78
            el.html(el.html() + $(this).text() + "<br />");
79
        })
80
        
81
        var formel = $this.find("input, select");
82
        var lbl = $this.find("label");
83
        var form = $this.closest("form");
84

    
85

    
86
        // append element on form row 
87
        // and apply the appropriate styles
88
        formel.closest(".form-row").append(el);
89
        errors.remove();
90
        var left = formel.width();
91
        var top = formel.height();
92
        var marginleft = lbl.width();
93
        
94
        // identify the position
95
        // forms with innerlbales class
96
        // display the label within the input fields
97
        if ($(form).hasClass("innerlabels")) {
98
            marginleft = 0;
99
        }
100
        
101
        var styles = {
102
            left: left + "px", 
103
            top: top + "px", 
104
            width: formel.outerWidth() - 10,
105
            marginLeft: marginleft,
106
            marginBottom: 5
107
        }
108
        
109
        if (formel.attr("type") != "checkbox") {
110
            el.css(styles);
111
        } else {
112
            el.css("margin-top", "5px");
113
        }
114
    });
115

    
116
  };
117
})( jQuery );
118