Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | {* |
2 | +--------------------------------------------------------------------+ | |
819d0d41 | 3 | | CiviCRM version 4.5 | |
6a488035 | 4 | +--------------------------------------------------------------------+ |
819d0d41 | 5 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | ||
48 | // check if it is wysiwyg element | |
49 | var editor = elementId.attr('editor'); | |
50 | ||
51 | //get the element type | |
52 | var elementType = elementId.attr('type'); | |
53 | ||
54 | // set the value for all the elements, elements needs to be handled are | |
55 | // select, checkbox, radio, date fields, text, textarea, multi-select | |
56 | // wysiwyg editor, advanced multi-select ( to do ) | |
57 | if ( elementType == 'radio' ) { | |
58 | firstElementValue = elementId.filter(':checked').eq(0).val(); | |
59 | elementId.filter("[value=" + firstElementValue + "]").prop("checked",true).change(); | |
6a488035 | 60 | } |
ae8f569f CW |
61 | else if ( elementType == 'checkbox' ) { |
62 | // handle checkbox | |
63 | // get the entity id of first element | |
64 | var firstEntityId = $('.crm-copy-fields > tbody > tr'); | |
6a488035 | 65 | |
ae8f569f CW |
66 | if ( firstEntityId.length == 0 ) { |
67 | firstEntityId = firstElement.closest('div.crm-grid-row'); | |
68 | } | |
6a488035 | 69 | |
ae8f569f | 70 | firstEntityId = firstEntityId.attr('entity_id'); |
6a488035 | 71 | |
ae8f569f | 72 | var firstCheckElement = $('.crm-copy-fields [type=checkbox][name^="field['+ firstEntityId +']['+ fname +']"][type!=hidden]'); |
6a488035 | 73 | |
ae8f569f CW |
74 | if ( firstCheckElement.length > 1 ) { |
75 | // lets uncheck all the checkbox except first one | |
76 | $('.crm-copy-fields [type=checkbox][name^="field["][name*="[' + fname +']"][type=checkbox]:not([name^="field['+ firstEntityId +']['+ fname +']["])').prop('checked', false); | |
77 | ||
78 | //here for each checkbox for first row, check if it is checked and set remaining checkboxes | |
79 | firstCheckElement.each(function() { | |
80 | if ($(this).prop('checked') ) { | |
81 | var elementName = $(this).attr('name'); | |
82 | var correctIndex = elementName.split('field['+ firstEntityId +']['+ fname +']['); | |
83 | correctIndexValue = correctIndex[1].replace(']', ''); | |
84 | $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']['+ correctIndexValue+']"][type!=hidden]').prop('checked',true).change(); | |
85 | } | |
86 | }); | |
6a488035 TO |
87 | } |
88 | else { | |
ae8f569f CW |
89 | if ( firstCheckElement.prop('checked') ) { |
90 | $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').prop('checked',true).change(); | |
91 | } | |
92 | else { | |
93 | $('.crm-copy-fields [type=checkbox][name^="field["][name*="['+ fname +']"][type!=hidden]').prop('checked', false).change(); | |
94 | } | |
6a488035 TO |
95 | } |
96 | } | |
ae8f569f | 97 | else if ( editor ) { |
6a488035 TO |
98 | var firstElementId = firstElement.attr('id'); |
99 | switch ( editor ) { | |
100 | case 'ckeditor': | |
101 | //get the content of first element | |
102 | oEditor = CKEDITOR.instances[firstElementId]; | |
103 | var htmlContent = oEditor.getData( ); | |
104 | ||
105 | // copy first element content to all the elements | |
106 | elementId.each( function() { | |
ae8f569f | 107 | var elemtId = $(this).attr('id'); |
6a488035 TO |
108 | oEditor = CKEDITOR.instances[elemtId]; |
109 | oEditor.setData( htmlContent ); | |
110 | }); | |
111 | break; | |
112 | case 'tinymce': | |
113 | //get the content of first element | |
114 | var htmlContent = tinyMCE.get( firstElementId ).getContent(); | |
115 | ||
116 | // copy first element content to all the elements | |
117 | elementId.each( function() { | |
ae8f569f | 118 | var elemtId = $(this).attr('id'); |
6a488035 TO |
119 | tinyMCE.get( elemtId ).setContent( htmlContent ); |
120 | }); | |
121 | break; | |
122 | case 'joomlaeditor': | |
123 | // TO DO | |
124 | case 'drupalwysiwyg': | |
125 | // TO DO | |
126 | default: | |
127 | elementId.val( firstElementValue ).change(); | |
128 | ||
129 | } | |
130 | } | |
131 | else { | |
bd8af239 | 132 | if (elementId.is('select') === true && firstElement.parent().find(':input').select().index() >= 1 && firstElement.parent().find('select').select().index < 1) { |
f813f78e | 133 | // its a multiselect case |
b5469340 | 134 | firstElement.parent().find(':input').select().each( function(count) { |
ae8f569f CW |
135 | var firstElementValue = $(this).val(); |
136 | var elementId = $('.crm-copy-fields [name^="field["][name*="[' + fname +'][' + count + '"][type!=hidden]'); | |
b5469340 DS |
137 | elementId.val(firstElementValue).not(":first").change(); |
138 | }); | |
139 | } | |
140 | else { | |
141 | elementId.val(firstElementValue).change(); | |
142 | } | |
6a488035 TO |
143 | } |
144 | ||
ae8f569f CW |
145 | // since we use different display field for date we also need to set it. |
146 | // also check for date time field and set the value correctly | |
147 | if ( isDateElement ) { | |
148 | copyValuesDate( fname ); | |
149 | } | |
150 | } | |
151 | ||
152 | /** | |
153 | * Special function to handle setting values for date fields | |
154 | * | |
155 | * @param fname string field name | |
156 | * @return void | |
157 | */ | |
158 | function copyValuesDate(fname) { | |
159 | var fnameDisplay = fname + '_display'; | |
160 | var fnameTime = fname + '_time'; | |
161 | ||
162 | var displayElement = $('.crm-copy-fields [name^="field_"][name$="_' + fnameDisplay +'"][type!=hidden]'); | |
163 | var timeElement = $('.crm-copy-fields [name^="field["][name*="[' + fnameTime +']"][type!=hidden]'); | |
164 | ||
165 | displayElement.val( displayElement.eq(0).val() ); | |
166 | timeElement.val( timeElement.eq(0).val() ); | |
6a488035 | 167 | } |
ae8f569f CW |
168 | |
169 | //bind the click event for action icon | |
170 | $('.action-icon').click(function( ) { | |
171 | copyFieldValues($(this).attr('fname')); | |
172 | }); | |
173 | }); | |
174 | ||
6a488035 TO |
175 | |
176 | </script> | |
177 | {/literal} |