OSDN Git Service

BugTrack/2453 Show alert message for leaving page action on editing bugtrack_2453_alert_on_leaving_page2
authorumorigu <umorigu@gmail.com>
Sun, 5 Nov 2017 23:15:18 +0000 (08:15 +0900)
committerumorigu <umorigu@gmail.com>
Sun, 5 Nov 2017 23:15:18 +0000 (08:15 +0900)
en.lng.php
ja.lng.php
lib/html.php
skin/main.js

index 35ed906..3d6dfb4 100644 (file)
@@ -59,6 +59,9 @@ $_msg_unsupported_webbrowser = 'This function doesn\'t support your current Web
 $_msg_use_alternative_link = 'Please go to the following link destination: $1';
 $_msg_general_error  = 'An error occurred while processing.';
 
+$_msg_edit_cancel_confirm = 'The text you have entered will be discarded. Is it OK?';
+$_msg_edit_unloadbefore_message = 'Data you have entered will not be saved.';
+
 ///////////////////////////////////////
 // Symbols
 $_symbol_anchor   = '&dagger;';
index 4f587af..e0fb800 100644 (file)
@@ -61,6 +61,9 @@ $_msg_unsupported_webbrowser = 'この機能はお使いのWebブラウザには
 $_msg_use_alternative_link = 'リンク先の機能をご利用ください: $1';
 $_msg_general_error  = '処理中にエラーが発生しました。';
 
+$_msg_edit_cancel_confirm = '編集中のテキストは破棄されます。よろしいですか ?';
+$_msg_edit_unloadbefore_message = '入力したデータは保存されません。';
+
 ///////////////////////////////////////
 // Symbols
 $_symbol_anchor   = '&dagger;';
index d890464..41d53e9 100644 (file)
@@ -218,7 +218,7 @@ function _decorate_Nth_word($matches)
  */
 function get_html_scripting_data()
 {
-       global $ticket_link_sites;
+       global $ticket_link_sites, $plugin;
        if (!isset($ticket_link_sites) || !is_array($ticket_link_sites)) {
                return '';
        }
@@ -243,6 +243,11 @@ EOS;
        $site_props = <<<EOS
 <div data-key="site-props" data-value="$props_json"></div>
 EOS;
+       $h_plugin = htmlsc($plugin);
+       $plugin_prop = <<<EOS
+<input type="hidden" class="plugin-name" value="$h_plugin" />
+EOS;
+
        // AutoTicketLink
        $filtered_ticket_link_sites = array();
        foreach ($ticket_link_sites as $s) {
@@ -259,6 +264,7 @@ EOS;
        $data = <<<EOS
 <div id="pukiwiki-site-properties" style="display:none;">
 $site_props
+$plugin_prop
 $ticketlink_data
 </div>
 EOS;
@@ -273,6 +279,7 @@ function edit_form($page, $postdata, $digest = FALSE, $b_template = TRUE)
        global $whatsnew, $_btn_template, $_btn_load, $load_template_func;
        global $notimeupdate;
        global $_title_list, $_label_template_pages;
+       global $_msg_edit_cancel_confirm, $_msg_edit_unloadbefore_message;
        global $rule_page;
 
        $script = get_base_uri();
@@ -381,14 +388,18 @@ EOD;
 
        // 'margin-bottom', 'float:left', and 'margin-top'
        // are for layout of 'cancel button'
+       $h_msg_edit_cancel_confirm = htmlsc($_msg_edit_cancel_confirm);
+       $h_msg_edit_unloadbefore_message = htmlsc($_msg_edit_unloadbefore_message);
        $body = <<<EOD
 <div class="edit_form">
- <form action="$script" method="post" style="margin-bottom:0px;">
+ <form action="$script" method="post" class="_plugin_edit_edit_form" style="margin-bottom:0px;">
 $template
   $addtag
   <input type="hidden" name="cmd"    value="edit" />
   <input type="hidden" name="page"   value="$s_page" />
   <input type="hidden" name="digest" value="$s_digest" />
+  <input type="hidden" id="_msg_edit_cancel_confirm" value="$h_msg_edit_cancel_confirm" />
+  <input type="hidden" id="_msg_edit_unloadbefore_message" value="$h_msg_edit_unloadbefore_message" />
   <textarea name="msg" rows="$rows" cols="$cols">$s_postdata</textarea>
   <br />
   <div style="float:left;">
@@ -399,7 +410,7 @@ $template
   </div>
   <textarea name="original" rows="1" cols="1" style="display:none">$s_original</textarea>
  </form>
- <form action="$script" method="post" style="margin-top:0px;">
+ <form action="$script" method="post" class="_plugin_edit_cancel" style="margin-top:0px;">
   <input type="hidden" name="cmd"    value="edit" />
   <input type="hidden" name="page"   value="$s_page" />
   <input type="submit" name="cancel" value="$_btn_cancel" accesskey="c" />
index ef722a8..09d4c61 100644 (file)
@@ -220,6 +220,62 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function(
     var target = document.getElementById('body');
     walkElement(target);
   }
+  function confirmEditFormLeaving() {
+    function trim(s) {
+      if (typeof s !== 'string') {
+        return s;
+      }
+      return s.replace(/^\s+|\s+$/g, '');
+    }
+    if (!document.querySelector) return;
+    var canceled = false;
+    var pluginNameE = document.querySelector('#pukiwiki-site-properties .plugin-name');
+    if (!pluginNameE) return;
+    var originalText = null;
+    if (pluginNameE.value !== 'edit') return;
+    var editForm = document.querySelector('.edit_form form._plugin_edit_edit_form');
+    if (!editForm) return;
+    var cancelMsgE = editForm.querySelector('#_msg_edit_cancel_confirm');
+    var unloadBeforeMsgE = editForm.querySelector('#_msg_edit_unloadbefore_message');
+    var textArea = editForm.querySelector('textarea[name="msg"]');
+    if (!textArea) return;
+    originalText = textArea.value;
+    var cancelForm = document.querySelector('.edit_form form._plugin_edit_cancel');
+    var submited = false;
+    editForm.addEventListener('submit', function() {
+      canceled = false;
+      submited = true;
+    });
+    cancelForm.addEventListener('submit', function() {
+      submited = false;
+      canceled = false;
+      if (trim(textArea.value) === trim(originalText)) {
+        canceled = true;
+        return false;
+      }
+      var message = 'The text you have entered will be discarded. Is it OK?';
+      if (cancelMsgE && cancelMsgE.value) {
+        message = cancelMsgE.value;
+      }
+      if (window.confirm(message)) { // eslint-disable-line no-alert
+        // Execute "Cancel"
+        canceled = true;
+        return true;
+      }
+      return false;
+    });
+    window.addEventListener('beforeunload', function(e) {
+      if (canceled) return;
+      if (submited) return;
+      if (trim(textArea.value) === trim(originalText)) return;
+      var message = 'Data you have entered will not be saved.';
+      if (unloadBeforeMsgE && unloadBeforeMsgE.value) {
+        message = unloadBeforeMsgE.value;
+      }
+      e.returnValue = message;
+    }, false);
+  }
   setYourName();
   autoTicketLink();
+  confirmEditFormLeaving();
 });