webmaster@1: // $Id: teaser.js,v 1.12 2008/01/09 12:10:04 goba Exp $ webmaster@1: webmaster@1: /** webmaster@1: * Auto-attach for teaser behavior. webmaster@1: * webmaster@1: * Note: depends on resizable textareas. webmaster@1: */ webmaster@1: Drupal.behaviors.teaser = function(context) { webmaster@1: // This breaks in Konqueror. Prevent it from running. webmaster@1: if (/KDE/.test(navigator.vendor)) { webmaster@1: return; webmaster@1: } webmaster@1: webmaster@1: $('textarea.teaser:not(.teaser-processed)', context).each(function() { webmaster@1: var teaser = $(this).addClass('teaser-processed'); webmaster@1: webmaster@1: // Move teaser textarea before body, and remove its form-item wrapper. webmaster@1: var body = $('#'+ Drupal.settings.teaser[this.id]); webmaster@1: var checkbox = $('#'+ Drupal.settings.teaserCheckbox[this.id]).parent(); webmaster@1: var checked = $(checkbox).children('input').attr('checked') ? true : false; webmaster@1: var parent = teaser[0].parentNode; webmaster@1: $(body).before(teaser); webmaster@1: $(parent).remove(); webmaster@1: webmaster@1: function trim(text) { webmaster@1: return text.replace(/^\s+/g, '').replace(/\s+$/g, ''); webmaster@1: } webmaster@1: webmaster@1: // Join the teaser back to the body. webmaster@1: function join_teaser() { webmaster@1: if (teaser.val()) { webmaster@1: body.val(trim(teaser.val()) +'\r\n\r\n'+ trim(body.val())); webmaster@1: } webmaster@1: // Empty, hide and disable teaser. webmaster@1: teaser[0].value = ''; webmaster@1: $(teaser).attr('disabled', 'disabled'); webmaster@1: $(teaser).parent().slideUp('fast'); webmaster@1: // Change label. webmaster@1: $(this).val(Drupal.t('Split summary at cursor')); webmaster@1: // Hide separate teaser checkbox. webmaster@1: $(checkbox).hide(); webmaster@1: // Force a hidden checkbox to be checked (to ensure that the body is webmaster@1: // correctly processed on form submit when teaser/body are in joined webmaster@1: // state), and remember the current checked status. webmaster@1: checked = $(checkbox).children('input').attr('checked') ? true : false; webmaster@1: $(checkbox).children('input').attr('checked', true); webmaster@1: } webmaster@1: webmaster@1: // Split the teaser from the body. webmaster@1: function split_teaser() { webmaster@1: body[0].focus(); webmaster@1: var selection = Drupal.getSelection(body[0]); webmaster@1: var split = selection.start; webmaster@1: var text = body.val(); webmaster@1: webmaster@1: // Note: using val() fails sometimes. jQuery bug? webmaster@1: teaser[0].value = trim(text.slice(0, split)); webmaster@1: body[0].value = trim(text.slice(split)); webmaster@1: // Reveal and enable teaser webmaster@1: $(teaser).attr('disabled', ''); webmaster@1: $(teaser).parent().slideDown('fast'); webmaster@1: // Change label webmaster@1: $(this).val(Drupal.t('Join summary')); webmaster@1: // Show separate teaser checkbox, restore checked value. webmaster@1: $(checkbox).show().children('input').attr('checked', checked); webmaster@1: } webmaster@1: webmaster@1: // Add split/join button. webmaster@1: var button = $('
'); webmaster@1: var include = $('#'+ this.id.substring(0, this.id.length - 2) +'include'); webmaster@1: $(include).parent().parent().before(button); webmaster@1: webmaster@1: // Extract the teaser from the body, if set. Otherwise, stay in joined mode. webmaster@1: var text = body.val().split('', 2); webmaster@1: if (text.length == 2) { webmaster@1: teaser[0].value = trim(text[0]); webmaster@1: body[0].value = trim(text[1]); webmaster@1: $(teaser).attr('disabled', ''); webmaster@1: $('input', button).val(Drupal.t('Join summary')).toggle(join_teaser, split_teaser); webmaster@1: } webmaster@1: else { webmaster@1: $('input', button).val(Drupal.t('Split summary at cursor')).toggle(split_teaser, join_teaser); webmaster@1: $(checkbox).hide().children('input').attr('checked', true); webmaster@1: } webmaster@1: webmaster@1: // Make sure that textarea.js has done its magic to ensure proper visibility state. webmaster@1: if (Drupal.behaviors.textarea && teaser.is(('.form-textarea:not(.textarea-processed)'))) { webmaster@1: Drupal.behaviors.textarea(teaser.parentNode); webmaster@1: } webmaster@1: // Set initial visibility webmaster@1: if ($(teaser).is('[@disabled]')) { webmaster@1: $(teaser).parent().hide(); webmaster@1: } webmaster@1: webmaster@1: }); webmaster@1: };