anchor.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. CKEDITOR.dialog.add( 'anchor', function( editor ) {
  6. // Function called in onShow to load selected element.
  7. var loadElements = function( element ) {
  8. this._.selectedElement = element;
  9. var attributeValue = element.data( 'cke-saved-name' );
  10. this.setValueOf( 'info', 'txtName', attributeValue || '' );
  11. };
  12. function createFakeAnchor( editor, attributes ) {
  13. return editor.createFakeElement( editor.document.createElement( 'a', {
  14. attributes: attributes
  15. } ), 'cke_anchor', 'anchor' );
  16. }
  17. return {
  18. title: editor.lang.link.anchor.title,
  19. minWidth: 300,
  20. minHeight: 60,
  21. onOk: function() {
  22. var name = CKEDITOR.tools.trim( this.getValueOf( 'info', 'txtName' ) );
  23. var attributes = {
  24. id: name,
  25. name: name,
  26. 'data-cke-saved-name': name
  27. };
  28. if ( this._.selectedElement ) {
  29. if ( this._.selectedElement.data( 'cke-realelement' ) ) {
  30. var newFake = createFakeAnchor( editor, attributes );
  31. newFake.replace( this._.selectedElement );
  32. // Selecting fake element for IE. (#11377)
  33. if ( CKEDITOR.env.ie )
  34. editor.getSelection().selectElement( newFake );
  35. } else {
  36. this._.selectedElement.setAttributes( attributes );
  37. }
  38. } else {
  39. var sel = editor.getSelection(),
  40. range = sel && sel.getRanges()[ 0 ];
  41. // Empty anchor
  42. if ( range.collapsed ) {
  43. var anchor = createFakeAnchor( editor, attributes );
  44. range.insertNode( anchor );
  45. } else {
  46. if ( CKEDITOR.env.ie && CKEDITOR.env.version < 9 )
  47. attributes[ 'class' ] = 'cke_anchor';
  48. // Apply style.
  49. var style = new CKEDITOR.style( { element: 'a', attributes: attributes } );
  50. style.type = CKEDITOR.STYLE_INLINE;
  51. editor.applyStyle( style );
  52. }
  53. }
  54. },
  55. onHide: function() {
  56. delete this._.selectedElement;
  57. },
  58. onShow: function() {
  59. var sel = editor.getSelection(),
  60. fullySelected = sel.getSelectedElement(),
  61. fakeSelected = fullySelected && fullySelected.data( 'cke-realelement' ),
  62. linkElement = fakeSelected ?
  63. CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, fullySelected ) :
  64. CKEDITOR.plugins.link.getSelectedLink( editor );
  65. if ( linkElement ) {
  66. loadElements.call( this, linkElement );
  67. !fakeSelected && sel.selectElement( linkElement );
  68. if ( fullySelected )
  69. this._.selectedElement = fullySelected;
  70. }
  71. this.getContentElement( 'info', 'txtName' ).focus();
  72. },
  73. contents: [ {
  74. id: 'info',
  75. label: editor.lang.link.anchor.title,
  76. accessKey: 'I',
  77. elements: [ {
  78. type: 'text',
  79. id: 'txtName',
  80. label: editor.lang.link.anchor.name,
  81. required: true,
  82. validate: function() {
  83. if ( !this.getValue() ) {
  84. alert( editor.lang.link.anchor.errorName ); // jshint ignore:line
  85. return false;
  86. }
  87. return true;
  88. }
  89. } ]
  90. } ]
  91. };
  92. } );