ckeditor/plugins/crossreference/dialogs/crossreference-link.js
changeset 0 44d330dccc59
child 5 c925ae656709
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ckeditor/plugins/crossreference/dialogs/crossreference-link.js	Thu Dec 15 18:10:20 2016 +0300
     1.3 @@ -0,0 +1,240 @@
     1.4 +CKEDITOR.dialog.add('crossreference-link-dialog', function(editor) {
     1.5 +	
     1.6 +	var config = editor.config.crossreference;
     1.7 +	
     1.8 +	var updateAnchors = function(dialog) {
     1.9 +		dialog.setState(CKEDITOR.DIALOG_STATE_BUSY);
    1.10 +		
    1.11 +		dialog.anchors = [];
    1.12 +		dialog.anchorsByGuid = {};
    1.13 +		dialog.anchorsGroupNameByGuid = {};
    1.14 +		
    1.15 +		var type = null;
    1.16 +		var typeName = dialog.getValueOf('tab-main', 'type');
    1.17 +		if (typeName)
    1.18 +			type = config.types[typeName];
    1.19 +		
    1.20 +		dialog.setValueOf('tab-main', 'filter', '');
    1.21 +		
    1.22 +		var anchorSelect = dialog.getContentElement('tab-main', 'anchor');
    1.23 +		anchorSelect.clear();
    1.24 +		anchorSelect.add('', '');
    1.25 +		
    1.26 +		var anchorGroupSelect = dialog.getContentElement('tab-main', 'anchorGroup');
    1.27 +		anchorGroupSelect.clear();
    1.28 +		anchorGroupSelect.add('', '');
    1.29 +		if (type && type.groupAnchors == true)
    1.30 +			anchorGroupSelect.getElement().show();
    1.31 +		else
    1.32 +			anchorGroupSelect.getElement().hide();
    1.33 +		
    1.34 +		config.findAnchors(config, editor, type, function(anchors) {
    1.35 +			dialog.anchors = anchors;
    1.36 +			dialog.anchorsByGuid = {};
    1.37 +			dialog.anchorsGroupNameByGuid = {};
    1.38 +			
    1.39 +			for (var i = 0; i < anchors.length; i++) {
    1.40 +				var anchor = anchors[i];
    1.41 +				var guid = anchor.guid;
    1.42 +				var label = anchor.text;
    1.43 +				anchorSelect.add(label, guid);
    1.44 +				
    1.45 +				if (type.groupAnchors == true && anchor.groupGuid) {
    1.46 +					dialog.anchorsGroupNameByGuid[anchor.groupGuid] = anchor.groupName;
    1.47 +				}
    1.48 +				dialog.anchorsByGuid[guid] = anchor;
    1.49 +			}
    1.50 +			
    1.51 +			if (type && type.groupAnchors == true) {
    1.52 +				for (var groupGuid in dialog.anchorsGroupNameByGuid) {
    1.53 +					var groupName = dialog.anchorsGroupNameByGuid[groupGuid];
    1.54 +					anchorGroupSelect.add(groupName, groupGuid);
    1.55 +				}
    1.56 +			}
    1.57 +			
    1.58 +			// fix &nbsp; and others
    1.59 +			$('option', anchorSelect.getInputElement().$).each(function() {
    1.60 +				var option = $(this);
    1.61 +				var text = option.text();
    1.62 +				option.html(text);
    1.63 +			});
    1.64 +			
    1.65 +			if (!dialog.insertMode)
    1.66 +				anchorSelect.setup(dialog.element);
    1.67 +			
    1.68 +			dialog.setState(CKEDITOR.DIALOG_STATE_IDLE);
    1.69 +		});
    1.70 +	};
    1.71 +	
    1.72 +	var filterAnchors = function(dialog) {
    1.73 +		var filter = dialog.getValueOf('tab-main', 'filter');
    1.74 +		if (filter)
    1.75 +			filter = filter.trim().toLowerCase();
    1.76 +		else
    1.77 +			filter = '';
    1.78 +		
    1.79 +		var type = null;
    1.80 +		var typeName = dialog.getValueOf('tab-main', 'type');
    1.81 +		if (typeName)
    1.82 +			type = config.types[typeName];
    1.83 +		
    1.84 +		var groupGuid = null;
    1.85 +		if (type && type.groupAnchors == true)
    1.86 +			groupGuid = dialog.getValueOf('tab-main', 'anchorGroup');
    1.87 +		
    1.88 +		var anchorSelect = dialog.getContentElement('tab-main', 'anchor');
    1.89 +		var prevValue = anchorSelect.getValue();
    1.90 +		var selected = null;
    1.91 +		$('option', anchorSelect.getInputElement().$).each(function() {
    1.92 +			var option = $(this);
    1.93 +			var guid = option.val();
    1.94 +			var anchor = dialog.anchorsByGuid[guid];
    1.95 +			var text = option.text();
    1.96 +			
    1.97 +			var matches = filter.length == 0 || text.toLowerCase().indexOf(filter) != -1;
    1.98 +			if (groupGuid)
    1.99 +				matches = matches && anchor && anchor.groupGuid == groupGuid;
   1.100 +			
   1.101 +			if (matches) {
   1.102 +				option.removeAttr('disabled', 'disabled');
   1.103 +				if (selected == null)
   1.104 +					selected = option.val();
   1.105 +				else if (guid == prevValue)
   1.106 +					selected = option.val();
   1.107 +			} else {
   1.108 +				option.attr('disabled', 'disabled');
   1.109 +			}
   1.110 +		});
   1.111 +		anchorSelect.setValue(selected);
   1.112 +	};
   1.113 +	
   1.114 +	return {
   1.115 +		title : editor.lang.crossreference.link,
   1.116 +		minWidth : 400,
   1.117 +		minHeight : 150,
   1.118 +		
   1.119 +		contents : [ 
   1.120 +			{
   1.121 +				id : 'tab-main',
   1.122 +				label : editor.lang.crossreference.link,
   1.123 +				elements : [ 
   1.124 +					{
   1.125 +						type : 'vbox',
   1.126 +						widths : [ '100%' ],
   1.127 +						children : [
   1.128 +							{
   1.129 +								type : 'html',
   1.130 +								id : 'description',
   1.131 +								html : '<div style="white-space: normal; text-align: justify;">' + editor.lang.crossreference.linkDescription + '</div>',
   1.132 +							},
   1.133 +							{
   1.134 +								type : 'select',
   1.135 +								id : 'type',
   1.136 +								width: '100%',
   1.137 +								label : editor.lang.crossreference.anchorType,
   1.138 +								required: true,
   1.139 +								items : [['']],
   1.140 +								onLoad: function() {
   1.141 +									this.getInputElement().setStyle('width', '100%');
   1.142 +									for (var typeName in config.types) {
   1.143 +										var type = config.types[typeName];
   1.144 +										var label = type.name;
   1.145 +										this.add(label, type.type);
   1.146 +									}
   1.147 +								},
   1.148 +								onChange: function() {
   1.149 +									updateAnchors(this.getDialog());
   1.150 +								},
   1.151 +								setup: function(element) {
   1.152 +									this.setValue(element.getAttribute('cross-reference'));
   1.153 +								},
   1.154 +								commit: function(element) {
   1.155 +									element.setAttribute('cross-reference', this.getValue());
   1.156 +									element.setAttribute('cross-link', '');
   1.157 +								}
   1.158 +							},
   1.159 +							{
   1.160 +								type : 'select',
   1.161 +								id : 'anchorGroup',
   1.162 +								width: '100%',
   1.163 +								label : editor.lang.crossreference.anchorGroup,
   1.164 +								required: false,
   1.165 +								items : [['']],
   1.166 +								onLoad: function() {
   1.167 +									this.getInputElement().setStyle('width', '100%');
   1.168 +								},
   1.169 +								onChange: function() {
   1.170 +									filterAnchors(this.getDialog());
   1.171 +								}
   1.172 +							},
   1.173 +							{
   1.174 +								type : 'text',
   1.175 +								id : 'filter',
   1.176 +								width: '100%',
   1.177 +								label : editor.lang.crossreference.linkFilter,
   1.178 +								onKeyUp : function() {
   1.179 +									filterAnchors(this.getDialog());
   1.180 +								}
   1.181 +							},
   1.182 +							{
   1.183 +								type : 'select',
   1.184 +								id : 'anchor',
   1.185 +								width: '100%',
   1.186 +								label : editor.lang.crossreference.anchor,
   1.187 +								required: true,
   1.188 +								items : [['']],
   1.189 +								onLoad: function() {
   1.190 +									this.getInputElement().setStyle('width', '100%');
   1.191 +								},
   1.192 +								setup: function(element) {
   1.193 +									this.setValue(element.getAttribute('cross-guid'));
   1.194 +								},
   1.195 +								commit: function(element) {
   1.196 +									element.setAttribute('cross-guid', this.getValue());
   1.197 +								}
   1.198 +							}
   1.199 +						]
   1.200 +					} 
   1.201 +				]
   1.202 +			} 
   1.203 +		],
   1.204 +		
   1.205 +		onLoad : function() {
   1.206 +
   1.207 +		},
   1.208 +		
   1.209 +		onShow : function() {
   1.210 +			var selection = editor.getSelection();
   1.211 +			this.element = selection.getStartElement();
   1.212 +			if (this.element)
   1.213 +				this.element = this.element.getAscendant('a', true);
   1.214 +			if (!this.element || this.element.getName() != 'a') {
   1.215 +				this.element = editor.document.createElement('a');
   1.216 +				this.element.setAttribute('cross-link', '');
   1.217 +				this.insertMode = true;
   1.218 +			} else {
   1.219 +				this.insertMode = false;
   1.220 +			}
   1.221 +			
   1.222 +			if (this.insertMode)
   1.223 +				updateAnchors(this);
   1.224 +			else
   1.225 +				this.setupContent(this.element);
   1.226 +		},
   1.227 +		
   1.228 +		onOk : function() {
   1.229 +			if (!this.getValueOf('tab-main', 'type'))
   1.230 +				return;
   1.231 +			if (!this.getValueOf('tab-main', 'anchor'))
   1.232 +				return;
   1.233 +			
   1.234 +			this.commitContent(this.element);
   1.235 +			
   1.236 +			if (this.insertMode)
   1.237 +				editor.insertElement(this.element);
   1.238 +			
   1.239 +			editor.execCommand('update-crossreferences');
   1.240 +		}
   1.241 +		
   1.242 +	};
   1.243 +});