PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/editors/codemirror/codemirror.php

https://github.com/joebushi/joomla
PHP | 192 lines | 111 code | 24 blank | 57 comment | 11 complexity | 1556fed955ab7e3c1482b7abd4617f90 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. // no direct access
  8. defined('_JEXEC') or die;
  9. jimport('joomla.plugin.plugin');
  10. /**
  11. * CodeMirror Editor Plugin.
  12. *
  13. * @package Joomla
  14. * @subpackage Editors
  15. * @since 1.6
  16. */
  17. class plgEditorCodemirror extends JPlugin
  18. {
  19. /**
  20. * Base path for editor files
  21. */
  22. protected $_basePath = 'plugins/editors/codemirror/codemirror/';
  23. /**
  24. * Initialises the Editor.
  25. *
  26. * @return string Returns nothing.
  27. */
  28. public function onInit()
  29. {
  30. JHtml::_('core');
  31. JHtml::_('script', 'codemirror.js', $this->_basePath);
  32. JHtml::_('stylesheet', 'codemirror.css', $this->_basePath.'css/');
  33. return '';
  34. }
  35. /**
  36. * Copy editor content to form field.
  37. *
  38. * @param string The id of the editor field.
  39. */
  40. public function onSave($id)
  41. {
  42. return "document.getElementById('$id').value = Joomla.editors.instances['$id'].getCode();\n";
  43. }
  44. /**
  45. * Get the editor content.
  46. *
  47. * @param string The id of the editor field.
  48. */
  49. public function onGetContent($id)
  50. {
  51. return "Joomla.editors.instances['$id'].getCode();\n";
  52. }
  53. /**
  54. * Set the editor content.
  55. *
  56. * @param string The id of the editor field.
  57. * @param string The content to set.
  58. */
  59. public function onSetContent($id, $content)
  60. {
  61. return "Joomla.editors.instances['$id'].setCode($content);\n";
  62. }
  63. /**
  64. */
  65. public function onGetInsertMethod()
  66. {
  67. static $done = false;
  68. // Do this only once.
  69. if (!$done)
  70. {
  71. $done = true;
  72. $doc = JFactory::getDocument();
  73. $js = "\tfunction jInsertEditorText(text, editor) {
  74. Joomla.editors.instances[editor].replaceSelection(text);\n
  75. }";
  76. $doc->addScriptDeclaration($js);
  77. }
  78. return true;
  79. }
  80. /**
  81. * Display the editor area.
  82. *
  83. * @param string The name of the editor area.
  84. * @param string The content of the field.
  85. * @param string The width of the editor area.
  86. * @param string The height of the editor area.
  87. * @param int The number of columns for the editor area.
  88. * @param int The number of rows for the editor area.
  89. * @param boolean True and the editor buttons will be displayed.
  90. * @param string An optional ID for the textarea (note: since 1.6). If not supplied the name is used.
  91. */
  92. public function onDisplay($name, $content, $width, $height, $col, $row, $buttons = true, $id = null)
  93. {
  94. if (empty($id)) {
  95. $id = $name;
  96. }
  97. // Only add "px" to width and height if they are not given as a percentage
  98. if (is_numeric($width)) {
  99. $width .= 'px';
  100. }
  101. if (is_numeric($height)) {
  102. $height .= 'px';
  103. }
  104. // Must pass the field id to the buttons in this editor.
  105. $buttons = $this->_displayButtons($id, $buttons);
  106. $options = new stdClass;
  107. $compressed = JFactory::getApplication()->getCfg('debug') ? '-uncompressed' : '';
  108. $options->basefiles = array('basefiles'.$compressed.'.js');
  109. $options->path = JURI::root(true).'/'.$this->_basePath;
  110. $options->parserfile = 'parsexml.js';
  111. $options->stylesheet = JURI::root(true).'/'.$this->_basePath.'css/xmlcolors.css';
  112. $options->height = $height;
  113. $options->width = $width;
  114. $options->continuousScanning = 500;
  115. if ($this->params->get('linenumbers', 0))
  116. {
  117. $options->lineNumbers = true;
  118. $options->textWrapping = false;
  119. }
  120. if ($this->params->get('tabmode', '') == 'shift') {
  121. $options->tabMode = 'shift';
  122. }
  123. $html = array();
  124. $html[] = "<textarea name=\"$name\" id=\"$id\" cols=\"$col\" rows=\"$row\">$content</textarea>";
  125. $html[] = $buttons;
  126. $html[] = '<script type="text/javascript">';
  127. $html[] = '(function() {';
  128. $html[] = 'var editor = CodeMirror.fromTextArea("'.$id.'", '.json_encode($options).');';
  129. $html[] = 'Joomla.editors.instances[\''.$id.'\'] = editor;';
  130. $html[] = '})()';
  131. $html[] = '</script>';
  132. return implode("\n", $html);
  133. }
  134. protected function _displayButtons($name, $buttons)
  135. {
  136. // Load modal popup behavior
  137. JHtml::_('behavior.modal', 'a.modal-button');
  138. $args['name'] = $name;
  139. $args['event'] = 'onGetInsertMethod';
  140. $return = '';
  141. $results[] = $this->update($args);
  142. foreach ($results as $result)
  143. {
  144. if (is_string($result) && trim($result)) {
  145. $return .= $result;
  146. }
  147. }
  148. if (!empty($buttons))
  149. {
  150. $results = $this->_subject->getButtons($name, $buttons);
  151. // This will allow plugins to attach buttons or change the behavior on the fly using AJAX
  152. $return .= "\n<div id=\"editor-xtd-buttons\">\n";
  153. foreach ($results as $button)
  154. {
  155. // Results should be an object
  156. if ($button->get('name'))
  157. {
  158. $modal = ($button->get('modal')) ? 'class="modal-button"' : null;
  159. $href = ($button->get('link')) ? 'href="'.$button->get('link').'"' : null;
  160. $onclick = ($button->get('onclick')) ? 'onclick="'.$button->get('onclick').'"' : null;
  161. $return .= "<div class=\"button2-left\"><div class=\"".$button->get('name')."\"><a ".$modal." title=\"".$button->get('text')."\" ".$href." ".$onclick." rel=\"".$button->get('options')."\">".$button->get('text')."</a></div></div>\n";
  162. }
  163. }
  164. $return .= "</div>\n";
  165. }
  166. return $return;
  167. }
  168. }