/plugins/editors/codemirror/codemirror.php
PHP | 264 lines | 144 code | 38 blank | 82 comment | 14 complexity | 00468cdc1e1e964f3ffd1278a0f8a560 MD5 | raw file
Possible License(s): LGPL-2.1
1<?php 2/** 3 * @package Joomla.Plugin 4 * @subpackage Editors.codemirror 5 * 6 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10defined('_JEXEC') or die; 11 12/** 13 * CodeMirror Editor Plugin. 14 * 15 * @package Joomla.Plugin 16 * @subpackage Editors.codemirror 17 * @since 1.6 18 */ 19class 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 $uncompressed = JFactory::getApplication()->getCfg('debug') ? '-uncompressed' : ''; 35 JHtml::_('script', $this->_basePath . 'js/codemirror'.$uncompressed.'.js', false, false, false, false); 36 JHtml::_('stylesheet', $this->_basePath . 'css/codemirror.css'); 37 38 return ''; 39 } 40 41 /** 42 * Copy editor content to form field. 43 * 44 * @param string $id The id of the editor field. 45 * 46 * @return string Javascript 47 */ 48 public function onSave($id) 49 { 50 return "document.getElementById('$id').value = Joomla.editors.instances['$id'].getCode();\n"; 51 } 52 53 /** 54 * Get the editor content. 55 * 56 * @param string $id The id of the editor field. 57 * 58 * @return string Javascript 59 */ 60 public function onGetContent($id) 61 { 62 return "Joomla.editors.instances['$id'].getCode();\n"; 63 } 64 65 /** 66 * Set the editor content. 67 * 68 * @param string $id The id of the editor field. 69 * @param string $content The content to set. 70 * 71 * @return string Javascript 72 */ 73 public function onSetContent($id, $content) 74 { 75 return "Joomla.editors.instances['$id'].setCode($content);\n"; 76 } 77 78 /** 79 * Adds the editor specific insert method. 80 * 81 * @return boolean 82 */ 83 public function onGetInsertMethod() 84 { 85 static $done = false; 86 87 // Do this only once. 88 if (!$done) 89 { 90 $done = true; 91 $doc = JFactory::getDocument(); 92 $js = "\tfunction jInsertEditorText(text, editor) { 93 Joomla.editors.instances[editor].replaceSelection(text);\n 94 }"; 95 $doc->addScriptDeclaration($js); 96 } 97 98 return true; 99 } 100 101 /** 102 * Display the editor area. 103 * 104 * @param string $name The control name. 105 * @param string $html The contents of the text area. 106 * @param string $width The width of the text area (px or %). 107 * @param string $height The height of the text area (px or %). 108 * @param int $col The number of columns for the textarea. 109 * @param int $row The number of rows for the textarea. 110 * @param boolean $buttons True and the editor buttons will be displayed. 111 * @param string $id An optional ID for the textarea (note: since 1.6). If not supplied the name is used. 112 * @param string $asset 113 * @param object $author 114 * @param array $params Associative array of editor parameters. 115 * 116 * @return string HTML 117 */ 118 public function onDisplay($name, $content, $width, $height, $col, $row, $buttons = true, $id = null, $asset = null, $author = null, $params = array()) 119 { 120 if (empty($id)) { 121 $id = $name; 122 } 123 124 // Only add "px" to width and height if they are not given as a percentage 125 if (is_numeric($width)) { 126 $width .= 'px'; 127 } 128 129 if (is_numeric($height)) { 130 $height .= 'px'; 131 } 132 133 // Must pass the field id to the buttons in this editor. 134 $buttons = $this->_displayButtons($id, $buttons, $asset, $author); 135 136 $compressed = JFactory::getApplication()->getCfg('debug') ? '-uncompressed' : ''; 137 138 // Default syntax 139 $parserFile = 'parsexml.js'; 140 $styleSheet = array('xmlcolors.css'); 141 142 // Look if we need special syntax coloring. 143 $syntax = JFactory::getApplication()->getUserState('editor.source.syntax'); 144 145 if ($syntax) { 146 switch($syntax) 147 { 148 case 'css': 149 $parserFile = 'parsecss.js'; 150 $styleSheet = array('csscolors.css'); 151 break; 152 153 case 'js': 154 $parserFile = array('tokenizejavascript.js', 'parsejavascript.js'); 155 $styleSheet = array('jscolors.css'); 156 break; 157 158 case 'html': 159 $parserFile = array('parsexml.js', 'parsecss.js', 'tokenizejavascript.js', 'parsejavascript.js', 'parsehtmlmixed.js'); 160 $styleSheet = array('xmlcolors.css', 'jscolors.css', 'csscolors.css'); 161 break; 162 163 case 'php': 164 $parserFile = array('parsexml.js', 'parsecss.js', 'tokenizejavascript.js', 'parsejavascript.js', 'tokenizephp.js', 'parsephp.js', 'parsephphtmlmixed.js'); 165 $styleSheet = array('xmlcolors.css', 'jscolors.css', 'csscolors.css', 'phpcolors.css'); 166 break; 167 168 default: 169 break; 170 } //switch 171 } 172 173 foreach ($styleSheet as &$style) 174 { 175 $style = JURI::root(true).'/'.$this->_basePath.'css/'.$style; 176 } 177 178 $options = new stdClass; 179 180 $options->basefiles = array('basefiles'.$compressed.'.js'); 181 $options->path = JURI::root(true).'/'.$this->_basePath.'js/'; 182 $options->parserfile = $parserFile; 183 $options->stylesheet = $styleSheet; 184 $options->height = $height; 185 $options->width = $width; 186 $options->continuousScanning = 500; 187 188 if ($this->params->get('linenumbers', 0)) { 189 $options->lineNumbers = true; 190 $options->textWrapping = false; 191 } 192 193 if ($this->params->get('tabmode', '') == 'shift') { 194 $options->tabMode = 'shift'; 195 } 196 197 $html = array(); 198 $html[] = "<textarea name=\"$name\" id=\"$id\" cols=\"$col\" rows=\"$row\">$content</textarea>"; 199 $html[] = $buttons; 200 $html[] = '<script type="text/javascript">'; 201 $html[] = '(function() {'; 202 $html[] = 'var editor = CodeMirror.fromTextArea("'.$id.'", '.json_encode($options).');'; 203 $html[] = 'Joomla.editors.instances[\''.$id.'\'] = editor;'; 204 $html[] = '})()'; 205 $html[] = '</script>'; 206 207 return implode("\n", $html); 208 } 209 210 /** 211 * Displays the editor buttons. 212 * 213 * @param string $name 214 * @param mixed $buttons [array with button objects | boolean true to display buttons] 215 * 216 * @return string HTML 217 */ 218 protected function _displayButtons($name, $buttons, $asset, $author) 219 { 220 // Load modal popup behavior 221 JHtml::_('behavior.modal', 'a.modal-button'); 222 223 $args['name'] = $name; 224 $args['event'] = 'onGetInsertMethod'; 225 226 $html = array(); 227 $results[] = $this->update($args); 228 229 foreach ($results as $result) 230 { 231 if (is_string($result) && trim($result)) { 232 $html[] = $result; 233 } 234 } 235 236 if (is_array($buttons) || (is_bool($buttons) && $buttons)) { 237 $results = $this->_subject->getButtons($name, $buttons, $asset, $author); 238 239 // This will allow plugins to attach buttons or change the behavior on the fly using AJAX 240 $html[] = '<div id="editor-xtd-buttons">'; 241 $html[] = '<div class="btn-toolbar">'; 242 243 foreach ($results as $button) 244 { 245 // Results should be an object 246 if ($button->get('name')) { 247 $modal = ($button->get('modal')) ? 'class="modal-button btn"' : null; 248 $href = ($button->get('link')) ? ' class="btn" href="'.JURI::base().$button->get('link').'"' : null; 249 $onclick = ($button->get('onclick')) ? 'onclick="'.$button->get('onclick').'"' : null; 250 $title = ($button->get('title')) ? $button->get('title') : $button->get('text'); 251 $html[] = '<a '.$modal.' title="'.$title.'" '.$href.' '.$onclick.' rel="'.$button->get('options').'">'; 252 $html[] = '<i class="icon-' . $button->get('name'). '"></i> '; 253 $html[] = $button->get('text').'</a>'; 254 } 255 } 256 257 $html[] = '</div>'; 258 $html[] = '</div>'; 259 $return .= "<div class=\"clearfix\"></div>\n"; 260 } 261 262 return implode("\n", $html); 263 } 264}