hotspot.js 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798
  1. window.HotspotQuestion = (function () {
  2. return function (settings) {
  3. var HotspotModel = function (attributes) {
  4. this.attributes = attributes;
  5. this.id = 0;
  6. this.name = '';
  7. this.changeEvent = null;
  8. };
  9. HotspotModel.prototype.set = function (key, value) {
  10. this.attributes[key] = value;
  11. if (this.changeEvent) {
  12. this.changeEvent(this);
  13. }
  14. };
  15. HotspotModel.prototype.get = function (key) {
  16. return this.attributes[key];
  17. };
  18. HotspotModel.prototype.onChange = function (callback) {
  19. this.changeEvent = callback;
  20. };
  21. HotspotModel.decode = function () {
  22. return new this;
  23. };
  24. HotspotModel.prototype.encode = function () {
  25. return '';
  26. };
  27. var SquareModel = function (attributes) {
  28. HotspotModel.call(this, attributes);
  29. };
  30. SquareModel.prototype = Object.create(HotspotModel.prototype);
  31. SquareModel.prototype.setStartPoint = function (x, y) {
  32. x = parseInt(x);
  33. y = parseInt(y);
  34. this.set('x', x);
  35. this.set('y', y);
  36. };
  37. SquareModel.prototype.setEndPoint = function (x, y) {
  38. var startX = this.get('x'),
  39. startY = this.get('y');
  40. x = parseInt(x);
  41. y = parseInt(y);
  42. if (x >= startX) {
  43. this.set('width', x - startX);
  44. }
  45. if (y >= startY) {
  46. this.set('height', y - startY);
  47. }
  48. };
  49. SquareModel.decode = function (hotspotInfo) {
  50. var coords = hotspotInfo.coord.split('|'),
  51. position = coords[0].split(';'),
  52. hotspot = new SquareModel({
  53. x: parseInt(position[0]),
  54. y: parseInt(position[1]),
  55. width: parseInt(coords[1]),
  56. height: parseInt(coords[2])
  57. });
  58. hotspot.id = hotspotInfo.iid;
  59. hotspot.name = hotspotInfo.answer;
  60. return hotspot;
  61. };
  62. SquareModel.prototype.encode = function () {
  63. return [
  64. [
  65. this.get('x'),
  66. this.get('y')
  67. ].join(';'),
  68. this.get('width'),
  69. this.get('height')
  70. ].join('|');
  71. };
  72. var EllipseModel = function (attributes) {
  73. HotspotModel.call(this, attributes);
  74. };
  75. EllipseModel.prototype = Object.create(HotspotModel.prototype);
  76. EllipseModel.prototype.setStartPoint = function (x, y) {
  77. x = parseInt(x);
  78. y = parseInt(y);
  79. this.set('centerX', x);
  80. this.set('centerY', y);
  81. };
  82. EllipseModel.prototype.setEndPoint = function (x, y) {
  83. var startX = this.get('centerX'),
  84. startY = this.get('centerY'),
  85. width = 0,
  86. height = 0;
  87. x = parseInt(x);
  88. y = parseInt(y);
  89. if (x >= startX) {
  90. width = x - startX;
  91. this.set('radiusX', Math.round(width / 2));
  92. this.set('centerX', startX + this.get('radiusX'));
  93. }
  94. if (y >= startY) {
  95. height = y - startY;
  96. this.set('radiusY', Math.round(height / 2));
  97. this.set('centerY', startY + this.get('radiusY'));
  98. }
  99. };
  100. EllipseModel.decode = function (hotspotInfo) {
  101. var coords = hotspotInfo.coord.split('|'),
  102. center = coords[0].split(';'),
  103. hotspot = new EllipseModel({
  104. centerX: parseInt(center[0]),
  105. centerY: parseInt(center[1]),
  106. radiusX: parseInt(coords[1]),
  107. radiusY: parseInt(coords[2])
  108. });
  109. hotspot.id = hotspotInfo.iid;
  110. hotspot.name = hotspotInfo.answer;
  111. return hotspot;
  112. };
  113. EllipseModel.prototype.encode = function () {
  114. return [
  115. [
  116. this.get('centerX'),
  117. this.get('centerY')
  118. ].join(';'),
  119. this.get('radiusX'),
  120. this.get('radiusY')
  121. ].join('|');
  122. };
  123. var PolygonModel = function (attributes) {
  124. HotspotModel.call(this, attributes);
  125. };
  126. PolygonModel.prototype = Object.create(HotspotModel.prototype);
  127. PolygonModel.prototype.addPoint = function (x, y) {
  128. var points = this.get('points');
  129. x = parseInt(x);
  130. y = parseInt(y);
  131. points.push([x, y]);
  132. this.set('points', points);
  133. };
  134. PolygonModel.decode = function (hotspotInfo) {
  135. var pairedPoints = hotspotInfo.coord.split('|'),
  136. points = [];
  137. $.each(pairedPoints, function (index, pair) {
  138. var point = pair.split(';');
  139. points.push([
  140. parseInt(point[0]),
  141. parseInt(point[1])
  142. ]);
  143. });
  144. var hotspot = new PolygonModel({
  145. points: points
  146. });
  147. hotspot.id = hotspotInfo.iid;
  148. hotspot.name = hotspotInfo.answer;
  149. return hotspot;
  150. };
  151. PolygonModel.prototype.encode = function () {
  152. var pairedPoints = [];
  153. this.get('points').forEach(function (point) {
  154. pairedPoints.push(
  155. point.join(';')
  156. );
  157. });
  158. return pairedPoints.join('|');
  159. };
  160. var AnswerModel = function (attributes) {
  161. this.attributes = attributes;
  162. this.changeEvent = null;
  163. };
  164. AnswerModel.prototype.set = function (key, value) {
  165. this.attributes[key] = value;
  166. if (this.changeEvent) {
  167. this.changeEvent(this);
  168. }
  169. };
  170. AnswerModel.prototype.get = function (key) {
  171. return this.attributes[key];
  172. };
  173. AnswerModel.prototype.onChange = function (callback) {
  174. this.changeEvent = callback;
  175. };
  176. AnswerModel.decode = function (answerInfo) {
  177. var coords = answerInfo.split(';');
  178. return new AnswerModel({
  179. x: coords[0],
  180. y: coords[1]
  181. });
  182. };
  183. var AnswersCollection = function () {
  184. this.models = [];
  185. this.length = 0;
  186. this.addEvent = null;
  187. };
  188. AnswersCollection.prototype.add = function (answerModel) {
  189. this.models.push(answerModel);
  190. this.length++;
  191. if (this.addEvent) {
  192. this.addEvent(answerModel);
  193. }
  194. };
  195. AnswersCollection.prototype.get = function (index) {
  196. return this.models[index];
  197. };
  198. AnswersCollection.prototype.onAdd = function (callback) {
  199. this.addEvent = callback;
  200. };
  201. var HotspotsCollection = function () {
  202. this.hotspots = [];
  203. this.length = 0;
  204. this.addEvent = null;
  205. };
  206. HotspotsCollection.prototype.add = function (hotspot) {
  207. this.hotspots.push(hotspot);
  208. this.length++;
  209. if (this.addEvent) {
  210. this.addEvent(hotspot);
  211. }
  212. };
  213. HotspotsCollection.prototype.get = function (index) {
  214. return this.hotspots[index];
  215. };
  216. HotspotsCollection.prototype.set = function (index, newHotspot) {
  217. this.hotspots[index] = newHotspot;
  218. };
  219. HotspotsCollection.prototype.onAdd = function (callback) {
  220. this.addEvent = callback;
  221. };
  222. var HotspotSVG = function (modelModel, index) {
  223. var self = this;
  224. this.model = modelModel;
  225. this.hotspotIndex = index;
  226. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  227. this.model.onChange(function (hotspotModel) {
  228. self.render();
  229. });
  230. };
  231. HotspotSVG.prototype.render = function () {
  232. var newEl = null;
  233. if (this.model instanceof SquareModel) {
  234. newEl = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  235. newEl.setAttribute('x', this.model.get('x'));
  236. newEl.setAttribute('y', this.model.get('y'));
  237. newEl.setAttribute('width', this.model.get('width'));
  238. newEl.setAttribute('height', this.model.get('height'));
  239. } else if (this.model instanceof EllipseModel) {
  240. newEl = document.createElementNS('http://www.w3.org/2000/svg', 'ellipse');
  241. newEl.setAttribute('cx', this.model.get('centerX'));
  242. newEl.setAttribute('cy', this.model.get('centerY'));
  243. newEl.setAttribute('rx', this.model.get('radiusX'));
  244. newEl.setAttribute('ry', this.model.get('radiusY'));
  245. } else if (this.model instanceof PolygonModel) {
  246. var pointsPaired = [];
  247. this.model.get('points').forEach(function (point) {
  248. pointsPaired.push(point.join(','));
  249. });
  250. newEl = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
  251. newEl.setAttribute(
  252. 'points',
  253. pointsPaired.join(' ')
  254. );
  255. }
  256. newEl.setAttribute('class', 'hotspot-' + this.hotspotIndex);
  257. if (this.el.parentNode) {
  258. this.el.parentNode.replaceChild(newEl, this.el);
  259. }
  260. this.el = newEl;
  261. return this;
  262. };
  263. var AnswerSVG = function (answerModel, index) {
  264. var self = this;
  265. this.model = answerModel;
  266. this.answerIndex = index;
  267. this.circleEl = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
  268. this.textEl = document.createElementNS('http://www.w3.org/2000/svg', 'text');
  269. this.model.onChange(function (answerModel) {
  270. self.render();
  271. });
  272. };
  273. AnswerSVG.prototype.render = function () {
  274. this.circleEl.setAttribute('cx', this.model.get('x'));
  275. this.circleEl.setAttribute('cy', this.model.get('y'));
  276. this.circleEl.setAttribute('r', 15);
  277. this.circleEl.setAttribute('class', 'hotspot-answer-point');
  278. this.textEl.setAttribute('x', this.model.get('x'));
  279. this.textEl.setAttribute('y', this.model.get('y'));
  280. this.textEl.setAttribute('dy', 5);
  281. this.textEl.setAttribute('text-anchor', 'middle');
  282. this.textEl.setAttribute('class', 'hotspot-answer-text');
  283. this.textEl.textContent = this.answerIndex + 1;
  284. return this;
  285. };
  286. var HotspotSelect = function (index, hotspotsCollection, hotspotSVG) {
  287. this.hotspotIndex = index;
  288. this.hotspotsCollection = hotspotsCollection;
  289. this.hotspotSVG = hotspotSVG;
  290. this.el = document.createElement('div');
  291. this.el.className = 'col-xs-6 col-sm-4 col-md-3 col-lg-2';
  292. selectedHotspotIndex = this.hotspotIndex;
  293. $('.input-group').removeClass('active');
  294. };
  295. HotspotSelect.prototype.render = function () {
  296. var self = this,
  297. $el = $(this.el);
  298. var template = '\n\
  299. <div class="input-group hotspot-' + this.hotspotIndex + ' active">\n\
  300. <span class="input-group-addon" id="hotspot-' + this.hotspotIndex + '">\n\
  301. <span class="fa fa-square fa-fw" data-hidden="true"></span>\n\
  302. <span class="sr-only">' + (this.hotspotSVG.model.name ? this.hotspotSVG.model.name : 'hotspot ' + this.hotspotIndex) + '</span>\n\
  303. </span>\n\
  304. <select class="form-control" aria-describedby="hotspot-' + this.hotspotIndex + '">\n\
  305. <option value="square">' + lang.Square + '</option>\n\
  306. <option value="ellipse">' + lang.Ellipse + '</option>\n\
  307. <option value="polygon">' + lang.Polygon + '</option>\n\
  308. </select>\n\
  309. </div>\n\
  310. ';
  311. $el.html(template);
  312. $el.find('select')
  313. .on('change', function () {
  314. selectedHotspotIndex = self.hotspotIndex;
  315. var newHotspot = null,
  316. changeEvent = self.hotspotSVG.model.changeEvent;
  317. switch (this.value) {
  318. case 'square':
  319. newHotspot = new SquareModel({
  320. x: 0,
  321. y: 0,
  322. width: 0,
  323. height: 0
  324. });
  325. $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Other);
  326. break;
  327. case 'ellipse':
  328. newHotspot = new EllipseModel({
  329. centerX: 0,
  330. centerY: 0,
  331. radiusX: 0,
  332. radiusY: 0
  333. });
  334. $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Other);
  335. break;
  336. case 'polygon':
  337. newHotspot = new PolygonModel({
  338. points: []
  339. });
  340. $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Polygon);
  341. break;
  342. }
  343. newHotspot.onChange(changeEvent);
  344. self.hotspotsCollection.set(self.hotspotIndex, newHotspot);
  345. self.hotspotSVG.model = newHotspot;
  346. })
  347. .on('focus', function () {
  348. $('.input-group').removeClass('active');
  349. $el.find('.input-group').addClass('active');
  350. selectedHotspotIndex = self.hotspotIndex;
  351. switch (this.value) {
  352. case 'square':
  353. $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Other);
  354. break;
  355. case 'ellipse':
  356. $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Other);
  357. break;
  358. case 'polygon':
  359. $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Polygon);
  360. break;
  361. }
  362. })
  363. .val(function () {
  364. if (self.hotspotSVG.model instanceof SquareModel) {
  365. $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Other);
  366. return 'square';
  367. }
  368. if (self.hotspotSVG.model instanceof EllipseModel) {
  369. $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Other);
  370. return 'ellipse';
  371. }
  372. if (self.hotspotSVG.model instanceof PolygonModel) {
  373. $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Polygon);
  374. return 'polygon';
  375. }
  376. });
  377. return this;
  378. };
  379. var ContextMenu = function () {
  380. this.el = document.createElement('ul');
  381. $(this.el).addClass('dropdown-menu').attr('id', "hotspot-context-menu");
  382. this.hideEvent = null;
  383. };
  384. ContextMenu.prototype.onHide = function (callback) {
  385. this.hideEvent = callback;
  386. };
  387. ContextMenu.prototype.render = function () {
  388. var self = this,
  389. template = '\n\
  390. <li>\n\
  391. <a href="#">' + lang.ClosePolygon + '</a>\n\
  392. </li>\n\
  393. ';
  394. $(this.el).html(template);
  395. $(this.el).find('a').on('click', function (e) {
  396. e.preventDefault();
  397. if (self.hideEvent) {
  398. self.hideEvent(e);
  399. }
  400. $(self.el).hide();
  401. });
  402. return this;
  403. };
  404. ContextMenu.prototype.show = function (x, y) {
  405. $(this.el).css({left: x, top: y}).show();
  406. };
  407. var AdminHotspotsSVG = function (hotspotsCollection, image) {
  408. var self = this;
  409. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  410. this.collection = hotspotsCollection;
  411. this.image = image;
  412. this.collection.onAdd(function (hotspotModel) {
  413. self.renderHotspot(hotspotModel);
  414. });
  415. };
  416. AdminHotspotsSVG.prototype.render = function () {
  417. this.el.setAttribute('version', '1.1');
  418. this.el.setAttribute('viewBox', '0 0 ' + this.image.width + ' ' + this.image.height);
  419. this.el.setAttribute('width', this.image.width);
  420. this.el.setAttribute('height', this.image.height);
  421. var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  422. imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'href', this.image.src);
  423. imageSvg.setAttribute('width', this.image.width);
  424. imageSvg.setAttribute('height', this.image.height);
  425. this.el.appendChild(imageSvg);
  426. this.setEvents();
  427. return this;
  428. };
  429. AdminHotspotsSVG.prototype.renderHotspot = function (hotspot) {
  430. var hotspotIndex = this.collection.length - 1,
  431. hotspotSVG = new HotspotSVG(hotspot, hotspotIndex);
  432. this.el.appendChild(
  433. hotspotSVG.render().el
  434. );
  435. var hotspotSelect = new HotspotSelect(hotspotIndex, this.collection, hotspotSVG);
  436. $(config.selector).parent().find('.row').append(
  437. hotspotSelect.render().el
  438. );
  439. };
  440. AdminHotspotsSVG.prototype.setEvents = function () {
  441. var self = this,
  442. $el = $(this.el);
  443. isDrawing = false;
  444. var startPoint = {
  445. x: 0,
  446. y: 0
  447. };
  448. $el
  449. .on('dragstart', function (e) {
  450. e.preventDefault();
  451. })
  452. .on('mousedown', function (e) {
  453. e.preventDefault();
  454. if (e.button > 0) {
  455. return;
  456. }
  457. if (self.collection.length <= 0) {
  458. return;
  459. }
  460. var currentHotspot = self.collection.get(selectedHotspotIndex);
  461. if (!currentHotspot) {
  462. return;
  463. }
  464. startPoint = getPointOnImage(self.el, e.clientX, e.clientY);
  465. if (currentHotspot instanceof SquareModel) {
  466. isDrawing = true;
  467. currentHotspot.set('x', startPoint.x);
  468. currentHotspot.set('y', startPoint.y);
  469. currentHotspot.set('width', 0);
  470. currentHotspot.set('height', 0);
  471. return;
  472. }
  473. if (currentHotspot instanceof EllipseModel) {
  474. isDrawing = true;
  475. currentHotspot.set('centerX', 0);
  476. currentHotspot.set('centerY', 0);
  477. currentHotspot.set('radiusX', 0);
  478. currentHotspot.set('radiusY', 0);
  479. return;
  480. }
  481. })
  482. .on('mousemove', function (e) {
  483. e.preventDefault();
  484. if (self.collection.length <= 0) {
  485. return;
  486. }
  487. if (!isDrawing) {
  488. return;
  489. }
  490. var currentHotspot = self.collection.get(selectedHotspotIndex),
  491. currentPoint = getPointOnImage(self.el, e.clientX, e.clientY);
  492. if (!currentHotspot) {
  493. return;
  494. }
  495. if (currentHotspot instanceof SquareModel) {
  496. if (startPoint.x < currentPoint.x) {
  497. currentHotspot.set('width', currentPoint.x - startPoint.x);
  498. } else {
  499. currentHotspot.set('x', currentPoint.x);
  500. currentHotspot.set('width', startPoint.x - currentPoint.x);
  501. }
  502. if (startPoint.y < currentPoint.y) {
  503. currentHotspot.set('height', currentPoint.y - startPoint.y);
  504. } else {
  505. currentHotspot.set('y', currentPoint.y);
  506. currentHotspot.set('height', startPoint.y - currentPoint.y);
  507. }
  508. return;
  509. }
  510. if (currentHotspot instanceof EllipseModel) {
  511. var width = 0,
  512. height = 0;
  513. if (startPoint.x < currentPoint.x) {
  514. width = currentPoint.x - startPoint.x;
  515. currentHotspot.set('radiusX', Math.round(width / 2));
  516. currentHotspot.set('centerX', startPoint.x + currentHotspot.get('radiusX'));
  517. } else {
  518. width = startPoint.x - currentPoint.x;
  519. currentHotspot.set('radiusX', Math.round(width / 2));
  520. currentHotspot.set('centerX', currentPoint.x + currentHotspot.get('radiusX'))
  521. }
  522. if (startPoint.y < currentPoint.y) {
  523. height = currentPoint.y - startPoint.y;
  524. currentHotspot.set('radiusY', Math.round(height / 2));
  525. currentHotspot.set('centerY', startPoint.y + currentHotspot.get('radiusY'));
  526. } else {
  527. height = startPoint.y - currentPoint.y;
  528. currentHotspot.set('radiusY', Math.round(height / 2));
  529. currentHotspot.set('centerY', currentPoint.y + currentHotspot.get('radiusY'));
  530. }
  531. return;
  532. }
  533. })
  534. .on('mouseup', function (e) {
  535. e.preventDefault();
  536. if (e.button > 0) {
  537. return;
  538. }
  539. if (self.collection.length <= 0) {
  540. return;
  541. }
  542. if (!isDrawing) {
  543. return;
  544. }
  545. var currentHotspot = self.collection.get(selectedHotspotIndex),
  546. hotspotTypeSelector = '[name="hotspot_type[' + (selectedHotspotIndex + 1) + ']"]',
  547. hotspotCoordSelector = '[name="hotspot_coordinates[' + (selectedHotspotIndex + 1) + ']"]';
  548. if (!currentHotspot) {
  549. return;
  550. }
  551. if (currentHotspot instanceof SquareModel) {
  552. $(hotspotTypeSelector).val('square');
  553. $(hotspotCoordSelector).val(currentHotspot.encode());
  554. isDrawing = false;
  555. } else if (currentHotspot instanceof EllipseModel) {
  556. $(hotspotTypeSelector).val('circle');
  557. $(hotspotCoordSelector).val(currentHotspot.encode());
  558. isDrawing = false;
  559. }
  560. })
  561. .on('click', function (e) {
  562. e.preventDefault();
  563. var currentHotspot = self.collection.get(selectedHotspotIndex),
  564. currentPoint = getPointOnImage(self.el, e.clientX, e.clientY);
  565. if (!currentHotspot) {
  566. return;
  567. }
  568. if (currentHotspot instanceof PolygonModel) {
  569. var points = [];
  570. if (!isDrawing) {
  571. isDrawing = true;
  572. } else {
  573. points = currentHotspot.get('points');
  574. }
  575. points.push([currentPoint.x, currentPoint.y]);
  576. currentHotspot.set('points', points);
  577. return;
  578. }
  579. })
  580. .on('contextmenu', function (e) {
  581. e.preventDefault();
  582. var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY),
  583. currentHotspot = self.collection.get(selectedHotspotIndex),
  584. hotspotTypeSelector = '[name="hotspot_type[' + (selectedHotspotIndex + 1) + ']"]',
  585. hotspotCoordSelector = '[name="hotspot_coordinates[' + (selectedHotspotIndex + 1) + ']"]';
  586. if (!currentHotspot) {
  587. return;
  588. }
  589. if (currentHotspot instanceof PolygonModel) {
  590. contextMenu.show(currentPoint.x, currentPoint.y);
  591. contextMenu.onHide(function () {
  592. $(hotspotTypeSelector).val('poly');
  593. $(hotspotCoordSelector).val(currentHotspot.encode());
  594. isDrawing = false;
  595. });
  596. }
  597. });
  598. };
  599. var startHotspotsAdmin = function (questionInfo) {
  600. var image = new Image();
  601. image.onload = function () {
  602. $(config.selector).html('');
  603. var hotspotsCollection = new HotspotsCollection(),
  604. hotspotsSVG = new AdminHotspotsSVG(hotspotsCollection, this);
  605. $(config.selector).css('width', this.width).append(hotspotsSVG.render().el);
  606. $(config.selector).parent().prepend('\n\
  607. <div id="hotspot-messages" class="alert alert-info">\n\
  608. <h4><span class="fa fa-info-circle" aria-hidden="true"></span> ' + lang.HotspotStatus1 + '</h4>\n\
  609. <span></span>\n\
  610. </div>\n\
  611. ');
  612. $(config.selector).parent().prepend('<div class="row"></div>');
  613. contextMenu = new ContextMenu();
  614. $(config.selector).append(
  615. contextMenu.render().el
  616. );
  617. $.each(questionInfo.hotspots, function (index, hotspotInfo) {
  618. var hotspot = null;
  619. switch (hotspotInfo.type) {
  620. case 'square':
  621. default:
  622. hotspot = SquareModel.decode(hotspotInfo);
  623. break;
  624. case 'circle':
  625. hotspot = EllipseModel.decode(hotspotInfo);
  626. break;
  627. case 'poly':
  628. hotspot = PolygonModel.decode(hotspotInfo);
  629. break;
  630. }
  631. hotspotsCollection.add(hotspot);
  632. });
  633. };
  634. image.src = questionInfo.image;
  635. lang = questionInfo.lang;
  636. };
  637. var UserHotspotsSVG = function (hotspotsCollection, answersCollection, image) {
  638. var self = this;
  639. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  640. this.hotspotsCollection = hotspotsCollection;
  641. this.answersCollection = answersCollection;
  642. this.image = image;
  643. this.answersCollection.onAdd(function (answerModel) {
  644. self.renderAnswer(answerModel);
  645. });
  646. };
  647. UserHotspotsSVG.prototype.render = function () {
  648. this.el.setAttribute('version', '1.1');
  649. this.el.setAttribute('viewBox', '0 0 ' + this.image.width + ' ' + this.image.height);
  650. this.el.setAttribute('width', this.image.width);
  651. this.el.setAttribute('height', this.image.height);
  652. var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  653. imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'href', this.image.src);
  654. imageSvg.setAttribute('width', this.image.width);
  655. imageSvg.setAttribute('height', this.image.height);
  656. this.el.appendChild(imageSvg);
  657. this.setEvents();
  658. return this;
  659. };
  660. UserHotspotsSVG.prototype.renderAnswer = function (answerModel) {
  661. var answerSVG = new AnswerSVG(answerModel, this.answersCollection.length - 1);
  662. answerSVG.render();
  663. this.el.appendChild(answerSVG.circleEl);
  664. this.el.appendChild(answerSVG.textEl);
  665. var hotspot = this.hotspotsCollection.get(this.answersCollection.length - 1),
  666. x = answerModel.get('x'),
  667. y = answerModel.get('y');
  668. $('<input>', {
  669. type: 'hidden',
  670. name: 'hotspot[' + config.questionId + '][' + hotspot.id + ']'
  671. }).val(function () {
  672. return [x, y].join(';');
  673. }).appendTo(this.el.parentNode);
  674. $('<input>', {
  675. type: 'hidden',
  676. name: 'choice[' + config.questionId + '][' + hotspot.id + ']'
  677. }).val(function () {
  678. return [x, y].join(';');
  679. }).appendTo(this.el.parentNode);
  680. };
  681. UserHotspotsSVG.prototype.setEvents = function () {
  682. var self = this,
  683. $el = $(this.el);
  684. var isMoving = false,
  685. answerIndex = null,
  686. hotspot = null,
  687. point = {};
  688. $el
  689. .on('dragstart', function (e) {
  690. e.preventDefault();
  691. })
  692. .on('click', function (e) {
  693. e.preventDefault();
  694. if (isMoving) {
  695. return;
  696. }
  697. if (self.answersCollection.length >= self.hotspotsCollection.length) {
  698. return;
  699. }
  700. var point = getPointOnImage(self.el, e.clientX, e.clientY);
  701. var answerModel = new AnswerModel({
  702. x: point.x,
  703. y: point.y
  704. });
  705. self.answersCollection.add(answerModel);
  706. if (self.answersCollection.length === self.hotspotsCollection.length) {
  707. $(config.selector).parent()
  708. .find('#hotspot-messages-' + config.questionId + ' span:not(.fa)').text(
  709. lang.HotspotExerciseFinished
  710. );
  711. return;
  712. }
  713. $(config.selector).parent()
  714. .find('#hotspot-messages-' + config.questionId + ' span:not(.fa)').text(
  715. lang.NextAnswer +
  716. ' ' +
  717. self.hotspotsCollection
  718. .get(self.answersCollection.length)
  719. .name
  720. );
  721. isMoving = false;
  722. })
  723. .on('mousedown', 'circle, text', function (e) {
  724. e.preventDefault();
  725. isMoving = true;
  726. if (e.target.tagName === 'circle') {
  727. //Hack to move correctly the hot spots if there are more than one HS question in same page
  728. answerIndex = $(e.target).next().html();
  729. answerIndex = parseInt(answerIndex) - 1;
  730. } else if (e.target.tagName === 'text') {
  731. //Hack to move correctly the hot spots if there are more than one HS question in same page
  732. answerIndex = $(e.target).html();
  733. answerIndex = parseInt(answerIndex) - 1;
  734. }
  735. hotspot = self.hotspotsCollection.get(answerIndex);
  736. })
  737. .on('mousemove', function (e) {
  738. if (!isMoving) {
  739. return;
  740. }
  741. e.preventDefault();
  742. point = getPointOnImage(self.el, e.clientX, e.clientY);
  743. self.answersCollection.get(answerIndex).set('x', point.x);
  744. self.answersCollection.get(answerIndex).set('y', point.y);
  745. })
  746. .on('mouseup', function (e) {
  747. if (!isMoving) {
  748. return;
  749. }
  750. e.preventDefault();
  751. $('[name="hotspot[' + config.questionId + '][' + hotspot.id + ']"]').val(function () {
  752. return [point.x, point.y].join(';');
  753. });
  754. $('[name="choice[' + config.questionId + '][' + hotspot.id + ']"]').val(function () {
  755. return [point.x, point.y].join(';');
  756. });
  757. isMoving = false;
  758. });
  759. };
  760. var startHotspotsUser = function (questionInfo) {
  761. var image = new Image();
  762. image.onload = function () {
  763. $(config.selector).html('');
  764. var hotspotsCollection = new HotspotsCollection(),
  765. answersCollection = new AnswersCollection(),
  766. hotspotsSVG = new UserHotspotsSVG(hotspotsCollection, answersCollection, this);
  767. $(config.selector).css('width', this.width).append(hotspotsSVG.render().el);
  768. $(config.selector).parent().prepend('\n\
  769. <div id="hotspot-messages-' + config.questionId + '" class="alert alert-info">\n\
  770. <h4>\n\
  771. <span class="fa fa-info-circle" aria-hidden="true"></span>\n\
  772. <span></span>\n\
  773. </h4>\n\
  774. </div>\n\
  775. ');
  776. $.each(questionInfo.hotspots, function (index, hotspotInfo) {
  777. var hotspot = null;
  778. switch (hotspotInfo.type) {
  779. case 'square':
  780. default:
  781. hotspot = SquareModel.decode(hotspotInfo);
  782. break;
  783. case 'circle':
  784. hotspot = EllipseModel.decode(hotspotInfo);
  785. break;
  786. case 'poly':
  787. hotspot = PolygonModel.decode(hotspotInfo);
  788. break;
  789. }
  790. hotspotsCollection.add(hotspot);
  791. });
  792. $.each(questionInfo.answers, function (index, answerInfo) {
  793. var answerModel = new AnswerModel({
  794. x: answerInfo.x,
  795. y: answerInfo.y
  796. });
  797. answersCollection.add(answerModel);
  798. });
  799. $(config.selector).parent().find('#hotspot-messages-' + config.questionId + ' span:not(.fa)')
  800. .text(
  801. lang.NextAnswer + ' ' + hotspotsCollection.get(0).name
  802. );
  803. };
  804. image.src = questionInfo.image;
  805. lang = questionInfo.lang;
  806. };
  807. var SolutionHotspotsSVG = function (hotspotsCollection, answersCollection, image) {
  808. this.hotspotsCollection = hotspotsCollection;
  809. this.answersCollection = answersCollection;
  810. this.image = image;
  811. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  812. var self = this;
  813. this.hotspotsCollection.onAdd(function (hotspotModel) {
  814. self.renderHotspot(hotspotModel);
  815. });
  816. this.answersCollection.onAdd(function (answerModel) {
  817. self.renderAnswer(answerModel);
  818. });
  819. };
  820. SolutionHotspotsSVG.prototype.render = function () {
  821. this.el.setAttribute('version', '1.1');
  822. this.el.setAttribute('viewBox', '0 0 ' + this.image.width + ' ' + this.image.height);
  823. this.el.setAttribute('width', this.image.width);
  824. this.el.setAttribute('height', this.image.height);
  825. var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  826. imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'href', this.image.src);
  827. imageSvg.setAttribute('width', this.image.width);
  828. imageSvg.setAttribute('height', this.image.height);
  829. this.el.appendChild(imageSvg);
  830. return this;
  831. };
  832. SolutionHotspotsSVG.prototype.renderHotspot = function (hotspotModel) {
  833. var hotspotIndex = this.hotspotsCollection.length - 1,
  834. hotspotSVG = new HotspotSVG(hotspotModel, hotspotIndex);
  835. this.el.appendChild(
  836. hotspotSVG.render().el
  837. );
  838. return this;
  839. };
  840. SolutionHotspotsSVG.prototype.renderAnswer = function (answerModel) {
  841. var pointSVG = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
  842. pointSVG.setAttribute('cx', answerModel.get('x'));
  843. pointSVG.setAttribute('cy', answerModel.get('y'));
  844. pointSVG.setAttribute('r', 15);
  845. pointSVG.setAttribute('class', 'hotspot-answer-point');
  846. var textSVG = document.createElementNS('http://www.w3.org/2000/svg', 'text');
  847. textSVG.setAttribute('x', answerModel.get('x'));
  848. textSVG.setAttribute('y', answerModel.get('y'));
  849. textSVG.setAttribute('dy', 5);
  850. textSVG.setAttribute('font-family', 'sans-serif');
  851. textSVG.setAttribute('text-anchor', 'middle');
  852. textSVG.setAttribute('fill', 'white');
  853. textSVG.textContent = this.answersCollection.length;
  854. this.el.appendChild(pointSVG);
  855. this.el.appendChild(textSVG);
  856. return this;
  857. };
  858. var startHotspotsSolution = function (questionInfo) {
  859. var image = new Image();
  860. image.onload = function () {
  861. $(config.selector).html('');
  862. var hotspotsCollection = new HotspotsCollection(),
  863. answersCollection = new AnswersCollection(),
  864. hotspotsSVG = new SolutionHotspotsSVG(hotspotsCollection, answersCollection, this);
  865. $(config.selector).css('width', this.width).append(hotspotsSVG.render().el);
  866. $.each(questionInfo.hotspots, function (index, hotspotInfo) {
  867. var hotspot = null;
  868. switch (hotspotInfo.type) {
  869. case 'square':
  870. default:
  871. hotspot = SquareModel.decode(hotspotInfo);
  872. break;
  873. case 'circle':
  874. hotspot = EllipseModel.decode(hotspotInfo);
  875. break;
  876. case 'poly':
  877. hotspot = PolygonModel.decode(hotspotInfo);
  878. break;
  879. }
  880. hotspotsCollection.add(hotspot);
  881. });
  882. $.each(questionInfo.answers, function (index, answerInfo) {
  883. var answer = AnswerModel.decode(answerInfo);
  884. answersCollection.add(answer);
  885. });
  886. };
  887. image.src = questionInfo.image;
  888. lang = questionInfo.lang;
  889. };
  890. var getPointOnImage = function (referenceElement, x, y) {
  891. var pointerPosition = {
  892. left: x + window.scrollX,
  893. top: y + window.scrollY
  894. },
  895. canvasOffset = {
  896. x: referenceElement.getBoundingClientRect().left + window.scrollX,
  897. y: referenceElement.getBoundingClientRect().top + window.scrollY
  898. };
  899. return {
  900. x: Math.round(pointerPosition.left - canvasOffset.x),
  901. y: Math.round(pointerPosition.top - canvasOffset.y)
  902. };
  903. };
  904. var config, lang, selectedHotspotIndex = 0, contextMenu;
  905. config = $.extend({
  906. questionId: 0,
  907. selector: ''
  908. }, settings);
  909. if (!config.questionId || !config.selector) {
  910. return;
  911. }
  912. $(config.selector).html('\n\
  913. <span class="fa fa-spinner fa-spin fa-3x" aria-hidden="hidden"></span>\n\
  914. <span class="sr-only">Loading</span>\n\
  915. ');
  916. var xhrQuestion = null;
  917. switch (config.for) {
  918. case 'admin':
  919. xhrQuestion = $.getJSON(config.relPath + 'exercise/hotspot_actionscript_admin.as.php?' + webCidReq, {
  920. modifyAnswers: parseInt(config.questionId)
  921. });
  922. break;
  923. case 'user':
  924. xhrQuestion = $.getJSON(config.relPath + 'exercise/hotspot_actionscript.as.php?' + webCidReq, {
  925. modifyAnswers: parseInt(config.questionId),
  926. exe_id: parseInt(config.exerciseId)
  927. });
  928. break;
  929. case 'solution':
  930. //no break
  931. case 'preview':
  932. xhrQuestion = $.getJSON(config.relPath + 'exercise/hotspot_answers.as.php?' + webCidReq, {
  933. modifyAnswers: parseInt(config.questionId),
  934. exerciseId: parseInt(config.exerciseId),
  935. exeId: parseInt(config.exeId)
  936. });
  937. break;
  938. }
  939. $.when(xhrQuestion).done(function (questionInfo) {
  940. switch (questionInfo.type) {
  941. case 'admin':
  942. startHotspotsAdmin(questionInfo);
  943. break;
  944. case 'user':
  945. startHotspotsUser(questionInfo);
  946. break;
  947. case 'solution':
  948. // no break
  949. case 'preview':
  950. startHotspotsSolution(questionInfo);
  951. break;
  952. }
  953. });
  954. };
  955. })();
  956. window.DelineationQuestion = (function () {
  957. var PolygonModel = function (attributes) {
  958. this.id = 0;
  959. this.name = '';
  960. this.attributes = attributes;
  961. this.event = null;
  962. };
  963. PolygonModel.prototype.set = function (key, value) {
  964. this.attributes[key] = value;
  965. if (this.event) {
  966. this.event(this);
  967. }
  968. };
  969. PolygonModel.prototype.get = function (key) {
  970. if (!this.attributes[key]) {
  971. return;
  972. }
  973. return this.attributes[key];
  974. };
  975. PolygonModel.prototype.onChange = function (callback) {
  976. this.event = callback;
  977. };
  978. PolygonModel.prototype.encode = function () {
  979. var pairedPoints = [];
  980. $.each(this.get('points'), function (index, point) {
  981. pairedPoints.push(
  982. point.join(';')
  983. );
  984. });
  985. return pairedPoints.join('|');
  986. };
  987. PolygonModel.decode = function (hotspotInfo) {
  988. var pairedPoints = hotspotInfo.coord.split('|'),
  989. points = [];
  990. $.each(pairedPoints, function (index, pair) {
  991. var point = pair.split(';');
  992. points.push([
  993. parseInt(point[0]),
  994. point[1] ? parseInt(point[1]) : 0
  995. ]);
  996. });
  997. var hotspot = null;
  998. if (hotspotInfo.type === 'delineation') {
  999. hotspot = new DelineationModel({
  1000. points: points
  1001. });
  1002. } else if (hotspotInfo.type === 'oar') {
  1003. hotspot = new OarModel({
  1004. points: points
  1005. });
  1006. }
  1007. if (!hotspot) {
  1008. return;
  1009. }
  1010. hotspot.id = hotspotInfo.id;
  1011. hotspot.name = hotspotInfo.answer;
  1012. return hotspot;
  1013. };
  1014. var DelineationModel = function (attributes) {
  1015. PolygonModel.call(this, attributes);
  1016. };
  1017. DelineationModel.prototype = Object.create(PolygonModel.prototype);
  1018. var OarModel = function (attributes) {
  1019. PolygonModel.call(this, attributes);
  1020. };
  1021. OarModel.prototype = Object.create(PolygonModel.prototype);
  1022. var AnswerModel = function (attributes) {
  1023. PolygonModel.call(this, attributes);
  1024. };
  1025. AnswerModel.prototype = Object.create(PolygonModel.prototype);
  1026. AnswerModel.prototype.encode = function () {
  1027. var pairedPoints = [];
  1028. $.each(this.get('points'), function (index, point) {
  1029. pairedPoints.push(point.join(';'));
  1030. });
  1031. return pairedPoints.join('/');
  1032. };
  1033. var PolygonCollection = function () {
  1034. this.models = [];
  1035. this.length = 0;
  1036. this.event = null;
  1037. };
  1038. PolygonCollection.prototype.add = function (model) {
  1039. this.models.push(model);
  1040. this.length++;
  1041. if (this.event) {
  1042. this.event(model);
  1043. }
  1044. };
  1045. PolygonCollection.prototype.get = function (index) {
  1046. return this.models[index];
  1047. };
  1048. PolygonCollection.prototype.set = function (index, model) {
  1049. this.models[index] = model;
  1050. };
  1051. PolygonCollection.prototype.onAdd = function (callback) {
  1052. this.event = callback;
  1053. };
  1054. var PolygonSvg = function (polygonModel) {
  1055. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
  1056. this.model = polygonModel;
  1057. this.model.onChange(function () {
  1058. self.render();
  1059. });
  1060. var self = this,
  1061. $el = $(this.el);
  1062. this.render = function () {
  1063. var newEl = document.createElementNS('http://www.w3.org/2000/svg', 'polygon'),
  1064. pointsPaired = [];
  1065. $.each(this.model.get('points'), function (index, point) {
  1066. pointsPaired.push(point.join(','));
  1067. });
  1068. newEl.setAttributeNS(null, 'points', pointsPaired.join(' '));
  1069. newEl.setAttributeNS(null, 'class', 'hotspot-' + this.model.id);
  1070. if ($el.parent().length > 0) {
  1071. $el.replaceWith(newEl);
  1072. }
  1073. if (this.el.parentNode) {
  1074. this.el.parentNode.replaceChild(newEl, this.el);
  1075. }
  1076. this.el = newEl;
  1077. return this;
  1078. };
  1079. };
  1080. var HotspotSelect = function (polygonModel) {
  1081. this.el = $('<div>').addClass('col-xs-6 col-sm-4 col-md-3 col-lg-2').get(0);
  1082. this.model = polygonModel;
  1083. selectedPolygonIndex = this.model.id;
  1084. var self = this,
  1085. $el = $(this.el);
  1086. this.render = function () {
  1087. var type = this.model instanceof OarModel ? 'oar' : 'delineation';
  1088. var template = '\n\
  1089. <div class="input-group hotspot-' + this.model.id + ' active">\n\
  1090. <span class="input-group-addon" id="hotspot-' + this.model.id + '">\n\
  1091. <span class="fa fa-square fa-fw" data-hidden="true"></span>\n\
  1092. <span class="sr-only">' + (type === 'delineation' ? lang.Delineation : lang.Oar) + '</span>\n\
  1093. </span>\n\
  1094. <select class="form-control" aria-describedby="hotspot-' + this.hotspotIndex + '">\n\
  1095. <option selected>' + (type === 'delineation' ? lang.Delineation : lang.Oar) + '</option>\n\
  1096. </select>\n\
  1097. </div>\n\
  1098. ';
  1099. $el.html(template);
  1100. $el.find('select')
  1101. .on('focus', function () {
  1102. $('.input-group').removeClass('active');
  1103. $el.find('.input-group').addClass('active');
  1104. selectedPolygonIndex = self.model.id;
  1105. });
  1106. return this;
  1107. };
  1108. };
  1109. var ContextMenu = function () {
  1110. this.el = $('<ul>', {
  1111. id: 'hotspot-context-menu'
  1112. }).addClass('dropdown-menu').get(0);
  1113. var self = this,
  1114. $el = $(this.el);
  1115. this.onHide = function (callback) {
  1116. $(this).on('hide', function () {
  1117. callback();
  1118. });
  1119. };
  1120. this.render = function () {
  1121. var template = '\n\
  1122. <li>\n\
  1123. <a href="#">' + lang.CloseDelineation + '</a>\n\
  1124. </li>\n\
  1125. ';
  1126. $el.html(template);
  1127. $el.find('a')
  1128. .on('click', function (e) {
  1129. e.preventDefault();
  1130. $(self).trigger('hide');
  1131. $el.hide();
  1132. });
  1133. return this;
  1134. };
  1135. this.show = function (x, y) {
  1136. $el.css({
  1137. left: x,
  1138. top: y
  1139. }).show();
  1140. };
  1141. };
  1142. var AdminSvg = function (polygonCollection, image) {
  1143. this.collection = polygonCollection;
  1144. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  1145. var self = this,
  1146. $el = $(this.el);
  1147. this.collection.onAdd(function (polygonModel) {
  1148. self.renderPolygon(polygonModel);
  1149. });
  1150. this.render = function () {
  1151. var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  1152. imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', image.src);
  1153. imageSvg.setAttribute('width', image.width);
  1154. imageSvg.setAttribute('height', image.height);
  1155. this.el.setAttribute('version', '1.1');
  1156. this.el.setAttribute('viewBox', '0 0 ' + image.width + ' ' + image.height);
  1157. this.el.setAttribute('width', image.width);
  1158. this.el.setAttribute('height', image.height);
  1159. this.el.appendChild(imageSvg);
  1160. var isDrawing = false;
  1161. var contextMenu = new ContextMenu();
  1162. contextMenu.onHide(function () {
  1163. var currentHotspot = self.collection.get(selectedPolygonIndex);
  1164. $('[name="hotspot_coordinates[' + (currentHotspot.id + 1) + ']"]').val(
  1165. currentHotspot.encode()
  1166. );
  1167. isDrawing = false;
  1168. });
  1169. $el.on({
  1170. 'dragstart': function (e) {
  1171. e.preventDefault();
  1172. },
  1173. 'click': function (e) {
  1174. e.preventDefault();
  1175. var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY),
  1176. points = [];
  1177. if (!isDrawing) {
  1178. isDrawing = true;
  1179. } else {
  1180. points = self.collection.get(selectedPolygonIndex).get('points');
  1181. }
  1182. points.push([currentPoint.x, currentPoint.y]);
  1183. self.collection.get(selectedPolygonIndex).set('points', points);
  1184. },
  1185. 'contextmenu': function (e) {
  1186. e.preventDefault();
  1187. if (!contextMenu.el.parentNode) {
  1188. $el.parent().append(contextMenu.render().el);
  1189. }
  1190. var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY);
  1191. contextMenu.show(currentPoint.x, currentPoint.y);
  1192. }
  1193. });
  1194. return this;
  1195. };
  1196. this.renderPolygon = function (oarModel) {
  1197. var oarSVG = new PolygonSvg(oarModel);
  1198. $el.append(oarSVG.render().el);
  1199. var oarSelect = new HotspotSelect(oarModel);
  1200. $el.parent().parent().find('.row').append(
  1201. oarSelect.render().el
  1202. );
  1203. return this;
  1204. };
  1205. };
  1206. var startAdminSvg = function (questionInfo) {
  1207. var image = new Image();
  1208. image.onload = function () {
  1209. $(config.selector).html('');
  1210. var polygonCollection = new PolygonCollection(),
  1211. adminSvg = new AdminSvg(polygonCollection, image);
  1212. $(config.selector)
  1213. .css('width', this.width)
  1214. .append(
  1215. adminSvg.render().el
  1216. );
  1217. $(config.selector).parent().prepend('\n\
  1218. <div id="delineation-messages" class="alert alert-info">\n\
  1219. <h4>\n\
  1220. <span class="fa fa-info-circle" aria-hidden="true"></span>\n\
  1221. <span>' + lang.DelineationStatus1 + '</span>\n\
  1222. </h4>\n\
  1223. </div>\n\
  1224. ');
  1225. $(config.selector).parent().prepend('<div class="row"></div>');
  1226. $.each(questionInfo.hotspots, function (index, hotspotInfo) {
  1227. $('.input-group').removeClass('active');
  1228. var polygonModel = PolygonModel.decode(hotspotInfo);
  1229. polygonModel.id = index;
  1230. polygonCollection.add(polygonModel);
  1231. });
  1232. };
  1233. image.src = questionInfo.image;
  1234. lang = questionInfo.lang;
  1235. };
  1236. var UserSvg = function (answerModel, image) {
  1237. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  1238. this.model = answerModel;
  1239. var self = this,
  1240. $el = $(this.el);
  1241. this.render = function () {
  1242. var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  1243. imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', image.src);
  1244. imageSvg.setAttribute('width', image.width);
  1245. imageSvg.setAttribute('height', image.height);
  1246. this.el.setAttribute('version', '1.1');
  1247. this.el.setAttribute('viewBox', '0 0 ' + image.width + ' ' + image.height);
  1248. this.el.setAttribute('width', image.width);
  1249. this.el.setAttribute('height', image.height);
  1250. this.el.appendChild(imageSvg);
  1251. this.renderDelineation();
  1252. var isDrawing = false;
  1253. var contextMenu = new ContextMenu();
  1254. contextMenu.onHide(function () {
  1255. var answerInput = $('hotspot[' + config.questionId + '][1]'),
  1256. choiceInput = $('choice[' + config.questionId + '][1]');
  1257. if (!answerInput.length) {
  1258. answerInput = $('<input>', {
  1259. type: 'hidden',
  1260. name: 'hotspot[' + config.questionId + '][1]'
  1261. }).insertAfter($el);
  1262. }
  1263. if (!choiceInput.length) {
  1264. choiceInput = $('<input>', {
  1265. type: 'hidden',
  1266. name: 'choice[' + config.questionId + '][1]'
  1267. }).insertAfter($el);
  1268. }
  1269. answerInput.val(self.model.encode());
  1270. choiceInput.val(1);
  1271. isDrawing = false;
  1272. });
  1273. $el.on({
  1274. 'dragstart': function (e) {
  1275. e.preventDefault();
  1276. },
  1277. 'click': function (e) {
  1278. e.preventDefault();
  1279. var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY),
  1280. points = [];
  1281. if (!isDrawing) {
  1282. isDrawing = true;
  1283. } else {
  1284. points = self.model.get('points');
  1285. }
  1286. points.push([currentPoint.x, currentPoint.y]);
  1287. self.model.set('points', points);
  1288. },
  1289. 'contextmenu': function (e) {
  1290. e.preventDefault();
  1291. if (!contextMenu.el.parentNode) {
  1292. $el.parent().append(contextMenu.render().el);
  1293. }
  1294. var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY);
  1295. contextMenu.show(currentPoint.x, currentPoint.y);
  1296. }
  1297. });
  1298. return this;
  1299. };
  1300. this.renderDelineation = function () {
  1301. var delineationSvg = new PolygonSvg(this.model);
  1302. $el.append(
  1303. delineationSvg.render().el
  1304. );
  1305. };
  1306. };
  1307. var startUserSvg = function (questionInfo) {
  1308. var image = new Image();
  1309. image.onload = function () {
  1310. $(config.selector).html('');
  1311. var answerModel = new AnswerModel({
  1312. points: []
  1313. }),
  1314. userSvg = new UserSvg(answerModel, image);
  1315. $(config.selector).parent().prepend('\n\
  1316. <div id="delineation-messages" class="alert alert-info">\n\
  1317. <h4>\n\
  1318. <span class="fa fa-info-circle" aria-hidden="true"></span>\n\
  1319. <span>' + lang.DelineationStatus1 + '</span>\n\
  1320. </h4>\n\
  1321. </div>\n\
  1322. ');
  1323. $(config.selector)
  1324. .css('width', this.width)
  1325. .append(
  1326. userSvg.render().el
  1327. );
  1328. };
  1329. image.src = questionInfo.image;
  1330. lang = questionInfo.lang;
  1331. };
  1332. var PreviewSVG = function (polygonCollection, image) {
  1333. this.collection = polygonCollection;
  1334. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  1335. var self = this,
  1336. $el = $(this.el);
  1337. this.collection.onAdd(function (polygonModel) {
  1338. self.renderPolygon(polygonModel);
  1339. });
  1340. this.render = function () {
  1341. var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  1342. imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', image.src);
  1343. imageSvg.setAttribute('width', image.width);
  1344. imageSvg.setAttribute('height', image.height);
  1345. this.el.setAttribute('version', '1.1');
  1346. this.el.setAttribute('viewBox', '0 0 ' + image.width + ' ' + image.height);
  1347. this.el.setAttribute('width', image.width);
  1348. this.el.setAttribute('height', image.height);
  1349. this.el.appendChild(imageSvg);
  1350. };
  1351. this.renderPolygon = function (oarModel) {
  1352. var oarSVG = new PolygonSvg(oarModel);
  1353. $el.append(oarSVG.render().el);
  1354. return this;
  1355. };
  1356. };
  1357. var startPreviewSvg = function (questionInfo) {
  1358. var image = new Image();
  1359. image.onload = function () {
  1360. $(config.selector).html('');
  1361. var polygonCollection = new PolygonCollection(),
  1362. previewSvg = new AdminSvg(polygonCollection, image);
  1363. $(config.selector)
  1364. .css('width', this.width)
  1365. .append(
  1366. previewSvg.render().el
  1367. );
  1368. $.each(questionInfo.hotspots, function (index, hotspotInfo) {
  1369. var polygonModel = PolygonModel.decode(hotspotInfo);
  1370. polygonModel.id = index;
  1371. polygonCollection.add(polygonModel);
  1372. });
  1373. };
  1374. image.src = questionInfo.image;
  1375. lang = questionInfo.lang;
  1376. };
  1377. var config = {
  1378. questionId: 0,
  1379. exerciseId: 0,
  1380. selector: null,
  1381. for: ''
  1382. },
  1383. lang = {},
  1384. selectedPolygonIndex = -1;
  1385. var getPointOnImage = function (referenceElement, x, y) {
  1386. var pointerPosition = {
  1387. left: x + window.scrollX,
  1388. top: y + window.scrollY
  1389. },
  1390. canvasOffset = {
  1391. x: referenceElement.getBoundingClientRect().left + window.scrollX,
  1392. y: referenceElement.getBoundingClientRect().top + window.scrollY
  1393. };
  1394. return {
  1395. x: Math.round(pointerPosition.left - canvasOffset.x),
  1396. y: Math.round(pointerPosition.top - canvasOffset.y)
  1397. };
  1398. };
  1399. return function (settings) {
  1400. config = $.extend({
  1401. questionId: 0,
  1402. selector: ''
  1403. }, settings);
  1404. if (!config.questionId || !config.selector) {
  1405. return;
  1406. }
  1407. $(config.selector).html('\n\
  1408. <span class="fa fa-spinner fa-spin fa-3x" aria-hidden="hidden"></span>\n\
  1409. <span class="sr-only">Loading</span>\n\
  1410. ');
  1411. var xhrQuestion = null;
  1412. switch (config.for) {
  1413. case 'admin':
  1414. xhrQuestion = $.getJSON(config.relPath + 'exercise/hotspot_actionscript_admin.as.php?' + webCidReq, {
  1415. modifyAnswers: parseInt(config.questionId)
  1416. });
  1417. break;
  1418. case 'user':
  1419. xhrQuestion = $.getJSON(config.relPath + 'exercise/hotspot_actionscript.as.php?' + webCidReq, {
  1420. modifyAnswers: parseInt(config.questionId)
  1421. });
  1422. break;
  1423. case 'solution':
  1424. // no break
  1425. case 'preview':
  1426. xhrQuestion = $.getJSON(config.relPath + 'exercise/hotspot_answers.as.php?' + webCidReq, {
  1427. modifyAnswers: parseInt(config.questionId),
  1428. exerciseId: parseInt(config.exerciseId),
  1429. exeId: parseInt(config.exeId)
  1430. });
  1431. break;
  1432. }
  1433. $.when(xhrQuestion).done(function (questionInfo) {
  1434. switch (questionInfo.type) {
  1435. case 'admin':
  1436. startAdminSvg(questionInfo);
  1437. break;
  1438. case 'user':
  1439. startUserSvg(questionInfo);
  1440. break;
  1441. case 'solution':
  1442. // no break
  1443. case 'preview':
  1444. startPreviewSvg(questionInfo);
  1445. break;
  1446. }
  1447. });
  1448. };
  1449. })();