ckeditor/plugins/crossreference/dialogs/crossreference-anchor.js
author indvd00m (gotoindvdum[at]gmail[dot]com)
Thu, 15 Dec 2016 18:10:20 +0300
changeset 0 44d330dccc59
child 5 c925ae656709
permissions -rw-r--r--
Init sample
     1 CKEDITOR.dialog.add('crossreference-anchor-dialog', function(editor) {
     2 	
     3 	var config = editor.config.crossreference;
     4 	
     5 	var generateUUID = function() {
     6 		var d = new Date().getTime();
     7 		if(window.performance && typeof window.performance.now === "function"){
     8 			d += performance.now(); // use high-precision timer if available
     9 		}
    10 		var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    11 			var r = (d + Math.random()*16)%16 | 0;
    12 			d = Math.floor(d/16);
    13 			return (c=='x' ? r : (r&0x3|0x8)).toString(16);
    14 		});
    15 		return uuid;
    16 	};
    17 	
    18 	return {
    19 		title : editor.lang.crossreference.anchor,
    20 		minWidth : 400,
    21 		minHeight : 150,
    22 		
    23 		contents : [ 
    24 			{
    25 				id : 'tab-main',
    26 				label : editor.lang.crossreference.anchor,
    27 				elements : [ 
    28 					{
    29 						type : 'vbox',
    30 						widths : [ '100%' ],
    31 						children : [
    32 							{
    33 								type : 'html',
    34 								id : 'description',
    35 								html : '<div style="white-space: normal; text-align: justify;">' + editor.lang.crossreference.anchorDescription + '</div>',
    36 							},
    37 							{
    38 								type : 'select',
    39 								id : 'type',
    40 								width: '100%',
    41 								label : editor.lang.crossreference.anchorType,
    42 								items : [],
    43 								required: true,
    44 								items : [['']],
    45 								onLoad: function() {
    46 									this.getInputElement().setStyle('width', '100%');
    47 									for (var typeName in config.types) {
    48 										var type = config.types[typeName];
    49 										var label = type.name;
    50 										if (type.allowCreateAnchors == false)
    51 											continue;
    52 										this.add(label, type.type);
    53 									}
    54 								},
    55 								setup: function(element) {
    56 									this.setValue(element.getAttribute('cross-reference'));
    57 								},
    58 								commit: function(element) {
    59 									element.setAttribute('cross-reference', this.getValue());
    60 									element.setAttribute('cross-anchor', '');
    61 								}
    62 							},
    63 							{
    64 								type : 'text',
    65 								id : 'name',
    66 								width: '100%',
    67 								label : editor.lang.crossreference.anchorName,
    68 								required: true,
    69 								setup: function(element) {
    70 									this.setValue(element.getAttribute('cross-name'));
    71 								},
    72 								commit: function(element) {
    73 									element.setAttribute('cross-name', this.getValue());
    74 								}
    75 							}
    76 						]
    77 					} 
    78 				]
    79 			} 
    80 		],
    81 		
    82 		onLoad : function() {
    83 		
    84 		},
    85 		
    86 		onShow : function() {
    87 			var selection = editor.getSelection();
    88 			this.element = selection.getStartElement();
    89 			if (this.element)
    90 				this.element = this.element.getAscendant('a', true);
    91 			if (!this.element || this.element.getName() != 'a') {
    92 				this.element = editor.document.createElement('a');
    93 				var guid = generateUUID();
    94 				this.element.setAttribute('cross-guid', guid);
    95 				this.insertMode = true;
    96 			} else {
    97 				this.insertMode = false;
    98 			}
    99 			
   100 			if (!this.insertMode)
   101 				this.setupContent(this.element);
   102 		},
   103 		
   104 		onOk : function() {
   105 			if (!this.getValueOf('tab-main', 'type'))
   106 				return;
   107 			
   108 			this.commitContent(this.element);
   109 			
   110 			if (this.insertMode)
   111 				editor.insertElement(this.element);
   112 			
   113 			editor.execCommand('update-crossreferences');
   114 		}
   115 		
   116 	};
   117 });