Source: lib/standardControls.js

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
 * JTSage-DateBox
 * @fileOverview Standard controls (selects and buttons)
 * @author J.T.Sage <jtsage+datebox@gmail.com>
 * @author {@link https://github.com/jtsage/jtsage-datebox/contributors|GitHub Contributors}
 * @license {@link https://github.com/jtsage/jtsage-datebox/blob/master/LICENSE.txt|MIT}
 * @version 5.2.0
 */

/**
 * Create a simple select html element
 *
 * @param {!Array<Array>} data Each inside array is [ value, title, selected(bool) ]
 * @param {string} id HTML ID for the select
 * @param {string} cls Class for the select
 * @returns {String} Completed select element 
 */
JTSageDateBox._stdSel = function( data, id, cls ) {
    var i, returnVal = "<select class='" + cls + "' id='" + id + "'>";

    for ( i = 0; i < data.length; i++ ) {
        returnVal += "<option value='" + data[i][0] + "'" +
            ( data[i][2] === true ? " selected='selected'" : "" ) + ">" +
            data[i][1] + "</option>";
    }
    returnVal += "</select>";

    return returnVal;
};

/**
 * Create standard buttons
 *
 * @type Object
 * @memberof JTSageDateBox
 * @namespace JTSageDateBox._stdbtn
 */
JTSageDateBox._stdBtn = {

    /**
     * Make a cancel button.
     *
     * @returns {Object} JQuery button object, with events attached
     * @memberof JTSageDateBox._stdbtn
     */
    cancel : function() {
        var w = this,
            o = this.options;

        return $(
            w.style_btn(
                o.theme_cancelBtn,
                w.__( "cancelButton" )
            ) )
            .on( o.clickEvent, function ( e ) {
                e.preventDefault();
                w._t({ method : "close", closeCancel : true });
            } );
    },

    /**
     * Make a clear button
     * 
     * @return {Object} JQuery button object, with events attached
     * @memberOf JTSageDateBox._stdbtn
     */
    clear : function() {
        var w = this,
            o = this.options;

        return $(
            w.style_btn(
                o.theme_clearBtn,
                w.__( "clearButton" )
            ) )
            .on( o.clickEvent, function( e ) {
                e.preventDefault();
                w.d.input.val( "" );
                w._t( { method : "clear" } );
                w._t( { method : "close", closeCancel : true } );
            });
    },

    /**
     * Make a close button
     * 
     * @param  {string} txt Button text, if any
     * @param  {boolean|string} trigger If trigger is false, run set, otherwise run named trigger 
     * @return {Object} JQuery button object, with events attached
     * @memberOf JTSageDateBox._stdbtn
     */
    close : function( txt, trigger ) {
        var w = this,
            o = this.options;

        if ( typeof trigger === "undefined" ) { trigger = false; }

        return $(
            w.style_btn(
                o.theme_closeBtn,
                txt
            ) )
            .addClass( "" +
                ( ( w.dateOK === true ) ? "" : "disabled" )
            )
            .on( o.clickEvent, function( e ) {
                e.preventDefault();

                if ( w.dateOK === true ) {
                    if ( trigger === false ) {
                        w._t( {
                            method : "set",
                            value  : w._formatter( w.__fmt(), w.theDate ),
                            date   : w.theDate
                        } );
                    } else {
                        w._t( trigger );
                    }
                    w._t( { method : "close" } );
                }
            });
    },

    /**
     * Make a today button
     * 
     * @return {Object} JQuery button object, with events attached
     * @memberOf JTSageDateBox._stdbtn
     */
    today : function() {
        var w = this,
            o = this.options;
        return $(
            w.style_btn(
                o.theme_todayBtn,
                w.__( "todayButtonLabel" )
            ) )
            .on( o.clickEvent, function( e ) {
                e.preventDefault();
                w.theDate = w._pa( [ 0, 0, 0 ], new w._date() );
                w._t( { method : "dorefresh" } );
                if ( o.closeTodayButton !== false ) {
                    w._t( { method : "doset" } );
                    w._t( { method : "close" } );
                }
            });
    },

    /**
     * Make a tomorrow button
     * 
     * @return {Object} JQuery button object, with events attached
     * @memberOf JTSageDateBox._stdbtn
     */
    tomorrow : function() {
        var w = this,
            o = this.options;

        return $(
            w.style_btn(
                o.theme_tomorrowBtn,
                w.__( "tomorrowButtonLabel" )
            ) )
            .on( o.clickEvent, function( e ) {
                e.preventDefault();
                w.theDate = w._pa( [ 0, 0, 0 ], new w._date() ).adj( 2, 1 );
                w._t( { method : "dorefresh" } );
                if ( o.closeTomorrowButton !== false ) {
                    w._t( { method : "doset" } );
                    w._t( { method : "close" } );
                }
            });
    },
};

/** 
 * Actually apply the bottom buttons to the control
 * 
 * @param {boolean} useSet Allow the set button to be displayed
 * @return {object} jQuery Object.
 */
JTSageDateBox._doBottomButtons = function ( useSet ) {
    var w   = this,
        o   = this.options,
        ctrlContainer, ctrlWrk;

    if ( ! (
        ( o.useSetButton && useSet )  ||
        o.useTodayButton              ||
        o.useTomorrowButton           ||
        o.useClearButton              ||
        o.useCancelButton
    ) ) {
        return "";
    }

    ctrlContainer = w.style_btnGrp( o.useCollapsedBut );
    
    if ( o.useSetButton && useSet ) {
        switch (o.mode) {
            case "timebox"         :
            case "timeflipbox"     :
                ctrlWrk = w.__( "setTimeButtonLabel" ); break;
            case "durationbox"     :
            case "duartionflipbox" :
                ctrlWrk = w.__( "setDurationButtonLabel" ); break;
            default  :
                ctrlWrk = w.__( "setDateButtonLabel" ); break;
        }
        w.setBut = w._stdBtn.close.call( w, ctrlWrk );
        w.setBut.appendTo( ctrlContainer );
    }

    if ( o.useTodayButton ) {
        ctrlContainer.append( w._stdBtn.today.call( w ) );
    }
    if ( o.useTomorrowButton ) {
        ctrlContainer.append( w._stdBtn.tomorrow.call( w ) );
    }
    if ( o.useClearButton ) {
        ctrlContainer.append( w._stdBtn.clear.call( w ) );
    }
    if ( o.useCancelButton ) {
        ctrlContainer.append( w._stdBtn.cancel.call( w ) );
    }

    if ( typeof w.style_btnGrpOut === "function" ) {
        // Used if the framework requires an additional wrap to button
        // groups.  Some do, notable jQM.
        ctrlContainer = w.style_btnGrpOut( o.useCollapsedBut, ctrlContainer );
    }
    
    return ctrlContainer;
};