Revision | dcc6b19cb04618da87a41965a10db2f48b37c25e (tree) |
---|---|
Zeit | 2017-11-06 08:15:18 |
Autor | umorigu <umorigu@gmai...> |
Commiter | umorigu |
BugTrack/2453 Show alert message for leaving page action on editing
@@ -59,6 +59,9 @@ $_msg_unsupported_webbrowser = 'This function doesn\'t support your current Web | ||
59 | 59 | $_msg_use_alternative_link = 'Please go to the following link destination: $1'; |
60 | 60 | $_msg_general_error = 'An error occurred while processing.'; |
61 | 61 | |
62 | +$_msg_edit_cancel_confirm = 'The text you have entered will be discarded. Is it OK?'; | |
63 | +$_msg_edit_unloadbefore_message = 'Data you have entered will not be saved.'; | |
64 | + | |
62 | 65 | /////////////////////////////////////// |
63 | 66 | // Symbols |
64 | 67 | $_symbol_anchor = '†'; |
@@ -61,6 +61,9 @@ $_msg_unsupported_webbrowser = 'この機能はお使いのWebブラウザには | ||
61 | 61 | $_msg_use_alternative_link = 'リンク先の機能をご利用ください: $1'; |
62 | 62 | $_msg_general_error = '処理中にエラーが発生しました。'; |
63 | 63 | |
64 | +$_msg_edit_cancel_confirm = '編集中のテキストは破棄されます。よろしいですか ?'; | |
65 | +$_msg_edit_unloadbefore_message = '入力したデータは保存されません。'; | |
66 | + | |
64 | 67 | /////////////////////////////////////// |
65 | 68 | // Symbols |
66 | 69 | $_symbol_anchor = '†'; |
@@ -218,7 +218,7 @@ function _decorate_Nth_word($matches) | ||
218 | 218 | */ |
219 | 219 | function get_html_scripting_data() |
220 | 220 | { |
221 | - global $ticket_link_sites; | |
221 | + global $ticket_link_sites, $plugin; | |
222 | 222 | if (!isset($ticket_link_sites) || !is_array($ticket_link_sites)) { |
223 | 223 | return ''; |
224 | 224 | } |
@@ -243,6 +243,11 @@ EOS; | ||
243 | 243 | $site_props = <<<EOS |
244 | 244 | <div data-key="site-props" data-value="$props_json"></div> |
245 | 245 | EOS; |
246 | + $h_plugin = htmlsc($plugin); | |
247 | + $plugin_prop = <<<EOS | |
248 | +<input type="hidden" class="plugin-name" value="$h_plugin" /> | |
249 | +EOS; | |
250 | + | |
246 | 251 | // AutoTicketLink |
247 | 252 | $filtered_ticket_link_sites = array(); |
248 | 253 | foreach ($ticket_link_sites as $s) { |
@@ -259,6 +264,7 @@ EOS; | ||
259 | 264 | $data = <<<EOS |
260 | 265 | <div id="pukiwiki-site-properties" style="display:none;"> |
261 | 266 | $site_props |
267 | +$plugin_prop | |
262 | 268 | $ticketlink_data |
263 | 269 | </div> |
264 | 270 | EOS; |
@@ -273,6 +279,7 @@ function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE) | ||
273 | 279 | global $whatsnew, $_btn_template, $_btn_load, $load_template_func; |
274 | 280 | global $notimeupdate; |
275 | 281 | global $_title_list, $_label_template_pages; |
282 | + global $_msg_edit_cancel_confirm, $_msg_edit_unloadbefore_message; | |
276 | 283 | global $rule_page; |
277 | 284 | |
278 | 285 | $script = get_base_uri(); |
@@ -381,14 +388,18 @@ EOD; | ||
381 | 388 | |
382 | 389 | // 'margin-bottom', 'float:left', and 'margin-top' |
383 | 390 | // are for layout of 'cancel button' |
391 | + $h_msg_edit_cancel_confirm = htmlsc($_msg_edit_cancel_confirm); | |
392 | + $h_msg_edit_unloadbefore_message = htmlsc($_msg_edit_unloadbefore_message); | |
384 | 393 | $body = <<<EOD |
385 | 394 | <div class="edit_form"> |
386 | - <form action="$script" method="post" style="margin-bottom:0px;"> | |
395 | + <form action="$script" method="post" class="_plugin_edit_edit_form" style="margin-bottom:0px;"> | |
387 | 396 | $template |
388 | 397 | $addtag |
389 | 398 | <input type="hidden" name="cmd" value="edit" /> |
390 | 399 | <input type="hidden" name="page" value="$s_page" /> |
391 | 400 | <input type="hidden" name="digest" value="$s_digest" /> |
401 | + <input type="hidden" id="_msg_edit_cancel_confirm" value="$h_msg_edit_cancel_confirm" /> | |
402 | + <input type="hidden" id="_msg_edit_unloadbefore_message" value="$h_msg_edit_unloadbefore_message" /> | |
392 | 403 | <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea> |
393 | 404 | <br /> |
394 | 405 | <div style="float:left;"> |
@@ -399,7 +410,7 @@ $template | ||
399 | 410 | </div> |
400 | 411 | <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea> |
401 | 412 | </form> |
402 | - <form action="$script" method="post" style="margin-top:0px;"> | |
413 | + <form action="$script" method="post" class="_plugin_edit_cancel" style="margin-top:0px;"> | |
403 | 414 | <input type="hidden" name="cmd" value="edit" /> |
404 | 415 | <input type="hidden" name="page" value="$s_page" /> |
405 | 416 | <input type="submit" name="cancel" value="$_btn_cancel" accesskey="c" /> |
@@ -220,6 +220,63 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function( | ||
220 | 220 | var target = document.getElementById('body'); |
221 | 221 | walkElement(target); |
222 | 222 | } |
223 | + function confirmEditFormLeaving() { | |
224 | + function trim(s) { | |
225 | + if (typeof s !== 'string') { | |
226 | + return s; | |
227 | + } | |
228 | + return s.replace(/^\s+|\s+$/g, ''); | |
229 | + } | |
230 | + if (!document.querySelector) return; | |
231 | + var canceled = false; | |
232 | + var pluginNameE = document.querySelector('#pukiwiki-site-properties .plugin-name'); | |
233 | + if (!pluginNameE) return; | |
234 | + var originalText = null; | |
235 | + if (pluginNameE.value !== 'edit') return; | |
236 | + var editForm = document.querySelector('.edit_form form._plugin_edit_edit_form'); | |
237 | + if (!editForm) return; | |
238 | + var cancelMsgE = editForm.querySelector('#_msg_edit_cancel_confirm'); | |
239 | + var unloadBeforeMsgE = editForm.querySelector('#_msg_edit_unloadbefore_message'); | |
240 | + var textArea = editForm.querySelector('textarea[name="msg"]'); | |
241 | + if (!textArea) return; | |
242 | + originalText = textArea.value; | |
243 | + var cancelForm = document.querySelector('.edit_form form._plugin_edit_cancel'); | |
244 | + var submited = false; | |
245 | + editForm.addEventListener('submit', function() { | |
246 | + canceled = false; | |
247 | + submited = true; | |
248 | + }); | |
249 | + cancelForm.addEventListener('submit', function(e) { | |
250 | + submited = false; | |
251 | + canceled = false; | |
252 | + if (trim(textArea.value) === trim(originalText)) { | |
253 | + canceled = true; | |
254 | + return false; | |
255 | + } | |
256 | + var message = 'The text you have entered will be discarded. Is it OK?'; | |
257 | + if (cancelMsgE && cancelMsgE.value) { | |
258 | + message = cancelMsgE.value; | |
259 | + } | |
260 | + if (window.confirm(message)) { // eslint-disable-line no-alert | |
261 | + // Execute "Cancel" | |
262 | + canceled = true; | |
263 | + return true; | |
264 | + } | |
265 | + e.preventDefault(); | |
266 | + return false; | |
267 | + }); | |
268 | + window.addEventListener('beforeunload', function(e) { | |
269 | + if (canceled) return; | |
270 | + if (submited) return; | |
271 | + if (trim(textArea.value) === trim(originalText)) return; | |
272 | + var message = 'Data you have entered will not be saved.'; | |
273 | + if (unloadBeforeMsgE && unloadBeforeMsgE.value) { | |
274 | + message = unloadBeforeMsgE.value; | |
275 | + } | |
276 | + e.returnValue = message; | |
277 | + }, false); | |
278 | + } | |
223 | 279 | setYourName(); |
224 | 280 | autoTicketLink(); |
281 | + confirmEditFormLeaving(); | |
225 | 282 | }); |