リビジョン | 1e7b1365d179c6821450e9d15d4349ef69020476 (tree) |
---|---|
日時 | 2017-11-06 08:15:18 |
作者 | umorigu <umorigu@gmai...> |
コミッター | 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 = '†'; |
@@ -216,7 +216,7 @@ function _decorate_Nth_word($matches) | ||
216 | 216 | */ |
217 | 217 | function get_html_scripting_data() |
218 | 218 | { |
219 | - global $ticket_link_sites; | |
219 | + global $ticket_link_sites, $plugin; | |
220 | 220 | if (!isset($ticket_link_sites) || !is_array($ticket_link_sites)) { |
221 | 221 | return ''; |
222 | 222 | } |
@@ -241,6 +241,11 @@ EOS; | ||
241 | 241 | $site_props = <<<EOS |
242 | 242 | <div data-key="site-props" data-value="$props_json"></div> |
243 | 243 | EOS; |
244 | + $h_plugin = htmlsc($plugin); | |
245 | + $plugin_prop = <<<EOS | |
246 | +<input type="hidden" class="plugin-name" value="$h_plugin" /> | |
247 | +EOS; | |
248 | + | |
244 | 249 | // AutoTicketLink |
245 | 250 | $filtered_ticket_link_sites = array(); |
246 | 251 | foreach ($ticket_link_sites as $s) { |
@@ -257,6 +262,7 @@ EOS; | ||
257 | 262 | $data = <<<EOS |
258 | 263 | <div id="pukiwiki-site-properties" style="display:none;"> |
259 | 264 | $site_props |
265 | +$plugin_prop | |
260 | 266 | $ticketlink_data |
261 | 267 | </div> |
262 | 268 | EOS; |
@@ -271,6 +277,7 @@ function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE) | ||
271 | 277 | global $whatsnew, $_btn_template, $_btn_load, $load_template_func; |
272 | 278 | global $notimeupdate; |
273 | 279 | global $_title_list, $_label_template_pages; |
280 | + global $_msg_edit_cancel_confirm, $_msg_edit_unloadbefore_message; | |
274 | 281 | global $rule_page; |
275 | 282 | |
276 | 283 | $script = get_base_uri(); |
@@ -379,14 +386,18 @@ EOD; | ||
379 | 386 | |
380 | 387 | // 'margin-bottom', 'float:left', and 'margin-top' |
381 | 388 | // are for layout of 'cancel button' |
389 | + $h_msg_edit_cancel_confirm = htmlsc($_msg_edit_cancel_confirm); | |
390 | + $h_msg_edit_unloadbefore_message = htmlsc($_msg_edit_unloadbefore_message); | |
382 | 391 | $body = <<<EOD |
383 | 392 | <div class="edit_form"> |
384 | - <form action="$script" method="post" style="margin-bottom:0px;"> | |
393 | + <form action="$script" method="post" class="_plugin_edit_edit_form" style="margin-bottom:0px;"> | |
385 | 394 | $template |
386 | 395 | $addtag |
387 | 396 | <input type="hidden" name="cmd" value="edit" /> |
388 | 397 | <input type="hidden" name="page" value="$s_page" /> |
389 | 398 | <input type="hidden" name="digest" value="$s_digest" /> |
399 | + <input type="hidden" id="_msg_edit_cancel_confirm" value="$h_msg_edit_cancel_confirm" /> | |
400 | + <input type="hidden" id="_msg_edit_unloadbefore_message" value="$h_msg_edit_unloadbefore_message" /> | |
390 | 401 | <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea> |
391 | 402 | <br /> |
392 | 403 | <div style="float:left;"> |
@@ -397,7 +408,7 @@ $template | ||
397 | 408 | </div> |
398 | 409 | <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea> |
399 | 410 | </form> |
400 | - <form action="$script" method="post" style="margin-top:0px;"> | |
411 | + <form action="$script" method="post" class="_plugin_edit_cancel" style="margin-top:0px;"> | |
401 | 412 | <input type="hidden" name="cmd" value="edit" /> |
402 | 413 | <input type="hidden" name="page" value="$s_page" /> |
403 | 414 | <input type="submit" name="cancel" value="$_btn_cancel" accesskey="c" /> |
@@ -220,6 +220,62 @@ 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() { | |
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 | + return false; | |
266 | + }); | |
267 | + window.addEventListener('beforeunload', function(e) { | |
268 | + if (canceled) return; | |
269 | + if (submited) return; | |
270 | + if (trim(textArea.value) === trim(originalText)) return; | |
271 | + var message = 'Data you have entered will not be saved.'; | |
272 | + if (unloadBeforeMsgE && unloadBeforeMsgE.value) { | |
273 | + message = unloadBeforeMsgE.value; | |
274 | + } | |
275 | + e.returnValue = message; | |
276 | + }, false); | |
277 | + } | |
223 | 278 | setYourName(); |
224 | 279 | autoTicketLink(); |
280 | + confirmEditFormLeaving(); | |
225 | 281 | }); |