fck_asciimath.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Chamilo LMS
  3. *
  4. * Copyright (c) 2009-2010 Ivan Tcholakov <ivantcholakov@gmail.com>
  5. * Copyright (c) 2009 Dokeos SPRL
  6. *
  7. * License:
  8. * GNU Lesser General Public License, Version 3, 29 June 2007
  9. * by Free Software Foundation, Inc. (http://www.gnu.org/licenses/lgpl.html)
  10. */
  11. var dialog = window.parent ;
  12. var oEditor = dialog.InnerDialogLoaded() ;
  13. var FCK = oEditor.FCK ;
  14. var FCKLang = oEditor.FCKLang ;
  15. var FCKConfig = oEditor.FCKConfig ;
  16. var FCKTools = oEditor.FCKTools ;
  17. var FCKBrowserInfo = oEditor.FCKBrowserInfo ;
  18. var FCKUndo = oEditor.FCKUndo ;
  19. var FCKAsciiMath = oEditor.FCKAsciiMath ;
  20. // Set the language direction.
  21. window.document.dir = FCKLang.Dir ;
  22. FCKLang["DlgAsciiIncompatibleBrowser"] = FCKLang["DlgAsciiIncompatibleBrowser"] ? FCKLang["DlgAsciiIncompatibleBrowser"] : 'Your browser is not able to show mathematical formulas. Please, use %s1 or Internet Explorer with %s2 plugin.' ;
  23. FCKLang['DlgAsciiIncompatibleBrowser'] = FCKLang['DlgAsciiIncompatibleBrowser'].replace( '%s1', '<a href="http://www.mozilla.com" onclick="javascript: window.open(this.href,\'_blank\');return false;">Mozilla Firefox 1.5+</a>, <a href="http://www.opera.com" onclick="javascript: window.open(this.href,\'_blank\');return false;">Opera 9.5+</a>' ) ;
  24. FCKLang['DlgAsciiIncompatibleBrowser'] = FCKLang['DlgAsciiIncompatibleBrowser'].replace( '%s2', '<a href="http://www.dessci.com/en/products/mathplayer/" onclick="javascript: window.open(this.href,\'_blank\');return false;">MathPlayer</a>' ) ;
  25. FCKLang["DlgAsciiMathOldIE"] = FCKLang["DlgAsciiMathOldIE"] ? FCKLang["DlgAsciiMathOldIE"] : 'Your browser is not able to show mathematical formulas. You need to upgrade to Internet Explorer 6.0+. Then you need to install the MathPlayer 2 plugin for Internet Explorer. Please, see %s for more information.' ;
  26. FCKLang['DlgAsciiMathOldIE'] = FCKLang['DlgAsciiMathOldIE'].replace( '%s', '<a href="http://www.dessci.com/en/products/mathplayer/" onclick="javascript: window.open(this.href,\'_blank\');return false;">http://www.dessci.com/en/products/mathplayer/</a>' ) ;
  27. FCKLang['DlgAsciiMathInstallMathPlayer'] = FCKLang['DlgAsciiMathInstallMathPlayer'] ? FCKLang['DlgAsciiMathInstallMathPlayer'] : 'Your browser is not able to show mathematical formulas. You need to install the MathPlayer 2 plugin for Internet Explorer. Please, see %s for more information.' ;
  28. FCKLang['DlgAsciiMathInstallMathPlayer'] = FCKLang['DlgAsciiMathInstallMathPlayer'].replace( '%s', '<a href="http://www.dessci.com/en/products/mathplayer/" onclick="javascript: window.open(this.href,\'_blank\');return false;">http://www.dessci.com/en/products/mathplayer/</a>' ) ;
  29. FCKLang['DlgAsciiMathOldMathPlayer'] = FCKLang['DlgAsciiMathOldMathPlayer'] ? FCKLang['DlgAsciiMathOldMathPlayer'] : 'Your browser is not able to show mathematical formulas. You need to upgrade the MathPlayer plugin for Internet Explorer to version 2. Please, see %s for more information.' ;
  30. FCKLang['DlgAsciiMathOldMathPlayer'] = FCKLang['DlgAsciiMathOldMathPlayer'].replace( '%s', '<a href="http://www.dessci.com/en/products/mathplayer/" onclick="javascript: window.open(this.href,\'_blank\');return false;">http://www.dessci.com/en/products/mathplayer/</a>' ) ;
  31. // Settings for ASCIIMathML.js
  32. // Checking for native MathML support, it is always needed for this dialog.
  33. var checkForMathML = true;
  34. // Suppressing the built-in notification messages when the browser is incompatible.
  35. var notifyIfNoMathML = false ;
  36. var alertIfNoMathML = false ;
  37. var notifyIfNoSVG = false ;
  38. var alertIfNoSVG = false ;
  39. // Formula translation will be called explicitly in this dialog after it loads.
  40. var translateOnLoad = false ;
  41. // Formula tooltips are hard-coded in this dialog, there is no need they to be generated.
  42. var showasciiformulaonhover = false ;
  43. // Font size of the formulas in this dialog.
  44. var mathfontsize = "1.1em" ;
  45. // oSpanAM: The actual selected span element in the editor that contains the formula.
  46. var oSpanAM = FCKAsciiMath.FindFormulaContainer( FCKAsciiMath.GetSearchElementFromSelection() ) ;
  47. if ( oSpanAM )
  48. {
  49. FCK.Selection.SelectNode( oSpanAM ) ;
  50. }
  51. function LoadSelection()
  52. {
  53. GetE( 'inputText' ).value = FCKAsciiMath.GetFormula( oSpanAM ) ;
  54. Preview() ;
  55. }
  56. function Ok()
  57. {
  58. if ( FCKBrowserInfo.IsIE )
  59. {
  60. if ( oSpanAM )
  61. {
  62. FCK.Selection.SelectNode( oSpanAM ) ;
  63. // For IE: Before updating, we have to move the selection outside the formula
  64. // in order to prevent "Unspecified error".
  65. var span_target = FCK.EditorDocument.createElement( 'span' ) ;
  66. span_target.innerHTML = '&nbsp;' ;
  67. span_target = oSpanAM.parentNode.insertBefore( span_target, oSpanAM ) ;
  68. FCK.Selection.SelectNode( span_target ) ;
  69. }
  70. }
  71. var formula = GetE( 'inputText' ).value ;
  72. if ( formula != '' )
  73. {
  74. FCK.InsertHtml( '<span class="AM">`' + formula + '`<\/span>' ) ;
  75. }
  76. else
  77. {
  78. FCK.Selection.Delete() ;
  79. }
  80. if ( FCKBrowserInfo.IsIE )
  81. {
  82. if ( oSpanAM )
  83. {
  84. FCKUndo.SaveUndoStep() ;
  85. oSpanAM.parentNode.removeChild( oSpanAM ) ;
  86. }
  87. }
  88. return true ;
  89. }
  90. window.onload = function()
  91. {
  92. // Translate the dialog box texts.
  93. oEditor.FCKLanguageManager.TranslatePage( document ) ;
  94. // Initialization of the script ASCIIMathML.js.
  95. init() ;
  96. // Load the selected element information (if any).
  97. LoadSelection() ;
  98. dialog.SetAutoSize( true ) ;
  99. // When MathML is available show the button "Show MathML code".
  100. if ( !noMathML ) {
  101. GetE( 'show_mathml' ).style.display = '' ;
  102. }
  103. // Activate the "OK" button.
  104. dialog.SetOkButton( true ) ;
  105. var inputField = GetE( 'inputText' ) ;
  106. inputField.focus() ;
  107. }
  108. function Set( string )
  109. {
  110. var inputField = GetE( 'inputText' ) ;
  111. inputField.value += string ;
  112. Preview() ;
  113. inputField.focus() ;
  114. return false ;
  115. }
  116. function Clear()
  117. {
  118. var inputField = GetE( 'inputText' ) ;
  119. inputField.value = '' ;
  120. Preview() ;
  121. inputField.focus() ;
  122. return false ;
  123. }
  124. function Delete()
  125. {
  126. Clear();
  127. dialog.Ok();
  128. }
  129. function Preview()
  130. {
  131. if ( GetE( 'inputText' ).value != '' )
  132. {
  133. var str = GetE( 'inputText' ).value ;
  134. var outnode = GetE( 'outputNode' ) ;
  135. var newnode = createElementXHTML( 'div' ) ;
  136. newnode.setAttribute( 'id', 'outputNode' ) ;
  137. outnode.parentNode.replaceChild( newnode, outnode ) ;
  138. outnode = GetE( 'outputNode' ) ;
  139. var n = outnode.childNodes.length ;
  140. for ( var i = 0; i < n; i++ )
  141. {
  142. outnode.removeChild( outnode.firstChild ) ;
  143. }
  144. outnode.appendChild( document.createComment( '`' + str + '`' ) ) ;
  145. AMprocessNode( outnode, true ) ;
  146. if ( FCKLang['DlgAsciiMathShowMathML'] )
  147. {
  148. GetE( 'show_mathml' ).value = FCKLang['DlgAsciiMathShowMathML'] ;
  149. }
  150. }
  151. else
  152. {
  153. var outnode = GetE( 'outputNode' ) ;
  154. var n = outnode.childNodes.length ;
  155. for ( var i = 0; i < n; i++ )
  156. {
  157. outnode.removeChild( outnode.firstChild ) ;
  158. }
  159. }
  160. }
  161. function AMnode2string( inNode, indent )
  162. {
  163. // thanks to James Frazer for contributing an initial version of this function
  164. var i, str = '' ;
  165. if ( inNode.nodeType == 1 )
  166. {
  167. var name = inNode.nodeName.toLowerCase() ; // (IE fix)
  168. str = '\r' + indent + '<' + name ;
  169. for ( i = 0; i < inNode.attributes.length; i++ )
  170. {
  171. if ( inNode.attributes[i].nodeValue != 'italic' &&
  172. inNode.attributes[i].nodeValue != '' && //stop junk attributes
  173. inNode.attributes[i].nodeValue != 'inherit' && // (mostly IE)
  174. inNode.attributes[i].nodeValue != undefined &&
  175. inNode.attributes[i].nodeName[0] != '-' )
  176. {
  177. str += ' ' + inNode.attributes[i].nodeName + '=' + '"' + inNode.attributes[i].nodeValue + '"' ;
  178. }
  179. }
  180. if ( name == 'math' )
  181. {
  182. str += ' xmlns="http://www.w3.org/1998/Math/MathML"' ;
  183. }
  184. str += '>' ;
  185. for ( i = 0; i < inNode.childNodes.length; i++ )
  186. {
  187. str += AMnode2string( inNode.childNodes[i], indent + ' ' ) ;
  188. }
  189. if ( name != 'mo' && name != 'mi' && name != 'mn' ) str += '\r' + indent ;
  190. str += '</' + name + '>' ;
  191. }
  192. else if( inNode.nodeType == 3 )
  193. {
  194. var st = inNode.nodeValue ;
  195. for ( i = 0; i < st.length; i++ )
  196. {
  197. if ( st.charCodeAt( i ) < 32 || st.charCodeAt( i ) > 126 )
  198. {
  199. str += '&#' + st.charCodeAt( i ) + ';' ;
  200. }
  201. else if ( st.charAt(i) == '<' && indent != ' ' ) str += '&lt;' ;
  202. else if ( st.charAt(i) == '>' && indent != ' ' ) str += '&gt;' ;
  203. else if ( st.charAt(i) == '&' && indent != ' ' ) str += '&amp;' ;
  204. else str += st.charAt( i ) ;
  205. }
  206. }
  207. return str ;
  208. }
  209. function ShowMathML()
  210. {
  211. if ( GetE( 'inputText' ).value != '' )
  212. {
  213. var math = GetE( 'outputNode' ).getElementsByTagName( 'math' )[0] ;
  214. if ( math )
  215. {
  216. var width ;
  217. if ( GetE( 'outputNode' ).offsetWidth )
  218. {
  219. width = GetE( 'outputNode' ).offsetWidth ;
  220. }
  221. math.parentNode.innerHTML = '<pre>' + FCKTools.HTMLEncode( AMnode2string( math, '' ) ) + '</pre>' ;
  222. if ( width && FCKBrowserInfo.IsGecko )
  223. {
  224. GetE( 'outputNode' ).style.width = width + 'px' ;
  225. }
  226. if ( FCKLang['DlgAsciiMathFormulaPreview'] )
  227. {
  228. GetE( 'show_mathml' ).value = FCKLang['DlgAsciiMathFormulaPreview'] ;
  229. }
  230. }
  231. else
  232. {
  233. Preview() ;
  234. }
  235. }
  236. else
  237. {
  238. Preview() ;
  239. }
  240. }
  241. // Highlighting formulas.
  242. function over(td)
  243. {
  244. td.className = 'LightBackground Hand' ;
  245. }
  246. function out(td)
  247. {
  248. td.className = 'Hand' ;
  249. }