1 | $(document).ready(function() { |
||
2 | function fail(element, title, message) { |
||
3 | element.addClass('ajax_save_failed'); |
||
4 | $.midcom_services_uimessage_add({ |
||
5 | type: 'error', |
||
6 | title: title, |
||
7 | message: message |
||
8 | }); |
||
9 | return false; |
||
10 | } |
||
11 | |||
12 | $('.ajax_editable') |
||
13 | .on('focus', function() { |
||
14 | $(this) |
||
15 | .data('orig-value', $(this).val()) |
||
16 | .addClass('ajax_focused'); |
||
17 | }) |
||
18 | .on('blur', function() { |
||
19 | $(this).removeClass('ajax_focused'); |
||
20 | var ajaxUrl = this.dataset.ajaxUrl, |
||
21 | element = $(this); |
||
22 | if (!ajaxUrl) { |
||
23 | throw 'Handler URL for cannot be found'; |
||
24 | } |
||
25 | |||
26 | if (element.val() !== element.data('orig-value')) { |
||
27 | element.addClass('ajax_saving'); |
||
28 | $.post(ajaxUrl, {guid: this.dataset.guid, title: element.val()}, function(data) { |
||
29 | element.removeClass('ajax_saving'); |
||
30 | if (data.status !== true) { |
||
31 | return fail(element, 'Saving failed', data.message); |
||
32 | } |
||
33 | element.addClass('ajax_saved'); |
||
34 | setTimeout(function() { |
||
35 | element.removeClass('ajax_saved'); |
||
36 | element.addClass('ajax_saved_fade'); |
||
37 | }, 1000); |
||
38 | setTimeout(function() { |
||
39 | element.removeClass('ajax_saved_fade'); |
||
40 | }, 2000); |
||
0 ignored issues
–
show
Best Practice
introduced
by
![]() |
|||
41 | }) |
||
42 | .fail(function() { |
||
43 | fail(element, 'Request failed', 'Request failed'); |
||
44 | }); |
||
45 | } |
||
46 | |||
47 | //If empty return to the default value (if one is provided) |
||
48 | if ( $(this).val() == '' |
||
49 | && this.dataset.ajaxDefault) { |
||
50 | $(this).val(this.dataset.ajaxDefault); |
||
51 | } |
||
52 | }); |
||
53 | }); |
||
54 |