/plugins/editors/codemirror/codemirror.php

https://bitbucket.org/eternaware/joomus · PHP · 264 lines · 144 code · 38 blank · 82 comment · 14 complexity · 00468cdc1e1e964f3ffd1278a0f8a560 MD5 · raw file

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