Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / invitations.js @ c566f369

History | View | Annotate | Download (5.5 kB)

1
// Copyright 2011 GRNET S.A. All rights reserved.
2
// 
3
// Redistribution and use in source and binary forms, with or
4
// without modification, are permitted provided that the following
5
// conditions are met:
6
// 
7
//   1. Redistributions of source code must retain the above
8
//      copyright notice, this list of conditions and the following
9
//      disclaimer.
10
// 
11
//   2. Redistributions in binary form must reproduce the above
12
//      copyright notice, this list of conditions and the following
13
//      disclaimer in the documentation and/or other materials
14
//      provided with the distribution.
15
// 
16
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
// POSSIBILITY OF SUCH DAMAGE.
28
// 
29
// The views and conclusions contained in the software and
30
// documentation are those of the authors and should not be
31
// interpreted as representing official policies, either expressed
32
// or implied, of GRNET S.A.
33
// 
34

    
35
/*
36
*        jQuery dynamicField plugin
37
*        Copyright 2009, Matt Quackenbush (http://www.quackfuzed.com/)
38
*
39
*        Find usage demos at http://www.quackfuzed.com/demos/jQuery/dynamicField/index.cfm)
40
*
41
*        Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
42
*        and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
43
*
44
*        Version: 1.0
45
*        Date:         8/13/2009
46
*/
47
;(function($) {
48
        $.fn.dynamicField = function(options) {
49
                if ( $(this).attr("id") == undefined ) {
50
                        throw "The dynamicField plugin could not be initialized.\n\nPlease check the selector.";
51
                        return $;
52
                }
53

    
54
                var f = $(this);
55

    
56
                var settings = $.extend({
57
                        maxFields: 5,
58
                        removeImgSrc: "/static/invitations/cross.png",
59
                        spacerImgSrc: "/static/invitations/spacer.gif",
60
                        addTriggerClass: "add-field-trigger",
61
                        removeImgClass: "remove-field-trigger",
62
                        hideClass: "hide",
63
                        cloneContainerId: f.attr("id").replace(/^(.+)([_-][0-9]+)$/,"$1"),
64
                        rowContainerClass: f.attr("class"),
65
                        labelText: f.children("label")
66
                                                        .html(),
67
                        baseName: f.children("input")
68
                                                                .attr("name")
69
                                                                .replace(/^(.+[_-])([0-9]+)$/,"$1"),
70
            baseNames: baseNames(),
71
                        addContainerId: "add-" + f.children("input")
72
                                                                .attr("name")
73
                                                                .replace(/^(.+)([_-][0-9]+)$/,"$1")
74
                                                                .replace(/_/g,"-") + "-container"
75
                },options);
76
                
77
                var getFields = function() {
78
                        return $("div." + settings.rowContainerClass);
79
                };
80

    
81
        function baseNames() {
82
            var names = new Array();
83
            $.each(f.children("input"), function(index, child){
84
                var name = child.name.replace(/^(.+[_-])([0-9]+)$/,"$1")
85
                names.push(name);
86
            });
87
            return names;
88
        }
89
                
90
                // handle hide/show, etc
91
                var addRemoveBtnCk = function() {
92
                        var fields = getFields();
93
                        var len = fields.length;
94
                        
95
                        fields.each(function(i,elem) {
96
                                $(elem)
97
                                        .children("img")
98
                                        .attr({
99
                                                "src":(len == 1) ? settings.spacerImgSrc : settings.removeImgSrc,
100
                                                "class":(len == 1) ? "" : settings.removeImgClass
101
                                        });
102
                        });
103
                        
104
                        if ( len > (settings.maxFields-1) ) {
105
                                $("div#" + settings.addContainerId).addClass(settings.hideClass);
106
                        } else {
107
                                $("div#" + settings.addContainerId).removeClass(settings.hideClass);
108
                        }
109
                };
110
                
111
                // handle field removal
112
                $("img." + settings.removeImgClass).live("click",function() {
113
                        // remove the selected row
114
                        $(this).parent("div." + settings.rowContainerClass).remove();
115
                        
116
                        // rebrand the remaining fields sequentially
117
                        getFields().each(function(i,elem) {
118
                                var pos = new Number(i+1);
119
                                var d = $(elem)
120
                                                        .attr("id",settings.cloneContainerId + "-" + pos);
121

    
122
                                d.children("label")
123
                                                        .attr("for",settings.baseName + pos)
124
                                                        .html((pos > 1) ? "" : settings.labelText);
125
                                
126
                names = settings.baseNames;
127
                                d.children("input").each(function(i){
128
                    $(this).attr({
129
                        "id": names[i] + pos,
130
                        "name": names[i] + pos
131
                    });
132
                });
133
                        });
134
                        
135
                        addRemoveBtnCk();
136
                });
137

    
138
                // handle field add
139
                $("div#" + settings.addContainerId + " span." + settings.addTriggerClass).click(function() {
140
                        var len = getFields().length;
141
                        var pos = new Number(len+1);
142
                        var newDiv = $("<div/>")
143
                                                        .attr("id",settings.cloneContainerId + "-" + pos)
144
                                                        .addClass(settings.rowContainerClass);
145
            
146
            $.each(settings.baseNames, function(index, name) {
147

    
148
                var input = $("<input/>").attr({
149
                                                                "id":name + pos,
150
                                                                "name":name + pos,
151
                                                                "value":""
152
                                                        });
153
                newDiv.append(input);
154
            });
155
            newDiv.append($("<img>").attr("src",settings.removeImgSrc));
156
            
157
                        if ( len > 0 ) {
158
                                $("div#" + settings.cloneContainerId + "-" + len).after(newDiv);
159
                        } else {
160
                                $("div#" + settings.addContainerId).before(newDiv);
161
                        }
162
                        
163
                        addRemoveBtnCk();
164
                });
165
        };
166
})(jQuery);