/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

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