source: trunk/web-app/js/taskShow.js @ 850

Last change on this file since 850 was 850, checked in by gav, 13 years ago

Task show ajax improvement, save/restore form and data if no response from server.

File size: 4.1 KB
Line 
1
2// Load data into createContainer and register events.
3function loadCreateContainer(data, createContainer, listContainer, button) {
4        // Load the response data and show container.
5        createContainer.html(data).slideDown(800);
6        // Scroll the window.
7        jQuery('html,body').animate({scrollTop: createContainer.offset().top - 70}, 900, function() {
8            createContainer.find(':input[name="comment"]').focus();
9        });
10        // Hijack form submit to use our function.
11        var eventData = {listContainer:listContainer,createContainer:createContainer, button:button};
12        createContainer.find('form:first').submit(eventData, submitCreateEntryForm);
13        // Register the close img click handler.
14        createContainer.find('.pane_close img').click(function(){
15            createContainer.slideUp(600);
16            button.show(600, function() {
17                if(jQuery.browser.msie) {
18                    jQuery(this).get(0).style.removeAttribute('filter'); // Remove blur/fuzzy text in IE.
19                }
20            });
21        });
22}
23
24// Submit a create Entry form via AJAX.
25function submitCreateEntryForm(event) {
26
27    var actionUrl = getContextPath()+"/entryDetailed/ajaxSave/";
28
29    event.preventDefault();
30    var listContainer = event.data.listContainer;
31    var createContainer = event.data.createContainer;
32    var button = event.data.button;
33    var form = createContainer.find('form:first');
34
35    // On success reload listContainer.
36    function success(data, textStatus, jqXHR){
37        createContainer.hide();
38        listContainer.html(data);
39        button.show(600, function() {
40            if(jQuery.browser.msie) {
41                jQuery(this).get(0).style.removeAttribute('filter'); // Remove blur/fuzzy text in IE.
42            }
43        });
44    }
45
46    // On create failure controller sets 403 and returns the form template.
47    function error(jqXHR, textStatus, errorThrown){
48        if(jqXHR.status == 403 && jqXHR.responseText){
49            loadCreateContainer(jqXHR.responseText, createContainer, listContainer, button);
50        }
51        else {
52            createContainer.html(savedHtml);
53            createContainer.prepend(errorIndication().show()).slideDown(600);
54            // Scroll the window.
55            jQuery('html,body').animate({scrollTop: createContainer.offset().top - 70}, 900, function() {
56                createContainer.find(':input[name="comment"]').focus();
57            });
58        }
59    }
60
61    // Start.
62    var savedHtml = createContainer.children().detach();
63    createContainer.html(loadingIndication().show()).slideDown(600);
64
65    jQuery.ajax({
66        url: actionUrl,
67        data: form.serializeArray(),
68        success: success,
69        error: error
70    });
71}
72
73// Get a create Entry form via AJAX.
74// @listContainer Container object to reload list into.
75// @createContainer Container object to load response into.
76// @button Button object used to trigger this function.
77// @params Params map to pass to actionUrl.
78function getCreateEntryForm(listContainer, createContainer, button, params) {
79
80    var actionUrl = getContextPath()+"/entryDetailed/ajaxCreate/";
81
82    // On success load createContainer.
83    function success(data, textStatus, jqXHR){
84        loadCreateContainer(data, createContainer, listContainer, button);
85    }
86
87    // On error show controller responseText or show default error.
88    function error(jqXHR, textStatus, errorThrown){
89        if(jqXHR.status == 403 && jqXHR.responseText){
90            loadCreateContainer(jqXHR.responseText, createContainer, listContainer, button);
91        }
92        else {
93            createContainer.html(errorIndication().show()).slideDown(600);
94        }
95        button.show(600, function() {
96            if(jQuery.browser.msie) {
97                jQuery(this).get(0).style.removeAttribute('filter'); // Remove blur/fuzzy text in IE.
98            }
99        });
100    }
101
102    // Start.
103    button.hide(600);
104    createContainer.html(loadingIndication().show()).slideDown(600);
105
106    jQuery.ajax({
107        url: actionUrl,
108        data: params,
109        success: success,
110        error: error
111    });
112}
113
Note: See TracBrowser for help on using the repository browser.