2014-04-11 20:19:54 +00:00
|
|
|
|
/**
|
2018-06-17 16:07:19 +00:00
|
|
|
|
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
|
|
|
|
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
2014-04-11 20:19:54 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
CKEDITOR.dialog.add( 'anchor', function( editor ) {
|
|
|
|
|
// Function called in onShow to load selected element.
|
|
|
|
|
var loadElements = function( element ) {
|
|
|
|
|
this._.selectedElement = element;
|
|
|
|
|
|
|
|
|
|
var attributeValue = element.data( 'cke-saved-name' );
|
|
|
|
|
this.setValueOf( 'info', 'txtName', attributeValue || '' );
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-17 16:07:19 +00:00
|
|
|
|
function createFakeAnchor( editor, attributes ) {
|
|
|
|
|
return editor.createFakeElement( editor.document.createElement( 'a', {
|
|
|
|
|
attributes: attributes
|
|
|
|
|
} ), 'cke_anchor', 'anchor' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getSelectedAnchor( selection ) {
|
|
|
|
|
var range = selection.getRanges()[ 0 ],
|
|
|
|
|
element = selection.getSelectedElement();
|
|
|
|
|
|
|
|
|
|
// In case of table cell selection, we want to shrink selection from td to a element.
|
|
|
|
|
range.shrink( CKEDITOR.SHRINK_ELEMENT );
|
|
|
|
|
element = range.getEnclosedNode();
|
|
|
|
|
|
|
|
|
|
if ( element && element.type === CKEDITOR.NODE_ELEMENT &&
|
|
|
|
|
( element.data( 'cke-real-element-type' ) === 'anchor' || element.is( 'a' ) ) ) {
|
|
|
|
|
return element;
|
|
|
|
|
}
|
2014-04-11 20:19:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
title: editor.lang.link.anchor.title,
|
|
|
|
|
minWidth: 300,
|
|
|
|
|
minHeight: 60,
|
|
|
|
|
onOk: function() {
|
|
|
|
|
var name = CKEDITOR.tools.trim( this.getValueOf( 'info', 'txtName' ) );
|
|
|
|
|
var attributes = {
|
|
|
|
|
id: name,
|
|
|
|
|
name: name,
|
|
|
|
|
'data-cke-saved-name': name
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if ( this._.selectedElement ) {
|
|
|
|
|
if ( this._.selectedElement.data( 'cke-realelement' ) ) {
|
2018-06-17 16:07:19 +00:00
|
|
|
|
var newFake = createFakeAnchor( editor, attributes );
|
2014-04-11 20:19:54 +00:00
|
|
|
|
newFake.replace( this._.selectedElement );
|
2018-06-17 16:07:19 +00:00
|
|
|
|
|
|
|
|
|
// Selecting fake element for IE. (https://dev.ckeditor.com/ticket/11377)
|
|
|
|
|
if ( CKEDITOR.env.ie ) {
|
|
|
|
|
editor.getSelection().selectElement( newFake );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-04-11 20:19:54 +00:00
|
|
|
|
this._.selectedElement.setAttributes( attributes );
|
2018-06-17 16:07:19 +00:00
|
|
|
|
}
|
2014-04-11 20:19:54 +00:00
|
|
|
|
} else {
|
|
|
|
|
var sel = editor.getSelection(),
|
|
|
|
|
range = sel && sel.getRanges()[ 0 ];
|
|
|
|
|
|
|
|
|
|
// Empty anchor
|
|
|
|
|
if ( range.collapsed ) {
|
2018-06-17 16:07:19 +00:00
|
|
|
|
var anchor = createFakeAnchor( editor, attributes );
|
2014-04-11 20:19:54 +00:00
|
|
|
|
range.insertNode( anchor );
|
|
|
|
|
} else {
|
|
|
|
|
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 9 )
|
|
|
|
|
attributes[ 'class' ] = 'cke_anchor';
|
|
|
|
|
|
|
|
|
|
// Apply style.
|
|
|
|
|
var style = new CKEDITOR.style( { element: 'a', attributes: attributes } );
|
|
|
|
|
style.type = CKEDITOR.STYLE_INLINE;
|
2018-06-17 16:07:19 +00:00
|
|
|
|
style.applyToRange( range );
|
2014-04-11 20:19:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onHide: function() {
|
|
|
|
|
delete this._.selectedElement;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onShow: function() {
|
2018-06-17 16:07:19 +00:00
|
|
|
|
var sel = editor.getSelection(),
|
|
|
|
|
fullySelected = getSelectedAnchor( sel ),
|
|
|
|
|
fakeSelected = fullySelected && fullySelected.data( 'cke-realelement' ),
|
|
|
|
|
linkElement = fakeSelected ?
|
|
|
|
|
CKEDITOR.plugins.link.tryRestoreFakeAnchor( editor, fullySelected ) :
|
|
|
|
|
CKEDITOR.plugins.link.getSelectedLink( editor );
|
|
|
|
|
|
|
|
|
|
if ( linkElement ) {
|
|
|
|
|
loadElements.call( this, linkElement );
|
|
|
|
|
!fakeSelected && sel.selectElement( linkElement );
|
|
|
|
|
|
|
|
|
|
if ( fullySelected ) {
|
2014-04-11 20:19:54 +00:00
|
|
|
|
this._.selectedElement = fullySelected;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.getContentElement( 'info', 'txtName' ).focus();
|
|
|
|
|
},
|
2018-06-17 16:07:19 +00:00
|
|
|
|
contents: [ {
|
2014-04-11 20:19:54 +00:00
|
|
|
|
id: 'info',
|
|
|
|
|
label: editor.lang.link.anchor.title,
|
|
|
|
|
accessKey: 'I',
|
2018-06-17 16:07:19 +00:00
|
|
|
|
elements: [ {
|
2014-04-11 20:19:54 +00:00
|
|
|
|
type: 'text',
|
|
|
|
|
id: 'txtName',
|
|
|
|
|
label: editor.lang.link.anchor.name,
|
|
|
|
|
required: true,
|
|
|
|
|
validate: function() {
|
|
|
|
|
if ( !this.getValue() ) {
|
2018-06-17 16:07:19 +00:00
|
|
|
|
alert( editor.lang.link.anchor.errorName ); // jshint ignore:line
|
2014-04-11 20:19:54 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-06-17 16:07:19 +00:00
|
|
|
|
} ]
|
|
|
|
|
} ]
|
2014-04-11 20:19:54 +00:00
|
|
|
|
};
|
|
|
|
|
} );
|