fckjustifycommands.js
上传用户:ah_jiwei
上传日期:2022-07-24
资源大小:54044k
文件大小:5k
源码类别:

数据库编程

开发平台:

Visual C++

  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.  * FCKJustifyCommand Class: controls block justification.
  22.  */
  23. var FCKJustifyCommand = function( alignValue )
  24. {
  25. this.AlignValue = alignValue ;
  26. // Detect whether this is the instance for the default alignment.
  27. var contentDir = FCKConfig.ContentLangDirection.toLowerCase() ;
  28. this.IsDefaultAlign = ( alignValue == 'left' && contentDir == 'ltr' ) ||
  29.   ( alignValue == 'right' && contentDir == 'rtl' ) ;
  30. // Get the class name to be used by this instance.
  31. var cssClassName = this._CssClassName = ( function()
  32. {
  33. var classes = FCKConfig.JustifyClasses ;
  34. if ( classes )
  35. {
  36. switch ( alignValue )
  37. {
  38. case 'left' :
  39. return classes[0] ;
  40. case 'center' :
  41. return classes[1] ;
  42. case 'right' :
  43. return classes[2] ;
  44. case 'justify' :
  45. return classes[3] ;
  46. }
  47. }
  48. return null ;
  49. } )() ;
  50. if ( cssClassName && cssClassName.length > 0 )
  51. this._CssClassRegex = new RegExp( '(?:^|\s+)' + cssClassName + '(?=$|\s)' ) ;
  52. }
  53. FCKJustifyCommand._GetClassNameRegex = function()
  54. {
  55. var regex = FCKJustifyCommand._ClassRegex ;
  56. if ( regex != undefined )
  57. return regex ;
  58. var names = [] ;
  59. var classes = FCKConfig.JustifyClasses ;
  60. if ( classes )
  61. {
  62. for ( var i = 0 ; i < 4 ; i++ )
  63. {
  64. var className = classes[i] ;
  65. if ( className && className.length > 0 )
  66. names.push( className ) ;
  67. }
  68. }
  69. if ( names.length > 0 )
  70. regex = new RegExp( '(?:^|\s+)(?:' + names.join( '|' ) + ')(?=$|\s)' ) ;
  71. else
  72. regex = null ;
  73. return FCKJustifyCommand._ClassRegex = regex ;
  74. }
  75. FCKJustifyCommand.prototype =
  76. {
  77. Execute : function()
  78. {
  79. // Save an undo snapshot before doing anything.
  80. FCKUndo.SaveUndoStep() ;
  81. var range = new FCKDomRange( FCK.EditorWindow ) ;
  82. range.MoveToSelection() ;
  83. var currentState = this.GetState() ;
  84. if ( currentState == FCK_TRISTATE_DISABLED )
  85. return ;
  86. // Store a bookmark of the selection since the paragraph iterator might
  87. // change the DOM tree and break selections.
  88. var bookmark = range.CreateBookmark() ;
  89. var cssClassName = this._CssClassName ;
  90. // Apply alignment setting for each paragraph.
  91. var iterator = new FCKDomRangeIterator( range ) ;
  92. var block ;
  93. while ( ( block = iterator.GetNextParagraph() ) )
  94. {
  95. block.removeAttribute( 'align' ) ;
  96. if ( cssClassName )
  97. {
  98. // Remove the any of the alignment classes from the className.
  99. var className = block.className.replace( FCKJustifyCommand._GetClassNameRegex(), '' ) ;
  100. // Append the desired class name.
  101. if ( currentState == FCK_TRISTATE_OFF )
  102. {
  103. if ( className.length > 0 )
  104. className += ' ' ;
  105. block.className = className + cssClassName ;
  106. }
  107. else if ( className.length == 0 )
  108. FCKDomTools.RemoveAttribute( block, 'class' ) ;
  109. }
  110. else
  111. {
  112. var style = block.style ;
  113. if ( currentState == FCK_TRISTATE_OFF )
  114. style.textAlign = this.AlignValue ;
  115. else
  116. {
  117. style.textAlign = '' ;
  118. if ( style.cssText.length == 0 )
  119. block.removeAttribute( 'style' ) ;
  120. }
  121. }
  122. }
  123. // Restore previous selection.
  124. range.MoveToBookmark( bookmark ) ;
  125. range.Select() ;
  126. FCK.Focus() ;
  127. FCK.Events.FireEvent( 'OnSelectionChange' ) ;
  128. },
  129. GetState : function()
  130. {
  131. // Disabled if not WYSIWYG.
  132. if ( FCK.EditMode != FCK_EDITMODE_WYSIWYG || ! FCK.EditorWindow )
  133. return FCK_TRISTATE_DISABLED ;
  134. // Retrieve the first selected block.
  135. var path = new FCKElementPath( FCKSelection.GetBoundaryParentElement( true ) ) ;
  136. var firstBlock = path.Block || path.BlockLimit ;
  137. if ( !firstBlock || firstBlock.nodeName.toLowerCase() == 'body' )
  138. return FCK_TRISTATE_OFF ;
  139. // Check if the desired style is already applied to the block.
  140. var currentAlign ;
  141. if ( FCKBrowserInfo.IsIE )
  142. currentAlign = firstBlock.currentStyle.textAlign ;
  143. else
  144. currentAlign = FCK.EditorWindow.getComputedStyle( firstBlock, '' ).getPropertyValue( 'text-align' );
  145. currentAlign = currentAlign.replace( /(-moz-|-webkit-|start|auto)/i, '' );
  146. if ( ( !currentAlign && this.IsDefaultAlign ) || currentAlign == this.AlignValue )
  147. return FCK_TRISTATE_ON ;
  148. return FCK_TRISTATE_OFF ;
  149. }
  150. } ;