Merge pull request #3921 from eileenmcnaughton/CRM-15168
[civicrm-core.git] / templates / CRM / common / batchCopy.tpl
index 5e40f94fa9c5a3d24f4ce6028c221d4bbbfe7e2d..2b6456b6fc12cd2f1e50ed0e51622d2e016f3c9f 100644 (file)
@@ -1,8 +1,8 @@
 {*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
 *}
 {literal}
 <script type="text/javascript">
-  cj( function() {
-    //bind the click event for action icon
-    cj('.action-icon').click( function( ) {
-      copyFieldValues( cj(this).attr('fname') );
-    });
-  });
-
-  /**
-   * This function use to copy fieldsi
-   *
-   * @param fname string field name
-   * @return void
-   */
-  function copyFieldValues( fname ) {
-    // this is the most common pattern for elements, so first check if it exits
-    // this check field starting with "field[" and contains [fname] and is not
-    // hidden ( for checkbox hidden element is created )
-    var elementId    = cj('.crm-copy-fields [name^="field["][name*="[' + fname +']"][type!=hidden]');
-
-    // get the first element and it's value
-    var firstElement = elementId.eq(0);
-    var firstElementValue = firstElement.val();
-
-    //console.log( elementId );
-    //console.log( firstElement );
-    //console.log( firstElementValue );
-
-    //check if it is date element
-    var isDateElement     = elementId.attr('format');
-
-    // check if it is wysiwyg element
-    var editor = elementId.attr('editor');
-
-    //get the element type
-    var elementType       = elementId.attr('type');
-
-    // set the value for all the elements, elements needs to be handled are
-    // select, checkbox, radio, date fields, text, textarea, multi-select
-    // wysiwyg editor, advanced multi-select ( to do )
-    if ( elementType == 'radio' ) {
-      firstElementValue = elementId.filter(':checked').eq(0).val();
-      elementId.filter("[value=" + firstElementValue + "]").prop("checked",true).change();
-    }
-    else if ( elementType == 'checkbox' ) {
-      // handle checkbox
-      // get the entity id of first element
-      var firstEntityId = cj('.crm-copy-fields > tbody > tr');
-
-      if ( firstEntityId.length == 0 ) {
-        firstEntityId = firstElement.closest('div.crm-grid-row');
+  CRM.$(function($) {
+    /**
+     * This function use to copy fields
+     *
+     * @param fname string field name
+     * @return void
+     */
+    function copyFieldValues( fname ) {
+      // this is the most common pattern for elements, so first check if it exits
+      // this check field starting with "field[" and contains [fname] and is not
+      // hidden ( for checkbox hidden element is created )
+      var elementId    = $('.crm-copy-fields [name^="field["][name*="[' + fname +']"][type!=hidden]');
+
+      // get the first element and it's value
+      var firstElement = elementId.eq(0);
+      var firstElementValue = firstElement.val();
+
+      //check if it is date element
+      var isDateElement     = elementId.attr('format');
+
+      // check if it is wysiwyg element
+      var editor = elementId.attr('editor');
+
+      //get the element type
+      var elementType       = elementId.attr('type');
+
+      // set the value for all the elements, elements needs to be handled are
+      // select, checkbox, radio, date fields, text, textarea, multi-select
+      // wysiwyg editor, advanced multi-select ( to do )
+      if ( elementType == 'radio' ) {
+        firstElementValue = elementId.filter(':checked').eq(0).val();
+        elementId.filter("[value=" + firstElementValue + "]").prop("checked",true).change();
       }
+      else if ( elementType == 'checkbox' ) {
+        // handle checkbox
+        // get the entity id of first element
+        var firstEntityId = $('.crm-copy-fields > tbody > tr');
 
-      firstEntityId = firstEntityId.attr('entity_id');
+        if ( firstEntityId.length == 0 ) {
+          firstEntityId = firstElement.closest('div.crm-grid-row');
+        }
 
-      var firstCheckElement = cj('.crm-copy-fields [type=checkbox][name^="field['+ firstEntityId +']['+ fname +']"][type!=hidden]');
+        firstEntityId = firstEntityId.attr('entity_id');
 
-      if ( firstCheckElement.length > 1 ) {
-        // lets uncheck all the checkbox except first one
-        cj('.crm-copy-fields [type=checkbox][name^="field["][name*="[' + fname +']"][type=checkbox]:not([name^="field['+ firstEntityId +']['+ fname +']["])').removeAttr('checked');
+        var firstCheckElement = $('.crm-copy-fields [type=checkbox][name^="field['+ firstEntityId +']['+ fname +']"][type!=hidden]');
 
-        //here for each checkbox for first row, check if it is checked and set remaining checkboxes
-        firstCheckElement.each(function() {
-          if (cj(this).prop('checked') ) {
-            var elementName = cj(this).attr('name');
-            var correctIndex = elementName.split('field['+ firstEntityId +']['+ fname +'][');
-            correctIndexValue = correctIndex[1].replace(']', '');
-            cj('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']['+ correctIndexValue+']"][type!=hidden]').attr('checked',true).change();
-          }
-        });
-      }
-      else {
-        if ( firstCheckElement.prop('checked') ) {
-          cj('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').attr('checked',true).change();
+        if ( firstCheckElement.length > 1 ) {
+          // lets uncheck all the checkbox except first one
+          $('.crm-copy-fields [type=checkbox][name^="field["][name*="[' + fname +']"][type=checkbox]:not([name^="field['+ firstEntityId +']['+ fname +']["])').prop('checked', false);
+
+          //here for each checkbox for first row, check if it is checked and set remaining checkboxes
+          firstCheckElement.each(function() {
+            if ($(this).prop('checked') ) {
+              var elementName = $(this).attr('name');
+              var correctIndex = elementName.split('field['+ firstEntityId +']['+ fname +'][');
+              correctIndexValue = correctIndex[1].replace(']', '');
+              $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']['+ correctIndexValue+']"][type!=hidden]').prop('checked',true).change();
+            }
+          });
         }
         else {
-          cj('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').removeAttr('checked').change();
+          if ( firstCheckElement.prop('checked') ) {
+            $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').prop('checked',true).change();
+          }
+          else {
+            $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').prop('checked', false).change();
+          }
         }
       }
-    }
-    else if ( editor ) {
+      else if ( editor ) {
         var firstElementId = firstElement.attr('id');
         switch ( editor ) {
           case 'ckeditor':
 
             // copy first element content to all the elements
             elementId.each( function() {
-              var elemtId = cj(this).attr('id');
+              var elemtId = $(this).attr('id');
               oEditor = CKEDITOR.instances[elemtId];
               oEditor.setData( htmlContent );
             });
 
             // copy first element content to all the elements
             elementId.each( function() {
-              var elemtId = cj(this).attr('id');
+              var elemtId = $(this).attr('id');
               tinyMCE.get( elemtId ).setContent( htmlContent );
             });
             break;
         }
       }
       else {
-        if (elementId.is('select') === true && firstElement.parent().find(':input').select().index() >= 1) {
+        if (elementId.is('select') === true && firstElement.parent().find(':input').select().index() >= 1 && firstElement.parent().find('select').select().index < 1) {
           // its a multiselect case
           firstElement.parent().find(':input').select().each( function(count) {
-            var firstElementValue = cj(this).val();
-            var elementId = cj('.crm-copy-fields [name^="field["][name*="[' + fname +'][' + count + '"][type!=hidden]');
+            var firstElementValue = $(this).val();
+            var elementId = $('.crm-copy-fields [name^="field["][name*="[' + fname +'][' + count + '"][type!=hidden]');
             elementId.val(firstElementValue).not(":first").change();
           });
         }
         }
       }
 
-    // since we use different display field for date we also need to set it.
-    // also check for date time field and set the value correctly
-    if ( isDateElement ) {
-      copyValuesDate( fname );
+      // since we use different display field for date we also need to set it.
+      // also check for date time field and set the value correctly
+      if ( isDateElement ) {
+        copyValuesDate( fname );
+      }
+    }
+
+    /**
+     * Special function to handle setting values for date fields
+     *
+     * @param fname string field name
+     * @return void
+     */
+    function copyValuesDate(fname) {
+      var fnameDisplay = fname + '_display';
+      var fnameTime    = fname + '_time';
+
+      var displayElement = $('.crm-copy-fields [name^="field_"][name$="_' + fnameDisplay +'"][type!=hidden]');
+      var timeElement    = $('.crm-copy-fields [name^="field["][name*="[' + fnameTime +']"][type!=hidden]');
+
+      displayElement.val( displayElement.eq(0).val() );
+      timeElement.val( timeElement.eq(0).val() );
     }
-  }
-
-  /**
-   * Special function to handle setting values for date fields
-   *
-   * @param fname string field name
-   * @return void
-   */
-  function copyValuesDate(fname) {
-    var fnameDisplay = fname + '_display';
-    var fnameTime    = fname + '_time';
-
-    var displayElement = cj('.crm-copy-fields [name^="field_"][name$="_' + fnameDisplay +'"][type!=hidden]');
-    var timeElement    = cj('.crm-copy-fields [name^="field["][name*="[' + fnameTime +']"][type!=hidden]');
-
-    displayElement.val( displayElement.eq(0).val() );
-    timeElement.val( timeElement.eq(0).val() );
-  }
+
+    //bind the click event for action icon
+    $('.action-icon').click(function( ) {
+      copyFieldValues($(this).attr('fname'));
+    });
+  });
+
 
 </script>
 {/literal}