/source/Plug-in/fck/editor/_source/commandclasses/fck_othercommands.js

http://prosporous.googlecode.com/ · JavaScript · 470 lines · 338 code · 71 blank · 61 comment · 40 complexity · 3adc62d1e88671ec977913e557185cb6 MD5 · raw file

  1. /*
  2. * FCKeditor - The text editor for Internet - http://www.fckeditor.net
  3. * Copyright (C) 2003-2007 Frederico Caldeira Knabben
  4. *
  5. * == BEGIN LICENSE ==
  6. *
  7. * Licensed under the terms of any of the following licenses at your
  8. * choice:
  9. *
  10. * - GNU General Public License Version 2 or later (the "GPL")
  11. * http://www.gnu.org/licenses/gpl.html
  12. *
  13. * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
  14. * http://www.gnu.org/licenses/lgpl.html
  15. *
  16. * - Mozilla Public License Version 1.1 or later (the "MPL")
  17. * http://www.mozilla.org/MPL/MPL-1.1.html
  18. *
  19. * == END LICENSE ==
  20. *
  21. * Definition of other commands that are not available internaly in the
  22. * browser (see FCKNamedCommand).
  23. */
  24. // ### General Dialog Box Commands.
  25. var FCKDialogCommand = function( name, title, url, width, height, getStateFunction, getStateParam )
  26. {
  27. this.Name = name ;
  28. this.Title = title ;
  29. this.Url = url ;
  30. this.Width = width ;
  31. this.Height = height ;
  32. this.GetStateFunction = getStateFunction ;
  33. this.GetStateParam = getStateParam ;
  34. this.Resizable = false ;
  35. }
  36. FCKDialogCommand.prototype.Execute = function()
  37. {
  38. FCKDialog.OpenDialog( 'FCKDialog_' + this.Name , this.Title, this.Url, this.Width, this.Height, null, null, this.Resizable ) ;
  39. }
  40. FCKDialogCommand.prototype.GetState = function()
  41. {
  42. if ( this.GetStateFunction )
  43. return this.GetStateFunction( this.GetStateParam ) ;
  44. else
  45. return FCK_TRISTATE_OFF ;
  46. }
  47. // Generic Undefined command (usually used when a command is under development).
  48. var FCKUndefinedCommand = function()
  49. {
  50. this.Name = 'Undefined' ;
  51. }
  52. FCKUndefinedCommand.prototype.Execute = function()
  53. {
  54. alert( FCKLang.NotImplemented ) ;
  55. }
  56. FCKUndefinedCommand.prototype.GetState = function()
  57. {
  58. return FCK_TRISTATE_OFF ;
  59. }
  60. // ### FormatBlock
  61. var FCKFormatBlockCommand = function()
  62. {}
  63. FCKFormatBlockCommand.prototype =
  64. {
  65. Name : 'FormatBlock',
  66. Execute : FCKStyleCommand.prototype.Execute,
  67. GetState : function()
  68. {
  69. return FCK.EditorDocument ? FCK_TRISTATE_OFF : FCK_TRISTATE_DISABLED ;
  70. }
  71. };
  72. // ### FontName
  73. var FCKFontNameCommand = function()
  74. {}
  75. FCKFontNameCommand.prototype =
  76. {
  77. Name : 'FontName',
  78. Execute : FCKStyleCommand.prototype.Execute,
  79. GetState : FCKFormatBlockCommand.prototype.GetState
  80. };
  81. // ### FontSize
  82. var FCKFontSizeCommand = function()
  83. {}
  84. FCKFontSizeCommand.prototype =
  85. {
  86. Name : 'FontSize',
  87. Execute : FCKStyleCommand.prototype.Execute,
  88. GetState : FCKFormatBlockCommand.prototype.GetState
  89. };
  90. // ### Preview
  91. var FCKPreviewCommand = function()
  92. {
  93. this.Name = 'Preview' ;
  94. }
  95. FCKPreviewCommand.prototype.Execute = function()
  96. {
  97. FCK.Preview() ;
  98. }
  99. FCKPreviewCommand.prototype.GetState = function()
  100. {
  101. return FCK_TRISTATE_OFF ;
  102. }
  103. // ### Save
  104. var FCKSaveCommand = function()
  105. {
  106. this.Name = 'Save' ;
  107. }
  108. FCKSaveCommand.prototype.Execute = function()
  109. {
  110. // Get the linked field form.
  111. var oForm = FCK.GetParentForm() ;
  112. if ( typeof( oForm.onsubmit ) == 'function' )
  113. {
  114. var bRet = oForm.onsubmit() ;
  115. if ( bRet != null && bRet === false )
  116. return ;
  117. }
  118. // Submit the form.
  119. // If there's a button named "submit" then the form.submit() function is masked and
  120. // can't be called in Mozilla, so we call the click() method of that button.
  121. if ( typeof( oForm.submit ) == 'function' )
  122. oForm.submit() ;
  123. else
  124. oForm.submit.click() ;
  125. }
  126. FCKSaveCommand.prototype.GetState = function()
  127. {
  128. return FCK_TRISTATE_OFF ;
  129. }
  130. // ### NewPage
  131. var FCKNewPageCommand = function()
  132. {
  133. this.Name = 'NewPage' ;
  134. }
  135. FCKNewPageCommand.prototype.Execute = function()
  136. {
  137. FCKUndo.SaveUndoStep() ;
  138. FCK.SetData( '' ) ;
  139. FCKUndo.Typing = true ;
  140. FCK.Focus() ;
  141. }
  142. FCKNewPageCommand.prototype.GetState = function()
  143. {
  144. return FCK_TRISTATE_OFF ;
  145. }
  146. // ### Source button
  147. var FCKSourceCommand = function()
  148. {
  149. this.Name = 'Source' ;
  150. }
  151. FCKSourceCommand.prototype.Execute = function()
  152. {
  153. if ( FCKConfig.SourcePopup ) // Until v2.2, it was mandatory for FCKBrowserInfo.IsGecko.
  154. {
  155. var iWidth = FCKConfig.ScreenWidth * 0.65 ;
  156. var iHeight = FCKConfig.ScreenHeight * 0.65 ;
  157. FCKDialog.OpenDialog( 'FCKDialog_Source', FCKLang.Source, 'dialog/fck_source.html', iWidth, iHeight, null, null, true ) ;
  158. }
  159. else
  160. FCK.SwitchEditMode() ;
  161. }
  162. FCKSourceCommand.prototype.GetState = function()
  163. {
  164. return ( FCK.EditMode == FCK_EDITMODE_WYSIWYG ? FCK_TRISTATE_OFF : FCK_TRISTATE_ON ) ;
  165. }
  166. // ### Undo
  167. var FCKUndoCommand = function()
  168. {
  169. this.Name = 'Undo' ;
  170. }
  171. FCKUndoCommand.prototype.Execute = function()
  172. {
  173. FCKUndo.Undo() ;
  174. }
  175. FCKUndoCommand.prototype.GetState = function()
  176. {
  177. return ( FCKUndo.CheckUndoState() ? FCK_TRISTATE_OFF : FCK_TRISTATE_DISABLED ) ;
  178. }
  179. // ### Redo
  180. var FCKRedoCommand = function()
  181. {
  182. this.Name = 'Redo' ;
  183. }
  184. FCKRedoCommand.prototype.Execute = function()
  185. {
  186. FCKUndo.Redo() ;
  187. }
  188. FCKRedoCommand.prototype.GetState = function()
  189. {
  190. return ( FCKUndo.CheckRedoState() ? FCK_TRISTATE_OFF : FCK_TRISTATE_DISABLED ) ;
  191. }
  192. // ### Page Break
  193. var FCKPageBreakCommand = function()
  194. {
  195. this.Name = 'PageBreak' ;
  196. }
  197. FCKPageBreakCommand.prototype.Execute = function()
  198. {
  199. // Take an undo snapshot before changing the document
  200. FCKUndo.SaveUndoStep() ;
  201. // var e = FCK.EditorDocument.createElement( 'CENTER' ) ;
  202. // e.style.pageBreakAfter = 'always' ;
  203. // Tidy was removing the empty CENTER tags, so the following solution has
  204. // been found. It also validates correctly as XHTML 1.0 Strict.
  205. var e = FCK.EditorDocument.createElement( 'DIV' ) ;
  206. e.style.pageBreakAfter = 'always' ;
  207. e.innerHTML = '<span style="DISPLAY:none">&nbsp;</span>' ;
  208. var oFakeImage = FCKDocumentProcessor_CreateFakeImage( 'FCK__PageBreak', e ) ;
  209. var oRange = new FCKDomRange( FCK.EditorWindow ) ;
  210. oRange.MoveToSelection() ;
  211. var oSplitInfo = oRange.SplitBlock() ;
  212. if ( oSplitInfo.NextBlock )
  213. oSplitInfo.NextBlock.parentNode.insertBefore( oFakeImage, oSplitInfo.NextBlock ) ;
  214. else
  215. oSplitInfo.PreviousBlock.parentNode.insertBefore( oFakeImage, oSplitInfo.PreviousBlock.nextSibling ) ;
  216. FCK.Events.FireEvent( 'OnSelectionChange' ) ;
  217. }
  218. FCKPageBreakCommand.prototype.GetState = function()
  219. {
  220. return 0 ; // FCK_TRISTATE_OFF
  221. }
  222. // FCKUnlinkCommand - by Johnny Egeland (johnny@coretrek.com)
  223. var FCKUnlinkCommand = function()
  224. {
  225. this.Name = 'Unlink' ;
  226. }
  227. FCKUnlinkCommand.prototype.Execute = function()
  228. {
  229. // Take an undo snapshot before changing the document
  230. FCKUndo.SaveUndoStep() ;
  231. if ( FCKBrowserInfo.IsGeckoLike )
  232. {
  233. var oLink = FCK.Selection.MoveToAncestorNode( 'A' ) ;
  234. // The unlink command can generate a span in Firefox, so let's do it our way. See #430
  235. if ( oLink )
  236. FCKTools.RemoveOuterTags( oLink ) ;
  237. return ;
  238. }
  239. FCK.ExecuteNamedCommand( this.Name ) ;
  240. }
  241. FCKUnlinkCommand.prototype.GetState = function()
  242. {
  243. var state = FCK.GetNamedCommandState( this.Name ) ;
  244. // Check that it isn't an anchor
  245. if ( state == FCK_TRISTATE_OFF && FCK.EditMode == FCK_EDITMODE_WYSIWYG )
  246. {
  247. var oLink = FCKSelection.MoveToAncestorNode( 'A' ) ;
  248. var bIsAnchor = ( oLink && oLink.name.length > 0 && oLink.href.length == 0 ) ;
  249. if ( bIsAnchor )
  250. state = FCK_TRISTATE_DISABLED ;
  251. }
  252. return state ;
  253. }
  254. // FCKSelectAllCommand
  255. var FCKSelectAllCommand = function()
  256. {
  257. this.Name = 'SelectAll' ;
  258. }
  259. FCKSelectAllCommand.prototype.Execute = function()
  260. {
  261. if ( FCK.EditMode == FCK_EDITMODE_WYSIWYG )
  262. {
  263. FCK.ExecuteNamedCommand( 'SelectAll' ) ;
  264. }
  265. else
  266. {
  267. // Select the contents of the textarea
  268. var textarea = FCK.EditingArea.Textarea ;
  269. if ( FCKBrowserInfo.IsIE )
  270. {
  271. textarea.createTextRange().execCommand( 'SelectAll' ) ;
  272. }
  273. else
  274. {
  275. textarea.selectionStart = 0 ;
  276. textarea.selectionEnd = textarea.value.length ;
  277. }
  278. textarea.focus() ;
  279. }
  280. }
  281. FCKSelectAllCommand.prototype.GetState = function()
  282. {
  283. return FCK_TRISTATE_OFF ;
  284. }
  285. // FCKPasteCommand
  286. var FCKPasteCommand = function()
  287. {
  288. this.Name = 'Paste' ;
  289. }
  290. FCKPasteCommand.prototype =
  291. {
  292. Execute : function()
  293. {
  294. if ( FCKBrowserInfo.IsIE )
  295. FCK.Paste() ;
  296. else
  297. FCK.ExecuteNamedCommand( 'Paste' ) ;
  298. },
  299. GetState : function()
  300. {
  301. return FCK.GetNamedCommandState( 'Paste' ) ;
  302. }
  303. } ;
  304. // FCKRuleCommand
  305. var FCKRuleCommand = function()
  306. {
  307. this.Name = 'Rule' ;
  308. }
  309. FCKRuleCommand.prototype =
  310. {
  311. Execute : function()
  312. {
  313. FCKUndo.SaveUndoStep() ;
  314. FCK.InsertElement( 'hr' ) ;
  315. },
  316. GetState : function()
  317. {
  318. return FCK.GetNamedCommandState( 'InsertHorizontalRule' ) ;
  319. }
  320. } ;
  321. // FCKCopyCommand
  322. var FCKCopyCommand = function()
  323. {
  324. this.Name = 'Copy' ;
  325. }
  326. FCKCopyCommand.prototype =
  327. {
  328. Execute : function()
  329. {
  330. FCK.ExecuteNamedCommand( this.Name ) ;
  331. },
  332. GetState : function()
  333. {
  334. // Strangely, the cut command happens to have the correct states for both Copy and Cut in all browsers.
  335. return FCK.GetNamedCommandState( 'Cut' ) ;
  336. }
  337. };
  338. var FCKAnchorDeleteCommand = function()
  339. {
  340. this.Name = 'AnchorDelete' ;
  341. }
  342. FCKAnchorDeleteCommand.prototype =
  343. {
  344. Execute : function()
  345. {
  346. if (FCK.Selection.GetType() == 'Control')
  347. {
  348. FCK.Selection.Delete();
  349. }
  350. else
  351. {
  352. var oFakeImage = FCK.Selection.GetSelectedElement() ;
  353. if ( oFakeImage )
  354. {
  355. if ( oFakeImage.tagName == 'IMG' && oFakeImage.getAttribute('_fckanchor') )
  356. oAnchor = FCK.GetRealElement( oFakeImage ) ;
  357. else
  358. oFakeImage = null ;
  359. }
  360. //Search for a real anchor
  361. if ( !oFakeImage )
  362. {
  363. oAnchor = FCK.Selection.MoveToAncestorNode( 'A' ) ;
  364. if ( oAnchor )
  365. FCK.Selection.SelectNode( oAnchor ) ;
  366. }
  367. // If it's also a link, then just remove the name and exit
  368. if ( oAnchor.href.length != 0 )
  369. {
  370. oAnchor.removeAttribute( 'name' ) ;
  371. // Remove temporary class for IE
  372. if ( FCKBrowserInfo.IsIE )
  373. oAnchor.className = oAnchor.className.replace( FCKRegexLib.FCK_Class, '' ) ;
  374. return ;
  375. }
  376. // We need to remove the anchor
  377. // If we got a fake image, then just remove it and we're done
  378. if ( oFakeImage )
  379. {
  380. oFakeImage.parentNode.removeChild( oFakeImage ) ;
  381. return ;
  382. }
  383. // Empty anchor, so just remove it
  384. if ( oAnchor.innerHTML.length == 0 )
  385. {
  386. oAnchor.parentNode.removeChild( oAnchor ) ;
  387. return ;
  388. }
  389. // Anchor with content, leave the content
  390. FCKTools.RemoveOuterTags( oAnchor ) ;
  391. }
  392. if ( FCKBrowserInfo.IsGecko )
  393. FCK.Selection.Collapse( true ) ;
  394. },
  395. GetState : function()
  396. {
  397. return FCK.GetNamedCommandState( 'Unlink') ;
  398. }
  399. };