masked input clipboard hack, part 2
[freeside.git] / httemplate / elements / tr-input-mask.html
1 % if ( !$init ) {
2 <script type="text/javascript" src="<%$p%>elements/masked_input_1.1.js">
3 </script>
4 % $init++;
5 % }
6 <& /elements/tr-input-text.html, id => $id, @_ &>
7 <script type="text/javascript">
8 <&| /elements/onload.js &>
9 MaskedInput({
10   elm: document.getElementById('<%$id%>'),
11   format: '<% $opt{format} %>',
12   <% $opt{allowed} ? "allowed: '$opt{allowed}'," : '' %>
13   <% $opt{typeon}  ? "typeon:  '$opt{typeon}',"  : '' %>
14 });
15 document.getElementById('<%$id%>').value = <% $value |js_string %>;
16 % if ( $clipboard_hack ) {
17 var t = document.getElementById('<% $id %>');
18 var container = document.getElementById('<%$id%>_clipboard');
19 var KeyHandlerDown = t.onkeydown
20 t.onkeydown = function(e) {
21   // intercept ctrl-c and ctrl-x
22   // and cmd-c and cmd-x on mac
23   // when text is selected
24   if ( ( e.ctrlKey || e.metaKey ) ) {
25     // do the dance
26     var separators = /[\\/:-]/g;
27     var s = t.value.substr(t.selectionStart, t.selectionEnd);
28     if ( s ) {
29       container.value = s.replace(separators, '');
30       container.previous = t;
31       container.focus();
32       container.select();
33       return true;
34     }
35   }
36   return KeyHandlerDown.call(t, e);
37 };
38 container.onkeyup = function(e) {
39   if ( container.previous ) {
40     setTimeout(function() {
41       container.previous.value = container.value;
42       container.previous.focus();
43     }, 10);
44   }
45   return true;
46 }
47 % } # clipboard hack
48 </&>
49 </script>
50 <textarea id="<%$id%>_clipboard" style="opacity:0"></textarea>
51 <%shared>
52 my $init = 0;
53 </%shared>
54 <%init>
55 my %opt = @_;
56 # must have a DOM id
57 my $id = $opt{id} || sprintf('input%04d',int(rand(10000)));
58 my $value = length($opt{curr_value}) ? $opt{curr_value} : $opt{value} || '';
59
60 my $clipboard_hack = $FS::CurrentUser::CurrentUser->option('enable_mask_clipboard_hack');
61 </%init>
62 <%doc>
63 Set up a text input field with input masking.
64
65 <& /elements/tr-input-mask.html,
66   format    => '____-__-__',
67   #typeon   => '_YMDhms',    # which characters in the format represent blanks
68   #allowed  => '0123456789', # characters allowed in the blanks
69   ... all other options as for tr-input-text.html
70 &>
71
72 Note that the value sent on form submission will contain the mask 
73 separators, and if value/curr_value is passed, it should also be 
74 formatted to fit the mask.
75
76 Uses masked_input_1.1.js by Kendall Conrad, available under a Creative Commons
77 Attribution-ShareAlike license.
78 </%doc>