webmaster@1
|
1 // $Id: teaser.js,v 1.12 2008/01/09 12:10:04 goba Exp $ |
webmaster@1
|
2 |
webmaster@1
|
3 /** |
webmaster@1
|
4 * Auto-attach for teaser behavior. |
webmaster@1
|
5 * |
webmaster@1
|
6 * Note: depends on resizable textareas. |
webmaster@1
|
7 */ |
webmaster@1
|
8 Drupal.behaviors.teaser = function(context) { |
webmaster@1
|
9 // This breaks in Konqueror. Prevent it from running. |
webmaster@1
|
10 if (/KDE/.test(navigator.vendor)) { |
webmaster@1
|
11 return; |
webmaster@1
|
12 } |
webmaster@1
|
13 |
webmaster@1
|
14 $('textarea.teaser:not(.teaser-processed)', context).each(function() { |
webmaster@1
|
15 var teaser = $(this).addClass('teaser-processed'); |
webmaster@1
|
16 |
webmaster@1
|
17 // Move teaser textarea before body, and remove its form-item wrapper. |
webmaster@1
|
18 var body = $('#'+ Drupal.settings.teaser[this.id]); |
webmaster@1
|
19 var checkbox = $('#'+ Drupal.settings.teaserCheckbox[this.id]).parent(); |
webmaster@1
|
20 var checked = $(checkbox).children('input').attr('checked') ? true : false; |
webmaster@1
|
21 var parent = teaser[0].parentNode; |
webmaster@1
|
22 $(body).before(teaser); |
webmaster@1
|
23 $(parent).remove(); |
webmaster@1
|
24 |
webmaster@1
|
25 function trim(text) { |
webmaster@1
|
26 return text.replace(/^\s+/g, '').replace(/\s+$/g, ''); |
webmaster@1
|
27 } |
webmaster@1
|
28 |
webmaster@1
|
29 // Join the teaser back to the body. |
webmaster@1
|
30 function join_teaser() { |
webmaster@1
|
31 if (teaser.val()) { |
webmaster@1
|
32 body.val(trim(teaser.val()) +'\r\n\r\n'+ trim(body.val())); |
webmaster@1
|
33 } |
webmaster@1
|
34 // Empty, hide and disable teaser. |
webmaster@1
|
35 teaser[0].value = ''; |
webmaster@1
|
36 $(teaser).attr('disabled', 'disabled'); |
webmaster@1
|
37 $(teaser).parent().slideUp('fast'); |
webmaster@1
|
38 // Change label. |
webmaster@1
|
39 $(this).val(Drupal.t('Split summary at cursor')); |
webmaster@1
|
40 // Hide separate teaser checkbox. |
webmaster@1
|
41 $(checkbox).hide(); |
webmaster@1
|
42 // Force a hidden checkbox to be checked (to ensure that the body is |
webmaster@1
|
43 // correctly processed on form submit when teaser/body are in joined |
webmaster@1
|
44 // state), and remember the current checked status. |
webmaster@1
|
45 checked = $(checkbox).children('input').attr('checked') ? true : false; |
webmaster@1
|
46 $(checkbox).children('input').attr('checked', true); |
webmaster@1
|
47 } |
webmaster@1
|
48 |
webmaster@1
|
49 // Split the teaser from the body. |
webmaster@1
|
50 function split_teaser() { |
webmaster@1
|
51 body[0].focus(); |
webmaster@1
|
52 var selection = Drupal.getSelection(body[0]); |
webmaster@1
|
53 var split = selection.start; |
webmaster@1
|
54 var text = body.val(); |
webmaster@1
|
55 |
webmaster@1
|
56 // Note: using val() fails sometimes. jQuery bug? |
webmaster@1
|
57 teaser[0].value = trim(text.slice(0, split)); |
webmaster@1
|
58 body[0].value = trim(text.slice(split)); |
webmaster@1
|
59 // Reveal and enable teaser |
webmaster@1
|
60 $(teaser).attr('disabled', ''); |
webmaster@1
|
61 $(teaser).parent().slideDown('fast'); |
webmaster@1
|
62 // Change label |
webmaster@1
|
63 $(this).val(Drupal.t('Join summary')); |
webmaster@1
|
64 // Show separate teaser checkbox, restore checked value. |
webmaster@1
|
65 $(checkbox).show().children('input').attr('checked', checked); |
webmaster@1
|
66 } |
webmaster@1
|
67 |
webmaster@1
|
68 // Add split/join button. |
webmaster@1
|
69 var button = $('<div class="teaser-button-wrapper"><input type="button" class="teaser-button" /></div>'); |
webmaster@1
|
70 var include = $('#'+ this.id.substring(0, this.id.length - 2) +'include'); |
webmaster@1
|
71 $(include).parent().parent().before(button); |
webmaster@1
|
72 |
webmaster@1
|
73 // Extract the teaser from the body, if set. Otherwise, stay in joined mode. |
webmaster@1
|
74 var text = body.val().split('<!--break-->', 2); |
webmaster@1
|
75 if (text.length == 2) { |
webmaster@1
|
76 teaser[0].value = trim(text[0]); |
webmaster@1
|
77 body[0].value = trim(text[1]); |
webmaster@1
|
78 $(teaser).attr('disabled', ''); |
webmaster@1
|
79 $('input', button).val(Drupal.t('Join summary')).toggle(join_teaser, split_teaser); |
webmaster@1
|
80 } |
webmaster@1
|
81 else { |
webmaster@1
|
82 $('input', button).val(Drupal.t('Split summary at cursor')).toggle(split_teaser, join_teaser); |
webmaster@1
|
83 $(checkbox).hide().children('input').attr('checked', true); |
webmaster@1
|
84 } |
webmaster@1
|
85 |
webmaster@1
|
86 // Make sure that textarea.js has done its magic to ensure proper visibility state. |
webmaster@1
|
87 if (Drupal.behaviors.textarea && teaser.is(('.form-textarea:not(.textarea-processed)'))) { |
webmaster@1
|
88 Drupal.behaviors.textarea(teaser.parentNode); |
webmaster@1
|
89 } |
webmaster@1
|
90 // Set initial visibility |
webmaster@1
|
91 if ($(teaser).is('[@disabled]')) { |
webmaster@1
|
92 $(teaser).parent().hide(); |
webmaster@1
|
93 } |
webmaster@1
|
94 |
webmaster@1
|
95 }); |
webmaster@1
|
96 }; |