diff options
author | Dries Buytaert <dries@buytaert.net> | 2007-04-09 13:58:03 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2007-04-09 13:58:03 +0000 |
commit | dcbb5fa3f4434be266793f6a610071a14c777414 (patch) | |
tree | 10a5bae9c26e0fad6a5d08eadf6fd082304354d2 /misc | |
parent | 15d739a5049091821c81006ad8080f0556053e16 (diff) | |
download | brdo-dcbb5fa3f4434be266793f6a610071a14c777414.tar.gz brdo-dcbb5fa3f4434be266793f6a610071a14c777414.tar.bz2 |
- Patch #107061 by Steven et al: add jQuery teaser splitter.
Diffstat (limited to 'misc')
-rw-r--r-- | misc/drupal.js | 20 | ||||
-rw-r--r-- | misc/teaser.js | 80 | ||||
-rw-r--r-- | misc/textarea.js | 6 |
3 files changed, 106 insertions, 0 deletions
diff --git a/misc/drupal.js b/misc/drupal.js index b4130e4ad..04176089d 100644 --- a/misc/drupal.js +++ b/misc/drupal.js @@ -200,6 +200,26 @@ Drupal.encodeURIComponent = function (item, uri) { return uri.indexOf('?q=') ? item : item.replace('%26', '%2526').replace('%23', '%2523'); }; +/** + * Get the text selection in a textarea. + */ +Drupal.getSelection = function (element) { + if (typeof(element.selectionStart) != 'number' && document.selection) { + // The current selection + var range1 = document.selection.createRange(); + var range2 = range1.duplicate(); + // Select all text. + range2.moveToElementText(element); + // Now move 'dummy' end point to end point of original range. + range2.setEndPoint('EndToEnd', range1); + // Now we can calculate start and end points. + var start = range2.text.length - range1.text.length; + var end = start + range1.text.length; + return { 'start': start, 'end': end }; + } + return { 'start': element.selectionStart, 'end': element.selectionEnd }; +} + // Global Killswitch on the <html> element if (Drupal.jsEnabled) { document.documentElement.className = 'js'; diff --git a/misc/teaser.js b/misc/teaser.js new file mode 100644 index 000000000..12275fc39 --- /dev/null +++ b/misc/teaser.js @@ -0,0 +1,80 @@ +// $Id$ + +/** + * Auto-attach for teaser behaviour. + * + * Note: depends on resizable textareas. + */ +Drupal.teaserAttach = function() { + $('textarea.teaser:not(.joined)').each(function() { + var teaser = $(this).addClass('joined'); + + // Move teaser textarea before body, and remove its form-item wrapper. + var body = $('#'+ Drupal.settings.teaser[this.id]); + var checkbox = $('#'+ Drupal.settings.teaserCheckbox[this.id]).parent(); + var parent = teaser[0].parentNode; + $(body).before(teaser); + $(parent).remove(); + + function trim(text) { + return text.replace(/^\s+/g, '').replace(/\s+$/g, ''); + } + + // Join the teaser back to the body. + function join_teaser() { + if (teaser.val()) { + body.val(trim(teaser.val()) +'\r\n\r\n'+ trim(body.val())); + } + // Hide and disable teaser + $(teaser).attr('disabled', 'disabled'); + $(teaser).parent().slideUp('fast'); + // Change label + $(this).val(Drupal.settings.teaserButton[1]); + // Show separate teaser checkbox + $(checkbox).hide(); + } + + // Split the teaser from the body. + function split_teaser() { + body[0].focus(); + var selection = Drupal.getSelection(body[0]); + var split = selection.start; + var text = body.val(); + + // Note: using val() fails sometimes. jQuery bug? + teaser[0].value = trim(text.slice(0, split)); + body[0].value = trim(text.slice(split)); + // Reveal and enable teaser + $(teaser).attr('disabled', ''); + $(teaser).parent().slideDown('fast'); + // Change label + $(this).val(Drupal.settings.teaserButton[0]); + // Show separate teaser checkbox + $(checkbox).show(); + } + + // Add split/join button. + var button = $('<div class="teaser-button-wrapper"><input type="button" class="teaser-button" /></div>'); + var include = $('#'+ this.id.substring(0, this.id.length - 2) +'include'); + $(include).parent().parent().before(button); + + // Extract the teaser from the body, if set. Otherwise, stay in joined mode. + var text = body.val().split('<!--break-->', 2); + if (text.length == 2) { + teaser[0].value = trim(text[0]); + body[0].value = trim(text[1]); + $(teaser).attr('disabled', ''); + $('input', button).val(Drupal.settings.teaserButton[0]).toggle(join_teaser, split_teaser); + } + else { + $(teaser).hide(); + $('input', button).val(Drupal.settings.teaserButton[1]).toggle(split_teaser, join_teaser); + $(checkbox).hide(); + } + + }); +} + +if (Drupal.jsEnabled) { + $(document).ready(Drupal.teaserAttach); +} diff --git a/misc/textarea.js b/misc/textarea.js index 6fe92234d..34217c7e5 100644 --- a/misc/textarea.js +++ b/misc/textarea.js @@ -7,6 +7,12 @@ Drupal.textareaAttach = function() { $(this).wrap('<div class="resizable-textarea"></div>') .parent().append($('<div class="grippie"></div>').mousedown(startDrag)); + // Inherit visibility + if ($(this).is(':hidden')) { + $(this).parent().hide(); + $(this).show(); + } + var grippie = $('div.grippie', $(this).parent())[0]; grippie.style.marginRight = (grippie.offsetWidth - $(this)[0].offsetWidth) +'px'; |