includes/clientside/static/editor.js
changeset 1343 2a31905a567d
parent 1334 b9ecf7618fbc
child 1344 dc96d6c5cd1e
equal deleted inserted replaced
1342:2164d18cb10c 1343:2a31905a567d
     6 var editor_img_path = cdnPath + '/images/editor';
     6 var editor_img_path = cdnPath + '/images/editor';
     7 var editor_save_lock = false;
     7 var editor_save_lock = false;
     8 var editor_wikitext_transform_enable = true;
     8 var editor_wikitext_transform_enable = true;
     9 var editor_orig_text = '';
     9 var editor_orig_text = '';
    10 var editor_last_draft = '';
    10 var editor_last_draft = '';
       
    11 var page_format = 'wikitext';
    11 
    12 
    12 window.ajaxEditor = function(revid)
    13 window.ajaxEditor = function(revid)
    13 {
    14 {
    14 	if ( KILL_SWITCH )
    15 	if ( KILL_SWITCH )
    15 		return true;
    16 		return true;
   144 		a.appendChild(gen_sprite(scriptPath + '/images/editor/sprite.png', 16, 16, 0, 112));
   145 		a.appendChild(gen_sprite(scriptPath + '/images/editor/sprite.png', 16, 16, 0, 112));
   145 		a.appendChild(document.createTextNode(' ' + $lang.get('editor_btn_graphical')));
   146 		a.appendChild(document.createTextNode(' ' + $lang.get('editor_btn_graphical')));
   146 		span_mce.appendChild(a);
   147 		span_mce.appendChild(a);
   147 		toggler.appendChild(span_mce);
   148 		toggler.appendChild(span_mce);
   148 		
   149 		
   149 		if ( response.page_format == 'xhtml' )
   150 		if ( response.page_format == 'wikitext' )
   150 		{
   151 		{
   151 			// Current selection is TinyMCE - make span_wiki have the link and span_mce be plaintext
   152 			// Current selection is a custom editor plugin - make span_wiki have the link and span_mce be plaintext
       
   153 			span_wiki.style.display = 'none';
       
   154 		}
       
   155 		else
       
   156 		{
       
   157 			// Current selection is wikitext - set span_wiki to plaintext and span_mce to link
   152 			span_mce.style.display = 'none';
   158 			span_mce.style.display = 'none';
   153 		}
       
   154 		else
       
   155 		{
       
   156 			// Current selection is wikitext - set span_wiki to plaintext and span_mce to link
       
   157 			span_wiki.style.display = 'none';
       
   158 		}
   159 		}
   159 	}
   160 	}
   160 	
   161 	
   161 	// Form (to allow submits from MCE to trigger a real save)
   162 	// Form (to allow submits from MCE to trigger a real save)
   162 	var form = document.createElement('form');
   163 	var form = document.createElement('form');
   491 	$dynano('ajaxEditArea').object._edTimestamp = timestamp;
   492 	$dynano('ajaxEditArea').object._edTimestamp = timestamp;
   492 	$dynano('ajaxEditArea').setContent(content);
   493 	$dynano('ajaxEditArea').setContent(content);
   493 	editor_orig_text = content;
   494 	editor_orig_text = content;
   494 	
   495 	
   495 	// If the editor preference is tinymce, switch the editor to TinyMCE now
   496 	// If the editor preference is tinymce, switch the editor to TinyMCE now
   496 	if ( response.page_format == 'xhtml' && allow_wysiwyg )
   497 	if ( response.page_format != 'wikitext' && allow_wysiwyg )
   497 	{
   498 	{
   498 		$dynano('ajaxEditArea').switchToMCE(false);
   499 		if ( typeof(editor_formats[response.page_format]) == 'object' )
       
   500 		{
       
   501 			// instruct the editor plugin to go ahead and build its UI
       
   502 			editor_formats[response.page_format].ui_construct();
       
   503 			window.page_format = response.page_format;
       
   504 		}
       
   505 		else
       
   506 		{
       
   507 			// Page was formatted with a plugin that no longer exists
       
   508 			miniPromptMessage({
       
   509 				title: $lang.get('editor_msg_convert_missing_plugin_title'),
       
   510 				message: $lang.get('editor_msg_convert_missing_plugin_body', { plugin: response.page_format }),
       
   511 				buttons: [
       
   512 					{
       
   513 						text: $lang.get('etc_ok'),
       
   514 						onclick: function()
       
   515 						{
       
   516 							miniPromptDestroy(this);
       
   517 							return false;
       
   518 						}
       
   519 					}
       
   520 				]
       
   521 			});
       
   522 		}
   499 	}
   523 	}
   500 	
   524 	
   501 	if ( allow_wysiwyg )
   525 	if ( allow_wysiwyg )
   502 	{
   526 	{
   503 		var a = document.getElementById('enano_edit_btn_pt').getElementsByTagName('a')[0];
   527 		var a = document.getElementById('enano_edit_btn_pt').getElementsByTagName('a')[0];
   547 	if ( is_draft && editor_save_lock )
   571 	if ( is_draft && editor_save_lock )
   548 		return false;
   572 		return false;
   549 	else
   573 	else
   550 		editor_save_lock = true;
   574 		editor_save_lock = true;
   551 	
   575 	
   552 	var ta_content = ( text_override ) ? text_override : $dynano('ajaxEditArea').getContent();
   576 	var ta_content = ( text_override ) ? text_override : ajaxEditorGetContent();
   553 	
   577 	
   554 	if ( !is_draft && ( ta_content == '' || ta_content == '<p></p>' || ta_content == '<p>&nbsp;</p>' ) )
   578 	if ( !is_draft && ( ta_content == '' || ta_content == '<p></p>' || ta_content == '<p>&nbsp;</p>' ) )
   555 	{
   579 	{
   556 		new MessageBox(MB_OK|MB_ICONSTOP, $lang.get('editor_err_no_text_title'), $lang.get('editor_err_no_text_body'));
   580 		new MessageBox(MB_OK|MB_ICONSTOP, $lang.get('editor_err_no_text_title'), $lang.get('editor_err_no_text_body'));
   557 		ajaxUnSetEditorLoading();
   581 		ajaxUnSetEditorLoading();
   583 		src: ta_content,
   607 		src: ta_content,
   584 		summary: edit_summ,
   608 		summary: edit_summ,
   585 		minor_edit: is_minor,
   609 		minor_edit: is_minor,
   586 		time: timestamp,
   610 		time: timestamp,
   587 		draft: ( is_draft == true ),
   611 		draft: ( is_draft == true ),
   588 		format: ( $dynano('ajaxEditArea').isMCE() ) ? 'xhtml' : 'wikitext',
   612 		format: window.page_format,
   589 		used_draft: used_draft
   613 		used_draft: used_draft
   590 	};
   614 	};
   591 	
   615 	
   592 	eval(setHook('editor_save_presend'));
   616 	eval(setHook('editor_save_presend'));
   593 	
   617 	
   694 						// The save was successful; reset flags and make another request for the new page content
   718 						// The save was successful; reset flags and make another request for the new page content
   695 						setAjaxLoading();
   719 						setAjaxLoading();
   696 						editor_open = false;
   720 						editor_open = false;
   697 						editor_save_lock = false;
   721 						editor_save_lock = false;
   698 						enableUnload();
   722 						enableUnload();
   699 						$dynano('ajaxEditArea').destroyMCE(false);
   723 						if ( window.page_format != 'wikitext' )
       
   724 						{
       
   725 							if ( typeof(editor_formats[window.page_format].ui_destroy) == 'function' )
       
   726 							{
       
   727 								editor_formats[window.page_format].ui_destroy();
       
   728 							}
       
   729 						}
   700 						changeOpac(0, 'ajaxEditContainer');
   730 						changeOpac(0, 'ajaxEditContainer');
   701 						ajaxGet(stdAjaxPrefix + '&_mode=getpage&noheaders', function(ajax)
   731 						ajaxGet(stdAjaxPrefix + '&_mode=getpage&noheaders', function(ajax)
   702 							{
   732 							{
   703 								if ( ajax.readyState == 4 && ajax.status == 200 )
   733 								if ( ajax.readyState == 4 && ajax.status == 200 )
   704 								{
   734 								{
   757 }
   787 }
   758 
   788 
   759 window.ajaxEditorGenPreview = function()
   789 window.ajaxEditorGenPreview = function()
   760 {
   790 {
   761 	ajaxSetEditorLoading();
   791 	ajaxSetEditorLoading();
   762 	var ta_content = $dynano('ajaxEditArea').getContent();
   792 	var ta_content = ajaxEditorGetContent();
   763 	ta_content = ajaxEscape(ta_content);
   793 	ta_content = ajaxEscape(ta_content);
   764 	if ( $dynano('enano_editor_preview').object.innerHTML != '' )
   794 	if ( $dynano('enano_editor_preview').object.innerHTML != '' )
   765 	{
   795 	{
   766 		opacity('enano_editor_preview', 100, 0, 500);
   796 		opacity('enano_editor_preview', 100, 0, 500);
   767 	}
   797 	}
   851 }
   881 }
   852 
   882 
   853 window.ajaxEditorShowDiffs = function()
   883 window.ajaxEditorShowDiffs = function()
   854 {
   884 {
   855 	ajaxSetEditorLoading();
   885 	ajaxSetEditorLoading();
   856 	var ta_content = $dynano('ajaxEditArea').getContent();
   886 	var ta_content = ajaxEditorGetContent();
   857 	ta_content = ajaxEscape(ta_content);
   887 	ta_content = ajaxEscape(ta_content);
   858 	if ( $dynano('enano_editor_preview').object.innerHTML != '' )
   888 	if ( $dynano('enano_editor_preview').object.innerHTML != '' )
   859 	{
   889 	{
   860 		opacity('enano_editor_preview', 100, 0, 500);
   890 		opacity('enano_editor_preview', 100, 0, 500);
   861 	}
   891 	}
   907 				}
   937 				}
   908 			]
   938 			]
   909 		});
   939 		});
   910 }
   940 }
   911 
   941 
   912 window.ajaxSetEditorMCE = function(confirmed)
   942 window.ajaxSetEditorMCE = function()
   913 {
   943 {
   914 	if ( editor_loading )
   944 	if ( editor_loading )
   915 		return false;
   945 		return false;
   916 	
   946 	
   917 	if ( !confirmed )
   947 	var len = 0;
       
   948 	for ( var i in editor_formats )
       
   949 	{
       
   950 		len++;
       
   951 	}
       
   952 	
       
   953 	if ( len == 0 )
   918 	{
   954 	{
   919 		miniPromptMessage({
   955 		miniPromptMessage({
   920 				title: $lang.get('editor_msg_convert_confirm_title'),
   956 				title: $lang.get('editor_msg_convert_no_plugins_title'),
   921 				message: $lang.get('editor_msg_convert_confirm_body'),
   957 				message: $lang.get('editor_msg_convert_no_plugins_body'),
   922 				buttons: [
   958 				buttons: [
   923 					{
       
   924 						color: 'blue',
       
   925 						text: $lang.get('editor_btn_graphical'),
       
   926 						style: {
       
   927 							fontWeight: 'bold'
       
   928 						},
       
   929 						sprite: [ editor_img_path + '/sprite.png', 16, 16, 0, 112 ],
       
   930 						onclick: function()
       
   931 						{
       
   932 							ajaxSetEditorMCE(true);
       
   933 							miniPromptDestroy(this);
       
   934 							return false;
       
   935 						}
       
   936 					},
       
   937 					{
   959 					{
   938 						text: $lang.get('etc_cancel'),
   960 						text: $lang.get('etc_cancel'),
   939 						onclick: function()
   961 						onclick: function()
   940 						{
   962 						{
   941 							miniPromptDestroy(this);
   963 							miniPromptDestroy(this);
   945 				]
   967 				]
   946 			});
   968 			});
   947 		return false;
   969 		return false;
   948 	}
   970 	}
   949 	
   971 	
   950 	// Clear out existing buttons
   972 	var mp = miniPrompt(function(div)
   951 	var span_wiki = $dynano('enano_edit_btn_pt').object;
   973 		{
   952 	var span_mce  = $dynano('enano_edit_btn_mce').object;
   974 			$(div).css('text-align', 'center');
   953 	span_wiki.style.display = 'inline';
   975 			$(div).append('<h3>' + $lang.get('editor_msg_convert_confirm_title') + '</h3>');
   954 	span_mce.style.display = 'none';
   976 			$(div).append('<p>' + $lang.get('editor_msg_convert_confirm_body') + '</p>');
   955 	
   977 			var select = '<select class="format">';
   956 	// Swap editor
   978 			for ( var i in editor_formats )
   957 	$dynano('ajaxEditArea').switchToMCE(true);
   979 			{
       
   980 				var obj = editor_formats[i];
       
   981 				select += '<option value="' + i + '">' + $lang.get(obj.name) + '</option>';
       
   982 			}
       
   983 			select += '</select>';
       
   984 			
       
   985 			$(div).append('<p class="format_drop">' + $lang.get('editor_msg_convert_lbl_plugin') + select + '</p>');
       
   986 			$(div).append('<p><a href="#" class="abutton abutton_green go_action" style="font-weight: bold;">' + gen_sprite_html(editor_img_path + '/sprite.png', 16, 16, 0, 112) + $lang.get('editor_btn_graphical_convert') + '</a>'
       
   987 					+ '<a href="#" class="abutton cancel_action">' + $lang.get('etc_cancel') + '</a></p>');
       
   988 			
       
   989 			$('a.go_action', div).click(function()
       
   990 				{
       
   991 					// go ahead with converting to this format
       
   992 					
       
   993 					var parent = miniPromptGetParent(this);
       
   994 					var whitey = whiteOutMiniPrompt(parent);
       
   995 					var plugin = $('select.format', parent).val();
       
   996 					ajaxEditorSetFormat(plugin, function()
       
   997 						{
       
   998 							if ( typeof(whitey) == 'object' )
       
   999 								whiteOutReportSuccess(whitey);
       
  1000 						});
       
  1001 					return false;
       
  1002 				});
       
  1003 			
       
  1004 			$('a.cancel_action', div).click(function()
       
  1005 				{
       
  1006 					miniPromptDestroy(this);
       
  1007 					return false;
       
  1008 				});
       
  1009 		});
       
  1010 	
       
  1011 	return false;
   958 }
  1012 }
   959 
  1013 
   960 window.ajaxSetEditorPlain = function(confirmed)
  1014 window.ajaxSetEditorPlain = function(confirmed)
   961 {
  1015 {
   962 	if ( editor_loading )
  1016 	if ( editor_loading )
  1000 	var span_mce  = $dynano('enano_edit_btn_mce').object;
  1054 	var span_mce  = $dynano('enano_edit_btn_mce').object;
  1001 	span_wiki.style.display = 'none';
  1055 	span_wiki.style.display = 'none';
  1002 	span_mce.style.display = 'inline';
  1056 	span_mce.style.display = 'inline';
  1003 	
  1057 	
  1004 	// Swap editor
  1058 	// Swap editor
  1005 	$dynano('ajaxEditArea').destroyMCE(true);
  1059 	if ( typeof(editor_formats[window.page_format].ui_destroy) == 'function' )
       
  1060 	{
       
  1061 		if ( typeof(editor_formats[window.page_format].convert_from) == 'function' )
       
  1062 		{
       
  1063 			var text = ajaxEditorGetContent();
       
  1064 			var newtext = editor_formats[window.page_format].convert_from(text);
       
  1065 			if ( typeof(newtext) != 'string' )
       
  1066 				newtext = text;
       
  1067 		}
       
  1068 		editor_formats[window.page_format].ui_destroy();
       
  1069 		$('#ajaxEditArea').val(newtext);
       
  1070 	}
       
  1071 	
       
  1072 	window.page_format = 'wikitext';
  1006 }
  1073 }
  1007 
  1074 
  1008 var editor_loading = false;
  1075 var editor_loading = false;
  1009 
  1076 
  1010 window.ajaxSetEditorLoading = function()
  1077 window.ajaxSetEditorLoading = function()
  1082 	if ( !aed )
  1149 	if ( !aed )
  1083 		return false;
  1150 		return false;
  1084 	var now = unix_time();
  1151 	var now = unix_time();
  1085 	aed.as_last_save = now;
  1152 	aed.as_last_save = now;
  1086 	
  1153 	
  1087 	var ta_content = $dynano('ajaxEditArea').getContent();
  1154 	var ta_content = ajaxEditorGetContent();
  1088 	
  1155 	
  1089 	if ( ta_content == '' || ta_content == '<p></p>' || ta_content == '<p>&nbsp;</p>' || ta_content == editor_orig_text || ta_content == editor_last_draft )
  1156 	if ( ta_content == '' || ta_content == '<p></p>' || ta_content == '<p>&nbsp;</p>' || ta_content == editor_orig_text || ta_content == editor_last_draft )
  1090 	{
  1157 	{
  1091 		return false;
  1158 		return false;
  1092 	}
  1159 	}
  1123 					return false;
  1190 					return false;
  1124 				}
  1191 				}
  1125 				
  1192 				
  1126 				editor_convert_if_needed(response.page_format);
  1193 				editor_convert_if_needed(response.page_format);
  1127 				
  1194 				
  1128 				$dynano('ajaxEditArea').setContent(response.src);
  1195 				if ( response.page_format != 'wikitext' && typeof(editor_formats[response.page_format]) == 'object' )
       
  1196 				{
       
  1197 					if ( typeof(editor_formats[response.page_format].set_text) == 'function' )
       
  1198 					{
       
  1199 						editor_formats[response.page_format].set_text(response.src);
       
  1200 					}
       
  1201 					else
       
  1202 					{
       
  1203 						$('#ajaxEditArea').val(response.src);
       
  1204 					}
       
  1205 				}
       
  1206 				else
       
  1207 				{
       
  1208 					$('#ajaxEditArea').val(response.src);
       
  1209 				}
       
  1210 				
  1129 				$dynano('ajaxEditArea').object.used_draft = true;
  1211 				$dynano('ajaxEditArea').object.used_draft = true;
  1130 				editor_orig_text = editor_last_draft = response.src;
  1212 				editor_orig_text = editor_last_draft = response.src;
  1131 				
  1213 				
  1132 				var es = document.getElementById('enano_editor_field_summary');
  1214 				var es = document.getElementById('enano_editor_field_summary');
  1133 				if ( es.value == '' )
  1215 				if ( es.value == '' )
  1142 }
  1224 }
  1143 
  1225 
  1144 window.editor_convert_if_needed = function(targetformat, noticetitle, noticebody)
  1226 window.editor_convert_if_needed = function(targetformat, noticetitle, noticebody)
  1145 {
  1227 {
  1146 	// Do we need to change the format?
  1228 	// Do we need to change the format?
  1147 	var need_to_mce = ( targetformat == 'xhtml' && !$dynano('ajaxEditArea').isMCE() );
  1229 	var need_to_mce = ( targetformat != 'wikitext' && page_format == 'wikitext' );
  1148 	var need_to_wkt = ( targetformat == 'wikitext' && $dynano('ajaxEditArea').isMCE() );
  1230 	var need_to_wkt = ( targetformat == 'wikitext' && page_format != 'wikitext' );
  1149 	if ( need_to_mce )
  1231 	if ( need_to_mce )
  1150 	{
  1232 	{
  1151 		$dynano('ajaxEditArea').setContent('');
  1233 		editor_formats[targetformat].ui_construct();
  1152 		$dynano('ajaxEditArea').switchToMCE(false);
  1234 		window.page_format = targetformat;
  1153 		
  1235 		
  1154 		// Clear out existing buttons
  1236 		// Clear out existing buttons
  1155 		var span_wiki = $dynano('enano_edit_btn_pt').object;
  1237 		var span_wiki = $dynano('enano_edit_btn_pt').object;
  1156 		var span_mce  = $dynano('enano_edit_btn_mce').object;
  1238 		var span_mce  = $dynano('enano_edit_btn_mce').object;
  1157 		span_wiki.style.display = 'inline';
  1239 		span_wiki.style.display = 'inline';
  1158 		span_mce.style.display = 'none';
  1240 		span_mce.style.display = 'none';
  1159 	}
  1241 	}
  1160 	else if ( need_to_wkt )
  1242 	else if ( need_to_wkt )
  1161 	{
  1243 	{
  1162 		$dynano('ajaxEditArea').setContent('');
  1244 		editor_formats[window.page_format].ui_construct();
  1163 		$dynano('ajaxEditArea').destroyMCE(false);
  1245 		window.page_format = 'wikitext';
  1164 		
  1246 		
  1165 		// Clear out existing buttons
  1247 		// Clear out existing buttons
  1166 		var span_wiki = $dynano('enano_edit_btn_pt').object;
  1248 		var span_wiki = $dynano('enano_edit_btn_pt').object;
  1167 		var span_mce  = $dynano('enano_edit_btn_mce').object;
  1249 		var span_mce  = $dynano('enano_edit_btn_mce').object;
  1168 		span_wiki.style.display = 'none';
  1250 		span_wiki.style.display = 'none';
  1190 					}
  1272 					}
  1191 				]
  1273 				]
  1192 			});
  1274 			});
  1193 	}
  1275 	}
  1194 }
  1276 }
       
  1277 
       
  1278 window.ajaxEditorSetFormat = function(plugin, success_func)
       
  1279 	{
       
  1280 		// perform conversion
       
  1281 		if ( typeof(editor_formats[plugin].convert_to) == 'function' )
       
  1282 		{
       
  1283 			var result = editor_formats[plugin].convert_to($('#ajaxEditArea').val());
       
  1284 		}
       
  1285 		else
       
  1286 		{
       
  1287 			var result = $('#ajaxEditArea').val();
       
  1288 		}
       
  1289 		if ( typeof(result) != 'string' )
       
  1290 		{
       
  1291 			result = $('#ajaxEditArea').val();
       
  1292 		}
       
  1293 		$('#ajaxEditArea').val(result);
       
  1294 		if ( typeof(editor_formats[plugin].ui_construct) == 'function' )
       
  1295 		{
       
  1296 			editor_formats[plugin].ui_construct();
       
  1297 		}
       
  1298 		success_func();
       
  1299 		window.page_format = plugin;
       
  1300 		
       
  1301 		// change the buttons over
       
  1302 		$('#enano_edit_btn_pt').css('display', 'inline');
       
  1303 		$('#enano_edit_btn_mce').css('display', 'none');
       
  1304 	};
       
  1305 	
       
  1306 window.ajaxEditorGetContent = function()
       
  1307 	{
       
  1308 		if ( window.page_format == 'wikitext' )
       
  1309 		{
       
  1310 			return $('#ajaxEditArea').val();
       
  1311 		}
       
  1312 		else
       
  1313 		{
       
  1314 			if ( typeof(editor_formats[window.page_format].get_text) == 'function' )
       
  1315 			{
       
  1316 				return editor_formats[window.page_format].get_text();
       
  1317 			}
       
  1318 			else
       
  1319 			{
       
  1320 				return $('#ajaxEditArea').val();
       
  1321 			}
       
  1322 		}
       
  1323 	};
       
  1324