]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/timestamp.js
copy/paste-o
[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
27         if (xul_param('title',{'modal_xulG':true})) { $('dialogheader').setAttribute('title',xul_param('title',{'modal_xulG':true})); }
28         if (xul_param('description',{'modal_xulG':true})) { $('dialogheader').setAttribute('description',xul_param('description',{'modal_xulG':true})); }
29
30         var x = $('msg_area');
31         if (x && xul_param('msg',{'modal_xulG':true})) {
32             var d = document.createElement('description');
33             var t = document.createTextNode( xul_param('msg',{'modal_xulG':true}) );
34             x.appendChild( d );
35             d.appendChild( t );
36         }
37
38         if (xul_param('allow_unset',{'modal_xulG':true})) { $('remove_btn').hidden = false; }
39
40         /* set widget behavior */
41         $('cancel_btn').addEventListener(
42             'command', function() { window.close(); }, false
43         );
44         $('apply_btn').addEventListener(
45             'command', 
46             gen_handle_apply(),
47             false
48         );
49         $('remove_btn').addEventListener(
50             'command', 
51             gen_handle_apply({'remove':true}),
52             false
53         );
54
55         $('datepicker').addEventListener(
56             'change',
57             function(ev) {
58                 try {
59                     var check = check_date( ev.target.value );
60                     if ( ! check.allowed ) { throw( check.reason ); }
61                     $('apply_btn').disabled = false;
62                 } catch(E) {
63                     JSAN.use('util.sound'); var sound = new util.sound(); sound.bad();
64                     var x = $('err_msg');
65                     if (x) {
66                         x.setAttribute('value', check.reason);
67                     }
68                     $('apply_btn').disabled = true;
69                 }
70                 dump('util.timestamp.js:date: ' + E + '\n');
71             },
72             false
73         );
74
75         default_focus();
76
77     } catch(E) {
78         var err_prefix = 'timestamp.js -> timestamp_init() : ';
79         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
80     }
81 }
82
83 function check_date(value) {
84     if (xul_param('disallow_future_dates',{'modal_xulG':true})) {
85         if ( ev.target.dateValue > new Date() ) { return { 'allowed' : false, 'reason' : $('commonStrings').getString('staff.util.timestamp_dialog.future_date_disallowed') }; }
86     }
87     if (xul_param('disallow_past_dates',{'modal_xulG':true})) {
88         if ( util.date.check_past('YYYY-MM-DD', ev.target.value) ) { return { 'allowed' : false, 'reason' : $('commonStrings').getString('staff.util.timestamp_dialog.past_date_disallowed') }; }
89     }
90     if (xul_param('disallow_today',{'modal_xulG':true})) {
91         if ( util.date.formatted_date(new Date(),'%F') == value) { return { 'allowed' : false, 'reason' : $('commonStrings').getString('staff.util.timestamp_dialog.today_disallowed') }; }
92     }
93     return { 'allowed' : true };
94 }
95
96 function gen_handle_apply(params) {
97     return function handle_apply(ev) {
98         try {
99
100             if (!params) { params = {}; }
101             if (params.remove) {
102                 update_modal_xulG(
103                     {
104                         'timestamp' : null,
105                         'complete' : 1
106                     }
107                 )
108                 window.close();
109             } else {
110
111                 var dp = $('datepicker');
112                 var tp = $('timepicker');
113
114                 var check = check_date( dp.value );
115                 if ( ! check.allowed ) { alert( check.reason ); $('apply_btn').disabled = true; return; }
116
117                 var tp_date = tp.dateValue;
118                 var dp_date = dp.dateValue;
119                 tp_date.setFullYear( dp_date.getFullYear() );
120                 tp_date.setMonth( dp_date.getMonth() );
121                 tp_date.setDate( dp_date.getDate() );
122
123                 update_modal_xulG(
124                     {
125                         'timestamp' : util.date.formatted_date(tp_date,'%{iso8601}'),
126                         'complete' : 1
127                     }
128                 )
129                 window.close();
130             }
131
132         } catch(E) {
133             alert('Error in timestamp.js, handle_apply(): ' + E);
134         }
135     };
136 }