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