diff --git a/sites/all/modules/saveguard/saveguard.info b/sites/all/modules/saveguard/saveguard.info index 6720e7c..24595b0 100644 --- a/sites/all/modules/saveguard/saveguard.info +++ b/sites/all/modules/saveguard/saveguard.info @@ -5,7 +5,7 @@ files[] = saveguard.install files[] = saveguard.module ; Information added by drupal.org packaging script on 2012-01-13 -version = "7.x-1.0" +version = "7.x-1.0-formids" core = "7.x" project = "saveguard" datestamp = "1326426643" diff --git a/sites/all/modules/saveguard/saveguard.js b/sites/all/modules/saveguard/saveguard.js index 9307456..dafba54 100644 --- a/sites/all/modules/saveguard/saveguard.js +++ b/sites/all/modules/saveguard/saveguard.js @@ -25,8 +25,11 @@ $(document).ready(function(){ msg = Drupal.settings.saveguard.msg; - $('input, select').change(function() {Drupal.markPageUnsaved(msg)}); // checkboxes, radio buttons and selects - $('input, textarea').keypress(function() {Drupal.markPageUnsaved(msg)}); // textfields and textareas + if (Drupal.settings.saveguard.always) Drupal.markPageUnsaved(msg); // turn on before change. + else { + $('input, select').change(function() {Drupal.markPageUnsaved(msg)}); // checkboxes, radio buttons and selects + $('input, textarea').keypress(function() {Drupal.markPageUnsaved(msg)}); // textfields and textareas + } }); })(jQuery); diff --git a/sites/all/modules/saveguard/saveguard.module b/sites/all/modules/saveguard/saveguard.module index 9779cfd..c0362ff 100644 --- a/sites/all/modules/saveguard/saveguard.module +++ b/sites/all/modules/saveguard/saveguard.module @@ -36,9 +36,17 @@ function saveguard_admin_settings() { ); $form['saveguard_form_ids'] = array( - '#type' => 'textfield', - '#title' => t('Apply to which form IDs? (leave blank for all)'), - '#default_value' => variable_get('saveguard_form_ids', NULL), + '#type' => 'textarea', + '#description' => t('List form Ids on per line. * is a wildcard, so *_node_form would match all node edit forms. * on its own to match ALL forms'), + '#title' => t('Form Ids to Saveguard when change detected.'), + '#default_value' => variable_get('saveguard_form_ids', '*'), + ); + + $form['saveguard_form_ids_always'] = array( + '#type' => 'textarea', + '#description' => t('List form Ids on per line. * is a wildcard, so *_node_form would match all node edit forms. * on its own to match ALL forms'), + '#title' => t('Form Ids to Saveguard even before a change. (workaround: change cannot be detected within rich text editors) '), + '#default_value' => variable_get('saveguard_form_ids_always', NULL), ); return system_settings_form($form); @@ -48,7 +56,7 @@ function saveguard_admin_settings() { /** * Implements hook_form_alter(). * - * Just add javascript to all forms. + * add javascript to forms if form_id specified, or all forms if not. * * @param $form_id * name of the form being acted on @@ -56,13 +64,45 @@ function saveguard_admin_settings() { * array with form structure */ function saveguard_form_alter(&$form, &$form_state, $form_id) { + + static $mode_previous; + + // limit behaviour to certain forms + $modes['change'] = variable_get('saveguard_form_ids', NULL); + $modes['always'] = variable_get('saveguard_form_ids_always', NULL); + + $match = false; + foreach ($modes as $mode=>$patterns) + { + foreach (explode("\n",$patterns) as $pattern) { + // ignore accidental emtpy patterns + if (!$pattern = trim($pattern)) continue; + + // check match, allowing wildcard * + if (preg_match( '/^' .str_replace('*','.*', $pattern) .'$/', $form_id)) { + $match = $mode; + break; // no need to check rest of patterns in this mode + } + } + } + // if no match, we should not alter form + if (!$match) return; + + // return if we're already done + if ($mode_previous == $match ) return; + + // upgrade the protection to always, if a previous form specified that. + if ($mode_previous && $mode_previous=='always') + $match = 'always'; + drupal_add_js(drupal_get_path('module', 'saveguard') . '/saveguard.js'); - static $done; - if (!$done) { - $settings['saveguard'] = array( - 'msg' => variable_get('saveguard_message', NULL), + + $settings['saveguard'] = array( + 'msg' => variable_get('saveguard_message', NULL), + 'always' => ($match == 'always') ); - drupal_add_js($settings, array('type' => 'setting', 'scope' => 'header', 'group' => JS_DEFAULT)); - $done = TRUE; - } + drupal_add_js($settings, array('type' => 'setting', 'scope' => 'header', 'group' => JS_DEFAULT)); + + // remember this mode in case we're called again (page with 2+ forms) + $mode_previous = $match; }