Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | {* |
2 | +--------------------------------------------------------------------+ | |
2c4c49ca | 3 | | CiviCRM version 4.7 | |
6a488035 | 4 | +--------------------------------------------------------------------+ |
2a73d3b0 | 5 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
6 | +--------------------------------------------------------------------+ |
7 | | This file is a part of CiviCRM. | | |
8 | | | | |
9 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
10 | | under the terms of the GNU Affero General Public License | | |
11 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
12 | | | | |
13 | | CiviCRM is distributed in the hope that it will be useful, but | | |
14 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
16 | | See the GNU Affero General Public License for more details. | | |
17 | | | | |
18 | | You should have received a copy of the GNU Affero General Public | | |
19 | | License and the CiviCRM Licensing Exception along | | |
20 | | with this program; if not, contact CiviCRM LLC | | |
21 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
22 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
23 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
24 | +--------------------------------------------------------------------+ | |
25 | *} | |
26 | {literal} | |
27 | <script type="text/javascript"> | |
3cc60a06 | 28 | CRM.$(function($) { |
ae8f569f CW |
29 | /** |
30 | * This function use to copy fields | |
31 | * | |
32 | * @param fname string field name | |
33 | * @return void | |
34 | */ | |
35 | function copyFieldValues( fname ) { | |
36 | // this is the most common pattern for elements, so first check if it exits | |
37 | // this check field starting with "field[" and contains [fname] and is not | |
38 | // hidden ( for checkbox hidden element is created ) | |
39 | var elementId = $('.crm-copy-fields [name^="field["][name*="[' + fname +']"][type!=hidden]'); | |
40 | ||
41 | // get the first element and it's value | |
42 | var firstElement = elementId.eq(0); | |
43 | var firstElementValue = firstElement.val(); | |
44 | ||
45 | //check if it is date element | |
46 | var isDateElement = elementId.attr('format'); | |
47 | ||
ae8f569f CW |
48 | //get the element type |
49 | var elementType = elementId.attr('type'); | |
50 | ||
51 | // set the value for all the elements, elements needs to be handled are | |
52 | // select, checkbox, radio, date fields, text, textarea, multi-select | |
53 | // wysiwyg editor, advanced multi-select ( to do ) | |
54 | if ( elementType == 'radio' ) { | |
55 | firstElementValue = elementId.filter(':checked').eq(0).val(); | |
56 | elementId.filter("[value=" + firstElementValue + "]").prop("checked",true).change(); | |
6a488035 | 57 | } |
ae8f569f CW |
58 | else if ( elementType == 'checkbox' ) { |
59 | // handle checkbox | |
60 | // get the entity id of first element | |
61 | var firstEntityId = $('.crm-copy-fields > tbody > tr'); | |
6a488035 | 62 | |
ae8f569f CW |
63 | if ( firstEntityId.length == 0 ) { |
64 | firstEntityId = firstElement.closest('div.crm-grid-row'); | |
65 | } | |
6a488035 | 66 | |
ae8f569f | 67 | firstEntityId = firstEntityId.attr('entity_id'); |
6a488035 | 68 | |
ae8f569f | 69 | var firstCheckElement = $('.crm-copy-fields [type=checkbox][name^="field['+ firstEntityId +']['+ fname +']"][type!=hidden]'); |
6a488035 | 70 | |
ae8f569f CW |
71 | if ( firstCheckElement.length > 1 ) { |
72 | // lets uncheck all the checkbox except first one | |
73 | $('.crm-copy-fields [type=checkbox][name^="field["][name*="[' + fname +']"][type=checkbox]:not([name^="field['+ firstEntityId +']['+ fname +']["])').prop('checked', false); | |
74 | ||
75 | //here for each checkbox for first row, check if it is checked and set remaining checkboxes | |
76 | firstCheckElement.each(function() { | |
77 | if ($(this).prop('checked') ) { | |
78 | var elementName = $(this).attr('name'); | |
79 | var correctIndex = elementName.split('field['+ firstEntityId +']['+ fname +']['); | |
80 | correctIndexValue = correctIndex[1].replace(']', ''); | |
81 | $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']['+ correctIndexValue+']"][type!=hidden]').prop('checked',true).change(); | |
82 | } | |
83 | }); | |
6a488035 TO |
84 | } |
85 | else { | |
ae8f569f CW |
86 | if ( firstCheckElement.prop('checked') ) { |
87 | $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').prop('checked',true).change(); | |
88 | } | |
89 | else { | |
90 | $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').prop('checked', false).change(); | |
91 | } | |
6a488035 TO |
92 | } |
93 | } | |
13d9bc82 CW |
94 | else if (elementId.is('textarea')) { |
95 | var text = CRM.wysiwyg.getVal(firstElement); | |
96 | elementId.each(function() { | |
97 | CRM.wysiwyg.setVal(this, text); | |
98 | }); | |
6a488035 TO |
99 | } |
100 | else { | |
bd8af239 | 101 | if (elementId.is('select') === true && firstElement.parent().find(':input').select().index() >= 1 && firstElement.parent().find('select').select().index < 1) { |
f813f78e | 102 | // its a multiselect case |
b5469340 | 103 | firstElement.parent().find(':input').select().each( function(count) { |
ae8f569f CW |
104 | var firstElementValue = $(this).val(); |
105 | var elementId = $('.crm-copy-fields [name^="field["][name*="[' + fname +'][' + count + '"][type!=hidden]'); | |
b5469340 DS |
106 | elementId.val(firstElementValue).not(":first").change(); |
107 | }); | |
108 | } | |
109 | else { | |
110 | elementId.val(firstElementValue).change(); | |
111 | } | |
6a488035 TO |
112 | } |
113 | ||
ae8f569f CW |
114 | // since we use different display field for date we also need to set it. |
115 | // also check for date time field and set the value correctly | |
116 | if ( isDateElement ) { | |
117 | copyValuesDate( fname ); | |
118 | } | |
119 | } | |
120 | ||
121 | /** | |
122 | * Special function to handle setting values for date fields | |
123 | * | |
124 | * @param fname string field name | |
125 | * @return void | |
126 | */ | |
127 | function copyValuesDate(fname) { | |
d99491c5 CW |
128 | var displayElement = $('.crm-copy-fields [name^="field_"][name*="_' + fname +'_display"]:visible'); |
129 | var timeElement = $('.crm-copy-fields [name^="field["][name*="[' + fname +'_time]"][type!=hidden]'); | |
ae8f569f CW |
130 | |
131 | displayElement.val( displayElement.eq(0).val() ); | |
132 | timeElement.val( timeElement.eq(0).val() ); | |
6a488035 | 133 | } |
ae8f569f CW |
134 | |
135 | //bind the click event for action icon | |
136 | $('.action-icon').click(function( ) { | |
137 | copyFieldValues($(this).attr('fname')); | |
138 | }); | |
139 | }); | |
140 | ||
6a488035 TO |
141 | |
142 | </script> | |
143 | {/literal} |