Project

General

Profile

Statistics
| Branch: | Revision:

root / docs / www / colonyscout / internal / jeditable / jquery.jeditable.time.js @ f59acf11

History | View | Annotate | Download (2.45 KB)

1
/*
2
 * Timepicker for Jeditable
3
 *
4
 * Copyright (c) 2008-2009 Mika Tuupola
5
 *
6
 * Licensed under the MIT license:
7
 *   http://www.opensource.org/licenses/mit-license.php
8
 *
9
 * Project home:
10
 *   http://www.appelsiini.net/projects/jeditable
11
 *
12
 * Revision: $Id$
13
 *
14
 */
15
 
16
$.editable.addInputType('time', {
17
    /* Create input element. */
18
    element : function(settings, original) {
19
        /* Create and pulldowns for hours and minutes. Append them to */
20
        /* form which is accessible as variable this.                 */        
21
        var hourselect = $('<select id="hour_" />');
22
        var minselect  = $('<select id="min_" />');
23
        
24
        for (var hour=0; hour <= 23; hour++) {
25
            if (hour < 10) {
26
                hour = '0' + hour;
27
            }
28
            var option = $('<option />').val(hour).append(hour);
29
            hourselect.append(option);
30
        }
31
        $(this).append(hourselect);
32

    
33
        for (var min=0; min <= 45; min = parseInt(min, 10) + 15) {
34
            if (min < 10) {
35
                min = '0' + min;
36
            }
37
            var option = $('<option />').val(min).append(min);
38
            minselect.append(option);
39
        }
40
        $(this).append(minselect);
41
                
42
        /* Last create an hidden input. This is returned to plugin. It will */
43
        /* later hold the actual value which will be submitted to server.   */
44
        var hidden = $('<input type="hidden" />');
45
        $(this).append(hidden);
46
        return(hidden);
47
    },
48
    /* Set content / value of previously created input element. */
49
    content : function(string, settings, original) {
50
        
51
        /* Select correct hour and minute in pulldowns. */
52
        var hour = parseInt(string.substr(0,2), 10);
53
        var min  = parseInt(string.substr(3,2), 10);
54

    
55
        $('#hour_', this).children().each(function() {
56
            if (hour == $(this).val()) {
57
                $(this).attr('selected', 'selected');
58
            }
59
        });
60
        $('#min_', this).children().each(function() {
61
            if (min == $(this).val()) {
62
                $(this).attr('selected', 'selected');
63
            }
64
        });
65

    
66
    },
67
    /* Call before submit hook. */
68
    submit: function (settings, original) {
69
        /* Take values from hour and minute pulldowns. Create string such as    */
70
        /* 13:45 from them. Set value of the hidden input field to this string. */
71
        var value = $('#hour_').val() + ':' + $('#min_').val();
72
        $('input', this).val(value);
73
    }
74
});