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

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

Rework entry create and save ajax to allow multiple page element updates with JSON.

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