1 | |
---|
2 | // Load data into createContainer and register events. |
---|
3 | function 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}, 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); |
---|
17 | }); |
---|
18 | } |
---|
19 | |
---|
20 | // Submit a create Entry form via AJAX. |
---|
21 | function submitCreateEntryForm(event) { |
---|
22 | |
---|
23 | var actionUrl = getContextPath()+"/entryDetailed/ajaxSave/"; |
---|
24 | |
---|
25 | event.preventDefault(); |
---|
26 | var listContainer = event.data.listContainer; |
---|
27 | var createContainer = event.data.createContainer; |
---|
28 | var button = event.data.button; |
---|
29 | var form = createContainer.find('form:first'); |
---|
30 | |
---|
31 | // On success reload listContainer. |
---|
32 | function success(data, textStatus, jqXHR){ |
---|
33 | createContainer.hide(); |
---|
34 | listContainer.html(data); |
---|
35 | button.show(600); |
---|
36 | } |
---|
37 | |
---|
38 | // On create failure controller sets 403 and returns the form template. |
---|
39 | function error(jqXHR, textStatus, errorThrown){ |
---|
40 | if(jqXHR.status == 403 && jqXHR.responseText){ |
---|
41 | loadCreateContainer(jqXHR.responseText, createContainer, listContainer, button); |
---|
42 | } |
---|
43 | else { |
---|
44 | createContainer.html(errorIndication().show()).slideDown(600); |
---|
45 | } |
---|
46 | button.show(600); |
---|
47 | } |
---|
48 | |
---|
49 | // Start. |
---|
50 | createContainer.html(loadingIndication().show()).slideDown(600); |
---|
51 | |
---|
52 | jQuery.ajax({ |
---|
53 | url: actionUrl, |
---|
54 | data: form.serializeArray(), |
---|
55 | success: success, |
---|
56 | error: error |
---|
57 | }); |
---|
58 | } |
---|
59 | |
---|
60 | // Get a create Entry form via AJAX. |
---|
61 | // @listContainer Container object to reload list into. |
---|
62 | // @createContainer Container object to load response into. |
---|
63 | // @button Button object used to trigger this function. |
---|
64 | // @params Params map to pass to actionUrl. |
---|
65 | function getCreateEntryForm(listContainer, createContainer, button, params) { |
---|
66 | |
---|
67 | var actionUrl = getContextPath()+"/entryDetailed/ajaxCreate/"; |
---|
68 | |
---|
69 | // On success load createContainer. |
---|
70 | function success(data, textStatus, jqXHR){ |
---|
71 | loadCreateContainer(data, createContainer, listContainer, button); |
---|
72 | } |
---|
73 | |
---|
74 | // On error show controller responseText or show default error. |
---|
75 | function error(jqXHR, textStatus, errorThrown){ |
---|
76 | if(jqXHR.status == 403 && jqXHR.responseText){ |
---|
77 | loadCreateContainer(jqXHR.responseText, createContainer, listContainer, button); |
---|
78 | } |
---|
79 | else { |
---|
80 | createContainer.html(errorIndication().show()).slideDown(600); |
---|
81 | } |
---|
82 | button.show(600); |
---|
83 | } |
---|
84 | |
---|
85 | // Start. |
---|
86 | button.hide(600); |
---|
87 | createContainer.html(loadingIndication().show()).slideDown(600); |
---|
88 | |
---|
89 | jQuery.ajax({ |
---|
90 | url: actionUrl, |
---|
91 | data: params, |
---|
92 | success: success, |
---|
93 | error: error |
---|
94 | }); |
---|
95 | } |
---|
96 | |
---|