]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/timestamp.js
141551cd8a19297c748c6e074434ce9eb76027ba
[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',{'modal_xulG':true}) || util.date.formatted_date(new Date(),'%F');
26         if (xul_param('default_time',{'modal_xulG':true})) {
27             $('timepicker').value = xul_param('default_time',{'modal_xulG':true});
28         }
29         if (xul_param('time_readonly',{'modal_xulG':true})) {
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',{'modal_xulG':true})) {
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',{'modal_xulG':true})) { $('dialogheader').setAttribute('title',xul_param('title',{'modal_xulG':true})); }
39         if (xul_param('description',{'modal_xulG':true})) { $('dialogheader').setAttribute('description',xul_param('description',{'modal_xulG':true})); }
40
41         var x = $('msg_area');
42         if (x && xul_param('msg',{'modal_xulG':true})) {
43             var d = document.createElement('description');
44             var t = document.createTextNode( xul_param('msg',{'modal_xulG':true}) );
45             x.appendChild( d );
46             d.appendChild( t );
47         }
48
49         if (xul_param('allow_unset',{'modal_xulG':true})) { $('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',{'modal_xulG':true})) {
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',{'modal_xulG':true})) {
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',{'modal_xulG':true})) {
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                 update_modal_xulG(
114                     {
115                         'timestamp' : null,
116                         'complete' : 1
117                     }
118                 )
119                 window.close();
120             } else {
121
122                 var dp = $('datepicker');
123                 var tp = $('timepicker');
124
125                 var check = check_date( dp.value );
126                 if ( ! check.allowed ) { alert( check.reason ); $('apply_btn').disabled = true; return; }
127
128                 var tp_date = tp.dateValue;
129                 var dp_date = dp.dateValue;
130                 dp_date.setHours( tp_date.getHours() );
131                 dp_date.setMinutes( tp_date.getMinutes() );
132
133                 update_modal_xulG(
134                     {
135                         'timestamp' : util.date.formatted_date(dp_date,'%{iso8601}'),
136                         'complete' : 1
137                     }
138                 )
139                 window.close();
140             }
141
142         } catch(E) {
143             alert('Error in timestamp.js, handle_apply(): ' + E);
144         }
145     };
146 }