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 */
22
23// Register the related commands.
24
25FCKCommands.RegisterCommand( 'fonts',
26   new FCKDialogCommand( FCKLang['FontsDlgTitle'], FCKLang['FontsDlgTitle'],
27   FCKConfig.PluginsPath + 'fonts/fonts.html',  550, 500 ) ) ;
28
29
30var oFontsTool		= new FCKToolbarButton( 'fonts',  FCKLang['FontsToolTip'] ) ;
31oFontsTool.IconPath	= FCKPlugins.Items['fonts'].Path  + 'images/fonts.png' ;
32
33FCKToolbarItems.RegisterItem( 'fonts', oFontsTool ) ;
34
35// The object used for all Fonts operations.
36var FCKFonts = new Object() ;
37
38FCKFonts.Insert = function(font_weight, font_family, font_size, fg_color, bg_color) {
39
40    var isSafari = false;
41    var style = " font-weight: " + font_weight + "; ";
42    style += " font-size: " + font_size + "; ";
43    style += " color: " + fg_color + "; ";
44	style += " font-family: " + font_family + "; ";
45    style += " background-color: " + bg_color + "; ";
46
47	var hrefStartHtml	=  '<span face="'+ font_family + '" style="' + style + '">';
48	var hrefEndHtml		=  '</span>';
49
50    var reset = false;
51    if(!FCKBrowserInfo.IsIE && !FCKBrowserInfo.IsGecko) isSafari = true;
52	mySelection = ( FCKBrowserInfo.IsIE) ? FCKSelection.GetSelectedHTML(isSafari) : removeBR(FCKSelection.GetSelectedHTML(isSafari),false);
53
54    mySelection = mySelection.replace(/<.*?>/g,"");
55    mySelection = mySelection.replace(/^\s+/,"");
56    mySelection = mySelection.replace(/\s+$/,"");
57    if(!mySelection) return false;
58    if(!mySelection) mySelection = "<br /><br />";
59    hrefHtml = hrefStartHtml+mySelection+hrefEndHtml;
60
61	FCK.InsertHtml(hrefHtml);
62    return true;
63}
64
65FCKFonts.getSelection = function(){
66  var isSafari = false;
67   var mySelection = removeBR(FCKSelection.GetSelectedHTML(isSafari),true);
68  return mySelection;
69}
70
71FCKFonts.haveSelection = function() {
72    isSafari = false;
73    if(FCKBrowserInfo.IsIE) return true;
74    if(!FCKBrowserInfo.IsIE && !FCKBrowserInfo.IsGecko) isSafari = true;
75
76	mySelection = ( FCKBrowserInfo.IsIE) ? FCKSelection.GetSelectedHTML(isSafari) : removeBR(FCKSelection.GetSelectedHTML(isSafari),false);
77
78    return mySelection;
79}
80FCKFonts.InsertEdited = function(val) {
81
82	hrefHtml = val;
83
84	FCK.InsertHtml(hrefHtml);
85}
86
87FCKFonts.isIE = function() {
88return FCKBrowserInfo.IsIE;
89}
90
91FCKSelection.GetSelectedHTML = function(isSafari) {
92
93							// see http://www.quirksmode.org/js/selected.html for other browsers
94	if( FCKBrowserInfo.IsIE) {
95        											// IE
96		var oRange = FCK.EditorDocument.selection.createRange() ;
97		//if an object like a table is deleted, the call to GetType before getting again a range returns Control
98		switch ( this.GetType() ) {
99			case 'Control' :
100			return oRange.item(0).outerHTML;
101
102			case 'None' :
103			return '' ;
104
105			default :
106			return oRange.htmlText ;
107		}
108	}
109	else if ( FCKBrowserInfo.IsGecko || isSafari ) {									// Mozilla, Safari
110
111									// Mozilla, Safari
112		var oSelection = FCK.EditorWindow.getSelection();
113		//Gecko doesn't provide a function to get the innerHTML of a selection,
114		//so we must clone the selection to a temporary element and check that innerHTML
115		var e = FCK.EditorDocument.createElement( 'DIV' );
116		for ( var i = 0 ; i < oSelection.rangeCount ; i++ ) {
117			e.appendChild( oSelection.getRangeAt(i).cloneContents() );
118		}
119		return e.innerHTML;
120	}
121}
122
123function removeBR(input, skip_space) {							/* Used with Gecko */
124	var output = "";
125	for (var i = 0; i < input.length; i++) {
126		if ((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10)) {
127			i++;
128            if(!skip_space)
129			output += " ";
130		}
131		else {
132			output += input.charAt(i);
133   		}
134	}
135	return output;
136}
137
138
139