PageRenderTime 38ms CodeModel.GetById 1ms app.highlight 30ms RepoModel.GetById 2ms app.codeStats 0ms

/pigeoncms/Plugins/fckeditor/editor/plugins/imgmap/jscripts/functions.js

http://pigeoncms.googlecode.com/
JavaScript | 375 lines | 268 code | 75 blank | 32 comment | 56 complexity | b2c16b60123faff016f0deb4d26dc4b8 MD5 | raw file
  1var myimgmap;
  2var img_obj = null;
  3var map_obj = null;
  4
  5// Less boring code
  6window.$ = function( id )
  7{
  8	return this.document.getElementById( id ) ;
  9} ;
 10
 11var oEditor = window.parent.InnerDialogLoaded();
 12var FCKConfig	= oEditor.FCKConfig ;
 13var FCKLang = oEditor.FCKLang ;
 14
 15document.write('<scr'+'ipt type="text/javascript" src="' + FCKConfig.FullBasePath + 'dialog/common/fck_dialog_common.js"></sc' + 'ript>');
 16
 17
 18window.onload = function()
 19{
 20	window.parent.SetOkButton(true);
 21//	window.parent.SetAutoSize(true);
 22
 23	//translate page elements
 24	oEditor.FCKLanguageManager.TranslatePage(document);
 25	oEditor.FCKLanguageManager.TranslateElements(document, 'A', 'innerHTML');
 26
 27	var btn;
 28	btn = $('imgpointer') ;
 29	btn.alt = btn.title = FCKLang.imgmapPointer ;
 30	btn = $('imgrectangle') ;
 31	btn.alt = btn.title = FCKLang.imgmapRectangle ;
 32	btn = $('imgcircle') ;
 33	btn.alt = btn.title = FCKLang.imgmapCircle ;
 34	btn = $('imgpolygon') ;
 35	btn.alt = btn.title = FCKLang.imgmapPolygon ;
 36
 37
 38	img_obj = oEditor.FCK.Selection.GetSelectedElement() ;
 39
 40	// On rare situations it's possible to launch the dialog without an image selected
 41	// -> in IE select an image, click outside the editor and the button will remain enabled, 
 42	//		but img_obj will be null
 43	if ( !img_obj )
 44	{
 45		alert( FCKLang.msgImageNotSelected ) ;
 46		window.parent.close() ;
 47		return ;
 48	}
 49
 50	// Autoselect the language based on the current FCKeditor language
 51	// Check if the plugin has the language file for the active language.
 52	var sLang ;
 53	if ( oEditor.FCKPlugins.Items['imgmap'].AvailableLangs.IndexOf( oEditor.FCKLanguageManager.ActiveLanguage.Code ) >= 0 )
 54		sLang = oEditor.FCKLanguageManager.ActiveLanguage.Code ;
 55	else
 56		// Load the english language file if the prefered by the user is not available.
 57		sLang = "en" ;
 58
 59	//late init
 60	myimgmap = new imgmap({
 61		mode : "editor2",
 62		button_container: $('button_container'),
 63		imgroot: 'images/',
 64		buttons : ['delete'],
 65		custom_callbacks : {
 66			'onSelectArea' : onSelectArea,
 67			'onRemoveArea'	: onRemoveArea
 68		},
 69		html_container: null, 
 70		pic_container: $('pic_container'),
 71		status_container: $('status_container'),
 72		form_container: null, // $('form_container'),
 73		bounding_box : false,
 74		lang : sLang
 75	});
 76
 77	//we need this to load languages
 78	myimgmap.onLoad();
 79
 80	myimgmap.loadImage(img_obj);
 81
 82	//check if the image has a valid map already assigned
 83	var mapname = img_obj.getAttribute('usemap', 2) || img_obj.usemap ;
 84	//console.log(mapname);
 85	if ( typeof mapname == 'string' && mapname !== '') {
 86		mapname = mapname.substr(1);
 87		var maps = oEditor.FCK.EditorDocument.getElementsByTagName('MAP');
 88		//console.log(maps);
 89		for (var i=0; i < maps.length; i++) {
 90			if (maps[i].name == mapname) {
 91				map_obj = maps[i];
 92				myimgmap.setMapHTML(map_obj);
 93
 94				$('MapName').value = mapname ;
 95				break;
 96			}
 97		}
 98	}
 99
100	// We must set up this listener only after the current data has been read
101	myimgmap.config.custom_callbacks.onAddArea = onAddArea ;
102
103	$('btnBrowse').style.display	= FCKConfig.LinkBrowser		? '' : 'none' ;
104
105	if ( map_obj !== null )
106	{
107		// Select the first area: 
108		myimgmap.selectedId = 0 ;
109		onSelectArea( myimgmap.areas[0] ) ;
110
111		setMode( 'pointer' ) ;
112	}
113	else
114		hightlightMode( 'rectangle' ) ;
115
116	RefreshSize() ;
117} ;
118
119function Ok() {
120	updateAreaValues() ;
121
122	if (img_obj !== null && img_obj.nodeName == "IMG") {
123		var MapInnerHTML = getMapInnerHTML(myimgmap);
124
125		// If there are no areas, then exit (and remove if neccesary the map).
126		if (MapInnerHTML == '')
127		{
128			removeMap();
129			return ;
130		}
131
132		oEditor.FCKUndo.SaveUndoStep();
133
134		if (typeof map_obj == 'undefined' || map_obj === null) {
135			map_obj = oEditor.FCK.EditorDocument.createElement('MAP');
136			img_obj.parentNode.appendChild(map_obj);
137		}
138
139		myimgmap.mapid = myimgmap.mapname = $('MapName').value ;
140
141		map_obj.innerHTML = MapInnerHTML ;
142
143		// IE bug: it's not possible to directly assing the name and make it work easily
144		// We remove the previous name
145		if ( map_obj.name )
146			map_obj.removeAttribute( 'name' ) ;
147
148		map_obj.name = myimgmap.getMapName();
149		map_obj.id   = myimgmap.getMapId();
150		
151		img_obj.setAttribute('usemap', "#" + myimgmap.getMapName(), 0);
152	}
153
154	return true;
155}
156
157//remove the map object and unset the usemap attribute
158function removeMap() {
159	oEditor.FCKUndo.SaveUndoStep();
160	if (img_obj !== null && img_obj.nodeName == "IMG") {
161		img_obj.removeAttribute('usemap', 0);
162	}
163	if (typeof map_obj != 'undefined' && map_obj !== null) {
164		map_obj.parentNode.removeChild(map_obj);
165	}
166
167	window.parent.close();
168}
169
170
171function changelabeling(obj) {
172	myimgmap.config.label = obj.value;
173	myimgmap._repaintAll();
174}
175
176function toggleFieldset(fieldset, on) {
177	if (fieldset) {
178		if (fieldset.className == 'fieldset_off' || on == 1) {
179			fieldset.className = '';
180			RefreshSize();
181		}
182		else {
183			fieldset.className = 'fieldset_off';
184		}
185	}
186}
187
188// We need to store here the area id because if the user clicks on an area, 
189// then the onSelectArea event will fire before the onchange or onblur of the editing inputs
190var currentAreaId = null ;
191
192// An area has been selected in the image
193function onSelectArea(obj) {
194	$( 'properties' ).style.visibility = '';
195
196	updateAreaValues() ;
197
198	currentAreaId = obj.aid ;
199	$( 'txtUrl' ).value = obj.ahref ;
200	$( 'cmbTarget' ).value = obj.atarget ;
201	$( 'txtAlt' ).value = obj.aalt ;
202	$( 'txtAttTitle' ).value = obj.atitle ;
203}
204
205// A new area has been added
206function onAddArea(id) {
207	$( 'properties' ).style.visibility = '';
208
209	updateAreaValues() ;
210
211	currentAreaId = id ;
212	$( 'txtUrl' ).value = '' ;
213	$( 'cmbTarget' ).value = '' ;
214	$( 'txtAlt' ).value = '' ;
215	$( 'txtAttTitle' ).value = '' ;
216}
217
218function onRemoveArea()
219{
220	currentAreaId = null ;
221	$( 'properties' ).style.visibility = 'hidden' ;
222}
223
224function updateAreaValues()
225{
226	if (currentAreaId !== null)
227	{
228		myimgmap.areas[currentAreaId].ahref = $( 'txtUrl' ).value ;
229		myimgmap.areas[currentAreaId].aalt = $( 'txtAlt' ).value ;
230		myimgmap.areas[currentAreaId].atitle = $( 'txtAttTitle' ).value ;
231		myimgmap._recalculate(currentAreaId);
232	}
233}
234
235// We use our own resizing because the default doesn't take into account Standards rendering mode.
236function RefreshSize()
237{
238		var oInnerDoc = document ;
239
240		var iFrameHeight ;
241		if ( document.all )
242			iFrameHeight = oInnerDoc.documentElement.offsetHeight ;
243		else
244			iFrameHeight = window.innerHeight ;
245
246		var iInnerHeight = oInnerDoc.body.scrollHeight ;
247
248		var iDiff = iInnerHeight - iFrameHeight ;
249
250		if ( iDiff !== 0 )
251		{
252			if ( document.all )
253				window.parent.dialogHeight = ( parseInt( window.parent.dialogHeight, 10 ) + iDiff ) + 'px' ;
254			else
255				window.parent.resizeBy( 0, iDiff ) ;
256		}
257}
258
259function setMode(mode) {
260	if (mode == 'pointer') {
261		myimgmap.is_drawing = 0;
262		myimgmap.nextShape = '';
263	}
264	else {
265		myimgmap.nextShape = mode;
266	}
267
268	hightlightMode(mode); 
269}
270
271var previousModeImg = null ;
272function hightlightMode(mode) {
273	// Reset previous button
274	if ( previousModeImg )
275		previousModeImg.className = '';
276
277	// Highlight new mode
278	previousModeImg = $( 'img' + mode );
279	previousModeImg.className = 'ActiveMode' ;
280}
281
282
283
284/* Call our custom version to protect URLs */
285
286function getMapInnerHTML( imgmap )
287{
288	var html = '' ;
289	//foreach area properties
290	for (var i=0; i< imgmap.areas.length; i++) {
291		html+= getAreaHtml( imgmap.areas[i] ) ;
292	}
293	return(html);
294}
295// Protect urls and add only the used attributes
296function getAreaHtml(area)
297{
298	if ( !area || area.shape == '')
299		return '';
300
301	var html = '<area shape="' + area.shape + '"' +
302							' coords="' + area.lastInput + '"' ;
303
304	if (area.aalt && area.aalt!='') html+= ' alt="' + area.aalt + '"' ;
305	if (area.atitle && area.atitle!='') html+= ' title="' + area.atitle + '"' ;
306	if (area.ahref && area.ahref!='') html+= ' href="' +	area.ahref + '" _fcksavedurl="' +	area.ahref + '"' ;
307	if (area.atarget && area.atarget!='') html+= ' target="' + area.atarget + '"' ;
308						
309	html+='/>';
310	return html;
311}
312
313/* edit the properties of an area */
314
315function SetUrl(value)
316{
317	var id = currentAreaId ;
318	if ( id !== null)
319	{
320		myimgmap.areas[id].ahref = value ;
321		myimgmap._recalculate(id);
322	}
323}
324
325
326function SetTarget(value)
327{
328	var id = currentAreaId ;
329	if ( id !== null)
330	{
331		myimgmap.areas[id].atarget = value ;
332		myimgmap._recalculate(id);
333	}
334}
335
336
337function SetAlt(value)
338{
339	var id = currentAreaId ;
340	if ( id !== null)
341	{
342		myimgmap.areas[id].aalt = value ;
343		myimgmap._recalculate(id);
344	}
345}
346
347
348function SetTitle(value)
349{
350	var id = currentAreaId ;
351	if ( id !== null)
352	{
353		myimgmap.areas[id].atitle = value ;
354		myimgmap._recalculate(id);
355	}
356}
357
358
359/* Browse server */
360
361function BrowseServer()
362{
363	OpenFileBrowser(
364		FCKConfig.LinkBrowserURL,
365		FCKConfig.LinkBrowserWindowWidth,
366		FCKConfig.LinkBrowserWindowHeight ) ;
367}
368
369function SetUrl( url, width, height, alt )
370{
371	GetE('txtUrl').value = url ;
372
373	if ( alt )
374		GetE('txtAlt').value = alt;
375}