Commit MetaInfo

修订版1e7b1365d179c6821450e9d15d4349ef69020476 (tree)
时间2017-11-06 08:15:18
作者umorigu <umorigu@gmai...>
Commiterumorigu

Log Message

BugTrack/2453 Show alert message for leaving page action on editing

更改概述

差异

--- a/en.lng.php
+++ b/en.lng.php
@@ -59,6 +59,9 @@ $_msg_unsupported_webbrowser = 'This function doesn\'t support your current Web
5959 $_msg_use_alternative_link = 'Please go to the following link destination: $1';
6060 $_msg_general_error = 'An error occurred while processing.';
6161
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+
6265 ///////////////////////////////////////
6366 // Symbols
6467 $_symbol_anchor = '&dagger;';
--- a/ja.lng.php
+++ b/ja.lng.php
@@ -61,6 +61,9 @@ $_msg_unsupported_webbrowser = 'この機能はお使いのWebブラウザには
6161 $_msg_use_alternative_link = 'リンク先の機能をご利用ください: $1';
6262 $_msg_general_error = '処理中にエラーが発生しました。';
6363
64+$_msg_edit_cancel_confirm = '編集中のテキストは破棄されます。よろしいですか ?';
65+$_msg_edit_unloadbefore_message = '入力したデータは保存されません。';
66+
6467 ///////////////////////////////////////
6568 // Symbols
6669 $_symbol_anchor = '&dagger;';
--- a/lib/html.php
+++ b/lib/html.php
@@ -216,7 +216,7 @@ function _decorate_Nth_word($matches)
216216 */
217217 function get_html_scripting_data()
218218 {
219- global $ticket_link_sites;
219+ global $ticket_link_sites, $plugin;
220220 if (!isset($ticket_link_sites) || !is_array($ticket_link_sites)) {
221221 return '';
222222 }
@@ -241,6 +241,11 @@ EOS;
241241 $site_props = <<<EOS
242242 <div data-key="site-props" data-value="$props_json"></div>
243243 EOS;
244+ $h_plugin = htmlsc($plugin);
245+ $plugin_prop = <<<EOS
246+<input type="hidden" class="plugin-name" value="$h_plugin" />
247+EOS;
248+
244249 // AutoTicketLink
245250 $filtered_ticket_link_sites = array();
246251 foreach ($ticket_link_sites as $s) {
@@ -257,6 +262,7 @@ EOS;
257262 $data = <<<EOS
258263 <div id="pukiwiki-site-properties" style="display:none;">
259264 $site_props
265+$plugin_prop
260266 $ticketlink_data
261267 </div>
262268 EOS;
@@ -271,6 +277,7 @@ function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
271277 global $whatsnew, $_btn_template, $_btn_load, $load_template_func;
272278 global $notimeupdate;
273279 global $_title_list, $_label_template_pages;
280+ global $_msg_edit_cancel_confirm, $_msg_edit_unloadbefore_message;
274281 global $rule_page;
275282
276283 $script = get_base_uri();
@@ -379,14 +386,18 @@ EOD;
379386
380387 // 'margin-bottom', 'float:left', and 'margin-top'
381388 // 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);
382391 $body = <<<EOD
383392 <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;">
385394 $template
386395 $addtag
387396 <input type="hidden" name="cmd" value="edit" />
388397 <input type="hidden" name="page" value="$s_page" />
389398 <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" />
390401 <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
391402 <br />
392403 <div style="float:left;">
@@ -397,7 +408,7 @@ $template
397408 </div>
398409 <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
399410 </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;">
401412 <input type="hidden" name="cmd" value="edit" />
402413 <input type="hidden" name="page" value="$s_page" />
403414 <input type="submit" name="cancel" value="$_btn_cancel" accesskey="c" />
--- a/skin/main.js
+++ b/skin/main.js
@@ -220,6 +220,62 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function(
220220 var target = document.getElementById('body');
221221 walkElement(target);
222222 }
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+ }
223278 setYourName();
224279 autoTicketLink();
280+ confirmEditFormLeaving();
225281 });
Show on old repository browser