Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21826 kshitij.so 1
/* Float Label Pattern Plugin for Bootstrap 3.1.0 by Travis Wilson
2
**************************************************/
3
(function ($) {
4
    $.fn.floatLabels = function (options) {
5
 
6
        // Settings
7
        var self = this;
8
        var settings = $.extend({}, options);
9
 
10
 
11
        // Event Handlers
12
        function registerEventHandlers() {
13
            self.on('input keyup change', 'input, textarea', function () {
14
                actions.swapLabels(this);
15
            });
16
        }
17
 
18
 
19
        // Actions
20
        var actions = {
21
            initialize: function() {
22
                self.each(function () {
23
                    var $this = $(this);
24
                    var $label = $this.children('label');
25
                    var $field = $this.find('input,textarea').first();
26
 
27
                    if ($this.children().first().is('label')) {
28
                        $this.children().first().remove();
29
                        $this.append($label);
30
                    }
31
 
32
                    var placeholderText = ($field.attr('placeholder') && $field.attr('placeholder') != $label.text()) ? $field.attr('placeholder') : $label.text();
33
 
34
                    $label.data('placeholder-text', placeholderText);
35
                    $label.data('original-text', $label.text());
36
 
37
                    if ($field.val() == '') {
38
                        $field.addClass('empty')
39
                    }
40
                });
41
            },
42
            swapLabels: function (field) {
43
                var $field = $(field);
44
                var $label = $(field).siblings('label').first();
45
                var isEmpty = Boolean($field.val());
46
 
47
                if (isEmpty) {
48
                    $field.removeClass('empty');
49
                    $label.text($label.data('original-text'));
50
                }
51
                else {
52
                    $field.addClass('empty');
53
                    $label.text($label.data('placeholder-text'));
54
                }
55
            }
56
        }
57
 
58
 
59
        // Initialization
60
        function init() {
61
            registerEventHandlers();
62
 
63
            actions.initialize();
64
            self.each(function () {
65
                actions.swapLabels($(this).find('input,textarea').first());
66
            });
67
        }
68
        init();
69
 
70
 
71
        return this;
72
    };
73
 
74
    $(function () {
75
        $('.float-label-control').floatLabels();
76
    });
77
})(jQuery);