PageRenderTime 32ms CodeModel.GetById 15ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/source/Plug-in/fck/editor/_source/commandclasses/fck_othercommands.js

http://prosporous.googlecode.com/
JavaScript | 470 lines | 338 code | 71 blank | 61 comment | 40 complexity | 3adc62d1e88671ec977913e557185cb6 MD5 | raw file
  1/*
  2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
  3 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
  4 *
  5 * == BEGIN LICENSE ==
  6 *
  7 * Licensed under the terms of any of the following licenses at your
  8 * choice:
  9 *
 10 *  - GNU General Public License Version 2 or later (the "GPL")
 11 *    http://www.gnu.org/licenses/gpl.html
 12 *
 13 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 14 *    http://www.gnu.org/licenses/lgpl.html
 15 *
 16 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 17 *    http://www.mozilla.org/MPL/MPL-1.1.html
 18 *
 19 * == END LICENSE ==
 20 *
 21 * Definition of other commands that are not available internaly in the
 22 * browser (see FCKNamedCommand).
 23 */
 24
 25// ### General Dialog Box Commands.
 26var FCKDialogCommand = function( name, title, url, width, height, getStateFunction, getStateParam )
 27{
 28	this.Name	= name ;
 29	this.Title	= title ;
 30	this.Url	= url ;
 31	this.Width	= width ;
 32	this.Height	= height ;
 33
 34	this.GetStateFunction	= getStateFunction ;
 35	this.GetStateParam		= getStateParam ;
 36
 37	this.Resizable = false ;
 38}
 39
 40FCKDialogCommand.prototype.Execute = function()
 41{
 42	FCKDialog.OpenDialog( 'FCKDialog_' + this.Name , this.Title, this.Url, this.Width, this.Height, null, null, this.Resizable ) ;
 43}
 44
 45FCKDialogCommand.prototype.GetState = function()
 46{
 47	if ( this.GetStateFunction )
 48		return this.GetStateFunction( this.GetStateParam ) ;
 49	else
 50		return FCK_TRISTATE_OFF ;
 51}
 52
 53// Generic Undefined command (usually used when a command is under development).
 54var FCKUndefinedCommand = function()
 55{
 56	this.Name = 'Undefined' ;
 57}
 58
 59FCKUndefinedCommand.prototype.Execute = function()
 60{
 61	alert( FCKLang.NotImplemented ) ;
 62}
 63
 64FCKUndefinedCommand.prototype.GetState = function()
 65{
 66	return FCK_TRISTATE_OFF ;
 67}
 68
 69
 70// ### FormatBlock
 71var FCKFormatBlockCommand = function()
 72{}
 73
 74FCKFormatBlockCommand.prototype = 
 75{
 76	Name : 'FormatBlock',
 77	
 78	Execute : FCKStyleCommand.prototype.Execute,
 79	
 80	GetState : function()
 81	{
 82		return FCK.EditorDocument ? FCK_TRISTATE_OFF : FCK_TRISTATE_DISABLED ;
 83	}
 84};
 85
 86// ### FontName
 87
 88var FCKFontNameCommand = function()
 89{}
 90
 91FCKFontNameCommand.prototype = 
 92{
 93	Name		: 'FontName',
 94	Execute		: FCKStyleCommand.prototype.Execute,
 95	GetState	: FCKFormatBlockCommand.prototype.GetState
 96};
 97
 98// ### FontSize
 99var FCKFontSizeCommand = function()
100{}
101
102FCKFontSizeCommand.prototype = 
103{
104	Name		: 'FontSize',
105	Execute		: FCKStyleCommand.prototype.Execute,
106	GetState	: FCKFormatBlockCommand.prototype.GetState
107};
108
109// ### Preview
110var FCKPreviewCommand = function()
111{
112	this.Name = 'Preview' ;
113}
114
115FCKPreviewCommand.prototype.Execute = function()
116{
117     FCK.Preview() ;
118}
119
120FCKPreviewCommand.prototype.GetState = function()
121{
122	return FCK_TRISTATE_OFF ;
123}
124
125// ### Save
126var FCKSaveCommand = function()
127{
128	this.Name = 'Save' ;
129}
130
131FCKSaveCommand.prototype.Execute = function()
132{
133	// Get the linked field form.
134	var oForm = FCK.GetParentForm() ;
135
136	if ( typeof( oForm.onsubmit ) == 'function' )
137	{
138		var bRet = oForm.onsubmit() ;
139		if ( bRet != null && bRet === false )
140			return ;
141	}
142
143	// Submit the form.
144	// If there's a button named "submit" then the form.submit() function is masked and
145	// can't be called in Mozilla, so we call the click() method of that button.
146	if ( typeof( oForm.submit ) == 'function' )
147		oForm.submit() ;
148	else
149		oForm.submit.click() ;
150}
151
152FCKSaveCommand.prototype.GetState = function()
153{
154	return FCK_TRISTATE_OFF ;
155}
156
157// ### NewPage
158var FCKNewPageCommand = function()
159{
160	this.Name = 'NewPage' ;
161}
162
163FCKNewPageCommand.prototype.Execute = function()
164{
165	FCKUndo.SaveUndoStep() ;
166	FCK.SetData( '' ) ;
167	FCKUndo.Typing = true ;
168	FCK.Focus() ;
169}
170
171FCKNewPageCommand.prototype.GetState = function()
172{
173	return FCK_TRISTATE_OFF ;
174}
175
176// ### Source button
177var FCKSourceCommand = function()
178{
179	this.Name = 'Source' ;
180}
181
182FCKSourceCommand.prototype.Execute = function()
183{
184	if ( FCKConfig.SourcePopup )	// Until v2.2, it was mandatory for FCKBrowserInfo.IsGecko.
185	{
186		var iWidth	= FCKConfig.ScreenWidth * 0.65 ;
187		var iHeight	= FCKConfig.ScreenHeight * 0.65 ;
188		FCKDialog.OpenDialog( 'FCKDialog_Source', FCKLang.Source, 'dialog/fck_source.html', iWidth, iHeight, null, null, true ) ;
189	}
190	else
191	    FCK.SwitchEditMode() ;
192}
193
194FCKSourceCommand.prototype.GetState = function()
195{
196	return ( FCK.EditMode == FCK_EDITMODE_WYSIWYG ? FCK_TRISTATE_OFF : FCK_TRISTATE_ON ) ;
197}
198
199// ### Undo
200var FCKUndoCommand = function()
201{
202	this.Name = 'Undo' ;
203}
204
205FCKUndoCommand.prototype.Execute = function()
206{
207	FCKUndo.Undo() ;
208}
209
210FCKUndoCommand.prototype.GetState = function()
211{
212	return ( FCKUndo.CheckUndoState() ? FCK_TRISTATE_OFF : FCK_TRISTATE_DISABLED ) ;
213}
214
215// ### Redo
216var FCKRedoCommand = function()
217{
218	this.Name = 'Redo' ;
219}
220
221FCKRedoCommand.prototype.Execute = function()
222{
223	FCKUndo.Redo() ;
224}
225
226FCKRedoCommand.prototype.GetState = function()
227{
228	return ( FCKUndo.CheckRedoState() ? FCK_TRISTATE_OFF : FCK_TRISTATE_DISABLED ) ;
229}
230
231// ### Page Break
232var FCKPageBreakCommand = function()
233{
234	this.Name = 'PageBreak' ;
235}
236
237FCKPageBreakCommand.prototype.Execute = function()
238{
239	// Take an undo snapshot before changing the document
240	FCKUndo.SaveUndoStep() ;
241
242//	var e = FCK.EditorDocument.createElement( 'CENTER' ) ;
243//	e.style.pageBreakAfter = 'always' ;
244
245	// Tidy was removing the empty CENTER tags, so the following solution has
246	// been found. It also validates correctly as XHTML 1.0 Strict.
247	var e = FCK.EditorDocument.createElement( 'DIV' ) ;
248	e.style.pageBreakAfter = 'always' ;
249	e.innerHTML = '<span style="DISPLAY:none">&nbsp;</span>' ;
250
251	var oFakeImage = FCKDocumentProcessor_CreateFakeImage( 'FCK__PageBreak', e ) ;
252	var oRange = new FCKDomRange( FCK.EditorWindow ) ;
253	oRange.MoveToSelection() ;
254	var oSplitInfo = oRange.SplitBlock() ;
255	if ( oSplitInfo.NextBlock )
256		oSplitInfo.NextBlock.parentNode.insertBefore( oFakeImage, oSplitInfo.NextBlock ) ;
257	else
258		oSplitInfo.PreviousBlock.parentNode.insertBefore( oFakeImage, oSplitInfo.PreviousBlock.nextSibling ) ;
259
260	FCK.Events.FireEvent( 'OnSelectionChange' ) ;
261}
262
263FCKPageBreakCommand.prototype.GetState = function()
264{
265	return 0 ; // FCK_TRISTATE_OFF
266}
267
268// FCKUnlinkCommand - by Johnny Egeland (johnny@coretrek.com)
269var FCKUnlinkCommand = function()
270{
271	this.Name = 'Unlink' ;
272}
273
274FCKUnlinkCommand.prototype.Execute = function()
275{
276	// Take an undo snapshot before changing the document
277	FCKUndo.SaveUndoStep() ;
278
279	if ( FCKBrowserInfo.IsGeckoLike )
280	{
281		var oLink = FCK.Selection.MoveToAncestorNode( 'A' ) ;
282		// The unlink command can generate a span in Firefox, so let's do it our way. See #430
283		if ( oLink )
284			FCKTools.RemoveOuterTags( oLink ) ;
285
286		return ;
287	}
288	
289	FCK.ExecuteNamedCommand( this.Name ) ;
290}
291
292FCKUnlinkCommand.prototype.GetState = function()
293{
294	var state = FCK.GetNamedCommandState( this.Name ) ;
295
296	// Check that it isn't an anchor
297	if ( state == FCK_TRISTATE_OFF && FCK.EditMode == FCK_EDITMODE_WYSIWYG )
298	{
299		var oLink = FCKSelection.MoveToAncestorNode( 'A' ) ;
300		var bIsAnchor = ( oLink && oLink.name.length > 0 && oLink.href.length == 0 ) ;
301		if ( bIsAnchor )
302			state = FCK_TRISTATE_DISABLED ;
303	}
304
305	return state ;
306}
307
308// FCKSelectAllCommand
309var FCKSelectAllCommand = function()
310{
311	this.Name = 'SelectAll' ;
312}
313
314FCKSelectAllCommand.prototype.Execute = function()
315{
316	if ( FCK.EditMode == FCK_EDITMODE_WYSIWYG )
317	{
318		FCK.ExecuteNamedCommand( 'SelectAll' ) ;
319	}
320	else
321	{
322		// Select the contents of the textarea
323		var textarea = FCK.EditingArea.Textarea ;
324		if ( FCKBrowserInfo.IsIE )
325		{
326			textarea.createTextRange().execCommand( 'SelectAll' ) ;
327		}
328		else
329		{
330			textarea.selectionStart = 0 ;
331			textarea.selectionEnd = textarea.value.length ;
332		}
333		textarea.focus() ;
334	}
335}
336
337FCKSelectAllCommand.prototype.GetState = function()
338{
339	return FCK_TRISTATE_OFF ;
340}
341
342// FCKPasteCommand
343var FCKPasteCommand = function()
344{
345	this.Name = 'Paste' ;
346}
347
348FCKPasteCommand.prototype =
349{
350	Execute : function()
351	{
352		if ( FCKBrowserInfo.IsIE )
353			FCK.Paste() ;
354		else
355			FCK.ExecuteNamedCommand( 'Paste' ) ;
356	},
357
358	GetState : function()
359	{
360		return FCK.GetNamedCommandState( 'Paste' ) ;
361	}
362} ;
363
364// FCKRuleCommand
365var FCKRuleCommand = function()
366{
367	this.Name = 'Rule' ;
368}
369
370FCKRuleCommand.prototype =
371{
372	Execute : function()
373	{
374		FCKUndo.SaveUndoStep() ;
375		FCK.InsertElement( 'hr' ) ;
376	},
377
378	GetState : function()
379	{
380		return FCK.GetNamedCommandState( 'InsertHorizontalRule' ) ;
381	}
382} ;
383
384// FCKCopyCommand
385var FCKCopyCommand = function()
386{
387	this.Name = 'Copy' ;
388}
389
390FCKCopyCommand.prototype = 
391{
392	Execute : function()
393	{
394		FCK.ExecuteNamedCommand( this.Name ) ;
395	},
396
397	GetState : function()
398	{
399		// Strangely, the cut command happens to have the correct states for both Copy and Cut in all browsers.
400		return FCK.GetNamedCommandState( 'Cut' ) ;
401	}
402};
403
404var FCKAnchorDeleteCommand = function()
405{
406	this.Name = 'AnchorDelete' ;
407}
408
409FCKAnchorDeleteCommand.prototype = 
410{
411	Execute : function()
412	{
413		if (FCK.Selection.GetType() == 'Control')
414		{
415			FCK.Selection.Delete();
416		}
417		else
418		{
419			var oFakeImage = FCK.Selection.GetSelectedElement() ;
420			if ( oFakeImage )
421			{
422				if ( oFakeImage.tagName == 'IMG' && oFakeImage.getAttribute('_fckanchor') )
423					oAnchor = FCK.GetRealElement( oFakeImage ) ;
424				else
425					oFakeImage = null ;
426			}
427
428			//Search for a real anchor
429			if ( !oFakeImage )
430			{
431				oAnchor = FCK.Selection.MoveToAncestorNode( 'A' ) ;
432				if ( oAnchor )
433					FCK.Selection.SelectNode( oAnchor ) ;
434			}
435
436			// If it's also a link, then just remove the name and exit
437			if ( oAnchor.href.length != 0 )
438			{
439				oAnchor.removeAttribute( 'name' ) ;
440				// Remove temporary class for IE
441				if ( FCKBrowserInfo.IsIE )
442					oAnchor.className = oAnchor.className.replace( FCKRegexLib.FCK_Class, '' ) ;
443				return ;
444			}
445
446			// We need to remove the anchor
447			// If we got a fake image, then just remove it and we're done
448			if ( oFakeImage )
449			{
450				oFakeImage.parentNode.removeChild( oFakeImage ) ;
451				return ;
452			}
453			// Empty anchor, so just remove it
454			if ( oAnchor.innerHTML.length == 0 )
455			{
456				oAnchor.parentNode.removeChild( oAnchor ) ;
457				return ;
458			}
459			// Anchor with content, leave the content
460			FCKTools.RemoveOuterTags( oAnchor ) ;
461		}
462		if ( FCKBrowserInfo.IsGecko )
463			FCK.Selection.Collapse( true ) ;
464	},
465
466	GetState : function()
467	{
468		return FCK.GetNamedCommandState( 'Unlink') ;
469	}
470};