comparison misc/teaser.js @ 1:c1f4ac30525a 6.0

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