BxBaseEditorCLEditor.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolEditor');
  10. /**
  11. * CLEditor editor representation.
  12. * @see BxDolEditor
  13. */
  14. class BxBaseEditorCLEditor extends BxDolEditor
  15. {
  16. /**
  17. * Common initialization params
  18. */
  19. protected static $CONF_COMMON = "
  20. jQuery('{bx_var_selector}').cleditor({
  21. {bx_var_custom_init}
  22. docCSSFile: '{bx_var_css_path}',
  23. bodyStyle: 'cursor:text',
  24. width: '100%'
  25. });
  26. ";
  27. /**
  28. * Standard view initialization params
  29. */
  30. protected static $CONF_STANDARD = "
  31. height: 250,
  32. controls: 'bold italic underline removeformat | style | bullets numbering | outdent indent | alignleft center alignright | undo redo | rule image link unlink | pastetext | source',
  33. ";
  34. /**
  35. * Minimal view initialization params
  36. */
  37. protected static $CONF_MINI = "
  38. height: 150,
  39. controls: 'bold italic underline removeformat | bullets numbering | alignleft center alignright | link unlink image',
  40. ";
  41. /**
  42. * Full view initialization params
  43. */
  44. protected static $CONF_FULL = "
  45. height: 300,
  46. controls: 'bold italic underline removeformat | subscript superscript | style | bullets numbering | outdent indent | alignleft center alignright justify | undo redo | rule image link unlink | pastetext | print source',
  47. ";
  48. protected $_oTemplate;
  49. protected $_bJsCssAdded = false;
  50. public function __construct ($aObject, $oTemplate)
  51. {
  52. parent::__construct ($aObject);
  53. if ($oTemplate)
  54. $this->_oTemplate = $oTemplate;
  55. else
  56. $this->_oTemplate = BxDolTemplate::getInstance();
  57. }
  58. /**
  59. * Attach editor to HTML element, in most cases - textarea.
  60. * @param $sSelector - jQuery selector to attach editor to.
  61. * @param $iViewMode - editor view mode: BX_EDITOR_STANDARD, BX_EDITOR_MINI, BX_EDITOR_FULL
  62. * @param $bDynamicMode - is AJAX mode or not, the HTML with editor area is loaded synamically.
  63. */
  64. public function attachEditor ($sSelector, $iViewMode = BX_EDITOR_STANDARD, $bDynamicMode = false)
  65. {
  66. // set visual mode
  67. switch ($iViewMode) {
  68. case BX_EDITOR_MINI:
  69. $sToolsItems = self::$CONF_MINI;
  70. break;
  71. case BX_EDITOR_FULL:
  72. $sToolsItems = self::$CONF_FULL;
  73. break;
  74. case BX_EDITOR_STANDARD:
  75. default:
  76. $sToolsItems = self::$CONF_STANDARD;
  77. }
  78. // initialize editor
  79. $sInitEditor = $this->_replaceMarkers(self::$CONF_COMMON, array(
  80. 'bx_var_custom_init' => $sToolsItems,
  81. 'bx_var_selector' => bx_js_string($sSelector, BX_ESCAPE_STR_APOS),
  82. 'bx_var_css_path' => bx_js_string($this->_oTemplate->getCssUrl('editor.css'), BX_ESCAPE_STR_APOS),
  83. ));
  84. if ($bDynamicMode) {
  85. $sScript = $this->_oTemplate->addCss(BX_DOL_URL_PLUGINS . 'cleditor/jquery.cleditor.css', true);
  86. $sScript .= "<script>
  87. if ('undefined' == typeof(jQuery(document).cleditor)) {
  88. $.getScript('" . bx_js_string(BX_DOL_URL_BASE . 'inc/js/editor.cleditor.js', BX_ESCAPE_STR_APOS) . "');
  89. $.getScript('" . bx_js_string(BX_DOL_URL_PLUGINS_PUBLIC . 'cleditor/jquery.cleditor.min.js', BX_ESCAPE_STR_APOS) . "', function(data, textStatus, jqxhr) {
  90. $sInitEditor
  91. });
  92. } else {
  93. setTimeout(function () {
  94. $sInitEditor
  95. }, 100);
  96. }
  97. </script>";
  98. } else {
  99. $sScript = "<script>
  100. $(document).ready(function () {
  101. $sInitEditor
  102. });
  103. </script>";
  104. }
  105. return $this->_addJsCss($bDynamicMode) . $sScript;
  106. }
  107. /**
  108. * Add css/js files which are needed for editor display and functionality.
  109. */
  110. protected function _addJsCss($bDynamicMode = false, $sInitEditor = '')
  111. {
  112. if ($bDynamicMode)
  113. return '';
  114. if ($this->_bJsCssAdded)
  115. return '';
  116. $this->_oTemplate->addCss(BX_DOL_URL_PLUGINS_PUBLIC . 'cleditor/jquery.cleditor.css');
  117. $this->_oTemplate->addJs(array('cleditor/jquery.cleditor.min.js', 'editor.cleditor.js'));
  118. $this->_bJsCssAdded = true;
  119. return '';
  120. }
  121. }
  122. /** @} */