mapping.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. (function () {
  2. var mapping = (function () {
  3. var currentAreaId = null;
  4. return {
  5. imgMap: null,
  6. imageSelected: null,
  7. mapSelected: null,
  8. previousImageMode: null,
  9. setMode: function (mode) {
  10. if (mode === 'pointer') {
  11. this.imgMap.is_drawing = 0;
  12. this.imgMap.nextShape = '';
  13. } else {
  14. this.imgMap.nextShape = mode;
  15. }
  16. this.activateMode(mode);
  17. },
  18. activateMode: function (mode) {
  19. if (mapping.previousImageMode) {
  20. mapping.previousImageMode.removeClass('map-button-active');
  21. }
  22. if (mode === 'pointer') {
  23. mapping.previousImageMode = mapping.dialog.getContentElement('info', 'btnPointer').getElement();
  24. mapping.previousImageMode.addClass('map-button-active');
  25. }
  26. },
  27. setCurrentAreaAttributes: function () {
  28. if (currentAreaId !== null) {
  29. mapping.imgMap.areas[currentAreaId].ahref = mapping.dialog.getValueOf('info', 'href');
  30. mapping.imgMap.areas[currentAreaId].aalt = mapping.dialog.getValueOf('info', 'alt');
  31. mapping.imgMap.areas[currentAreaId].atitle = mapping.dialog.getValueOf('info', 'title');
  32. }
  33. },
  34. onSelectedArea: function (obj) {
  35. mapping.setPropertiesVisible(true);
  36. mapping.setCurrentAreaAttributes();
  37. currentAreaId = obj.aid;
  38. mapping.dialog.setValueOf('info', 'href', obj.ahref);
  39. mapping.dialog.setValueOf('info', 'target', obj.atarget || 'notSet');
  40. mapping.dialog.setValueOf('info', 'alt', obj.aalt);
  41. mapping.dialog.setValueOf('info', 'title', obj.atitle);
  42. },
  43. setAreaProperty: function () {
  44. var id = currentAreaId;
  45. if (id !== null) {
  46. mapping.imgMap.areas[id][ "a" + this.id ] = this.getValue();
  47. mapping.imgMap._recalculate(id);
  48. }
  49. },
  50. setPropertiesVisible: function (shouldShow) {
  51. var fieldset = mapping.dialog.getContentElement('info', 'areaProperties');
  52. var fieldsetElement = fieldset.getElement();
  53. if (shouldShow) {
  54. fieldsetElement.setStyle('visibility', '');
  55. } else {
  56. fieldsetElement.setStyle('visibility', 'hidden');
  57. }
  58. },
  59. generateMapHTML: function () {
  60. var html = '';
  61. for (var i = 0; i < this.imgMap.areas.length; i++) {
  62. html += (function (area) {
  63. if (!area || area.shape === '') {
  64. return '';
  65. }
  66. var html = '<area shape="' + area.shape + '"' +
  67. ' coords="' + area.lastInput + '"';
  68. if (area.aalt) {
  69. html += ' alt="' + area.aalt + '"';
  70. }
  71. if (area.atitle) {
  72. html += ' title="' + area.atitle + '"';
  73. }
  74. if (area.ahref) {
  75. html += ' href="' + area.ahref + '" data-cke-saved-href="' + area.ahref + '"';
  76. }
  77. if (area.atarget && area.atarget !== 'notSet') {
  78. html += ' target="' + area.atarget + '"';
  79. }
  80. html += '/>';
  81. return html;
  82. })(this.imgMap.areas[i]);
  83. }
  84. return html;
  85. },
  86. setCurrentAreaId: function (id) {
  87. currentAreaId = id;
  88. },
  89. dialog: null
  90. };
  91. })();
  92. CKEDITOR.dialog.add('Mapping', function (editor) {
  93. var lang = editor.lang.mapping;
  94. var canvasDrawer = 'canvasContainer' + CKEDITOR.tools.getNextNumber();
  95. var statusContainer = 'statusContainer' + CKEDITOR.tools.getNextNumber();
  96. var dialogReady = false;
  97. (function () {
  98. var plugin = editor.plugins.mapping;
  99. if (CKEDITOR.env.ie && typeof window.CanvasRenderingContext2D === 'undefined') {
  100. CKEDITOR.scriptLoader.load(plugin.path + "lib/excanvas.js", show);
  101. }
  102. if (typeof imgmap === 'undefined') {
  103. CKEDITOR.scriptLoader.load(plugin.path + "dialogs/imgmap.js", show);
  104. }
  105. var cssNode = CKEDITOR.document.getHead().append('link');
  106. cssNode.setAttribute("rel", "stylesheet");
  107. cssNode.setAttribute("type", "text/css");
  108. cssNode.setAttribute("href", plugin.path + "css/mapping.css");
  109. })();
  110. var show = function () {
  111. if (!dialogReady) {
  112. return;
  113. }
  114. if (typeof imgmap === 'undefined') {
  115. return;
  116. }
  117. if (CKEDITOR.env.ie && typeof window.CanvasRenderingContext2D === 'undefined') {
  118. return;
  119. }
  120. mapping.setCurrentAreaId(null);
  121. mapping.mapSelected = null;
  122. var elementSelected = editor.getSelection().getSelectedElement();
  123. if (!elementSelected || !elementSelected.is("img")) {
  124. alert(lang.youMustSelectAnImageBeforeUsingTheMapEditor);
  125. mapping.dialog.hide();
  126. return;
  127. }
  128. mapping.imageSelected = elementSelected;
  129. var src = null;
  130. if (mapping.imageSelected.data) {
  131. src = mapping.imageSelected.data("cke-saved-src");
  132. } else {
  133. src = mapping.imageSelected.getAttribute("_cke_saved_src");
  134. }
  135. mapping.imageSelected = mapping.imageSelected.$;
  136. mapping.imgMap = new imgmap({
  137. mode: "editor2",
  138. label: '%a',
  139. custom_callbacks: {
  140. onSelectArea: mapping.onSelectedArea,
  141. onRemoveArea: function () {
  142. mapping.setCurrentAreaId(null);
  143. mapping.setPropertiesVisible(false);
  144. },
  145. onStatusMessage: function (str) {
  146. document.getElementById(statusContainer).innerHTML = str;
  147. },
  148. onLoadImage: function (pic) {
  149. var ckPic = new CKEDITOR.dom.element(pic);
  150. ckPic.on("dragstart", function (e) {
  151. e.data.preventDefault();
  152. });
  153. }
  154. },
  155. pic_container: document.getElementById(canvasDrawer),
  156. bounding_box: false,
  157. lang: ''
  158. });
  159. mapping.imgMap.loadStrings(imgmapStrings);
  160. mapping.imgMap.loadImage(src, parseInt(mapping.imageSelected.style.width || 0, 10), parseInt(mapping.imageSelected.style.height || 0, 10));
  161. var mapname = mapping.imageSelected.getAttribute('usemap', 2) || mapping.imageSelected.usemap;
  162. if (mapname) {
  163. mapname = mapname.substr(1);
  164. var maps = editor.document.$.getElementsByTagName('MAP');
  165. for (var i = 0; i < maps.length; i++) {
  166. if (maps[i].name === mapname || maps[i].id === mapname) {
  167. mapping.mapSelected = maps[i];
  168. mapping.imgMap.setMapHTML(mapping.mapSelected);
  169. mapping.dialog.setValueOf('info', 'MapName', mapname);
  170. break;
  171. }
  172. }
  173. }
  174. mapping.imgMap.config.custom_callbacks.onAddArea = function (id) {
  175. mapping.setPropertiesVisible(true);
  176. mapping.setCurrentAreaAttributes();
  177. mapping.setCurrentAreaId(id);
  178. mapping.dialog.getContentElement('info', 'href').setValue('', true);
  179. mapping.dialog.getContentElement('info', 'target').setValue('notSet', true);
  180. mapping.dialog.getContentElement('info', 'alt').setValue('', true);
  181. mapping.dialog.getContentElement('info', 'title').setValue('', true);
  182. };
  183. if (mapping.mapSelected) {
  184. mapping.imgMap.selectedId = 0;
  185. mapping.onSelectedArea(mapping.imgMap.areas[0]);
  186. mapping.setMode('pointer');
  187. } else {
  188. mapping.activateMode('rect');
  189. }
  190. };
  191. var removeMap = function () {
  192. editor.fire('saveSnapshot');
  193. if (mapping.imageSelected && mapping.imageSelected.nodeName === "IMG") {
  194. mapping.imageSelected.removeAttribute('usemap', 0);
  195. }
  196. if (mapping.mapSelected) {
  197. mapping.mapSelected.parentNode.removeChild(mapping.mapSelected);
  198. }
  199. mapping.dialog.hide();
  200. };
  201. return {
  202. title: lang.mappingProperties,
  203. minWidth: 600,
  204. minHeight: 400,
  205. buttons: [
  206. {
  207. type: 'button',
  208. label: lang.removeMap,
  209. onClick: removeMap
  210. },
  211. CKEDITOR.dialog.okButton,
  212. CKEDITOR.dialog.cancelButton
  213. ],
  214. contents: [
  215. {
  216. id: 'info',
  217. label: editor.lang.common.generalTab,
  218. title: editor.lang.common.generalTab,
  219. elements: [
  220. {
  221. id: 'MapName',
  222. type: 'text',
  223. label: lang.mapName,
  224. onChange: function () {
  225. mapping.imgMap.mapname = this.getValue();
  226. }
  227. },
  228. {
  229. type: 'hbox',
  230. label: 'panels',
  231. widths: ['30%', '40%'],
  232. children: [
  233. {
  234. type: 'vbox',
  235. children: [
  236. {
  237. type: 'hbox',
  238. children: [
  239. {
  240. type: 'button',
  241. id: 'btnPointer',
  242. label: lang.selector,
  243. className: 'map-button map-button-pointer',
  244. onClick: function () {
  245. mapping.setMode('pointer');
  246. }
  247. },
  248. {
  249. type: 'button',
  250. id: 'btnRemove',
  251. label: lang.removeArea,
  252. className: 'map-button map-button-remove',
  253. onClick: function () {
  254. mapping.imgMap.removeArea(mapping.imgMap.currentid);
  255. }
  256. }
  257. ]
  258. },
  259. {
  260. type: 'hbox',
  261. children: [
  262. {
  263. type: 'select',
  264. id: 'slcShape',
  265. label: lang.shape,
  266. items: [
  267. [lang.rectangle, 'rect'],
  268. [lang.circle, 'circle'],
  269. [lang.polygon, 'poly']
  270. ],
  271. onChange: function () {
  272. mapping.setMode(this.getValue());
  273. }
  274. },
  275. {
  276. type: 'select',
  277. id: 'slcZoom',
  278. label: lang.zoom,
  279. onChange: function () {
  280. var scale = this.getValue();
  281. var currentImage = document.getElementById(canvasDrawer).getElementsByTagName('img')[0];
  282. if (!currentImage) {
  283. return;
  284. }
  285. if (!currentImage.oldwidth) {
  286. currentImage.oldwidth = currentImage.width;
  287. }
  288. if (!currentImage.oldheight) {
  289. currentImage.oldheight = currentImage.height;
  290. }
  291. currentImage.style.width = (currentImage.oldwidth * scale) + "px";
  292. currentImage.style.height = (currentImage.oldheight * scale) + "px";
  293. currentImage.style.minWidth = (currentImage.oldwidth * scale) + "px";
  294. currentImage.style.minHeight = (currentImage.oldheight * scale) + "px";
  295. mapping.imgMap.scaleAllAreas(scale);
  296. },
  297. default: '1',
  298. items: [
  299. ['25%', '0.25'],
  300. ['50%', '0.5'],
  301. ['100%', '1'],
  302. ['200%', '2'],
  303. ['300%', '3']
  304. ]
  305. }
  306. ]
  307. },
  308. {
  309. type: 'fieldset',
  310. id: 'areaProperties',
  311. label: lang.area,
  312. style: 'visibility:hidden',
  313. children: [
  314. {
  315. type: 'hbox',
  316. children: [
  317. {
  318. type: 'text',
  319. id: 'href',
  320. label: lang.link,
  321. onChange: mapping.setAreaProperty
  322. },
  323. {
  324. type: 'button',
  325. id: 'browse',
  326. label: editor.lang.common.browseServer,
  327. style: 'margin-top:10px;',
  328. hidden: 'true',
  329. filebrowser: 'info:href'
  330. }
  331. ]
  332. },
  333. {
  334. type: 'select',
  335. id: 'target',
  336. label: lang.target,
  337. items: [
  338. [lang.notSet, 'notSet'],
  339. [lang.targetSelf, '_self'],
  340. [lang.targetBlank, '_blank'],
  341. [lang.targetTop, '_top']
  342. ],
  343. onChange: mapping.setAreaProperty
  344. },
  345. {
  346. type: 'text',
  347. id: 'title',
  348. label: lang.title,
  349. onChange: mapping.setAreaProperty
  350. },
  351. {
  352. type: 'text',
  353. id: 'alt',
  354. label: lang.alternativeText,
  355. onChange: mapping.setAreaProperty
  356. }
  357. ]
  358. },
  359. {
  360. type: 'html',
  361. html: '<p id="' + statusContainer + '">&nbsp;</p>'
  362. }
  363. ]
  364. },
  365. {
  366. type: 'html',
  367. html: '<div id="' + canvasDrawer + '" class="map-canvas-drawer"></div>'
  368. }
  369. ]
  370. }
  371. ]
  372. }
  373. ],
  374. onLoad: function () {
  375. mapping.dialog = this;
  376. },
  377. onShow: function () {
  378. dialogReady = true;
  379. show();
  380. },
  381. onHide: function () {
  382. if (mapping.previousImageMode) {
  383. mapping.previousImageMode.removeClass('map-button-active');
  384. mapping.previousImageMode = null;
  385. }
  386. document.getElementById(canvasDrawer).innerHTML = '';
  387. },
  388. onOk: function () {
  389. mapping.setCurrentAreaAttributes();
  390. if (mapping.imageSelected && mapping.imageSelected.nodeName === "IMG") {
  391. var currentGeneratedMap = mapping.generateMapHTML();
  392. if (!currentGeneratedMap) {
  393. removeMap();
  394. return;
  395. }
  396. mapping.imgMap.mapid = mapping.imgMap.mapname = mapping.dialog.getValueOf('info', 'MapName');
  397. var result = editor.fire('mapping.validate', mapping.imgMap);
  398. if (typeof result !== 'object') {
  399. return false;
  400. }
  401. editor.fire('saveSnapshot');
  402. if (!mapping.mapSelected) {
  403. mapping.mapSelected = editor.document.$.createElement('map');
  404. mapping.imageSelected.parentNode.appendChild(mapping.mapSelected);
  405. }
  406. mapping.mapSelected.innerHTML = currentGeneratedMap;
  407. if (mapping.mapSelected.name) {
  408. mapping.mapSelected.removeAttribute('name');
  409. }
  410. mapping.mapSelected.name = mapping.imgMap.getMapName();
  411. mapping.mapSelected.id = mapping.imgMap.getMapId();
  412. mapping.imageSelected.setAttribute('usemap', "#" + mapping.mapSelected.name, 0);
  413. if (CKEDITOR.plugins.mapping && CKEDITOR.plugins.mapping.generate) {
  414. CKEDITOR.plugins.mapping.generate(mapping.imageSelected, mapping.mapSelected);
  415. }
  416. }
  417. }
  418. };
  419. });
  420. })();