b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (0.9.x) [ class tree: plugins ] [ index: plugins ] [ all elements ]

Source for file _quicktags.toolbar.php

Documentation is available at _quicktags.toolbar.php

  1. <?php
  2. /**
  3.  * This file implements the Quicktahs 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-2005 by Francois PLANQUE - {@link http://fplanque.net/}
  12.  *
  13.  * @package plugins
  14.  */
  15. if!defined('DB_USER') ) die'Please, do not access this page directly.' );
  16.  
  17. /**
  18.  * Includes:
  19.  */
  20. require_once dirname(__FILE__).'/../toolbar.class.php';
  21.  
  22. /**
  23.  * @package plugins
  24.  */
  25. {
  26.     /**
  27.      * Should be toolbar be displayed?
  28.      */
  29.     var $display = true;
  30.  
  31.     var $code = 'b2evQTag';
  32.     var $name = 'Quick Tags';
  33.     var $priority = 30;
  34.     var $short_desc;
  35.     var $long_desc;
  36.  
  37.  
  38.     /**
  39.      * Constructor
  40.      *
  41.      * {@internal quicktags_Toolbarplugin::quicktags_Toolbarplugin(-)}}
  42.      */
  43.     function quicktags_Toolbarplugin()
  44.     {
  45.         $this->short_desc = T_('Easy HTML tags inserting');
  46.         $this->long_desc = T_('No description available');
  47.     }
  48.  
  49.     /**
  50.      * Display the toolbar
  51.      *
  52.      * {@internal quicktags_Toolbarplugin::render(-)}}
  53.      */
  54.     function display()
  55.     {    
  56.         global $is_lynx;
  57.  
  58.         if!$this->display )
  59.         {    // We don't want to show this toolbar
  60.             return false;
  61.         }
  62.  
  63.         if($is_lynx)    
  64.         {    // let's deactivate quicktags on Lynx, because they don't work there.
  65.             return;
  66.         }
  67.  
  68.         ?>
  69.         <script language="JavaScript" type="text/javascript">
  70.         <!--
  71.         var b2evoButtons = new Array();
  72.         var b2evoLinks = new Array();
  73.         var b2evoOpenTags = new Array();
  74.  
  75.         function b2evoButton(id, display, tagStart, tagEnd, access, tit, open) {
  76.             this.id = id;                            // used to name the toolbar button
  77.             this.display = display;        // label on button
  78.             this.tagStart = tagStart; // open tag
  79.             this.tagEnd = tagEnd;            // close tag
  80.             this.access = access;            // access key
  81.             this.tit = tit;                        // title
  82.             this.open = open;                    // set to -1 if tag does not need to be closed
  83.         }
  84.  
  85.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  86.                 'b2evo_ins'
  87.                 ,'ins'
  88.                 ,'<ins>','</ins>'
  89.                 ,'i'
  90.                 ,'<?php echo T_('INSerted [Alt-I]'?>'
  91.             );
  92.  
  93.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  94.                 'b2evo_del'
  95.                 ,'del'
  96.                 ,'<del>','</del>'
  97.                 ,'d'
  98.                 ,'<?php echo T_('DELeted [Alt-D]'?>'
  99.             );
  100.  
  101.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  102.                 'b2evo_strong'
  103.                 ,'str'
  104.                 ,'<strong>','</strong>'
  105.                 ,'s'
  106.                 ,'<?php echo T_('STRong [Alt-S]'?>'
  107.             );
  108.  
  109.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  110.                 'b2evo_em'
  111.                 ,'em'
  112.                 ,'<em>','</em>'
  113.                 ,'e'
  114.                 ,'<?php echo T_('EMphasis [Alt-E]'?>'
  115.             );
  116.  
  117.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  118.                 'b2evo_code'
  119.                 ,'code'
  120.                 ,'<code>','</code>'
  121.                 ,'c'
  122.                 ,'<?php echo T_('CODE [Alt-C]'?>'
  123.             );
  124.  
  125.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  126.                 'b2evo_par'
  127.                 ,'p'
  128.                 ,'<p>','</p>'
  129.                 ,'p'
  130.                 ,'<?php echo T_('Paragraph [Alt-P]'?>'
  131.             );
  132.  
  133.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  134.                 'b2evo_ul'
  135.                 ,'ul'
  136.                 ,'<ul>\n','</ul>\n\n'
  137.                 ,'u'
  138.                 ,'<?php echo T_('Unordered List [Alt-U]'?>'
  139.             );
  140.  
  141.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  142.                 'b2evo_ol'
  143.                 ,'ol'
  144.                 ,'<ol>\n','</ol>\n\n'
  145.                 ,'o'
  146.             );
  147.  
  148.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  149.                 'b2evo_li'
  150.                 ,'li'
  151.                 ,'  <li>','</li>\n'
  152.                 ,'l'
  153.                 ,'<?php echo T_('List Item [Alt-L]'?>'
  154.             );
  155.  
  156.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  157.                 'b2evo_block'
  158.                 ,'block'
  159.                 ,'<blockquote>','</blockquote>'
  160.                 ,'b'
  161.                 ,'<?php echo T_('BLOCKQUOTE [Alt-B]'?>'
  162.             );
  163.  
  164.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  165.                 'b2evo_img'
  166.                 ,'img'
  167.                 ,'',''
  168.                 ,'g'
  169.                 ,'<?php echo T_('IMaGe [Alt-G]'?>'
  170.                 ,-1
  171.             ); // special case
  172.  
  173.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  174.                 'b2evo_link'
  175.                 ,'link'
  176.                 ,'','</a>'
  177.                 ,'a'
  178.                 ,'<?php echo T_('A href [Alt-A]'?>'
  179.             ); // special case
  180.  
  181.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  182.                 'b2evo_pre'
  183.                 ,'pre'
  184.                 ,'<pre>','</pre>'
  185.                 ,'r'
  186.                 ,'[Alt-R]'
  187.             );
  188.  
  189.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  190.                 'b2evo_more'
  191.                 ,'!M'
  192.                 ,'<!-'+'-more-'+'->',''
  193.                 ,'m'
  194.                 ,'<?php echo T_('More [Alt-M]'?>'
  195.                 ,-1
  196.             );
  197.  
  198.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  199.                 'b2evo_noteaser'
  200.                 ,'!NT'
  201.                 ,'<!-'+'-noteaser-'+'->',''
  202.                 ,'t'
  203.                 ,'<?php echo T_('no teaser [Alt-T]'?>'
  204.                 ,-1
  205.             );
  206.  
  207.         b2evoButtons[b2evoButtons.length] = new b2evoButton(
  208.                 'b2evo_next'
  209.                 ,'!NP'
  210.                 ,'<!-'+'-nextpage-'+'->',''
  211.                 ,'q'
  212.                 ,'<?php echo T_('next page [Alt-Q]'?>'
  213.                 ,-1
  214.             );
  215.  
  216.         function b2evoLink() {
  217.             this.display = '';
  218.             this.URL = '';
  219.             this.newWin = 0;
  220.         }
  221.  
  222.         function b2evoShowButton(button, i)
  223.         {
  224.             if (button.id == 'b2evo_img')
  225.             {
  226.                 document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" title="' + button.tit + '" class="quicktags" onclick="b2evoInsertImage(b2evoCanvas);" value="' + button.display + '" />');
  227.             }
  228.             else if (button.id == 'b2evo_link')
  229.             {
  230.                 document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" title="' + button.tit + '" class="quicktags" onclick="b2evoInsertLink(b2evoCanvas, ' + i + ');" value="' + button.display + '" />');
  231.             }
  232.             else
  233.             {
  234.                 document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" title="' + button.tit + '" class="quicktags" onclick="b2evoInsertTag(b2evoCanvas, ' + i + ');" value="' + button.display + '"  />');
  235.             }
  236.         }
  237.  
  238.         function b2evoAddTag(button) {
  239.             if (b2evoButtons[button].tagEnd != '') {
  240.                 b2evoOpenTags[b2evoOpenTags.length] = button;
  241.                 document.getElementById(b2evoButtons[button].id).value = '/' + document.getElementById(b2evoButtons[button].id).value;
  242.             }
  243.         }
  244.  
  245.         function b2evoRemoveTag(button) {
  246.             for (i = 0; i < b2evoOpenTags.length; i++) {
  247.                 if (b2evoOpenTags[i] == button) {
  248.                     b2evoOpenTags.splice(i, 1);
  249.                     document.getElementById(b2evoButtons[button].id).value =         document.getElementById(b2evoButtons[button].id).value.replace('/', '');
  250.                 }
  251.             }
  252.         }
  253.  
  254.         function b2evoCheckOpenTags(button) {
  255.             var tag = 0;
  256.             for (i = 0; i < b2evoOpenTags.length; i++) {
  257.                 if (b2evoOpenTags[i] == button) {
  258.                     tag++;
  259.                 }
  260.             }
  261.             if (tag > 0) {
  262.                 return true; // tag found
  263.             }
  264.             else {
  265.                 return false; // tag not found
  266.             }
  267.         }
  268.  
  269.         function b2evoCloseAllTags() {
  270.             var count = b2evoOpenTags.length;
  271.             for (o = 0; o < count; o++) {
  272.                 b2evoInsertTag(b2evoCanvas, b2evoOpenTags[b2evoOpenTags.length - 1]);
  273.             }
  274.         }
  275.  
  276.         function b2evoToolbar() {
  277.             document.write('<div>');
  278.             for (i = 0; i < b2evoButtons.length; i++) {
  279.                 b2evoShowButton(b2evoButtons[i], i);
  280.             }
  281.             document.write('<input type="button" id="b2evo_close" class="quicktags" onclick="b2evoCloseAllTags();" title="<?php echo T_('Close all tags'?>" value="X" />');
  282.             document.write('</div>');
  283.         }
  284.  
  285.         // insertion code
  286.         function b2evoInsertTag(myField, i)
  287.         {
  288.             //IE support
  289.             if (document.selection)
  290.             {
  291.                 myField.focus();
  292.                     sel = document.selection.createRange();
  293.                 if (sel.text.length > 0) {
  294.                     sel.text = b2evoButtons[i].tagStart + sel.text + b2evoButtons[i].tagEnd;
  295.                 }
  296.                 else {
  297.                     if (!b2evoCheckOpenTags(i) || b2evoButtons[i].tagEnd == '') {
  298.                         sel.text = b2evoButtons[i].tagStart;
  299.                         b2evoAddTag(i);
  300.                     }
  301.                     else {
  302.                         sel.text = b2evoButtons[i].tagEnd;
  303.                         b2evoRemoveTag(i);
  304.                     }
  305.                 }
  306.                 myField.focus();
  307.             }
  308.             //MOZILLA/NETSCAPE support
  309.             else if (myField.selectionStart || myField.selectionStart == '0')
  310.             {
  311.                 var startPos = myField.selectionStart;
  312.                 var endPos = myField.selectionEnd;
  313.                 var cursorPos = endPos;
  314.  
  315.                 var scrollTop, scrollLeft;
  316.                 if( myField.type == 'textarea' && typeof myField.scrollTop != 'undefined' )
  317.                 { // remember old position
  318.                     scrollTop = myField.scrollTop;
  319.                     scrollLeft = myField.scrollLeft;
  320.                 }
  321.  
  322.                 if (startPos != endPos)
  323.                 { // some text selected
  324.                     myField.value = myField.value.substring(0, startPos)
  325.                                                 + b2evoButtons[i].tagStart
  326.                                                 + myField.value.substring(startPos, endPos)
  327.                                                 + b2evoButtons[i].tagEnd
  328.                                                 + myField.value.substring(endPos, myField.value.length);
  329.                     cursorPos += b2evoButtons[i].tagStart.length + b2evoButtons[i].tagEnd.length;
  330.                 }
  331.                 else
  332.                 {
  333.                     if (!b2evoCheckOpenTags(i) || b2evoButtons[i].tagEnd == '')
  334.                     {
  335.                         myField.value = myField.value.substring(0, startPos)
  336.                                                     + b2evoButtons[i].tagStart
  337.                                                     + myField.value.substring(endPos, myField.value.length);
  338.                         b2evoAddTag(i);
  339.                         cursorPos = startPos + b2evoButtons[i].tagStart.length;
  340.                     }
  341.                     else
  342.                     {
  343.                         myField.value = myField.value.substring(0, startPos)
  344.                                                     + b2evoButtons[i].tagEnd
  345.                                                     + myField.value.substring(endPos, myField.value.length);
  346.                         b2evoRemoveTag(i);
  347.                         cursorPos = startPos + b2evoButtons[i].tagEnd.length;
  348.                     }
  349.                 }
  350.  
  351.                 if( typeof scrollTop != 'undefined' )
  352.                 { // scroll to old position
  353.                     myField.scrollTop = scrollTop;
  354.                     myField.scrollLeft = scrollLeft;
  355.                 }
  356.  
  357.                 myField.focus();
  358.                 myField.selectionStart = cursorPos;
  359.                 myField.selectionEnd = cursorPos;
  360.             }
  361.             else
  362.             { // Browser not especially supported
  363.                 if (!b2evoCheckOpenTags(i) || b2evoButtons[i].tagEnd == '') {
  364.                     myField.value += b2evoButtons[i].tagStart;
  365.                     b2evoAddTag(i);
  366.                 }
  367.                 else {
  368.                     myField.value += b2evoButtons[i].tagEnd;
  369.                     b2evoRemoveTag(i);
  370.                 }
  371.                 myField.focus();
  372.             }
  373.         }
  374.         
  375.         function b2evoInsertContent(myField, myValue) {
  376.             //IE support
  377.             if (document.selection) {
  378.                 myField.focus();
  379.                 sel = document.selection.createRange();
  380.                 sel.text = myValue;
  381.                 myField.focus();
  382.             }
  383.             //MOZILLA/NETSCAPE support
  384.             else if (myField.selectionStart || myField.selectionStart == '0') {
  385.                 var startPos = myField.selectionStart;
  386.                 var endPos = myField.selectionEnd;
  387.                 myField.value = myField.value.substring(0, startPos)
  388.                                             + myValue
  389.                                             + myField.value.substring(endPos, myField.value.length);
  390.                 myField.focus();
  391.                 myField.selectionStart = startPos + myValue.length;
  392.                 myField.selectionEnd = startPos + myValue.length;
  393.             } else {
  394.                 myField.value += myValue;
  395.                 myField.focus();
  396.             }
  397.         }
  398.         
  399.         function b2evoInsertLink(myField, i, defaultValue) {
  400.             if (!defaultValue) {
  401.                 defaultValue = 'http://';
  402.             }
  403.             if (!b2evoCheckOpenTags(i)) {
  404.                 var URL = prompt('<?php echo T_('URL'?>:' ,defaultValue);
  405.                 if (URL) {
  406.                     b2evoButtons[i].tagStart = '<a href="' + URL + '">';
  407.                     b2evoInsertTag(myField, i);
  408.                 }
  409.             }
  410.             else {
  411.                 b2evoInsertTag(myField, i);
  412.             }
  413.         }
  414.         
  415.         function b2evoInsertImage(myField) {
  416.             var myValue = prompt('<?php echo T_('URL'?>:', 'http://');
  417.             if (myValue) {
  418.                 myValue = '<img src="'
  419.                         + myValue
  420.                         + '" alt="' + prompt('<?php echo T_('ALTernate text'?>:', '')
  421.                         + '" title="' + prompt('<?php echo T_('Title'?>:', '')
  422.                         + '" />';
  423.                 b2evoInsertContent(myField, myValue);
  424.             }
  425.         }
  426.     // -->
  427.         </script>
  428.         <div class="edit_toolbar"><script language="JavaScript" type="text/javascript">b2evoToolbar();</script></div>
  429.         <?php        
  430.     }
  431. }
  432.  
  433. // Register the plugin:
  434. $this->registernew quicktags_Toolbarplugin() );
  435.  
  436. ?>

Documentation generated on Tue, 20 May 2008 01:55:39 +0200 by phpDocumentor 1.4.2