Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <!-- FIXME: CSS conventions and polish --> |
2 | <div class="crm-block crm-form-block crm-queue-runner-form-block"> | |
3 | <div id="crm-queue-runner-progress"></div> | |
4 | <div id="crm-queue-runner-desc"> | |
5 | <div id="crm-queue-runner-buttonset" style="right:20px;position:absolute;"> | |
6 | <button id="crm-queue-runner-retry">Retry</button> | |
7 | <button id="crm-queue-runner-skip">Skip</button> | |
8 | </div> | |
9 | <div>[<span id="crm-queue-runner-title"></span>]</div> | |
10 | </div> | |
11 | <div id="crm-queue-runner-message"></div> | |
12 | </div> | |
13 | ||
14 | {literal} | |
15 | <script type="text/javascript"> | |
16 | ||
3cc60a06 | 17 | CRM.$(function($) { |
6a488035 TO |
18 | // Note: Queue API provides "#remaining tasks" but not "#completed tasks" or "#total tasks". |
19 | // To compute a %complete, we manually track #completed. This only works nicely if we | |
20 | // assume that the queue began with a fixed #tasks. | |
31037a42 | 21 | |
6a488035 TO |
22 | var queueRunnerData = {/literal}{$queueRunnerData|@json}{literal}; |
23 | ||
24 | var displayResponseData = function(data, textStatus, jqXHR) { | |
25 | if (data.redirect_url) { | |
f4af7a1e | 26 | window.location.href = data.redirect_url; |
6a488035 TO |
27 | return; |
28 | } | |
31037a42 | 29 | |
6a488035 | 30 | var pct = 100 * queueRunnerData.completed / (queueRunnerData.completed + queueRunnerData.numberOfItems); |
ae8f569f | 31 | $("#crm-queue-runner-progress").progressbar({ value: pct }); |
31037a42 | 32 | |
6a488035 | 33 | if (data.is_error) { |
ae8f569f | 34 | $("#crm-queue-runner-buttonset").show(); |
6a488035 | 35 | if (queueRunnerData.isEnded) { |
ae8f569f | 36 | $('#crm-queue-runner-skip').button('disable'); |
6a488035 | 37 | } |
ae8f569f | 38 | $('#crm-queue-runner-title').text('Error: ' + data.last_task_title); |
6a488035 | 39 | } else if (!data.is_continue && queueRunnerData.numberOfItems == 0) { |
ae8f569f | 40 | $('#crm-queue-runner-title').text('Done'); |
6a488035 | 41 | } else { |
ae8f569f | 42 | $('#crm-queue-runner-title').text('Executed: ' + data.last_task_title); |
6a488035 | 43 | } |
31037a42 | 44 | |
6a488035 | 45 | if (data.exception) { |
ae8f569f CW |
46 | $('#crm-queue-runner-message').html(''); |
47 | $('<div></div>').html(data.exception).prependTo('#crm-queue-runner-message'); | |
6a488035 | 48 | } |
31037a42 | 49 | |
6a488035 | 50 | }; |
31037a42 | 51 | |
6a488035 TO |
52 | var handleError = function(jqXHR, textStatus, errorThrown) { |
53 | // Do this regardless of whether the response was well-formed | |
ae8f569f | 54 | $("#crm-queue-runner-buttonset").show(); |
31037a42 | 55 | |
ae8f569f | 56 | var data = $.parseJSON(jqXHR.responseText) |
6a488035 TO |
57 | if (data) { |
58 | displayResponseData(data); | |
59 | } | |
60 | }; | |
31037a42 | 61 | |
6a488035 TO |
62 | var handleSuccess = function(data, textStatus, jqXHR) { |
63 | if (!data.is_error) { | |
64 | queueRunnerData.completed++; | |
65 | } | |
66 | if (data.numberOfItems) { | |
67 | queueRunnerData.numberOfItems = parseInt(data.numberOfItems); | |
68 | } | |
31037a42 | 69 | |
6a488035 | 70 | displayResponseData(data); |
31037a42 | 71 | |
6a488035 TO |
72 | // FIXME re-consider merits of is_continue in the corner-case of executing last step |
73 | if (data.is_continue) { | |
74 | window.setTimeout(runNext, 50); | |
75 | } else if (!data.is_continue && queueRunnerData.numberOfItems == 0 && !queueRunnerData.isEnded) { | |
76 | queueRunnerData.isEnded = true; | |
77 | window.setTimeout(runNext, 50); | |
78 | } | |
79 | }; | |
31037a42 | 80 | |
6a488035 TO |
81 | // Dequeue and execute the next item |
82 | var runNext = function() { | |
ae8f569f | 83 | $.ajax({ |
6a488035 TO |
84 | type: 'POST', |
85 | url: (queueRunnerData.isEnded ? queueRunnerData.onEndAjax : queueRunnerData.runNextAjax), | |
86 | data: { | |
87 | qrid: queueRunnerData.qrid | |
88 | }, | |
89 | dataType: 'json', | |
90 | beforeSend: function(jqXHR, settings) { | |
ae8f569f | 91 | $("#crm-queue-runner-buttonset").hide(); |
6a488035 TO |
92 | }, |
93 | error: handleError, | |
94 | success: handleSuccess | |
95 | }); | |
96 | } | |
31037a42 | 97 | |
6a488035 | 98 | var retryNext = function() { |
ae8f569f | 99 | $('#crm-queue-runner-message').html(''); |
6a488035 TO |
100 | runNext(); |
101 | } | |
31037a42 | 102 | |
6a488035 TO |
103 | // Dequeue and the next item, then move on to runNext for the subsequent items |
104 | var skipNext = function() { | |
ae8f569f | 105 | $.ajax({ |
6a488035 TO |
106 | type: 'POST', |
107 | url: queueRunnerData.skipNextAjax, | |
108 | data: { | |
109 | qrid: queueRunnerData.qrid | |
110 | }, | |
111 | dataType: 'json', | |
112 | beforeSend: function(jqXHR, settings) { | |
ae8f569f CW |
113 | $('#crm-queue-runner-message').html(''); |
114 | $("#crm-queue-runner-buttonset").hide(); | |
6a488035 TO |
115 | }, |
116 | error: handleError, | |
117 | success: handleSuccess | |
118 | }); | |
119 | } | |
31037a42 | 120 | |
6a488035 | 121 | // Set up the UI |
31037a42 | 122 | |
ae8f569f | 123 | $("#crm-queue-runner-progress").progressbar({ value: 0 }); |
6a488035 | 124 | if (queueRunnerData.buttons.retry == 1) { |
ae8f569f | 125 | $("#crm-queue-runner-retry").button({ |
6a488035 TO |
126 | text: false, |
127 | icons: {primary: 'ui-icon-refresh'} | |
128 | }).click(retryNext); | |
129 | } else { | |
ae8f569f | 130 | $("#crm-queue-runner-retry").remove(); |
6a488035 TO |
131 | } |
132 | if (queueRunnerData.buttons.skip == 1) { | |
ae8f569f | 133 | $("#crm-queue-runner-skip").button({ |
6a488035 TO |
134 | text: false, |
135 | icons: {primary: 'ui-icon-seek-next'} | |
136 | }).click(skipNext); | |
137 | } else { | |
ae8f569f | 138 | $("#crm-queue-runner-skip").remove(); |
6a488035 | 139 | } |
ae8f569f CW |
140 | $("#crm-queue-runner-buttonset").buttonset(); |
141 | $("#crm-queue-runner-buttonset").hide(); | |
6a488035 TO |
142 | window.setTimeout(runNext, 50); |
143 | }); | |
144 | ||
145 | </script> | |
f4af7a1e | 146 | {/literal} |