Source for file codemirror.php

Documentation is available at codemirror.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Editors.codemirror
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * CodeMirror Editor Plugin.
  14.  *
  15.  * @package     Joomla.Plugin
  16.  * @subpackage  Editors.codemirror
  17.  * @since       1.6
  18.  */
  19. class PlgEditorCodemirror extends JPlugin
  20. {
  21.     /**
  22.      * Base path for editor files
  23.      */
  24.     protected $_basePath = 'media/editors/codemirror/';
  25.  
  26.     /**
  27.      * Initialises the Editor.
  28.      *
  29.      * @return  string  JavaScript Initialization string.
  30.      */
  31.     public function onInit()
  32.     {
  33.         JHtml::_('behavior.framework');
  34.         JHtml::_('script'$this->_basePath . 'js/codemirror.js'falsefalsefalsefalse);
  35.         JHtml::_('script'$this->_basePath . 'js/fullscreen.js'falsefalsefalsefalse);
  36.         JHtml::_('stylesheet'$this->_basePath . 'css/codemirror.css');
  37.         JHtml::_('stylesheet'$this->_basePath . 'css/configuration.css');
  38.  
  39.         return '';
  40.     }
  41.  
  42.     /**
  43.      * Copy editor content to form field.
  44.      *
  45.      * @param   string  $id  The id of the editor field.
  46.      *
  47.      * @return  string Javascript
  48.      */
  49.     public function onSave($id)
  50.     {
  51.         return "document.getElementById('$id').value = Joomla.editors.instances['$id'].getValue();\n";
  52.     }
  53.  
  54.     /**
  55.      * Get the editor content.
  56.      *
  57.      * @param   string  $id  The id of the editor field.
  58.      *
  59.      * @return  string  Javascript
  60.      */
  61.     public function onGetContent($id)
  62.     {
  63.         return "Joomla.editors.instances['$id'].getValue();\n";
  64.     }
  65.  
  66.     /**
  67.      * Set the editor content.
  68.      *
  69.      * @param   string  $id       The id of the editor field.
  70.      * @param   string  $content  The content to set.
  71.      *
  72.      * @return  string  Javascript
  73.      */
  74.     public function onSetContent($id$content)
  75.     {
  76.         return "Joomla.editors.instances['$id'].setValue($content);\n";
  77.     }
  78.  
  79.     /**
  80.      * Adds the editor specific insert method.
  81.      *
  82.      * @return  boolean 
  83.      */
  84.     public function onGetInsertMethod()
  85.     {
  86.         static $done false;
  87.  
  88.         // Do this only once.
  89.         if (!$done)
  90.         {
  91.             $done true;
  92.             $doc JFactory::getDocument();
  93.             $js "\tfunction jInsertEditorText(text, editor)
  94.                 {
  95.                     Joomla.editors.instances[editor].replaceSelection(text);\n
  96.             }";
  97.             $doc->addScriptDeclaration($js);
  98.         }
  99.  
  100.         return true;
  101.     }
  102.  
  103.     /**
  104.      * Display the editor area.
  105.      *
  106.      * @param   string   $name     The control name.
  107.      * @param   string   $content  The contents of the text area.
  108.      * @param   string   $width    The width of the text area (px or %).
  109.      * @param   string   $height   The height of the text area (px or %).
  110.      * @param   integer  $col      The number of columns for the textarea.
  111.      * @param   integer  $row      The number of rows for the textarea.
  112.      * @param   boolean  $buttons  True and the editor buttons will be displayed.
  113.      * @param   string   $id       An optional ID for the textarea (note: since 1.6). If not supplied the name is used.
  114.      * @param   string   $asset    Unused
  115.      * @param   object   $author   Unused
  116.      * @param   array    $params   Associative array of editor parameters.
  117.      *
  118.      * @return  string  HTML Output
  119.      */
  120.     public function onDisplay($name$content$width$height$col$row$buttons true$id null$asset null$author null$params array())
  121.     {
  122.         if (empty($id))
  123.         {
  124.             $id $name;
  125.         }
  126.  
  127.         // Only add "px" to width and height if they are not given as a percentage
  128.         if (is_numeric($width))
  129.         {
  130.             $width .= 'px';
  131.         }
  132.  
  133.         if (is_numeric($height))
  134.         {
  135.             $height .= 'px';
  136.         }
  137.  
  138.         // Must pass the field id to the buttons in this editor.
  139.         $buttons $this->_displayButtons($id$buttons$asset$author);
  140.  
  141.         // Look if we need special syntax coloring.
  142.         $syntax $this->params->get('syntax''php');
  143.  
  144.         if ($syntax)
  145.         {
  146.             switch ($syntax)
  147.             {
  148.                 case 'css':
  149.                     $parserFile        array('css.js''closebrackets.js');
  150.                     $mode              'text/css';
  151.                     $autoCloseBrackets true;
  152.                     $autoCloseTags     false;
  153.                     $fold              true;
  154.                     $matchTags         false;
  155.                     $matchBrackets     true;
  156.                     JHtml::_('script'$this->_basePath 'js/brace-fold.js'falsefalsefalsefalse);
  157.                     break;
  158.  
  159.                 case 'ini':
  160.                     $parserFile        array('css.js');
  161.                     $mode              'text/css';
  162.                     $autoCloseBrackets false;
  163.                     $autoCloseTags     false;
  164.                     $fold              false;
  165.                     $matchTags         false;
  166.                     $matchBrackets     false;
  167.                     break;
  168.  
  169.                 case 'xml':
  170.                     $parserFile        array('xml.js''closetag.js');
  171.                     $mode              'application/xml';
  172.                     $fold              true;
  173.                     $autoCloseBrackets false;
  174.                     $autoCloseTags     true;
  175.                     $matchTags         true;
  176.                     $matchBrackets     false;
  177.                     JHtml::_('script'$this->_basePath 'js/xml-fold.js'falsefalsefalsefalse);
  178.                     break;
  179.  
  180.                 case 'js':
  181.                     $parserFile        array('javascript.js''closebrackets.js');
  182.                     $mode              'text/javascript';
  183.                     $autoCloseBrackets true;
  184.                     $autoCloseTags     false;
  185.                     $fold              true;
  186.                     $matchTags         false;
  187.                     $matchBrackets     true;
  188.                     JHtml::_('script'$this->_basePath 'js/brace-fold.js'falsefalsefalsefalse);
  189.                     break;
  190.  
  191.                 case 'less':
  192.                     $parserFile        array('less.js''css.js''closebrackets.js');
  193.                     $mode              'text/x-less';
  194.                     $autoCloseBrackets true;
  195.                     $autoCloseTags     false;
  196.                     $fold              true;
  197.                     $matchTags         false;
  198.                     $matchBrackets     true;
  199.                     JHtml::_('script'$this->_basePath 'js/brace-fold.js'falsefalsefalsefalse);
  200.                     break;
  201.  
  202.                 case 'php':
  203.                     $parserFile        array('xml.js''clike.js''css.js''javascript.js''htmlmixed.js''php.js''closebrackets.js''closetag.js');
  204.                     $mode              'application/x-httpd-php';
  205.                     $autoCloseBrackets true;
  206.                     $autoCloseTags     true;
  207.                     $fold              true;
  208.                     $matchTags         true;
  209.                     $matchBrackets     true;
  210.                     JHtml::_('script'$this->_basePath 'js/brace-fold.js'falsefalsefalsefalse);
  211.                     JHtml::_('script'$this->_basePath 'js/xml-fold.js'falsefalsefalsefalse);
  212.                     break;
  213.  
  214.                 default:
  215.                     $parserFile        false;
  216.                     $mode              'text/plain';
  217.                     $autoCloseBrackets false;
  218.                     $autoCloseTags     false;
  219.                     $fold              false;
  220.                     $matchTags         false;
  221.                     $matchBrackets     false;
  222.                     break;
  223.             }
  224.         }
  225.  
  226.         if ($parserFile)
  227.         {
  228.             foreach ($parserFile as $file)
  229.             {
  230.                 JHtml::_('script'$this->_basePath 'js/' $filefalsefalsefalsefalse);
  231.             }
  232.         }
  233.  
  234.         $options    new stdClass;
  235.  
  236.         $options->mode $mode;
  237.         $options->smartIndent true;
  238.  
  239.         // Enabled the line numbers.
  240.         if ($this->params->get('lineNumbers'== "1")
  241.         {
  242.             $options->lineNumbers true;
  243.         }
  244.  
  245.         if ($this->params->get('autoFocus'== "1")
  246.         {
  247.             $options->autofocus    true;
  248.         }
  249.  
  250.         if ($this->params->get('autoCloseBrackets'== "1")
  251.         {
  252.             $options->autoCloseBrackets    $autoCloseBrackets;
  253.         }
  254.  
  255.         if ($this->params->get('autoCloseTags'== "1")
  256.         {
  257.             $options->autoCloseTags    $autoCloseTags;
  258.         }
  259.  
  260.         if ($this->params->get('matchTags'== "1")
  261.         {
  262.             $options->matchTags $matchTags;
  263.             JHtml::_('script'$this->_basePath 'js/matchtags.js'falsefalsefalsefalse);
  264.         }
  265.  
  266.         if ($this->params->get('matchBrackets'== "1")
  267.         {
  268.             $options->matchBrackets $matchBrackets;
  269.             JHtml::_('script'$this->_basePath 'js/matchbrackets.js'falsefalsefalsefalse);
  270.         }
  271.  
  272.         if ($this->params->get('marker-gutter'== "1")
  273.         {
  274.             $options->foldGutter $fold;
  275.             $options->gutters array('CodeMirror-linenumbers''CodeMirror-foldgutter''breakpoints');
  276.             JHtml::_('script'$this->_basePath 'js/foldcode.js'falsefalsefalsefalse);
  277.             JHtml::_('script'$this->_basePath 'js/foldgutter.js'falsefalsefalsefalse);
  278.         }
  279.  
  280.         if ($this->params->get('theme'''== 'ambiance')
  281.         {
  282.             $options->theme    'ambiance';
  283.             JHtml::_('stylesheet'$this->_basePath 'css/ambiance.css');
  284.         }
  285.  
  286.         if ($this->params->get('lineWrapping'== "1")
  287.         {
  288.             $options->lineWrapping true;
  289.         }
  290.  
  291.         if ($this->params->get('tabmode'''== 'shift')
  292.         {
  293.             $options->tabMode 'shift';
  294.         }
  295.  
  296.         $html array();
  297.         $html[]    "<textarea name=\"$name\" id=\"$id\" cols=\"$col\" rows=\"$row\">$content</textarea>";
  298.         $html[$buttons;
  299.         $html['<script type="text/javascript">';
  300.         $html['(function() {';
  301.         $html['        var editor = CodeMirror.fromTextArea(document.getElementById("' $id '"), ' json_encode($options');';
  302.         $html['        editor.setOption("extraKeys", {';
  303.         $html['            "Ctrl-Q": function(cm) {';
  304.         $html['                setFullScreen(cm, !isFullScreen(cm));';
  305.         $html['            },';
  306.         $html['            "Esc": function(cm) {';
  307.         $html['                if (isFullScreen(cm)) setFullScreen(cm, false);';
  308.         $html['            }';
  309.         $html['        });';
  310.         $html['        editor.on("gutterClick", function(cm, n) {';
  311.         $html['            var info = cm.lineInfo(n)';
  312.         $html['            cm.setGutterMarker(n, "breakpoints", info.gutterMarkers ? null : makeMarker())';
  313.         $html['        })';
  314.         $html['        function makeMarker() {';
  315.         $html['            var marker = document.createElement("div")';
  316.         $html['            marker.style.color = "#822"';
  317.         $html['            marker.innerHTML = "●"';
  318.         $html['            return marker';
  319.         $html['        }';
  320.         $html['        Joomla.editors.instances[\'' $id '\'] = editor;';
  321.         $html['})()';
  322.         $html['</script>';
  323.  
  324.         return implode("\n"$html);
  325.     }
  326.  
  327.     /**
  328.      * Displays the editor buttons.
  329.      *
  330.      * @param   string  $name     The editor name
  331.      * @param   mixed   $buttons  [array with button objects | boolean true to display buttons]
  332.      * @param   string  $asset    The object asset
  333.      * @param   object  $author   The author.
  334.      *
  335.      * @return  string HTML
  336.      */
  337.     protected function _displayButtons($name$buttons$asset$author)
  338.     {
  339.         // Load modal popup behavior
  340.         JHtml::_('behavior.modal''a.modal-button');
  341.  
  342.         $args['name'$name;
  343.         $args['event''onGetInsertMethod';
  344.  
  345.         $html array();
  346.         $results[$this->update($args);
  347.  
  348.         foreach ($results as $result)
  349.         {
  350.             if (is_string($result&& trim($result))
  351.             {
  352.                 $html[$result;
  353.             }
  354.         }
  355.  
  356.         if (is_array($buttons|| (is_bool($buttons&& $buttons))
  357.         {
  358.             $results $this->_subject->getButtons($name$buttons$asset$author);
  359.  
  360.             // This will allow plugins to attach buttons or change the behavior on the fly using AJAX
  361.             $html['<div id="editor-xtd-buttons">';
  362.             $html['<div class="btn-toolbar">';
  363.  
  364.             foreach ($results as $button)
  365.             {
  366.                 // Results should be an object
  367.                 if ($button->get('name'))
  368.                 {
  369.                     $modal        ($button->get('modal')) 'class="modal-button btn"' null;
  370.                     $href        ($button->get('link')) ' class="btn" href="' JUri::base($button->get('link''"' null;
  371.                     $onclick    ($button->get('onclick')) 'onclick="' $button->get('onclick''"' null;
  372.                     $title      ($button->get('title')) $button->get('title'$button->get('text');
  373.                     $html['<a ' $modal ' title="' $title '" ' $href ' ' $onclick ' rel="' $button->get('options''">';
  374.                     $html['<i class="icon-' $button->get('name''"></i> ';
  375.                     $html[$button->get('text''</a>';
  376.                 }
  377.             }
  378.  
  379.             $html['</div>';
  380.             $html['</div>';
  381.         }
  382.  
  383.         return implode("\n"$html);
  384.     }
  385. }

Documentation generated on Tue, 19 Nov 2013 14:56:03 +0100 by phpDocumentor 1.4.3