b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (Version 1.9) [ class tree: plugins ] [ index: plugins ] [ all elements ]

Source for file _quicktags.plugin.php

Documentation is available at _quicktags.plugin.php

  1. <?php
  2. /**
  3.  * This file implements the Quicktags Toolbar plugin for b2evolution
  4.  *
  5.  * This is Ron's remix!
  6.  * Includes code from the WordPress team -
  7.  *  http://sourceforge.net/project/memberlist.php?group_id=51422
  8.  *
  9.  * b2evolution - {@link http://b2evolution.net/}
  10.  * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
  11.  * @copyright (c)2003-2006 by Francois PLANQUE - {@link http://fplanque.net/}
  12.  *
  13.  * @package plugins
  14.  */
  15. if!defined('EVO_MAIN_INIT') ) die'Please, do not access this page directly.' );
  16.  
  17. /**
  18.  * @package plugins
  19.  */
  20. class quicktags_plugin extends Plugin
  21. {
  22.     var $code = 'b2evQTag';
  23.     var $name = 'Quick Tags';
  24.     var $priority = 30;
  25.     var $version = '1.9-dev';
  26.  
  27.     /**
  28.      * Init
  29.      */
  30.     function PluginInit$params )
  31.     {
  32.         $this->short_desc = T_('Easy HTML tags inserting');
  33.         $this->long_desc = T_('This plugin will display a toolbar with buttons to quickly insert HTML tags around selected text in a post.');
  34.     }
  35.  
  36.  
  37.     /**
  38.      * Display a toolbar
  39.      *
  40.      * @param array Associative array of parameters
  41.      * @return boolean did we display a toolbar?
  42.      */
  43.     function AdminDisplayToolbar$params )
  44.     {
  45.         global $Hit;
  46.  
  47.         if$params['edit_layout'== 'simple' )
  48.         {    // Thsi is too complex for simple mode, don't display it:
  49.             return false;
  50.         }
  51.  
  52.         if$Hit->is_lynx )
  53.         // let's deactivate quicktags on Lynx, because they don't work there.
  54.             return false;
  55.         }
  56.         ?>
  57.  
  58.         <script type="text/javascript">
  59.         //<![CDATA[
  60.         var b2evoButtons = new Array();
  61.         var b2evoLinks = new Array();
  62.         var b2evoOpenTags = new Array();
  63.  
  64.         function b2evoButton(id, display, tagStart, tagEnd, access, tit, open) {
  65.             this.id = id;                            // used to name the toolbar button
  66.             this.display = display;        // label on button
  67.             this.tagStart = tagStart; // open tag
  68.             this.tagEnd = tagEnd;            // close tag
  69.             this.access = access;            // access key
  70.             this.tit = tit;                        // title
  71.             this.open = open;                    // set to -1 if tag does not need to be closed
  72.         }
  73.  
  74.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  75.                 'b2evo_ins'
  76.                 ,'ins'
  77.                 ,'<ins>','</ins>'
  78.                 ,'i'
  79.                 ,'<?php echo T_('INSerted [Alt-I]'?>'
  80.             );
  81.  
  82.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  83.                 'b2evo_del'
  84.                 ,'del'
  85.                 ,'<del>','</del>'
  86.                 ,'d'
  87.                 ,'<?php echo T_('DELeted [Alt-D]'?>'
  88.             );
  89.  
  90.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  91.                 'b2evo_strong'
  92.                 ,'str'
  93.                 ,'<strong>','</strong>'
  94.                 ,'s'
  95.                 ,'<?php echo T_('STRong [Alt-S]'?>'
  96.             );
  97.  
  98.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  99.                 'b2evo_em'
  100.                 ,'em'
  101.                 ,'<em>','</em>'
  102.                 ,'e'
  103.                 ,'<?php echo T_('EMphasis [Alt-E]'?>'
  104.             );
  105.  
  106.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  107.                 'b2evo_code'
  108.                 ,'code'
  109.                 ,'<code>','</code>'
  110.                 ,'c'
  111.                 ,'<?php echo T_('CODE [Alt-C]'?>'
  112.             );
  113.  
  114.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  115.                 'b2evo_par'
  116.                 ,'p'
  117.                 ,'<p>','</p>'
  118.                 ,'p'
  119.                 ,'<?php echo T_('Paragraph [Alt-P]'?>'
  120.             );
  121.  
  122.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  123.                 'b2evo_ul'
  124.                 ,'ul'
  125.                 ,'<ul>\n','</ul>\n\n'
  126.                 ,'u'
  127.                 ,'<?php echo T_('Unordered List [Alt-U]'?>'
  128.             );
  129.  
  130.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  131.                 'b2evo_ol'
  132.                 ,'ol'
  133.                 ,'<ol>\n','</ol>\n\n'
  134.                 ,'o'
  135.             );
  136.  
  137.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  138.                 'b2evo_li'
  139.                 ,'li'
  140.                 ,'  <li>','</li>\n'
  141.                 ,'l'
  142.                 ,'<?php echo T_('List Item [Alt-L]'?>'
  143.             );
  144.  
  145.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  146.                 'b2evo_block'
  147.                 ,'block'
  148.                 ,'<blockquote>','</blockquote>'
  149.                 ,'b'
  150.                 ,'<?php echo T_('BLOCKQUOTE [Alt-B]'?>'
  151.             );
  152.  
  153.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  154.                 'b2evo_img'
  155.                 ,'img'
  156.                 ,'',''
  157.                 ,'g'
  158.                 ,'<?php echo T_('IMaGe [Alt-G]'?>'
  159.                 ,-1
  160.             ); // special case
  161.  
  162.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  163.                 'b2evo_link'
  164.                 ,'link'
  165.                 ,'','</a>'
  166.                 ,'a'
  167.                 ,'<?php echo T_('A href [Alt-A]'?>'
  168.             ); // special case
  169.  
  170.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  171.                 'b2evo_pre'
  172.                 ,'pre'
  173.                 ,'<pre>','</pre>'
  174.                 ,'r'
  175.                 ,'[Alt-R]'
  176.             );
  177.  
  178.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  179.                 'b2evo_more'
  180.                 ,'!M'
  181.                 ,'<!-'+'-more-'+'->',''
  182.                 ,'m'
  183.                 ,'<?php echo T_('More [Alt-M]'?>'
  184.                 ,-1
  185.             );
  186.  
  187.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  188.                 'b2evo_noteaser'
  189.                 ,'!NT'
  190.                 ,'<!-'+'-noteaser-'+'->',''
  191.                 ,'t'
  192.                 ,'<?php echo T_('no teaser [Alt-T]'?>'
  193.                 ,-1
  194.             );
  195.  
  196.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  197.                 'b2evo_next'
  198.                 ,'!NP'
  199.                 ,'<!-'+'-nextpage-'+'->',''
  200.                 ,'q'
  201.                 ,'<?php echo T_('next page [Alt-Q]'?>'
  202.                 ,-1
  203.             );
  204.  
  205.         function b2evoLink() {
  206.             this.display = '';
  207.             this.URL = '';
  208.             this.newWin = 0;
  209.         }
  210.  
  211.         function b2evoShowButton(button, i)
  212.         {
  213.             if (button.id == 'b2evo_img')
  214.             {
  215.                 document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" title="' + button.tit + '" class="quicktags" onclick="b2evoInsertImage(b2evoCanvas);" value="' + button.display + '" />');
  216.             }
  217.             else if (button.id == 'b2evo_link')
  218.             {
  219.                 document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" title="' + button.tit + '" class="quicktags" onclick="b2evoInsertLink(b2evoCanvas, ' + i + ');" value="' + button.display + '" />');
  220.             }
  221.             else
  222.             {
  223.                 document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" title="' + button.tit + '" class="quicktags" onclick="b2evoInsertTag(b2evoCanvas, ' + i + ');" value="' + button.display + '"  />');
  224.             }
  225.         }
  226.  
  227.         function b2evoAddTag(button) {
  228.             if (b2evoButtons[button].tagEnd != '') {
  229.                 b2evoOpenTags[b2evoOpenTags.length] = button;
  230.                 document.getElementById(b2evoButtons[button].id).value = '/' + document.getElementById(b2evoButtons[button].id).value;
  231.             }
  232.         }
  233.  
  234.         function b2evoRemoveTag(button) {
  235.             for (i = 0; i < b2evoOpenTags.length; i++) {
  236.                 if (b2evoOpenTags[i] == button) {
  237.                     b2evoOpenTags.splice(i, 1);
  238.                     document.getElementById(b2evoButtons[button].id).value =         document.getElementById(b2evoButtons[button].id).value.replace('/', '');
  239.                 }
  240.             }
  241.         }
  242.  
  243.         function b2evoCheckOpenTags(button) {
  244.             var tag = 0;
  245.             for (i = 0; i < b2evoOpenTags.length; i++) {
  246.                 if (b2evoOpenTags[i] == button) {
  247.                     tag++;
  248.                 }
  249.             }
  250.             if (tag > 0) {
  251.                 return true; // tag found
  252.             }
  253.             else {
  254.                 return false; // tag not found
  255.             }
  256.         }
  257.  
  258.         function b2evoCloseAllTags() {
  259.             var count = b2evoOpenTags.length;
  260.             for (o = 0; o < count; o++) {
  261.                 b2evoInsertTag(b2evoCanvas, b2evoOpenTags[b2evoOpenTags.length - 1]);
  262.             }
  263.         }
  264.  
  265.         function b2evoToolbar() {
  266.             document.write('<div>');
  267.             for (var i = 0; i < b2evoButtons.length; i++) {
  268.                 b2evoShowButton(b2evoButtons[i], i);
  269.             }
  270.             document.write('<input type="button" id="b2evo_close" class="quicktags" onclick="b2evoCloseAllTags();" title="<?php echo T_('Close all tags'?>" value="X" />');
  271.             document.write('</div>');
  272.         }
  273.  
  274.         // insertion code
  275.         function b2evoInsertTag(myField, i)
  276.         {
  277.             if( typeof b2evo_Callbacks == 'object' )
  278.             { // see if there's a callback registered that should handle this:
  279.                 if (!b2evoCheckOpenTags(i) || b2evoButtons[i].tagEnd == '') {
  280.                     if( b2evo_Callbacks.trigger_callback("insert_raw_into_"+myField.id, b2evoButtons[i].tagStart) )
  281.                     {
  282.                         b2evoAddTag(i);
  283.                         return;
  284.                     }
  285.                 }
  286.                 else {
  287.                     if( b2evo_Callbacks.trigger_callback("insert_raw_into_"+myField.id, b2evoButtons[i].tagEnd) )
  288.                     {
  289.                         b2evoRemoveTag(i);
  290.                         return;
  291.                     }
  292.                 }
  293.  
  294.             }
  295.  
  296.             //IE support
  297.             if (document.selection)
  298.             {
  299.                 myField.focus();
  300.                     sel = document.selection.createRange();
  301.                 if (sel.text.length > 0) {
  302.                     sel.text = b2evoButtons[i].tagStart + sel.text + b2evoButtons[i].tagEnd;
  303.                 }
  304.                 else {
  305.                     if (!b2evoCheckOpenTags(i) || b2evoButtons[i].tagEnd == '') {
  306.                         sel.text = b2evoButtons[i].tagStart;
  307.                         b2evoAddTag(i);
  308.                     }
  309.                     else {
  310.                         sel.text = b2evoButtons[i].tagEnd;
  311.                         b2evoRemoveTag(i);
  312.                     }
  313.                 }
  314.                 myField.focus();
  315.             }
  316.             //MOZILLA/NETSCAPE support
  317.             else if (myField.selectionStart || myField.selectionStart == '0')
  318.             {
  319.                 var startPos = myField.selectionStart;
  320.                 var endPos = myField.selectionEnd;
  321.                 var cursorPos = endPos;
  322.  
  323.                 var scrollTop, scrollLeft;
  324.                 if( myField.type == 'textarea' && typeof myField.scrollTop != 'undefined' )
  325.                 { // remember old position
  326.                     scrollTop = myField.scrollTop;
  327.                     scrollLeft = myField.scrollLeft;
  328.                 }
  329.  
  330.                 if (startPos != endPos)
  331.                 { // some text selected
  332.                     myField.value = myField.value.substring(0, startPos)
  333.                                                 + b2evoButtons[i].tagStart
  334.                                                 + myField.value.substring(startPos, endPos)
  335.                                                 + b2evoButtons[i].tagEnd
  336.                                                 + myField.value.substring(endPos, myField.value.length);
  337.                     cursorPos += b2evoButtons[i].tagStart.length + b2evoButtons[i].tagEnd.length;
  338.                 }
  339.                 else
  340.                 {
  341.                     if (!b2evoCheckOpenTags(i) || b2evoButtons[i].tagEnd == '')
  342.                     {
  343.                         myField.value = myField.value.substring(0, startPos)
  344.                                                     + b2evoButtons[i].tagStart
  345.                                                     + myField.value.substring(endPos, myField.value.length);
  346.                         b2evoAddTag(i);
  347.                         cursorPos = startPos + b2evoButtons[i].tagStart.length;
  348.                     }
  349.                     else
  350.                     {
  351.                         myField.value = myField.value.substring(0, startPos)
  352.                                                     + b2evoButtons[i].tagEnd
  353.                                                     + myField.value.substring(endPos, myField.value.length);
  354.                         b2evoRemoveTag(i);
  355.                         cursorPos = startPos + b2evoButtons[i].tagEnd.length;
  356.                     }
  357.                 }
  358.  
  359.                 if( typeof scrollTop != 'undefined' )
  360.                 { // scroll to old position
  361.                     myField.scrollTop = scrollTop;
  362.                     myField.scrollLeft = scrollLeft;
  363.                 }
  364.  
  365.                 myField.focus();
  366.                 myField.selectionStart = cursorPos;
  367.                 myField.selectionEnd = cursorPos;
  368.             }
  369.             else
  370.             { // Browser not especially supported
  371.                 if (!b2evoCheckOpenTags(i) || b2evoButtons[i].tagEnd == '') {
  372.                     myField.value += b2evoButtons[i].tagStart;
  373.                     b2evoAddTag(i);
  374.                 }
  375.                 else {
  376.                     myField.value += b2evoButtons[i].tagEnd;
  377.                     b2evoRemoveTag(i);
  378.                 }
  379.                 myField.focus();
  380.             }
  381.         }
  382.  
  383.         function b2evoInsertContent(myField, myValue) {
  384.             if( typeof b2evo_Callbacks == 'object' )
  385.             { // see if there's a callback registered that should handle this:
  386.                 if( b2evo_Callbacks.trigger_callback("insert_raw_into_"+myField.id, myValue) )
  387.                 {
  388.                     return;
  389.                 }
  390.             }
  391.  
  392.             //IE support
  393.             if (document.selection) {
  394.                 myField.focus();
  395.                 sel = document.selection.createRange();
  396.                 sel.text = myValue;
  397.                 myField.focus();
  398.             }
  399.             //MOZILLA/NETSCAPE support
  400.             else if (myField.selectionStart || myField.selectionStart == '0') {
  401.                 var startPos = myField.selectionStart;
  402.                 var endPos = myField.selectionEnd;
  403.                 myField.value = myField.value.substring(0, startPos)
  404.                                             + myValue
  405.                                             + myField.value.substring(endPos, myField.value.length);
  406.                 myField.focus();
  407.                 myField.selectionStart = startPos + myValue.length;
  408.                 myField.selectionEnd = startPos + myValue.length;
  409.             } else {
  410.                 myField.value += myValue;
  411.                 myField.focus();
  412.             }
  413.         }
  414.  
  415.         function b2evoInsertLink(myField, i, defaultValue) {
  416.             if (!defaultValue) {
  417.                 defaultValue = 'http://';
  418.             }
  419.             if (!b2evoCheckOpenTags(i)) {
  420.                 var URL = prompt('<?php echo T_('URL'?>:' ,defaultValue);
  421.                 if (URL) {
  422.                     b2evoButtons[i].tagStart = '<a href="' + URL + '">';
  423.                     b2evoInsertTag(myField, i);
  424.                 }
  425.             }
  426.             else {
  427.                 b2evoInsertTag(myField, i);
  428.             }
  429.         }
  430.  
  431.         function b2evoInsertImage(myField) {
  432.             var myValue = prompt('<?php echo T_('URL'?>:', 'http://');
  433.             if (myValue) {
  434.                 myValue = '<img src="'
  435.                         + myValue
  436.                         + '" alt="' + prompt('<?php echo T_('ALTernate text'?>:', '')
  437.                         + '" title="' + prompt('<?php echo T_('Title'?>:', '')
  438.                         + '" />';
  439.                 b2evoInsertContent(myField, myValue);
  440.             }
  441.         }
  442.         //]]>
  443.         </script>
  444.  
  445.         <div class="edit_toolbar"><script type="text/javascript">b2evoToolbar();</script></div>
  446.  
  447.         <?php
  448.         return true;
  449.     }
  450. }
  451.  
  452. /*
  453.  * $Log: _quicktags.plugin.php,v $
  454.  * Revision 1.23.2.3  2006/11/04 19:55:12  fplanque
  455.  * Reinjected old Log blocks. Removing them from CVS was a bad idea -- especially since Daniel has decided branch 1.9 was his HEAD...
  456.  *
  457.  * Revision 1.23  2006/07/16 23:04:05  fplanque
  458.  * Most plugins should keep as quiet as possible in simple mode.
  459.  * Smilies are about the only thing simple enough for simple mode.
  460.  *
  461.  * Revision 1.22  2006/07/12 21:13:17  blueyed
  462.  * Javascript callback handler (e.g., for interaction of WYSIWYG editors with toolbar plugins)
  463.  *
  464.  * Revision 1.21  2006/07/10 20:19:30  blueyed
  465.  * Fixed PluginInit behaviour. It now gets called on both installed and non-installed Plugins, but with the "is_installed" param appropriately set.
  466.  *
  467.  * Revision 1.20  2006/07/07 21:26:49  blueyed
  468.  * Bumped to 1.9-dev
  469.  *
  470.  * Revision 1.19  2006/07/06 19:56:29  fplanque
  471.  * no message
  472.  *
  473.  * Revision 1.18  2006/06/26 23:10:24  fplanque
  474.  * minor / doc
  475.  *
  476.  * Revision 1.17  2006/06/24 05:19:39  smpdawg
  477.  * Fixed various javascript warnings and errors.
  478.  * Spelling corrections.
  479.  * Fixed PHP warnings.
  480.  *
  481.  * Revision 1.16  2006/06/16 21:30:57  fplanque
  482.  * Started clean numbering of plugin versions (feel free do add dots...)
  483.  *
  484.  * Revision 1.15  2006/05/30 20:31:18  blueyed
  485.  * typo
  486.  *
  487.  * Revision 1.14  2006/05/30 19:39:55  fplanque
  488.  * plugin cleanup
  489.  *
  490.  * Revision 1.13  2006/03/12 23:09:28  fplanque
  491.  * doc cleanup
  492.  *
  493.  * Revision 1.12  2005/12/22 23:13:40  blueyed
  494.  * Plugins' API changed and handling optimized
  495.  *
  496.  * Revision 1.11  2005/11/21 18:16:29  fplanque
  497.  * okay, a TWO liner :P
  498.  *
  499.  */
  500. ?>

Documentation generated on Tue, 18 Dec 2007 22:51:10 +0100 by phpDocumentor 1.4.0