PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/v1.4/javascript/htmlarea/dsrte/lib/dsrte.command.php

http://mightmedia.googlecode.com/
PHP | 184 lines | 85 code | 17 blank | 82 comment | 3 complexity | 5ba3236a830e676899e8a01a447cfaf3 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * Damn Small Rich Text Editor v0.2.4 for jQuery
  4. * by Roi Avidan <roi@avidansoft.com>
  5. * Demo: http://www.avidansoft.com/dsrte/
  6. * Released under the GPL License
  7. *
  8. * Basic Command classes.
  9. */
  10. /**
  11. * Base Command Class.
  12. *
  13. * All command buttons, selects and plugins should inherit and implement at least one
  14. * method (getHTML) of this base class.
  15. */
  16. abstract class dsRTECommand
  17. {
  18. protected $id;
  19. protected $command;
  20. protected $arguments;
  21. protected $title;
  22. protected $icon_offset;
  23. protected $attributes = array();
  24. private $scriptsOnce = array();
  25. /**
  26. * Constructor.
  27. *
  28. * @param String $id
  29. * dsRTE instance ID.
  30. * @param String $command
  31. * Implemented command's name (usually the command value for JS's ExecCommand() function).
  32. * @param String $arguments
  33. * Command arguements (used to identify special commands in the JS part).
  34. * @param String $title
  35. * Tooltip hint string for command buttons or title (first select element) for comboboxes.
  36. * @param Mixed $icon_offset
  37. * If String - then it represents a path to a button icon to be displayed
  38. * If Number - then it's a zero-based offset into the icons.gif image for this command's icon.
  39. */
  40. protected function __construct( $id, $command, $arguments, $title, $icon_offset )
  41. {
  42. $this->id = $id;
  43. $this->command = $command;
  44. $this->arguments = $arguments;
  45. $this->title = $title;
  46. $this->icon_offset = is_numeric( $icon_offset ) ? 18*$icon_offset : $icon_offset;
  47. if ( $command )
  48. $this->attributes[] = '"cmd":"'.$command.'"';
  49. if ( $arguments )
  50. $this->attributes[] = '"args":"'.$arguments.'"';
  51. }
  52. /**
  53. * This method is called for each command to generate it's command button or selectbox
  54. * for that specific command.
  55. * It is generally not necessary to override it in derived classes if you're creating
  56. * a new class and deriving it from either dsRTECommandSelect or dsRTECommandButton which
  57. * both provide the necessary implementation select boxes and icons respectively.
  58. */
  59. abstract public function getHTML();
  60. /**
  61. * Return HTML for a command's special hidden panel.
  62. * See example in the dsRTELinkCommand class.
  63. */
  64. public function getPanelHTML()
  65. {
  66. return '';
  67. }
  68. /**
  69. * Return SCRIPT (and/or LINK) tags that should be placed into the document's HEAD
  70. * tag for this command to work.
  71. * This method is only used by plugins which need to add extra JavaScript or CSS files
  72. * to the current document.
  73. * See example in the Image plugin.
  74. */
  75. public function getScripts()
  76. {
  77. return '';
  78. }
  79. /**
  80. * Return the command's Attributes JavaScript code (in jQuery) that should be executed to
  81. * complete the command's initialization.
  82. */
  83. final public function getAttributes()
  84. {
  85. return empty( $this->attributes ) ? '' : '$("#cmd-'.$this->id.'-'.$this->command.'").attr({'.implode( ',', $this->attributes ).'});';
  86. }
  87. /**
  88. * Checks if the current browser is Internet Explorer.
  89. * I know there are better ways, but this one does the trick in most cases.
  90. */
  91. public static function isMSIE()
  92. {
  93. return strpos( strtolower( $_SERVER['HTTP_USER_AGENT'] ), 'msie' ) !== false;
  94. }
  95. }
  96. /**
  97. * Special case class for defining command separation.
  98. * I cannot imagine any need to override this class, thus it's marked as 'final'.
  99. */
  100. final class dsRTECommandSeparator extends dsRTECommand
  101. {
  102. /**
  103. * Return HTML for a block separator.
  104. */
  105. public function getHTML()
  106. {
  107. return '<div class="sep"></div>';
  108. }
  109. }
  110. /**
  111. * Implement a base class for handling simple select options (comboboxes).
  112. * Used by the Font and Font Size commands.
  113. */
  114. class dsRTECommandSelect extends dsRTECommand
  115. {
  116. /**
  117. * Get HTML code for a combobox.
  118. */
  119. public function getHTML()
  120. {
  121. @$html .= '<select id="cmd-'.$this->id.'-'.$this->command.'" size="1"><option value="">'.$this->title.'</option>';
  122. foreach ( explode( ' ', $this->icon_offset ) as $opt )
  123. $html .= '<option value="'.$opt.'">'.$opt.'</option>';
  124. $html .= '</select>';
  125. return $html;
  126. }
  127. }
  128. /**
  129. * Implement a base class for button commands.
  130. * Used (and subclassed) by almost all the commands in the system.
  131. */
  132. class dsRTECommandButton extends dsRTECommand
  133. {
  134. /**
  135. * Return a command icon either by offset into the icons.gif image or as an external icon image.
  136. */
  137. public function getHTML()
  138. {
  139. $html = '<a class="cmd" id="cmd-'.$this->id.'-'.$this->command.'" title="'.$this->title.'">';
  140. if ( is_numeric( $this->icon_offset ) )
  141. $html .= '<span style="background-position:-'.$this->icon_offset.'px 0px"></span>';
  142. else
  143. $html .= '<span style="background:url('.$this->icon_offset.') no-repeat 0 0"></span>';
  144. $html .= '</a>';
  145. return $html;
  146. }
  147. }
  148. /**
  149. * Implementation of the HTML Insert command.
  150. */
  151. class dsRTEHTMLCommand extends dsRTECommandButton
  152. {
  153. /**
  154. * Prepare a row for inserting HTML code.
  155. */
  156. public function getPanelHTML()
  157. {
  158. $html = '<div class="rte panel" id="'.$this->id.'-'.$this->arguments.'">';
  159. $html .= t( 'HTML' ).': ';
  160. $html .= '<input size="40" id="'.$this->id.'-'.$this->arguments.'-html" />';
  161. $html .= '<input type="button" id="'.$this->id.'-'.$this->arguments.'-ok" value="'.t( 'OK' ).'" />';
  162. $html .= '<input type="button" value="'.t( 'Cancel' ).'" onclick="$(\'#'.$this->id.'-'.$this->arguments.'\').slideUp()" />';
  163. $html .= '</div>';
  164. return $html;
  165. }
  166. }
  167. ?>