ckeditor/plugins/crossreference/dialogs/crossreference-link.js
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 21 Dec 2016 17:20:19 +0300
changeset 5 c925ae656709
parent 0 44d330dccc59
permissions -rw-r--r--
Update crossreference plugin
     1 CKEDITOR.dialog.add('crossreference-link-dialog', function(editor) {
     2 	
     3 	var config = editor.config.crossreference;
     4 	
     5 	var updateAnchors = function(dialog) {
     6 		dialog.setState(CKEDITOR.DIALOG_STATE_BUSY);
     7 		
     8 		dialog.anchors = [];
     9 		dialog.anchorsByGuid = {};
    10 		dialog.anchorsGroupNameByGuid = {};
    11 		
    12 		var type = null;
    13 		var typeName = dialog.getValueOf('tab-main', 'type');
    14 		if (typeName)
    15 			type = config.types[typeName];
    16 		
    17 		var anchorSelect = dialog.getContentElement('tab-main', 'anchor');
    18 		anchorSelect.clear();
    19 		anchorSelect.add('', '');
    20 		
    21 		var anchorGroupSelect = dialog.getContentElement('tab-main', 'anchorGroup');
    22 		anchorGroupSelect.clear();
    23 		anchorGroupSelect.add('', '');
    24 		if (type && type.groupAnchors == true)
    25 			anchorGroupSelect.getElement().show();
    26 		else
    27 			anchorGroupSelect.getElement().hide();
    28 		
    29 		config.findAnchors(config, editor, type, function(anchors) {
    30 			dialog.anchors = anchors;
    31 			dialog.anchorsByGuid = {};
    32 			dialog.anchorsGroupNameByGuid = {};
    33 			
    34 			for (var i = 0; i < anchors.length; i++) {
    35 				var anchor = anchors[i];
    36 				var guid = anchor.guid;
    37 				var label = anchor.text;
    38 				anchorSelect.add(label, guid);
    39 				
    40 				if (type.groupAnchors == true && anchor.groupGuid) {
    41 					dialog.anchorsGroupNameByGuid[anchor.groupGuid] = anchor.groupName;
    42 				}
    43 				dialog.anchorsByGuid[guid] = anchor;
    44 			}
    45 			
    46 			if (type && type.groupAnchors == true) {
    47 				for (var groupGuid in dialog.anchorsGroupNameByGuid) {
    48 					var groupName = dialog.anchorsGroupNameByGuid[groupGuid];
    49 					anchorGroupSelect.add(groupName, groupGuid);
    50 				}
    51 			}
    52 			
    53 			// fix &nbsp; and others
    54 			$('option', anchorSelect.getInputElement().$).each(function() {
    55 				var option = $(this);
    56 				var text = option.text();
    57 				option.html(text);
    58 			});
    59 			
    60 			if (dialog.insertMode)
    61 				filterAnchors(dialog);
    62 			else
    63 				anchorSelect.setup(dialog.element);
    64 			
    65 			dialog.setState(CKEDITOR.DIALOG_STATE_IDLE);
    66 		});
    67 	};
    68 	
    69 	var filterAnchors = function(dialog) {
    70 		var filter = dialog.getValueOf('tab-main', 'filter');
    71 		if (filter)
    72 			filter = filter.trim().toLowerCase();
    73 		else
    74 			filter = '';
    75 		
    76 		var type = null;
    77 		var typeName = dialog.getValueOf('tab-main', 'type');
    78 		if (typeName)
    79 			type = config.types[typeName];
    80 		
    81 		var groupGuid = null;
    82 		if (type && type.groupAnchors == true)
    83 			groupGuid = dialog.getValueOf('tab-main', 'anchorGroup');
    84 		
    85 		var anchorSelect = dialog.getContentElement('tab-main', 'anchor');
    86 		var prevValue = anchorSelect.getValue();
    87 		var selected = null;
    88 		$('option', anchorSelect.getInputElement().$).each(function() {
    89 			var option = $(this);
    90 			var guid = option.val();
    91 			var anchor = dialog.anchorsByGuid[guid];
    92 			var text = option.text();
    93 			
    94 			var matches = filter.length == 0 || text.toLowerCase().indexOf(filter) != -1;
    95 			if (groupGuid)
    96 				matches = matches && anchor && anchor.groupGuid == groupGuid;
    97 			
    98 			if (matches) {
    99 				option.removeAttr('disabled', 'disabled');
   100 				if (selected == null)
   101 					selected = option.val();
   102 				else if (guid == prevValue)
   103 					selected = option.val();
   104 			} else {
   105 				option.attr('disabled', 'disabled');
   106 			}
   107 		});
   108 		anchorSelect.setValue(selected);
   109 	};
   110 	
   111 	return {
   112 		title : editor.lang.crossreference.link,
   113 		minWidth : 400,
   114 		minHeight : 150,
   115 		
   116 		contents : [ 
   117 			{
   118 				id : 'tab-main',
   119 				label : editor.lang.crossreference.link,
   120 				elements : [ 
   121 					{
   122 						type : 'vbox',
   123 						widths : [ '100%' ],
   124 						children : [
   125 							{
   126 								type : 'html',
   127 								id : 'description',
   128 								html : '<div style="white-space: normal; text-align: justify;">' + editor.lang.crossreference.linkDescription + '</div>',
   129 							},
   130 							{
   131 								type : 'select',
   132 								id : 'type',
   133 								width: '100%',
   134 								label : editor.lang.crossreference.anchorType,
   135 								required: true,
   136 								items : [['']],
   137 								onLoad: function() {
   138 									this.getInputElement().setStyle('width', '100%');
   139 									for (var typeName in config.types) {
   140 										var type = config.types[typeName];
   141 										var label = type.name;
   142 										this.add(label, type.type);
   143 									}
   144 								},
   145 								onChange: function() {
   146 									updateAnchors(this.getDialog());
   147 								},
   148 								setup: function(element) {
   149 									this.setValue(element.getAttribute('cross-reference'));
   150 								},
   151 								commit: function(element) {
   152 									element.setAttribute('cross-reference', this.getValue());
   153 									element.setAttribute('cross-link', '');
   154 								}
   155 							},
   156 							{
   157 								type : 'select',
   158 								id : 'anchorGroup',
   159 								width: '100%',
   160 								label : editor.lang.crossreference.anchorGroup,
   161 								required: false,
   162 								items : [['']],
   163 								onLoad: function() {
   164 									this.getInputElement().setStyle('width', '100%');
   165 								},
   166 								onChange: function() {
   167 									filterAnchors(this.getDialog());
   168 								}
   169 							},
   170 							{
   171 								type : 'text',
   172 								id : 'filter',
   173 								width: '100%',
   174 								label : editor.lang.crossreference.linkFilter,
   175 								onKeyUp : function() {
   176 									filterAnchors(this.getDialog());
   177 								}
   178 							},
   179 							{
   180 								type : 'select',
   181 								id : 'anchor',
   182 								width: '100%',
   183 								label : editor.lang.crossreference.anchor,
   184 								required: true,
   185 								items : [['']],
   186 								onLoad: function() {
   187 									this.getInputElement().setStyle('width', '100%');
   188 								},
   189 								setup: function(element) {
   190 									this.setValue(element.getAttribute('cross-guid'));
   191 								},
   192 								commit: function(element) {
   193 									element.setAttribute('cross-guid', this.getValue());
   194 								}
   195 							}
   196 						]
   197 					} 
   198 				]
   199 			} 
   200 		],
   201 		
   202 		onLoad : function() {
   203 
   204 		},
   205 		
   206 		onShow : function() {
   207 			var selection = editor.getSelection();
   208 			this.element = selection.getStartElement();
   209 			if (this.element)
   210 				this.element = this.element.getAscendant('a', true);
   211 			if (!this.element || this.element.getName() != 'a' || !this.element.hasAttribute('cross-link')) {
   212 				this.element = editor.document.createElement('a');
   213 				this.element.setAttribute('cross-link', '');
   214 				this.insertMode = true;
   215 			} else {
   216 				this.insertMode = false;
   217 			}
   218 			
   219 			if (this.insertMode) {
   220 				this.setValueOf('tab-main', 'filter', selection.getSelectedText().trim());
   221 				updateAnchors(this);
   222 			} else
   223 				this.setupContent(this.element);
   224 		},
   225 		
   226 		onOk : function() {
   227 			if (!this.getValueOf('tab-main', 'type'))
   228 				return;
   229 			if (!this.getValueOf('tab-main', 'anchor'))
   230 				return;
   231 			
   232 			this.commitContent(this.element);
   233 			
   234 			if (this.insertMode)
   235 				editor.insertElement(this.element);
   236 			
   237 			editor.execCommand('update-crossreferences');
   238 		}
   239 		
   240 	};
   241 });