WordReplacement form improvement - auto-check new items and warn unsaved changes
authorColeman Watts <coleman@civicrm.org>
Wed, 21 Jan 2015 02:08:16 +0000 (21:08 -0500)
committerColeman Watts <coleman@civicrm.org>
Wed, 21 Jan 2015 15:01:24 +0000 (10:01 -0500)
CRM/Admin/Form/WordReplacements.php
templates/CRM/Admin/Form/WordReplacements.tpl

index 009060b42ee8446dcd93f400fbd44ca1f892c2bc..d33f3e846a6665f465d858c839707479387efa34 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
- */
+*/
 
 /**
  *
@@ -39,6 +39,8 @@ class CRM_Admin_Form_WordReplacements extends CRM_Core_Form {
 
   protected $_defaults = NULL;
 
+  public $unsavedChangesWarn = TRUE;
+
   public function preProcess() {
     // This controller was originally written to CRUD $config->locale_custom_strings,
     // but that's no longer the canonical store. Re-sync from canonical store to ensure
@@ -48,16 +50,6 @@ class CRM_Admin_Form_WordReplacements extends CRM_Core_Form {
 
     $this->_soInstance = CRM_Utils_Array::value('instance', $_GET);
     $this->assign('soInstance', $this->_soInstance);
-    $breadCrumbUrl = CRM_Utils_System::url('civicrm/admin/options/wordreplacements',
-      "reset=1"
-    );
-    $breadCrumb = array(
-      array(
-        'title' => ts('Word Replacements'),
-        'url' => $breadCrumbUrl,
-      ),
-    );
-    CRM_Utils_System::appendBreadCrumb($breadCrumb);
   }
 
   /**
@@ -281,5 +273,4 @@ class CRM_Admin_Form_WordReplacements extends CRM_Core_Form {
       ));
     }
   }
-
 }
index 214f3a5c785853ca76cf8cb3f3e1683b982ba374..85f841715e1f647ac2599bb3dc3da9854d3746aa 100644 (file)
@@ -26,7 +26,8 @@
 
 {* template for a single row *}
 {if $soInstance}
-  <tr class="string-override-row row-{$soInstance} {if $soInstance % 2}odd{else}even{/if}-row">
+  <tr class="string-override-row {if $soInstance % 2}odd{else}even{/if}-row" data-row="{$soInstance}"
+      xmlns="http://www.w3.org/1999/html">
     <td>{$form.enabled.$soInstance.html}</td>
     <td>{$form.old.$soInstance.html}</td>
     <td>{$form.new.$soInstance.html}</td>
     <table class="form-layout-compressed">
       <tr>
         <td>
-          <table>
-            <tr class="columnheader">
-              <td>{ts}Enabled{/ts}</td>
-              <td>{ts}Original{/ts}</td>
-              <td>{ts}Replacement{/ts}</td>
-              <td>{ts}Exact Match{/ts}</td>
-            </tr>
-
-            {section name="numStrings" start=1 step=1 loop=$numStrings+1}
-              {include file="CRM/Admin/Form/WordReplacements.tpl" soInstance=$smarty.section.numStrings.index}
-            {/section}
+          <table class="string-override-table row-highlight">
+            <thead>
+              <tr class="columnheader">
+                <th>{ts}Enabled{/ts}</th>
+                <th>{ts}Original{/ts}</th>
+                <th>{ts}Replacement{/ts}</th>
+                <th>{ts}Exact Match{/ts}</th>
+              </tr>
+            </thead>
+            <tbody>
+              {section name="numStrings" start=1 step=1 loop=$numStrings+1}
+                {include file="CRM/Admin/Form/WordReplacements.tpl" soInstance=$smarty.section.numStrings.index}
+              {/section}
+            </tbody>
           </table>
           &nbsp;&nbsp;&nbsp;<a class="action-item crm-hover-button buildStringOverrideRow" href="#"><span class="icon ui-icon-circle-plus"></span> {ts}Add row{/ts}</a>
         </td>
     CRM.$(function($) {
       {/literal}
       {if $stringOverrideInstances}
-      {foreach from=$stringOverrideInstances key="index" item="instance"}
-      buildStringOverrideRow( {$instance} );
-      {/foreach}
+        {* Rebuild necessary rows in case of form error *}
+        {foreach from=$stringOverrideInstances key="index" item="instance"}
+          buildStringOverrideRow( {$instance} );
+        {/foreach}
       {/if}
       {literal}
 
       function buildStringOverrideRow( curInstance ) {
-        var rowId = 'string_override_row_';
+        var newRowNum;
 
         if (curInstance) {
-          if (curInstance <= 10) return;
-          currentInstance = curInstance;
-          previousInstance = currentInstance - 1;
+          // Don't fetch if already present
+          if ($('tr.string-override-row[data-row=' + curInstance + ']').length) {
+            return;
+          }
+          newRowNum = curInstance;
         } else {
-          var previousInstance = $('[id^="'+ rowId +'"]:last').attr('id').slice(rowId.length);
-          var currentInstance = parseInt(previousInstance) + 1;
+          newRowNum = 1 + $('tr.string-override-row:last').data('row');
         }
 
         var dataUrl = {/literal}"{crmURL q='snippet=4' h=0}"{literal};
-        dataUrl += "&instance="+currentInstance;
-
-        var prevInstRowId = '#string_override_row_' + previousInstance;
+        dataUrl += "&instance="+newRowNum;
 
         $.ajax({
           url: dataUrl,
           async: false,
           success: function(html) {
-            $(prevInstRowId).after(html);
-            $('#old_'+currentInstance).TextAreaResizer();
-            $('#new_'+currentInstance).TextAreaResizer();
+            $('.string-override-table tbody').append(html);
+            $('tr.string-override-row:last').trigger('crmLoad');
           }
         });
       }
         buildStringOverrideRow(false);
         e.preventDefault();
       });
+
+      // Auto-check new items
+      $('.string-override-table').on('keyup', 'textarea', function() {
+        if (!$(this).data('crm-initial-value')) {
+          var otherValue = $(this).closest('tr').find('textarea').not(this).val();
+          if ($(this).val() && otherValue) {
+            $(this).closest('tr').find('input[type=checkbox]').first().prop('checked', true);
+          }
+        }
+      });
+
     });
   </script>
 {/literal}