      1 /*
      2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
      3 For licensing, see LICENSE.html or http://ckeditor.com/license
      4 */
      5
      6 CKEDITOR.dialog.add( 'scaytcheck', function( editor )
      7 {
      8 	var firstLoad = true,
      9 		captions,
     10 		doc = CKEDITOR.document,
     11 		fckLang = 'en';
     12 	var dic_buttons = [
     13 		// [0] contains buttons for creating
     14 		"dic_create,dic_restore",
     15 		// [1] contains buton for manipulation
     16 		"dic_rename,dic_delete"
     17 	];
     18
     19 	var init_with_captions = function()
     20 	{
     21 		var dialog = this,
     22 			lang_list = dialog.data.scayt.getLangList(),
     23 			buttons = [ 'dic_create','dic_delete','dic_rename','dic_restore' ],
     24 			labels = [ 'mixedCase','mixedWithDigits','allCaps','ignoreDomainNames' ],
     25 			i;
     26
     27 		// Add buttons titles
     28 		for ( i in buttons )
     29 		{
     30 			var button = buttons[ i ];
     31 			doc.getById( button ).setHtml( '<span class="cke_dialog_ui_button">' + captions[ 'button_' + button]  +'</span>' );
     32 		}
     33 		doc.getById( 'dic_info' ).setHtml( captions[ 'dic_info' ] );
     34
     35 		// Fill options and dictionary labels.
     36 		for ( i in labels )
     37 		{
     38 			var label = 'label_' + labels[ i ],
     39 				labelElement = doc.getById( label );
     40
     41 			if (  'undefined' != typeof labelElement
     42 			   && 'undefined' != typeof captions[ label ]
     43 			   && 'undefined' != typeof dialog.options[labels[ i ]] )
     44 			{
     45 				labelElement.setHtml( captions[ label ] );
     46 				var labelParent = labelElement.getParent();
     47 				labelParent.$.style.display = "block";
     48 			}
     49 		}
     50
     51 		var about = '<p>' + captions[ 'about_throwt_image' ] + '</p>'+
     52 					'<p>' + captions[ 'version' ]  + dialog.data.scayt.version.toString() + '</p>' +
     53 					'<p>' + captions[ 'about_throwt_copy' ] + '</p>';
     54
     55 		doc.getById( 'scayt_about' ).setHtml( about );
     56
     57 		// Create languages tab.
     58 		var createOption = function( option, list )
     59 		{
     60 			var label = doc.createElement( 'label' );
     61 			label.setAttribute( 'for', 'cke_option' + option );
     62 			label.setHtml( list[ option ] );
     63
     64 			if ( dialog.sLang == option )	// Current.
     65 				dialog.chosed_lang = option;
     66
     67 			var div = doc.createElement( 'div' );
     68 			var radio = CKEDITOR.dom.element.createFromHtml( '<input id="cke_option' +
     69 					option + '" type="radio" ' +
     70 					( dialog.sLang == option ? 'checked="checked"' : '' ) +
     71 					' value="' + option + '" name="scayt_lang" />' );
     72
     73 			radio.on( 'click', function()
     74 				{
     75 					this.$.checked = true;
     76 					dialog.chosed_lang = option;
     77 				});
     78
     79 			div.append( radio );
     80 			div.append( label );
     81
     82 			return {
     83 				lang : list[ option ],
     84 				code : option,
     85 				radio : div
     86 			};
     87 		};
     88
     89 		var langList = [];
     90 		for ( i in lang_list.rtl )
     91 			langList[ langList.length ] = createOption( i, lang_list.ltr );
     92
     93 		for ( i in lang_list.ltr )
     94 			langList[ langList.length  ] = createOption( i, lang_list.ltr );
     95
     96 		langList.sort( 	function( lang1, lang2 )
     97 			{
     98 				return ( lang2.lang > lang1.lang ) ? -1 : 1 ;
     99 			});
    100
    101 		var fieldL = doc.getById( 'scayt_lcol' ),
    102 			fieldR = doc.getById( 'scayt_rcol' );
    103 		for ( i=0; i < langList.length; i++ )
    104 		{
    105 			var field = ( i < langList.length / 2 ) ? fieldL : fieldR;
    106 			field.append( langList[ i ].radio );
    107 		}
    108
    109 		// user dictionary handlers
    110 		var dic = {};
    111 		dic.dic_create = function( el, dic_name , dic_buttons )
    112 			{
    113 				// comma separated button's ids include repeats if exists
    114 				var all_buttons = dic_buttons[0] + ',' + dic_buttons[1];
    115
    116 				var err_massage = captions["err_dic_create"];
    117 				var suc_massage = captions["succ_dic_create"];
    118 				//console.info("--plugin ");
    119
    120 				scayt.createUserDictionary(dic_name,
    121 					function(arg)
    122 						{
    123 							//console.info( "dic_create callback called with args" , arg );
    124 							hide_dic_buttons ( all_buttons );
    125 							display_dic_buttons ( dic_buttons[1] );
    126 							suc_massage = suc_massage.replace("%s" , arg.dname );
    127 							dic_success_message (suc_massage);
    128 						},
    129 					function(arg)
    130 						{
    131 							//console.info( "dic_create errorback called with args" , arg )
    132 							err_massage = err_massage.replace("%s" ,arg.dname );
    133 							dic_error_message ( err_massage + "( "+ (arg.message || "") +")");
    134 						});
    135
    136 			};
    137
    138 		dic.dic_rename = function( el, dic_name , dic_buttons )
    139 			{
    140 				//
    141 				// try to rename dictionary
    142 				// @TODO: rename dict
    143 				//console.info ( captions["err_dic_rename"] )
    144 				var err_massage = captions["err_dic_rename"] || "";
    145 				var suc_massage = captions["succ_dic_rename"] || "";
    146 				scayt.renameUserDictionary(dic_name,
    147 					function(arg)
    148 						{
    149 							//console.info( "dic_rename callback called with args" , arg );
    150 							suc_massage = suc_massage.replace("%s" , arg.dname );
    151 							set_dic_name( dic_name );
    152 							dic_success_message ( suc_massage );
    153 						},
    154 					function(arg)
    155 						{
    156 							//console.info( "dic_rename errorback called with args" , arg )
    157 							err_massage = err_massage.replace("%s" , arg.dname  );
    158 							set_dic_name( dic_name );
    159 							dic_error_message( err_massage + "( " + ( arg.message || "" ) + " )" );
    160 						});
    161 			};
    162
    163 		dic.dic_delete = function ( el, dic_name , dic_buttons )
    164 			{
    165 				var all_buttons = dic_buttons[0] + ',' + dic_buttons[1];
    166 				var err_massage = captions["err_dic_delete"];
    167 				var suc_massage = captions["succ_dic_delete"];
    168
    169 				// try to delete dictionary
    170 				// @TODO: delete dict
    171 				scayt.deleteUserDictionary(
    172 					function(arg)
    173 						{
    174 							//console.info( "dic_delete callback " , dic_name ,arg );
    175 							suc_massage = suc_massage.replace("%s" , arg.dname );
    176 							hide_dic_buttons ( all_buttons );
    177 							display_dic_buttons ( dic_buttons[0] );
    178 							set_dic_name( "" ); // empty input field
    179 							dic_success_message( suc_massage );
    180 						},
    181 					function(arg)
    182 						{
    183 							//console.info( " dic_delete errorback called with args" , arg )
    184 							err_massage = err_massage.replace("%s" , arg.dname );
    185 							dic_error_message(err_massage);
    186 						});
    187 			};
    188
    189 		dic.dic_restore = dialog.dic_restore || function ( el, dic_name , dic_buttons )
    190 			{
    191 				// try to restore existing dictionary
    192 				var all_buttons = dic_buttons[0] + ',' + dic_buttons[1];
    193 				var err_massage = captions["err_dic_restore"];
    194 				var suc_massage = captions["succ_dic_restore"];
    195
    196 				scayt.restoreUserDictionary(dic_name,
    197 					function(arg)
    198 						{
    199 							//console.info( "dic_restore callback called with args" , arg );
    200 							suc_massage = suc_massage.replace("%s" , arg.dname );
    201 							hide_dic_buttons ( all_buttons );
    202 							display_dic_buttons(dic_buttons[1]);
    203 							dic_success_message( suc_massage );
    204 						},
    205 					function(arg)
    206 						{
    207 							//console.info( " dic_restore errorback called with args" , arg )
    208 							err_massage = err_massage.replace("%s" , arg.dname );
    209 							dic_error_message( err_massage );
    210 						});
    211 			};
    212
    213 		// ** bind event listeners
    214 		var arr_buttons = ( dic_buttons[0] + ',' + dic_buttons[1] ).split( ',' ),
    215 			l;
    216
    217 		for ( i = 0, l = arr_buttons.length ; i < l ; i += 1 )
    218 		{
    219 		 	var dic_button = doc.getById(arr_buttons[i]);
    220
    221 			dic_button.on( 'click', function ()
    222 				{
    223 					var dic_name = doc.getById('dic_name').getValue();
    224 					if ( !dic_name )
    225 					{
    226 						dic_error_message(" Dictionary name should not be empty. ");
    227 						return false;
    228 					}
    229 					//apply handler
    230 					dic[ this.getId() ].apply( null, [ this, dic_name, dic_buttons ] );
    231
    232 					return true;
    233 				});
    234 		}
    235 	};
    236 	var reload = function()
    237 	{
    238 		var dialog = this;
    239
    240 		// Animate options.
    241 		for ( var i in dialog.options )
    242 		{
    243 			var checkbox = doc.getById( i );
    244 			if ( checkbox )
    245 			{
    246 				checkbox.removeAttribute( 'checked' );
    247 				if ( dialog.options[ i ] == 1 )
    248 					checkbox.setAttribute( 'checked', 'checked' );
    249
    250 				// Bind events. Do it only once.
    251 				if ( firstLoad )
    252 				{
    253 					checkbox.on( 'click', function()
    254 						{
    255 							dialog.options[ this.getId() ] = this.$.checked ? 1 : 0 ;
    256 						} );
    257 				}
    258 			}
    259 		}
    260
    261 		// * user dictionary
    262 		scayt.getNameUserDictionary(
    263 			function( o )
    264 			{
    265 				var dic_name = o.dname;
    266 				if ( dic_name )
    267 				{
    268 					doc.getById( 'dic_name' ).setValue(dic_name);
    269 					display_dic_buttons( dic_buttons[1] );
    270 				}
    271 				else
    272 					display_dic_buttons( dic_buttons[0] );
    273
    274 			},
    275 			function ()
    276 				{
    277 					doc.getById( 'dic_name' ).setValue("");
    278 				});
    279
    280 		dic_success_message("");
    281 	};
    282
    283 	function dic_error_message ( m )
    284 		{
    285 			doc.getById('dic_message').setHtml('<span style="color:red;">' + m + '</span>' );
    286 		}
    287         function dic_success_message ( m )
    288 		{
    289 			doc.getById('dic_message').setHtml('<span style="color:blue;">' + m + '</span>') ;
    290 		}
    291 	function display_dic_buttons ( sIds )
    292 		{
    293
    294 			sIds = new String( sIds );
    295 			var aIds = sIds.split(',');
    296 			for ( var i=0, l = aIds.length; i < l ; i+=1)
    297 			{
    298 				doc.getById( aIds[i] ).$.style.display = "inline";
    299 			}
    300
    301 		}
    302 	function hide_dic_buttons ( sIds )
    303 		{
    304 			sIds = new String( sIds );
    305 			var aIds = sIds.split(',');
    306 			for ( var i = 0, l = aIds.length; i < l ; i += 1 )
    307 			{
    308 				doc.getById( aIds[i] ).$.style.display = "none";
    309 			}
    310 		}
    311 	function set_dic_name ( dic_name )
    312 		{
    313 			doc.getById('dic_name').$.value= dic_name;
    314 		}
    315
    316 	return {
    317 		title : editor.lang.scayt.title,
    318 		minWidth : 340,
    319 		minHeight : 200,
    320 		onShow : function()
    321 			{
    322 				var dialog = this;
    323 				dialog.data = editor.fire( 'scaytDialog', {} );
    324 				dialog.options = dialog.data.scayt_control.option();
    325 				dialog.sLang = dialog.data.scayt_control.sLang;
    326
    327 				if ( !dialog.data || !dialog.data.scayt || !dialog.data.scayt_control )
    328 				{
    329 					alert( 'Error loading application service' );
    330 					dialog.hide();
    331 					return;
    332 				}
    333
    334 				var stop = 0;
    335 				if ( firstLoad )
    336 				{
    337 					dialog.data.scayt.getCaption( 'en', function( caps )
    338 						{
    339 							if ( stop++ > 0 )	// Once only
    340 								return;
    341 							captions = caps;
    342 							init_with_captions.apply( dialog );
    343 							reload.apply( dialog );
    344 							firstLoad = false;
    345 						});
    346 				}
    347 				else
    348 					reload.apply( dialog );
    349
    350 				dialog.selectPage( dialog.data.tab );
    351 			},
    352 			onOk : function()
    353 			{
    354 				var scayt_control =  this.data.scayt_control,
    355 					o = scayt_control.option(),
    356 					c = 0;
    357
    358 				// Set up options if any was set.
    359 				for ( var oN in this.options )
    360 				{
    361 					if (o[oN] != this.options[ oN ] && c === 0 )
    362 					{
    363 						scayt_control.option( this.options );
    364 						c++;
    365 					}
    366 				}
    367
    368 				// Setup languge if it was changed.
    369 				var csLang = this.chosed_lang;
    370 				if ( csLang && this.data.sLang != csLang )
    371 				{
    372 					scayt_control.setLang( csLang );
    373 					c++;
    374 				}
    375 				if ( c > 0 )
    376 					scayt_control.refresh();
    377 			},
    378 			contents : [
    379 				{
    380 					id : 'options',
    381 					label : editor.lang.scayt.optionsTab,
    382 					elements : [
    383 						{
    384 							type : 'html',
    385 							id : 'options',
    386 							html : 	'<div class="inner_options">' +
    387 									'	<div class="messagebox"></div>' +
    388 									'	<div style="display:none;">' +
    389 									'		<input type="checkbox" value="0" id="allCaps" />' +
    390 									'		<label for="allCaps" id="label_allCaps"></label>' +
    391 									'	</div>' +
    392 									'	<div style="display:none;">' +
    393 									'		<input type="checkbox" value="0" id="ignoreDomainNames" />' +
    394 									'		<label for="ignoreDomainNames" id="label_ignoreDomainNames"></label>' +
    395 									'	</div>' +
    396 									'	<div style="display:none;">' +
    397 									'	<input type="checkbox" value="0" id="mixedCase" />' +
    398 									'		<label for="mixedCase" id="label_mixedCase"></label>' +
    399 									'	</div>' +
    400 									'	<div style="display:none;">' +
    401 									'		<input type="checkbox" value="0" id="mixedWithDigits" />' +
    402 									'		<label for="mixedWithDigits" id="label_mixedWithDigits"></label>' +
    403 									'	</div>' +
    404 									'</div>'
    405 						}
    406 					]
    407 				},
    408 				{
    409 					id : 'langs',
    410 					label : editor.lang.scayt.languagesTab,
    411 					elements : [
    412 						{
    413 							type : 'html',
    414 							id : 'langs',
    415 							html : 	'<div class="inner_langs">' +
    416 									'	<div class="messagebox"></div>	' +
    417 									'   <div style="float:left;width:47%;margin-left:5px;" id="scayt_lcol" ></div>' +
    418 									'   <div style="float:left;width:47%;margin-left:15px;" id="scayt_rcol"></div>' +
    419 									'</div>'
    420 						}
    421 					]
    422 				},
    423 				{
    424 					id : 'dictionaries',
    425 					label : editor.lang.scayt.dictionariesTab,
    426 					elements : [
    427 						{
    428 							type : 'html',
    429 							style: '',
    430 							id : 'dic',
    431 							html : 	'<div class="inner_dictionary" style="text-align:left; white-space:normal;">' +
    432 									'	<div style="margin:5px auto; width:80%;white-space:normal; overflow:hidden;" id="dic_message"> </div>' +
    433 									'	<div style="margin:5px auto; width:80%;white-space:normal;"> ' +
    434 									'       <span class="cke_dialog_ui_labeled_label" >Dictionary name</span><br>'+
    435 									'		<span class="cke_dialog_ui_labeled_content" >'+
    436 									'			<div class="cke_dialog_ui_input_text">'+
    437 									'				<input id="dic_name" type="text" class="cke_dialog_ui_input_text"/>'+
    438 									'		</div></span></div>'+
    439 									'		<div style="margin:5px auto; width:80%;white-space:normal;">'+
    440 									'			<a style="display:none;" class="cke_dialog_ui_button" href="javascript:void(0)" id="dic_create">'+
    441 									'				</a>' +
    442 									'			<a  style="display:none;" class="cke_dialog_ui_button" href="javascript:void(0)" id="dic_delete">'+
    443 									'				</a>' +
    444 									'			<a  style="display:none;" class="cke_dialog_ui_button" href="javascript:void(0)" id="dic_rename">'+
    445 									'				</a>' +
    446 									'			<a  style="display:none;" class="cke_dialog_ui_button" href="javascript:void(0)" id="dic_restore">'+
    447 									'				</a>' +
    448 									'		</div>' +
    449 									'	<div style="margin:5px auto; width:95%;white-space:normal;" id="dic_info"></div>' +
    450 									'</div>'
    451 						}
    452 					]
    453 				},
    454 				{
    455 					id : 'about',
    456 					label : editor.lang.scayt.aboutTab,
    457 					elements : [
    458 						{
    459 							type : 'html',
    460 							id : 'about',
    461 							style : 'margin: 10px 40px;',
    462 							html : '<div id="scayt_about"></div>'
    463 						}
    464 					]
    465 				}
    466 			]
    467         };
    468 });
    469