|
19 | 19 | More about this license: http://www.question2answer.org/license.php |
20 | 20 | */ |
21 | 21 |
|
22 | | -var qa_recalc_running = 0; |
| 22 | +const qa_recalcProcesses = new Map(); |
23 | 23 |
|
24 | | -window.onbeforeunload = function(event) |
25 | | -{ |
26 | | - if (qa_recalc_running > 0) { |
27 | | - event = event || window.event; |
28 | | - var message = qa_warning_recalc; |
29 | | - event.returnValue = message; |
30 | | - return message; |
| 24 | +window.onbeforeunload = event => { |
| 25 | + for (let [processKey, process] of qa_recalcProcesses.entries()) { |
| 26 | + if (process.clientRunning) { |
| 27 | + event.preventDefault(); |
| 28 | + event.returnValue = true; |
| 29 | + } |
31 | 30 | } |
32 | 31 | }; |
33 | 32 |
|
34 | | -function qa_recalc_click(state, elem, value, noteid) |
| 33 | +/** |
| 34 | + * @param {String} processKey |
| 35 | + * @param {object} options - See keys and default values below |
| 36 | + * @returns {boolean} |
| 37 | + */ |
| 38 | +function qa_recalc_click(processKey, options = {}) |
35 | 39 | { |
36 | | - if (elem.qa_recalc_running) { |
37 | | - elem.qa_recalc_stopped = true; |
38 | | - |
| 40 | + options = { |
| 41 | + forceRestart: false, |
| 42 | + requiresServerTracking: true, |
| 43 | + callbackStart: process => {}, |
| 44 | + callbackStop: hasFinished => {}, |
| 45 | + ...options, |
| 46 | + }; |
| 47 | + |
| 48 | + let process = qa_recalcProcesses.get(processKey) ?? {processKey: processKey}; |
| 49 | + |
| 50 | + const startButton = document.getElementById(processKey); |
| 51 | + const continueButton = document.getElementById(processKey + '_continue'); |
| 52 | + const statusLabel = document.getElementById(processKey + '_status'); |
| 53 | + |
| 54 | + if (process.clientRunning) { |
| 55 | + process.stopRequest = true; |
39 | 56 | } else { |
40 | | - elem.qa_recalc_running = true; |
41 | | - elem.qa_recalc_stopped = false; |
42 | | - qa_recalc_running++; |
| 57 | + process = { |
| 58 | + ...process, |
| 59 | + "startButton": startButton, |
| 60 | + "continueButton": continueButton, |
| 61 | + "statusLabel": statusLabel, |
| 62 | + "clientRunning": true, |
| 63 | + "stopRequest": false, |
| 64 | + "options": options |
| 65 | + }; |
| 66 | + |
| 67 | + qa_recalcProcesses.set(processKey, process); |
43 | 68 |
|
44 | | - document.getElementById(noteid).innerHTML = ''; |
45 | | - elem.qa_original_value = elem.value; |
46 | | - if (value) |
47 | | - elem.value = value; |
| 69 | + qa_conceal(process.continueButton); |
48 | 70 |
|
49 | | - qa_recalc_update(elem, state, noteid); |
| 71 | + statusLabel.innerHTML = qa_langs.please_wait; |
| 72 | + startButton.value = qa_langs.process_stop; |
| 73 | + |
| 74 | + process.options.callbackStart(process); |
| 75 | + |
| 76 | + qa_recalc_update(process); |
50 | 77 | } |
51 | 78 |
|
52 | 79 | return false; |
53 | 80 | } |
54 | 81 |
|
55 | | -function qa_recalc_update(elem, state, noteid) |
| 82 | +function qa_recalc_update(process) |
56 | 83 | { |
57 | | - if (state) { |
58 | | - var recalcCode = elem.form.elements.code_recalc ? elem.form.elements.code_recalc.value : elem.form.elements.code.value; |
59 | | - qa_ajax_post( |
60 | | - 'recalc', |
61 | | - {state: state, code: recalcCode}, |
62 | | - function(lines) { |
63 | | - if (lines[0] == '1') { |
64 | | - if (lines[2]) |
65 | | - document.getElementById(noteid).innerHTML = lines[2]; |
66 | | - |
67 | | - if (elem.qa_recalc_stopped) |
68 | | - qa_recalc_cleanup(elem); |
69 | | - else |
70 | | - qa_recalc_update(elem, lines[1], noteid); |
71 | | - |
72 | | - } else if (lines[0] == '0') { |
73 | | - document.getElementById(noteid).innerHTML = lines[1]; |
74 | | - qa_recalc_cleanup(elem); |
75 | | - |
76 | | - } else { |
| 84 | + const recalcCode = process.startButton.form.elements.code.value; |
| 85 | + |
| 86 | + qa_ajax_post( |
| 87 | + 'recalc', |
| 88 | + { |
| 89 | + process: process.processKey, |
| 90 | + forceRestart: process.options.forceRestart, |
| 91 | + code: recalcCode |
| 92 | + }, |
| 93 | + function (lines) { |
| 94 | + const result = lines[0] ?? null; |
| 95 | + const message = lines[1] ?? null; |
| 96 | + const hasFinished = (lines[2] ?? '0') === '1'; |
| 97 | + |
| 98 | + switch (result) { |
| 99 | + case '1': |
| 100 | + if (message !== null) { |
| 101 | + process.statusLabel.innerHTML = message; |
| 102 | + } |
| 103 | + |
| 104 | + process.serverProcessPending = process.options.requiresServerTracking ? !hasFinished : false; |
| 105 | + if (hasFinished || process.stopRequest) { |
| 106 | + qa_recalc_cleanup(process, hasFinished); |
| 107 | + } else { |
| 108 | + process.options.forceRestart = false; |
| 109 | + qa_recalc_update(process); |
| 110 | + } |
| 111 | + break; |
| 112 | + case '0': |
| 113 | + process.statusLabel.innerHTML = message; |
| 114 | + process.serverProcessPending = true; |
| 115 | + qa_recalc_cleanup(process, false, message); |
| 116 | + break; |
| 117 | + default: |
| 118 | + process.serverProcessPending = true; |
| 119 | + qa_recalc_cleanup(process); |
77 | 120 | qa_ajax_error(); |
78 | | - qa_recalc_cleanup(elem); |
79 | | - } |
80 | 121 | } |
81 | | - ); |
82 | | - } else { |
83 | | - qa_recalc_cleanup(elem); |
84 | | - } |
| 122 | + } |
| 123 | + ); |
85 | 124 | } |
86 | 125 |
|
87 | | -function qa_recalc_cleanup(elem) |
| 126 | +function qa_recalc_cleanup(process, hasFinished = false, message = null) |
88 | 127 | { |
89 | | - elem.value = elem.qa_original_value; |
90 | | - elem.qa_recalc_running = null; |
91 | | - qa_recalc_running--; |
| 128 | + process.clientRunning = false; |
| 129 | + |
| 130 | + process.options.callbackStop(hasFinished); |
| 131 | + |
| 132 | + if (process.options.requiresServerTracking && process.serverProcessPending) { |
| 133 | + process.startButton.value = qa_langs.process_restart; |
| 134 | + process.statusLabel.innerHTML = message ?? qa_langs.process_unfinished; |
| 135 | + qa_reveal(process.continueButton); |
| 136 | + } else { |
| 137 | + process.startButton.value = qa_langs.process_start; |
| 138 | + qa_conceal(process.continueButton); |
| 139 | + } |
92 | 140 | } |
93 | 141 |
|
94 | 142 | function qa_mailing_start(noteid, pauseid) |
|
0 commit comments