hotspot.js 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  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?' + _p.web_cid_query, {
  920. modifyAnswers: parseInt(config.questionId)
  921. });
  922. break;
  923. case 'user':
  924. xhrQuestion = $.getJSON(config.relPath + 'exercise/hotspot_actionscript.as.php?' + _p.web_cid_query, {
  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?' + _p.web_cid_query, {
  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?' + _p.web_cid_query, {
  1415. modifyAnswers: parseInt(config.questionId)
  1416. });
  1417. break;
  1418. case 'user':
  1419. xhrQuestion = $.getJSON(config.relPath + 'exercise/hotspot_actionscript.as.php?' + _p.web_cid_query, {
  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?' + _p.web_cid_query, {
  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. })();