]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/timestamp.js
internal: an alternative to default_focus
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / timestamp.js
1 var data; var error; var network; var sound;
2
3 function $(id) { return document.getElementById(id); }
4
5 function default_focus() { $('cancel_btn').focus(); } // parent interfaces often call this
6
7 function timestamp_init() {
8     try {
9
10         commonStrings = $('commonStrings');
11
12         if (typeof JSAN == 'undefined') {
13             throw(
14                 commonStrings.getString('common.jsan.missing')
15             );
16         }
17
18         JSAN.errorLevel = "die"; // none, warn, or die
19         JSAN.addRepository('..');
20
21         JSAN.use('util.error'); error = new util.error();
22         JSAN.use('util.sound'); sound = new util.sound();
23         JSAN.use('util.date'); 
24
25         $('datepicker').value = xul_param('default_date') || util.date.formatted_date(new Date(),'%F');
26         if (xul_param('default_time')) {
27             $('timepicker').value = xul_param('default_time');
28         }
29         if (xul_param('time_readonly')) {
30             $('timepicker').readonly = true; // This isn't working correctly with xulrunner 1.9.2
31             $('timepicker').disabled = true; // So, poor man's kludge
32         }
33         if (xul_param('date_readonly')) {
34             $('datepicker').readonly = true; // This isn't working correctly with xulrunner 1.9.2
35             $('datepicker').disabled = true; // So, poor man's kludge
36         }
37
38         if (xul_param('title')) { $('dialogheader').setAttribute('title',xul_param('title')); }
39         if (xul_param('description')) { $('dialogheader').setAttribute('description',xul_param('description')); }
40
41         var x = $('msg_area');
42         if (x && xul_param('msg')) {
43             var d = document.createElement('description');
44             var t = document.createTextNode( xul_param('msg') );
45             x.appendChild( d );
46             d.appendChild( t );
47         }
48
49         if (xul_param('allow_unset')) { $('remove_btn').hidden = false; }
50
51         /* set widget behavior */
52         $('cancel_btn').addEventListener(
53             'command', function() { window.close(); }, false
54         );
55         $('apply_btn').addEventListener(
56             'command', 
57             gen_handle_apply(),
58             false
59         );
60         $('remove_btn').addEventListener(
61             'command', 
62             gen_handle_apply({'remove':true}),
63             false
64         );
65
66         $('datepicker').addEventListener(
67             'change',
68             function(ev) {
69                 try {
70                     var check = check_date( ev.target.value );
71                     if ( ! check.allowed ) { throw( check.reason ); }
72                     $('apply_btn').disabled = false;
73                 } catch(E) {
74                     JSAN.use('util.sound'); var sound = new util.sound(); sound.bad();
75                     var x = $('err_msg');
76                     if (x) {
77                         x.setAttribute('value', check.reason);
78                     }
79                     $('apply_btn').disabled = true;
80                 }
81                 dump('util.timestamp.js:date: ' + E + '\n');
82             },
83             false
84         );
85
86         default_focus();
87
88     } catch(E) {
89         var err_prefix = 'timestamp.js -> timestamp_init() : ';
90         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
91     }
92 }
93
94 function check_date(value) {
95     if (xul_param('disallow_future_dates')) {
96         if ( value > new Date() ) { return { 'allowed' : false, 'reason' : $('commonStrings').getString('staff.util.timestamp_dialog.future_date_disallowed') }; }
97     }
98     if (xul_param('disallow_past_dates')) {
99         if ( util.date.check_past('YYYY-MM-DD', value) ) { return { 'allowed' : false, 'reason' : $('commonStrings').getString('staff.util.timestamp_dialog.past_date_disallowed') }; }
100     }
101     if (xul_param('disallow_today')) {
102         if ( util.date.formatted_date(new Date(),'%F') == value) { return { 'allowed' : false, 'reason' : $('commonStrings').getString('staff.util.timestamp_dialog.today_disallowed') }; }
103     }
104     return { 'allowed' : true };
105 }
106
107 function gen_handle_apply(params) {
108     return function handle_apply(ev) {
109         try {
110
111             if (!params) { params = {}; }
112             if (params.remove) {
113                 xulG.timestamp = null;
114                 xulG.complete = 1;
115                 window.close();
116             } else {
117
118                 var dp = $('datepicker');
119                 var tp = $('timepicker');
120
121                 var check = check_date( dp.value );
122                 if ( ! check.allowed ) { alert( check.reason ); $('apply_btn').disabled = true; return; }
123
124                 var tp_date = tp.dateValue;
125                 var dp_date = dp.dateValue;
126                 dp_date.setHours( tp_date.getHours() );
127                 dp_date.setMinutes( tp_date.getMinutes() );
128
129                 xulG.timestamp = util.date.formatted_date(dp_date,'%{iso8601}');
130                 xulG.complete = 1;
131                 window.close();
132             }
133
134         } catch(E) {
135             alert('Error in timestamp.js, handle_apply(): ' + E);
136         }
137     };
138 }