plugin.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview The "elementspath" plugin. It shows all elements in the DOM
  7. * parent tree relative to the current selection in the editing area.
  8. */
  9. ( function() {
  10. var commands = {
  11. toolbarFocus: {
  12. editorFocus: false,
  13. readOnly: 1,
  14. exec: function( editor ) {
  15. var idBase = editor._.elementsPath.idBase;
  16. var element = CKEDITOR.document.getById( idBase + '0' );
  17. // Make the first button focus accessible for IE. (#3417)
  18. // Adobe AIR instead need while of delay.
  19. element && element.focus( CKEDITOR.env.ie || CKEDITOR.env.air );
  20. }
  21. }
  22. };
  23. var emptyHtml = '<span class="cke_path_empty">&nbsp;</span>';
  24. var extra = '';
  25. // Some browsers don't cancel key events in the keydown but in the
  26. // keypress.
  27. // TODO: Check if really needed.
  28. if ( CKEDITOR.env.gecko && CKEDITOR.env.mac )
  29. extra += ' onkeypress="return false;"';
  30. // With Firefox, we need to force the button to redraw, otherwise it
  31. // will remain in the focus state.
  32. if ( CKEDITOR.env.gecko )
  33. extra += ' onblur="this.style.cssText = this.style.cssText;"';
  34. var pathItemTpl = CKEDITOR.addTemplate( 'pathItem', '<a' +
  35. ' id="{id}"' +
  36. ' href="{jsTitle}"' +
  37. ' tabindex="-1"' +
  38. ' class="cke_path_item"' +
  39. ' title="{label}"' +
  40. extra +
  41. ' hidefocus="true" ' +
  42. ' onkeydown="return CKEDITOR.tools.callFunction({keyDownFn},{index}, event );"' +
  43. ' onclick="CKEDITOR.tools.callFunction({clickFn},{index}); return false;"' +
  44. ' role="button" aria-label="{label}">' +
  45. '{text}' +
  46. '</a>' );
  47. CKEDITOR.plugins.add( 'elementspath', {
  48. // jscs:disable maximumLineLength
  49. lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
  50. // jscs:enable maximumLineLength
  51. init: function( editor ) {
  52. editor._.elementsPath = {
  53. idBase: 'cke_elementspath_' + CKEDITOR.tools.getNextNumber() + '_',
  54. filters: []
  55. };
  56. editor.on( 'uiSpace', function( event ) {
  57. if ( event.data.space == 'bottom' )
  58. initElementsPath( editor, event.data );
  59. } );
  60. }
  61. } );
  62. function initElementsPath( editor, bottomSpaceData ) {
  63. var spaceId = editor.ui.spaceId( 'path' ),
  64. spaceElement,
  65. getSpaceElement = function() {
  66. if ( !spaceElement )
  67. spaceElement = CKEDITOR.document.getById( spaceId );
  68. return spaceElement;
  69. },
  70. elementsPath = editor._.elementsPath,
  71. idBase = elementsPath.idBase;
  72. bottomSpaceData.html += '<span id="' + spaceId + '_label" class="cke_voice_label">' + editor.lang.elementspath.eleLabel + '</span>' +
  73. '<span id="' + spaceId + '" class="cke_path" role="group" aria-labelledby="' + spaceId + '_label">' + emptyHtml + '</span>';
  74. // Register the ui element to the focus manager.
  75. editor.on( 'uiReady', function() {
  76. var element = editor.ui.space( 'path' );
  77. element && editor.focusManager.add( element, 1 );
  78. } );
  79. function onClick( elementIndex ) {
  80. var element = elementsPath.list[ elementIndex ];
  81. if ( element.equals( editor.editable() ) || element.getAttribute( 'contenteditable' ) == 'true' ) {
  82. var range = editor.createRange();
  83. range.selectNodeContents( element );
  84. range.select();
  85. } else {
  86. editor.getSelection().selectElement( element );
  87. }
  88. // It is important to focus() *after* the above selection
  89. // manipulation, otherwise Firefox will have troubles. #10119
  90. editor.focus();
  91. }
  92. elementsPath.onClick = onClick;
  93. var onClickHanlder = CKEDITOR.tools.addFunction( onClick ),
  94. onKeyDownHandler = CKEDITOR.tools.addFunction( function( elementIndex, ev ) {
  95. var idBase = elementsPath.idBase,
  96. element;
  97. ev = new CKEDITOR.dom.event( ev );
  98. var rtl = editor.lang.dir == 'rtl';
  99. switch ( ev.getKeystroke() ) {
  100. case rtl ? 39 : 37: // LEFT-ARROW
  101. case 9: // TAB
  102. element = CKEDITOR.document.getById( idBase + ( elementIndex + 1 ) );
  103. if ( !element )
  104. element = CKEDITOR.document.getById( idBase + '0' );
  105. element.focus();
  106. return false;
  107. case rtl ? 37 : 39: // RIGHT-ARROW
  108. case CKEDITOR.SHIFT + 9: // SHIFT + TAB
  109. element = CKEDITOR.document.getById( idBase + ( elementIndex - 1 ) );
  110. if ( !element )
  111. element = CKEDITOR.document.getById( idBase + ( elementsPath.list.length - 1 ) );
  112. element.focus();
  113. return false;
  114. case 27: // ESC
  115. editor.focus();
  116. return false;
  117. case 13: // ENTER // Opera
  118. case 32: // SPACE
  119. onClick( elementIndex );
  120. return false;
  121. }
  122. return true;
  123. } );
  124. editor.on( 'selectionChange', function() {
  125. var html = [],
  126. elementsList = elementsPath.list = [],
  127. namesList = [],
  128. filters = elementsPath.filters,
  129. isContentEditable = true,
  130. // Use elementPath to consider children of editable only (#11124).
  131. elementsChain = editor.elementPath().elements,
  132. name;
  133. // Starts iteration from body element, skipping html.
  134. for ( var j = elementsChain.length; j--; ) {
  135. var element = elementsChain[ j ],
  136. ignore = 0;
  137. if ( element.data( 'cke-display-name' ) )
  138. name = element.data( 'cke-display-name' );
  139. else if ( element.data( 'cke-real-element-type' ) )
  140. name = element.data( 'cke-real-element-type' );
  141. else
  142. name = element.getName();
  143. isContentEditable = element.hasAttribute( 'contenteditable' ) ?
  144. element.getAttribute( 'contenteditable' ) == 'true' : isContentEditable;
  145. // If elem is non-contenteditable, and it's not specifying contenteditable
  146. // attribute - then elem should be ignored.
  147. if ( !isContentEditable && !element.hasAttribute( 'contenteditable' ) )
  148. ignore = 1;
  149. for ( var i = 0; i < filters.length; i++ ) {
  150. var ret = filters[ i ]( element, name );
  151. if ( ret === false ) {
  152. ignore = 1;
  153. break;
  154. }
  155. name = ret || name;
  156. }
  157. if ( !ignore ) {
  158. elementsList.unshift( element );
  159. namesList.unshift( name );
  160. }
  161. }
  162. for ( var iterationLimit = elementsList.length, index = 0; index < iterationLimit; index++ ) {
  163. name = namesList[ index ];
  164. var label = editor.lang.elementspath.eleTitle.replace( /%1/, name ),
  165. item = pathItemTpl.output( {
  166. id: idBase + index,
  167. label: label,
  168. text: name,
  169. jsTitle: 'javascript:void(\'' + name + '\')', // jshint ignore:line
  170. index: index,
  171. keyDownFn: onKeyDownHandler,
  172. clickFn: onClickHanlder
  173. } );
  174. html.unshift( item );
  175. }
  176. var space = getSpaceElement();
  177. space.setHtml( html.join( '' ) + emptyHtml );
  178. editor.fire( 'elementsPathUpdate', { space: space } );
  179. } );
  180. function empty() {
  181. spaceElement && spaceElement.setHtml( emptyHtml );
  182. delete elementsPath.list;
  183. }
  184. editor.on( 'readOnly', empty );
  185. editor.on( 'contentDomUnload', empty );
  186. editor.addCommand( 'elementsPathFocus', commands.toolbarFocus );
  187. editor.setKeystroke( CKEDITOR.ALT + 122 /*F11*/, 'elementsPathFocus' );
  188. }
  189. } )();
  190. /**
  191. * Fired when the contents of the elementsPath are changed.
  192. *
  193. * @event elementsPathUpdate
  194. * @member CKEDITOR.editor
  195. * @param {CKEDITOR.editor} editor This editor instance.
  196. * @param data
  197. * @param {CKEDITOR.dom.element} data.space The elementsPath container.
  198. */