Bug 14060: Force the input to contain a valid date
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / ajax.js
1 if ( KOHA === undefined ) var KOHA = {};
2
3 KOHA.AJAX = {
4     Submit: function ( options ) {
5         var error_callback = options.error;
6         $.extend( options, {
7             cache: false,
8             dataType: 'json',
9             type: 'POST',
10             error: function ( xhr, stat, error ) { KOHA.AJAX.BaseError( error_callback, xhr, stat, error ); }
11         } );
12         $.ajax( options );
13     },
14     BaseError: function ( callback, xhr, stat, e ) {
15         KOHA.xhr = xhr;
16         if ( !xhr.getResponseHeader( 'content-type' ).match( 'application/json' ) ) {
17             // Something really failed
18             humanMsg.displayAlert( MSG_INTERNAL_SERVER_ERROR );
19             return;
20         }
21
22         var error = eval( '(' + xhr.responseText + ')' );
23
24         if ( error.type == 'auth' ) {
25             humanMsg.displayMsg( MSG_SESSION_TIMED_OUT );
26         }
27
28         if ( callback ) {
29             callback( error );
30         } else {
31             humanMsg.displayAlert( MSG_DATA_NOT_SAVED );
32         }
33     },
34     MarkRunning: function ( selector, text ) {
35         text = text || _("Loading...");
36         $( selector )
37             .prop('disabled', true)
38             .each( function () {
39                 var $spinner = $( '<span class="loading"></span>' );
40                 var selector_type = this.localName;
41                 if (selector_type === undefined) selector_type = this.nodeName; // IE only
42                 switch ( selector_type.toLowerCase() ) {
43                     case 'input':
44                         $( this ).data( 'original-text', this.value );
45                         this.value = text;
46                         break;
47                     case 'a':
48                         $( this )
49                             .data( 'original-text', $( this ).text )
50                             .text( text )
51                             .before( $spinner )
52                             .bind( 'click.disabled', function () { return false; } );
53                         break;
54                     case 'button':
55                         $( this )
56                             .data( 'original-text', $( this ).text() )
57                             .text( text )
58                             .prepend( $spinner );
59                         break;
60                 }
61             } );
62     },
63     MarkDone: function ( selector ) {
64         $( selector )
65             .prop('disabled', false)
66             .each( function () {
67                 var selector_type = this.localName;
68                 if (selector_type === undefined) selector_type = this.nodeName; // IE only
69                 switch ( selector_type.toLowerCase() ) {
70                     case 'input':
71                         this.value = $( this ).data( 'original-text' );
72                         break;
73                     case 'a':
74                         $( this )
75                             .text( $( this ).data( 'original-text' ) )
76                             .unbind( 'click.disabled' )
77                             .prevAll( 'span.loading' ).remove();
78                         break;
79                     case 'button':
80                         $( this )
81                             .text( $( this ).data( 'original-text' ) )
82                             .find( 'span.loading' ).remove();
83                         break;
84                 }
85             } )
86             .removeData( 'original-text' );
87     }
88 };