public
Description: jQuery edit in place plugin. Extendable via plugin architecture. Plugins for plugin. Really.
Homepage: http://www.appelsiini.net/projects/jeditable
Clone URL: git://github.com/tuupola/jquery_jeditable.git
tuupola (author)
Fri Nov 06 06:28:18 -0800 2009
commit  39b62fc41129672417475f0781fb567a43faf628
tree    4d2f1e2a48958126282e25e02e2d2d85217c6009
parent  e6ed20276d24250d78a4f034d4b920d43140d60e
jquery_jeditable / jquery.jeditable.time.js
100644 75 lines (67 sloc) 2.509 kb
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
74
75
/*
* Timepicker for Jeditable
*
* Copyright (c) 2008-2009 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* http://www.appelsiini.net/projects/jeditable
*
* Revision: $Id$
*
*/
 
$.editable.addInputType('time', {
    /* Create input element. */
    element : function(settings, original) {
        /* Create and pulldowns for hours and minutes. Append them to */
        /* form which is accessible as variable this. */
        var hourselect = $('<select id="hour_" />');
        var minselect = $('<select id="min_" />');
        
        for (var hour=0; hour <= 23; hour++) {
            if (hour < 10) {
                hour = '0' + hour;
            }
            var option = $('<option />').val(hour).append(hour);
            hourselect.append(option);
        }
        $(this).append(hourselect);
 
        for (var min=0; min <= 45; min = parseInt(min, 10) + 15) {
            if (min < 10) {
                min = '0' + min;
            }
            var option = $('<option />').val(min).append(min);
            minselect.append(option);
        }
        $(this).append(minselect);
                
        /* Last create an hidden input. This is returned to plugin. It will */
        /* later hold the actual value which will be submitted to server. */
        var hidden = $('<input type="hidden" />');
        $(this).append(hidden);
        return(hidden);
    },
    /* Set content / value of previously created input element. */
    content : function(string, settings, original) {
        
        /* Select correct hour and minute in pulldowns. */
        var hour = parseInt(string.substr(0,2), 10);
        var min = parseInt(string.substr(3,2), 10);
 
        $('#hour_', this).children().each(function() {
            if (hour == $(this).val()) {
                $(this).attr('selected', 'selected');
            }
        });
        $('#min_', this).children().each(function() {
            if (min == $(this).val()) {
                $(this).attr('selected', 'selected');
            }
        });
 
    },
    /* Call before submit hook. */
    submit: function (settings, original) {
        /* Take values from hour and minute pulldowns. Create string such as */
        /* 13:45 from them. Set value of the hidden input field to this string. */
        var value = $('#hour_').val() + ':' + $('#min_').val();
        $('input', this).val(value);
    }
});