]> git.immae.eu Git - perso/Immae/Projets/packagist/piedsjaloux-ckeditor-component.git/blob - sources/plugins/link/dialogs/anchor.js
Initial commit
[perso/Immae/Projets/packagist/piedsjaloux-ckeditor-component.git] / sources / plugins / link / dialogs / anchor.js
1 /**
2 * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md or http://ckeditor.com/license
4 */
5
6 CKEDITOR.dialog.add( 'anchor', function( editor ) {
7 // Function called in onShow to load selected element.
8 var loadElements = function( element ) {
9 this._.selectedElement = element;
10
11 var attributeValue = element.data( 'cke-saved-name' );
12 this.setValueOf( 'info', 'txtName', attributeValue || '' );
13 };
14
15 function createFakeAnchor( editor, attributes ) {
16 return editor.createFakeElement( editor.document.createElement( 'a', {
17 attributes: attributes
18 } ), 'cke_anchor', 'anchor' );
19 }
20
21 return {
22 title: editor.lang.link.anchor.title,
23 minWidth: 300,
24 minHeight: 60,
25 onOk: function() {
26 var name = CKEDITOR.tools.trim( this.getValueOf( 'info', 'txtName' ) );
27 var attributes = {
28 id: name,
29 name: name,
30 'data-cke-saved-name': name
31 };
32
33 if ( this._.selectedElement ) {
34 if ( this._.selectedElement.data( 'cke-realelement' ) ) {
35 var newFake = createFakeAnchor( editor, attributes );
36 newFake.replace( this._.selectedElement );
37
38 // Selecting fake element for IE. (#11377)
39 if ( CKEDITOR.env.ie )
40 editor.getSelection().selectElement( newFake );
41 } else {
42 this._.selectedElement.setAttributes( attributes );
43 }
44 } else {
45 var sel = editor.getSelection(),
46 range = sel && sel.getRanges()[ 0 ];
47
48 // Empty anchor
49 if ( range.collapsed ) {
50 var anchor = createFakeAnchor( editor, attributes );
51 range.insertNode( anchor );
52 } else {
53 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 9 )
54 attributes[ 'class' ] = 'cke_anchor';
55
56 // Apply style.
57 var style = new CKEDITOR.style( { element: 'a', attributes: attributes } );
58 style.type = CKEDITOR.STYLE_INLINE;
59 editor.applyStyle( style );
60 }
61 }
62 },
63
64 onHide: function() {
65 delete this._.selectedElement;
66 },
67
68 onShow: function() {
69 var sel = editor.getSelection(),
70 fullySelected = sel.getSelectedElement(),
71 fakeSelected = fullySelected && fullySelected.data( 'cke-realelement' ),
72 linkElement = fakeSelected ?
73 CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, fullySelected ) :
74 CKEDITOR.plugins.link.getSelectedLink( editor );
75
76 if ( linkElement ) {
77 loadElements.call( this, linkElement );
78 !fakeSelected && sel.selectElement( linkElement );
79
80 if ( fullySelected )
81 this._.selectedElement = fullySelected;
82 }
83
84 this.getContentElement( 'info', 'txtName' ).focus();
85 },
86 contents: [ {
87 id: 'info',
88 label: editor.lang.link.anchor.title,
89 accessKey: 'I',
90 elements: [ {
91 type: 'text',
92 id: 'txtName',
93 label: editor.lang.link.anchor.name,
94 required: true,
95 validate: function() {
96 if ( !this.getValue() ) {
97 alert( editor.lang.link.anchor.errorName ); // jshint ignore:line
98 return false;
99 }
100 return true;
101 }
102 } ]
103 } ]
104 };
105 } );