]> git.immae.eu Git - perso/Immae/Projets/packagist/ludivine-ckeditor-component.git/blob - sources/plugins/link/dialogs/anchor.js
Update to 4.7.3
[perso/Immae/Projets/packagist/ludivine-ckeditor-component.git] / sources / plugins / link / dialogs / anchor.js
1 /**
2 * @license Copyright (c) 2003-2017, 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
22 function getSelectedAnchor( selection ) {
23 var range = selection.getRanges()[ 0 ],
24 element = selection.getSelectedElement();
25
26 // In case of table cell selection, we want to shrink selection from td to a element.
27 range.shrink( CKEDITOR.SHRINK_ELEMENT );
28 element = range.getEnclosedNode();
29
30 if ( element && element.type === CKEDITOR.NODE_ELEMENT &&
31 ( element.data( 'cke-real-element-type' ) === 'anchor' || element.is( 'a' ) ) ) {
32 return element;
33 }
34 }
35
36 return {
37 title: editor.lang.link.anchor.title,
38 minWidth: 300,
39 minHeight: 60,
40 onOk: function() {
41 var name = CKEDITOR.tools.trim( this.getValueOf( 'info', 'txtName' ) );
42 var attributes = {
43 id: name,
44 name: name,
45 'data-cke-saved-name': name
46 };
47
48 if ( this._.selectedElement ) {
49 if ( this._.selectedElement.data( 'cke-realelement' ) ) {
50 var newFake = createFakeAnchor( editor, attributes );
51 newFake.replace( this._.selectedElement );
52
53 // Selecting fake element for IE. (http://dev.ckeditor.com/ticket/11377)
54 if ( CKEDITOR.env.ie ) {
55 editor.getSelection().selectElement( newFake );
56 }
57 } else {
58 this._.selectedElement.setAttributes( attributes );
59 }
60 } else {
61 var sel = editor.getSelection(),
62 range = sel && sel.getRanges()[ 0 ];
63
64 // Empty anchor
65 if ( range.collapsed ) {
66 var anchor = createFakeAnchor( editor, attributes );
67 range.insertNode( anchor );
68 } else {
69 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 9 )
70 attributes[ 'class' ] = 'cke_anchor';
71
72 // Apply style.
73 var style = new CKEDITOR.style( { element: 'a', attributes: attributes } );
74 style.type = CKEDITOR.STYLE_INLINE;
75 style.applyToRange( range );
76 }
77 }
78 },
79
80 onHide: function() {
81 delete this._.selectedElement;
82 },
83
84 onShow: function() {
85 var sel = editor.getSelection(),
86 fullySelected = getSelectedAnchor( sel ),
87 fakeSelected = fullySelected && fullySelected.data( 'cke-realelement' ),
88 linkElement = fakeSelected ?
89 CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, fullySelected ) :
90 CKEDITOR.plugins.link.getSelectedLink( editor );
91
92 if ( linkElement ) {
93 loadElements.call( this, linkElement );
94 !fakeSelected && sel.selectElement( linkElement );
95
96 if ( fullySelected ) {
97 this._.selectedElement = fullySelected;
98 }
99 }
100
101 this.getContentElement( 'info', 'txtName' ).focus();
102 },
103 contents: [ {
104 id: 'info',
105 label: editor.lang.link.anchor.title,
106 accessKey: 'I',
107 elements: [ {
108 type: 'text',
109 id: 'txtName',
110 label: editor.lang.link.anchor.name,
111 required: true,
112 validate: function() {
113 if ( !this.getValue() ) {
114 alert( editor.lang.link.anchor.errorName ); // jshint ignore:line
115 return false;
116 }
117 return true;
118 }
119 } ]
120 } ]
121 };
122 } );