Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.1 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
          var isRadio = $this.hasClass('radio');
12

    
13
      // add class to identify form rows which contain a checkbox
14
      form.addClass("with-checkbox");
15

    
16
      
17
      if ($this.prev().length > 0) {
18
        var lbl = $this.prev()[0];
19
        if (lbl.nodeName == "LABEL" || lbl.nodeName == "label") {
20
            $(lbl).addClass("checkbox-label");
21

    
22
            $(lbl).click(function(e){
23
                    if (isRadio && $this.attr('checked')){ return; }
24
                var src = e.srcElement.nodeName;
25
                if (src == "LABEL" || src == "label") {
26
                    el.toggleClass("checked");        
27
                    $this.attr('checked', el.hasClass("checked"));
28
                    $this.trigger('changed');
29
                };
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
              parentForm = $(this).parents('form');
45
              if ( parentForm.hasClass('hidden-submit') ){
46
                      $('.hidden-submit .form-row.submit').slideDown(500);
47
              } 
48
              
49
                if (isRadio && $this.attr('checked')){ return; }
50
        el.toggleClass("checked");
51
        $this.attr('checked', el.hasClass("checked"));
52
        $this.trigger('changed');
53
      });
54
      
55
      el.keypress(function(e){
56
              
57
              if (e.keyCode == 0 || e.keyCode == 32){
58
                      if (isRadio && $this.attr('checked')){ return; }
59
                      e.preventDefault();
60
                      el.toggleClass("checked");
61
                $this.attr('checked', el.hasClass("checked"));
62
                $this.trigger('changed');
63
              }
64
      })
65

    
66
      $this.prev('label').before(el);
67
    });
68

    
69

    
70
  }
71

    
72
  $.fn.formErrors = function(options) {  
73
    return this.each(function() {
74
        var $this = $(this);
75

    
76
        // does the field has any errors ?
77
        var errors = $this.find(".errorlist");
78
        if (errors.length == 0) {
79
            return;
80
        }
81
        
82
        // create the custom error message block
83
        // and copy the contents of the original
84
        // error list
85
        var el = $('<div class="form-error" />');
86
        errors.find("li").each(function(){
87
            el.html(el.html() + $(this).html() + "<br />");
88
        })
89
        
90
        var formel = $this.find("input, select");
91
        var lbl = $this.find("label");
92
        var form = $this.closest("form");
93

    
94

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

    
125
  };
126
  
127

    
128
  function renewToken() {
129
    var MIDDLEWARE_TOKEN_INPUT_NAME = window.MIDDLEWARE_TOKEN_INPUT_NAME || 'csrfmiddlewaretoken';
130
    var CHANGE_TOKEN_URL = window.CHANGE_TOKEN_URL;
131
    var csrf_value = $("input[name="+MIDDLEWARE_TOKEN_INPUT_NAME+"]").val();
132
    var url = CHANGE_TOKEN_URL;
133
    var form = $("<form>");
134
    var csrf = $('<input type="hidden">');
135

    
136
    form.attr('action', url);
137
    form.attr('method', 'POST');
138
    csrf.attr('value', csrf_value);
139
    csrf.attr('name', MIDDLEWARE_TOKEN_INPUT_NAME);
140
    form.append(csrf);
141
    $("body").prepend(form);
142
    form.submit();
143
  }
144

    
145
  window.renewToken= renewToken;
146

    
147
})( jQuery );
148

    
149

    
150