aboutsummaryrefslogtreecommitdiffstats
path: root/alias-angular/app/lib/jquery.forms.js
blob: 19127294af77712c992849dee6ecad4ccab36373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * Rendering of an xmpp form as an html form
 * See http://xmpp.org/extensions/xep-0004.html
 */
(function( $ ){

    var methods = {

        render: function(){
            var result = $('<form></form>');
            if ( this.find('title').length !== 0 ){
                result.append('<p class="title">' + this.find('title') + '</p>');
            }

            if ( this.find('instructions').length !== 0 ){
                result.append('<p class="instructions">'
                              + this.find('instructions') + '</p>');
            }

            this.find('field').each(function(index){
                var type = $(this).attr("type");
                var name = $(this).attr("var");
                var required = $(this).find('required').length !== 0;

                if ( $(this).find('desc').length !== 0 ){
                    result.append('<p class="description">'
                                  + $(this).find('desc').text()
                                  + '</p>');
                }

                if ( $(this).attr('label') !== undefined ){
                    result.append('<label for="form-' + name + '">'
                                  + $(this).attr('label')
                                  + (required ? ' (*): ' : ': ')
                                  + '</label>');
                }

                switch(type){
                    case("text-single"):
                        var input = $('<input/>');
                        input.attr('type', 'text');
                        input.attr('name', name);
                        input.attr('id', 'form-' + name);

                        if ( $(this).find('value').length !== 0 ){
                            input.attr('value', $(this).find('value').text());
                        }
                        result.append(input);
                        result.append('<br/>');
                }
            });
            return result;
        }
    };

    /*
     * Register the 'xmppForm' method to the jQuery objects
     * the first argument of this method is the submethod
     * you want to call
     */
    $.fn.xmppForm = function(method) {
        if ( methods[method] ) {
            return methods[method].apply(this, Array.prototype.slice
                                         .call(arguments, 1));
        } else if ( typeof method === 'object' || !method ) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.xmppForm');
        }
    };

})(jQuery);