Dialog & select2 improvements
[civicrm-core.git] / templates / CRM / Mailing / Form / InsertTokens.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.5 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2014 |
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
27 {* Include a modified version of jquery-fieldselection
28 * FIXME: we use this plugin for so little it's hard to justify having it at all *}
29 <script type="text/javascript" src="{$config->resourceBase}packages/jquery/plugins/jquery-fieldselection.js"></script>
30
31 <script type="text/javascript">
32 cj('form#{$form.formName}').data('tokens', {$tokens|@json_encode});
33 var text_message = null;
34 var html_message = null;
35 var isPDF = false;
36 var isMailing = false;
37
38
39 {if $form.formName eq 'MessageTemplates'}
40 {literal}
41 text_message = "msg_text";
42 html_message = "msg_html";
43 {/literal}
44 {elseif $form.formName eq 'Address'}
45 {literal}
46 text_message = "mailing_format";
47 isMailing = false;
48 {/literal}
49 {else}
50 {literal}
51 text_message = "text_message";
52 html_message = (cj("#edit-html-message-value").length > 0) ? "edit-html-message-value" : "html_message";
53 isMailing = true;
54 {/literal}
55 {/if}
56
57 {if $form.formName eq 'PDF'}
58 {literal}
59 isPDF = true;
60 {/literal}
61 {/if}
62
63 {if $templateSelected}
64 {literal}
65 if ( document.getElementsByName("saveTemplate")[0].checked ) {
66 document.getElementById('template').selectedIndex = {/literal}{$templateSelected}{literal};
67 }
68 {/literal}
69 {/if}
70 {literal}
71
72 var editor = {/literal}"{$editor}"{literal};
73 function showSaveUpdateChkBox() {
74 if (document.getElementById('template') == null) {
75 if (document.getElementsByName("saveTemplate")[0].checked){
76 document.getElementById("saveDetails").style.display = "block";
77 document.getElementById("editMessageDetails").style.display = "block";
78 }
79 else {
80 document.getElementById("saveDetails").style.display = "none";
81 document.getElementById("editMessageDetails").style.display = "none";
82 }
83 return;
84 }
85
86 if (document.getElementsByName("saveTemplate")[0].checked &&
87 document.getElementsByName("updateTemplate")[0].checked == false) {
88 document.getElementById("updateDetails").style.display = "none";
89 }
90 else if ( document.getElementsByName("saveTemplate")[0].checked &&
91 document.getElementsByName("updateTemplate")[0].checked ){
92 document.getElementById("editMessageDetails").style.display = "block";
93 document.getElementById("saveDetails").style.display = "block";
94 }
95 else if ( document.getElementsByName("saveTemplate")[0].checked == false &&
96 document.getElementsByName("updateTemplate")[0].checked ) {
97 document.getElementById("saveDetails").style.display = "none";
98 document.getElementById("editMessageDetails").style.display = "block";
99 }
100 else {
101 document.getElementById("saveDetails").style.display = "none";
102 document.getElementById("editMessageDetails").style.display = "none";
103 }
104 }
105
106 function selectValue( val ) {
107 document.getElementsByName("saveTemplate")[0].checked = false;
108 document.getElementsByName("updateTemplate")[0].checked = false;
109 showSaveUpdateChkBox();
110 if ( !val ) {
111 document.getElementById("subject").value ="";
112 if ( !isPDF ) {
113 document.getElementById(text_message).value ="";
114 }
115 if ( editor == "ckeditor" ) {
116 oEditor = CKEDITOR.instances[html_message];
117 oEditor.setData('');
118 }
119 else if ( editor == "tinymce" ) {
120 tinyMCE.getInstanceById(html_message).setContent( html_body );
121 }
122 else if ( editor == "joomlaeditor" ) {
123 document.getElementById(html_message).value = '' ;
124 tinyMCE.execCommand('mceSetContent',false, '');
125 }
126 else if ( editor =="drupalwysiwyg" ) {
127 if (Drupal.wysiwyg.instances[html_message].setContent) {
128 Drupal.wysiwyg.instances[html_message].setContent(html_body);
129 }
130 // @TODO: Remove this when http://drupal.org/node/614146 drops
131 else if (Drupal.wysiwyg.instances[html_message].insert) {
132 alert("Please note your editor doesn't completely support this function. You may need to clear the contents of the editor prior to choosing a new template.");
133 Drupal.wysiwyg.instances[html_message].insert(html_body);
134 }
135 else {
136 alert("Sorry, your editor doesn't support this function yet.");
137 }
138 }
139 else {
140 document.getElementById(html_message).value = '' ;
141 }
142 if ( isPDF ) {
143 showBindFormatChkBox();
144 }
145 return;
146 }
147
148 var dataUrl = {/literal}"{crmURL p='civicrm/ajax/template' h=0 }"{literal};
149
150 cj.post( dataUrl, {tid: val}, function( data ) {
151 cj("#subject").val( data.subject );
152 if ( !isPDF ) {
153 if ( data.msg_text ) {
154 cj("#"+text_message).val( data.msg_text );
155 cj("div.text").show();
156 cj(".head").find('span').removeClass().addClass('ui-icon ui-icon-triangle-1-s');
157 cj("#helptext").show();
158 }
159 else {
160 cj("#"+text_message).val("");
161 }
162 }
163 var html_body = "";
164 if ( data.msg_html ) {
165 html_body = data.msg_html;
166 }
167
168 if (editor == "ckeditor") {
169 oEditor = CKEDITOR.instances[html_message];
170 oEditor.setData( html_body );
171 }
172 else if (editor == "tinymce") {
173 tinyMCE.execInstanceCommand('html_message',"mceInsertContent",false, html_body );
174 }
175 else if (editor == "joomlaeditor") {
176 cj("#"+ html_message).val( html_body );
177 tinyMCE.execCommand('mceSetContent',false, html_body);
178 }
179 else if ( editor =="drupalwysiwyg") {
180 if (Drupal.wysiwyg.instances[html_message].setContent) {
181 Drupal.wysiwyg.instances[html_message].setContent(html_body);
182 }
183 // @TODO: Remove this when http://drupal.org/node/614146 drops
184 else if (Drupal.wysiwyg.instances[html_message].insert) {
185 alert("Please note your editor doesn't completely support this function. You may need to clear the contents of the editor prior to choosing a new template.");
186 Drupal.wysiwyg.instances[html_message].insert(html_body);
187 }
188 else {
189 alert("Sorry, your editor doesn't support this function yet.");
190 }
191 }
192 else {
193 cj("#"+ html_message).val( html_body );
194 }
195 if (isPDF) {
196 var bind = data.pdf_format_id ? true : false ;
197 selectFormat( data.pdf_format_id, bind );
198 if (!bind) {
199 document.getElementById("bindFormat").style.display = "none";
200 }
201 }
202 }, 'json');
203 }
204
205 if ( isMailing ) {
206 document.getElementById("editMessageDetails").style.display = "block";
207
208 function verify(select) {
209 if (document.getElementsByName("saveTemplate")[0].checked == false) {
210 document.getElementById("saveDetails").style.display = "none";
211 }
212 document.getElementById("editMessageDetails").style.display = "block";
213
214 var templateExists = true;
215 if (document.getElementById('template') == null) {
216 templateExists = false;
217 }
218
219 if (templateExists && document.getElementById('template').value) {
220 document.getElementById("updateDetails").style.display = '';
221 }
222 else {
223 document.getElementById("updateDetails").style.display = 'none';
224 }
225
226 document.getElementById("saveTemplateName").disabled = false;
227 }
228
229 function showSaveDetails(chkbox) {
230 if (chkbox.checked) {
231 document.getElementById("saveDetails").style.display = "block";
232 document.getElementById("saveTemplateName").disabled = false;
233 }
234 else {
235 document.getElementById("saveDetails").style.display = "none";
236 document.getElementById("saveTemplateName").disabled = true;
237 }
238 }
239
240 showSaveUpdateChkBox();
241
242 {/literal}
243 {if $editor eq "ckeditor"}
244 {literal}
245 CRM.$(function($) {
246 oEditor = CKEDITOR.instances['html_message'];
247 oEditor.BaseHref = '' ;
248 oEditor.UserFilesPath = '' ;
249 oEditor.on( 'focus', verify );
250 });
251 {/literal}
252 {elseif $editor eq "tinymce"}
253 {literal}
254 CRM.$(function($) {
255 if ( isMailing ) {
256 cj('div.html').hover(
257 function( ) {
258 if ( tinyMCE.get(html_message) ) {
259 tinyMCE.get(html_message).onKeyUp.add(function() {
260 verify( );
261 });
262 }
263 },
264 function( ) {
265 if ( tinyMCE.get(html_message) ) {
266 if ( tinyMCE.get(html_message).getContent() ) {
267 verify( );
268 }
269 }
270 }
271 );
272 }
273 });
274 {/literal}
275 {elseif $editor eq "drupalwysiwyg"}
276 {literal}
277 CRM.$(function($) {
278 if ( isMailing ) {
279 cj('div.html').hover(
280 verify,
281 verify
282 );
283 }
284 });
285 {/literal}
286 {/if}
287 {literal}
288 }
289
290 CRM.$(function($) {
291 function insertToken() {
292 var
293 token = $(this).val(),
294 field = $(this).data('field');
295 if (field === 'html_message') {
296 tokenReplHtml(token);
297 } else {
298 field = textMsgID($(this));
299 $('#' + field).replaceSelection(token);
300 }
301 $(this).select2('val', '');
302 if (isMailing) {
303 verify();
304 }
305 }
306
307 function textMsgID(obj) {
308 if (obj.parents().is("#sms")) {
309 field = 'sms #' + obj.data('field');
310 }
311 else if(obj.parents().is("#email")) {
312 field = 'email #' + obj.data('field');
313 }
314 else {
315 field = obj.data('field');
316 }
317
318 return field;
319 }
320
321 function tokenReplHtml(token) {
322 var editor = {/literal}"{$editor}"{literal};
323 if ( editor == "tinymce" ) {
324 tinyMCE.execInstanceCommand('html_message',"mceInsertContent",false, token );
325 }
326 else if ( editor == "joomlaeditor" ) {
327 tinyMCE.execCommand('mceInsertContent',false, token);
328 var msg = document.getElementById(html_message).value;
329 var cursorlen = document.getElementById(html_message).selectionStart;
330 var textlen = msg.length;
331 document.getElementById(html_message).value = msg.substring(0, cursorlen) + token + msg.substring(cursorlen, textlen);
332 var cursorPos = (cursorlen + token.length);
333 document.getElementById(html_message).selectionStart = cursorPos;
334 document.getElementById(html_message).selectionEnd = cursorPos;
335 document.getElementById(html_message).focus();
336 }
337 else if ( editor == "ckeditor" ) {
338 oEditor = CKEDITOR.instances[html_message];
339 oEditor.insertHtml(token.toString() );
340 }
341 else if ( editor == "drupalwysiwyg" ) {
342 if (Drupal.wysiwyg.instances[html_message].insert) {
343 Drupal.wysiwyg.instances[html_message].insert(token.toString() );
344 }
345 else {
346 alert("Sorry, your editor doesn't support this function yet.");
347 }
348 }
349 else {
350 cj( "#"+ html_message ).replaceSelection( token );
351 }
352 }
353
354 // Initialize token selector widgets
355 var form = $('#{/literal}{$form.formName}{literal}');
356 $('input.crm-token-selector', form)
357 .addClass('crm-action-menu')
358 .change(insertToken)
359 .crmSelect2({
360 data: form.data('tokens'),
361 placeholder: '{/literal}{ts escape='js'}Insert Token{/ts}{literal}'
362 });
363
364 cj('.accordion .head').addClass( "ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ");
365 cj('.resizable-textarea textarea').css( 'width', '99%' );
366 cj('.grippie').css( 'margin-right', '3px');
367 cj('.accordion .head').hover( function() { cj(this).addClass( "ui-state-hover");
368 }, function() { cj(this).removeClass( "ui-state-hover");
369 }).bind('click', function() {
370 var checkClass = cj(this).find('span').attr( 'class' );
371 var len = checkClass.length;
372 if ( checkClass.substring( len - 1, len ) == 's' ) {
373 cj(this).find('span').removeClass().addClass('ui-icon ui-icon-triangle-1-e');
374 cj("span#help"+cj(this).find('span').attr('id')).hide();
375 }
376 else {
377 cj(this).find('span').removeClass().addClass('ui-icon ui-icon-triangle-1-s');
378 cj("span#help"+cj(this).find('span').attr('id')).show();
379 }
380 cj(this).next().toggle(); return false;
381 }).next().hide();
382 cj('span#html').removeClass().addClass('ui-icon ui-icon-triangle-1-s');
383 cj("div.html").show();
384
385 if ( !isMailing ) {
386 cj("div.text").show();
387 }
388
389 function setSignature() {
390 var emailID = $("#fromEmailAddress").val( );
391 if ( !isNaN( emailID ) ) {
392 var dataUrl = {/literal}"{crmURL p='civicrm/ajax/signature' h=0 }"{literal};
393 $.post( dataUrl, {emailID: emailID}, function( data ) {
394 var editor = {/literal}"{$editor}"{literal};
395
396 if (data.signature_text) {
397 // get existing text & html and append signatue
398 var textMessage = $("#"+ text_message).val( ) + '\n\n--\n' + data.signature_text;
399
400 // append signature
401 $("#"+ text_message).val( textMessage );
402 }
403
404 if ( data.signature_html ) {
405 var htmlMessage = $("#"+ html_message).val( ) + '<br/><br/>--<br/>' + data.signature_html;
406
407 // set wysiwg editor
408 if ( editor == "ckeditor" ) {
409 oEditor = CKEDITOR.instances[html_message];
410 var htmlMessage = oEditor.getData( ) + '<br/><br/>--' + data.signature_html;
411 oEditor.setData( htmlMessage );
412 }
413 else if ( editor == "tinymce" ) {
414 tinyMCE.execInstanceCommand('html_message',"mceInsertContent",false, htmlMessage);
415 }
416 else if ( editor == "drupalwysiwyg" ) {
417 if (Drupal.wysiwyg.instances[html_message].setContent) {
418 Drupal.wysiwyg.instances[html_message].setContent(htmlMessage);
419 }
420 // @TODO: Remove this when http://drupal.org/node/614146 drops
421 else if (Drupal.wysiwyg.instances[html_message].insert) {
422 alert("Please note your editor doesn't completely support this function. You may need to clear the contents of the editor prior to choosing a new template.");
423 Drupal.wysiwyg.instances[html_message].insert(htmlMessage);
424 }
425 else {
426 alert("Sorry, your editor doesn't support this function yet.");
427 }
428 }
429 else {
430 $("#"+ html_message).val(htmlMessage);
431 }
432 }
433 }, 'json');
434 }
435 }
436 if (!cj().find('div.crm-error').text()) {
437 cj(window).load(function () {
438 setSignature();
439 });
440 }
441 cj("#fromEmailAddress").change( function( ) {
442 setSignature( );
443 });
444 });
445
446 </script>
447 {/literal}