hotspot.js 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  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. var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  420. imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'href', this.image.src);
  421. imageSvg.setAttribute('width', this.image.width);
  422. imageSvg.setAttribute('height', this.image.height);
  423. this.el.appendChild(imageSvg);
  424. this.setEvents();
  425. return this;
  426. };
  427. AdminHotspotsSVG.prototype.renderHotspot = function (hotspot) {
  428. var hotspotIndex = this.collection.length - 1,
  429. hotspotSVG = new HotspotSVG(hotspot, hotspotIndex);
  430. this.el.appendChild(
  431. hotspotSVG.render().el
  432. );
  433. var hotspotSelect = new HotspotSelect(hotspotIndex, this.collection, hotspotSVG);
  434. $(config.selector).parent().find('.row').append(
  435. hotspotSelect.render().el
  436. );
  437. };
  438. AdminHotspotsSVG.prototype.setEvents = function () {
  439. var self = this,
  440. $el = $(this.el);
  441. isDrawing = false;
  442. var startPoint = {
  443. x: 0,
  444. y: 0
  445. };
  446. $el
  447. .on('dragstart', function (e) {
  448. e.preventDefault();
  449. })
  450. .on('mousedown', function (e) {
  451. e.preventDefault();
  452. if (e.button > 0) {
  453. return;
  454. }
  455. if (self.collection.length <= 0) {
  456. return;
  457. }
  458. var currentHotspot = self.collection.get(selectedHotspotIndex);
  459. if (!currentHotspot) {
  460. return;
  461. }
  462. startPoint = getPointOnImage(self.el, e.clientX, e.clientY);
  463. if (currentHotspot instanceof SquareModel) {
  464. isDrawing = true;
  465. currentHotspot.set('x', startPoint.x);
  466. currentHotspot.set('y', startPoint.y);
  467. currentHotspot.set('width', 0);
  468. currentHotspot.set('height', 0);
  469. return;
  470. }
  471. if (currentHotspot instanceof EllipseModel) {
  472. isDrawing = true;
  473. currentHotspot.set('centerX', 0);
  474. currentHotspot.set('centerY', 0);
  475. currentHotspot.set('radiusX', 0);
  476. currentHotspot.set('radiusY', 0);
  477. return;
  478. }
  479. })
  480. .on('mousemove', function (e) {
  481. e.preventDefault();
  482. if (self.collection.length <= 0) {
  483. return;
  484. }
  485. if (!isDrawing) {
  486. return;
  487. }
  488. var currentHotspot = self.collection.get(selectedHotspotIndex),
  489. currentPoint = getPointOnImage(self.el, e.clientX, e.clientY);
  490. if (!currentHotspot) {
  491. return;
  492. }
  493. if (currentHotspot instanceof SquareModel) {
  494. if (startPoint.x < currentPoint.x) {
  495. currentHotspot.set('width', currentPoint.x - startPoint.x);
  496. } else {
  497. currentHotspot.set('x', currentPoint.x);
  498. currentHotspot.set('width', startPoint.x - currentPoint.x);
  499. }
  500. if (startPoint.y < currentPoint.y) {
  501. currentHotspot.set('height', currentPoint.y - startPoint.y);
  502. } else {
  503. currentHotspot.set('y', currentPoint.y);
  504. currentHotspot.set('height', startPoint.y - currentPoint.y);
  505. }
  506. return;
  507. }
  508. if (currentHotspot instanceof EllipseModel) {
  509. var width = 0,
  510. height = 0;
  511. if (startPoint.x < currentPoint.x) {
  512. width = currentPoint.x - startPoint.x;
  513. currentHotspot.set('radiusX', Math.round(width / 2));
  514. currentHotspot.set('centerX', startPoint.x + currentHotspot.get('radiusX'));
  515. } else {
  516. width = startPoint.x - currentPoint.x;
  517. currentHotspot.set('radiusX', Math.round(width / 2));
  518. currentHotspot.set('centerX', currentPoint.x + currentHotspot.get('radiusX'))
  519. }
  520. if (startPoint.y < currentPoint.y) {
  521. height = currentPoint.y - startPoint.y;
  522. currentHotspot.set('radiusY', Math.round(height / 2));
  523. currentHotspot.set('centerY', startPoint.y + currentHotspot.get('radiusY'));
  524. } else {
  525. height = startPoint.y - currentPoint.y;
  526. currentHotspot.set('radiusY', Math.round(height / 2));
  527. currentHotspot.set('centerY', currentPoint.y + currentHotspot.get('radiusY'));
  528. }
  529. return;
  530. }
  531. })
  532. .on('mouseup', function (e) {
  533. e.preventDefault();
  534. if (e.button > 0) {
  535. return;
  536. }
  537. if (self.collection.length <= 0) {
  538. return;
  539. }
  540. if (!isDrawing) {
  541. return;
  542. }
  543. var currentHotspot = self.collection.get(selectedHotspotIndex),
  544. hotspotTypeSelector = '[name="hotspot_type[' + (selectedHotspotIndex + 1) + ']"]',
  545. hotspotCoordSelector = '[name="hotspot_coordinates[' + (selectedHotspotIndex + 1) + ']"]';
  546. if (!currentHotspot) {
  547. return;
  548. }
  549. if (currentHotspot instanceof SquareModel) {
  550. $(hotspotTypeSelector).val('square');
  551. $(hotspotCoordSelector).val(currentHotspot.encode());
  552. isDrawing = false;
  553. } else if (currentHotspot instanceof EllipseModel) {
  554. $(hotspotTypeSelector).val('circle');
  555. $(hotspotCoordSelector).val(currentHotspot.encode());
  556. isDrawing = false;
  557. }
  558. })
  559. .on('click', function (e) {
  560. e.preventDefault();
  561. var currentHotspot = self.collection.get(selectedHotspotIndex),
  562. currentPoint = getPointOnImage(self.el, e.clientX, e.clientY);
  563. if (!currentHotspot) {
  564. return;
  565. }
  566. if (currentHotspot instanceof PolygonModel) {
  567. var points = [];
  568. if (!isDrawing) {
  569. isDrawing = true;
  570. } else {
  571. points = currentHotspot.get('points');
  572. }
  573. points.push([currentPoint.x, currentPoint.y]);
  574. currentHotspot.set('points', points);
  575. return;
  576. }
  577. })
  578. .on('contextmenu', function (e) {
  579. e.preventDefault();
  580. var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY),
  581. currentHotspot = self.collection.get(selectedHotspotIndex),
  582. hotspotTypeSelector = '[name="hotspot_type[' + (selectedHotspotIndex + 1) + ']"]',
  583. hotspotCoordSelector = '[name="hotspot_coordinates[' + (selectedHotspotIndex + 1) + ']"]';
  584. if (!currentHotspot) {
  585. return;
  586. }
  587. if (currentHotspot instanceof PolygonModel) {
  588. contextMenu.show(currentPoint.x, currentPoint.y);
  589. contextMenu.onHide(function () {
  590. $(hotspotTypeSelector).val('poly');
  591. $(hotspotCoordSelector).val(currentHotspot.encode());
  592. isDrawing = false;
  593. });
  594. }
  595. });
  596. };
  597. var startHotspotsAdmin = function (questionInfo) {
  598. var image = new Image();
  599. image.onload = function () {
  600. $(config.selector).html('');
  601. var hotspotsCollection = new HotspotsCollection(),
  602. hotspotsSVG = new AdminHotspotsSVG(hotspotsCollection, this);
  603. $(config.selector).css('width', this.width).append(hotspotsSVG.render().el);
  604. $(config.selector).parent().prepend('\n\
  605. <div id="hotspot-messages" class="alert alert-info">\n\
  606. <h4><span class="fa fa-info-circle" aria-hidden="true"></span> ' + lang.HotspotStatus1 + '</h4>\n\
  607. <span></span>\n\
  608. </div>\n\
  609. ');
  610. $(config.selector).parent().prepend('<div class="row"></div>');
  611. contextMenu = new ContextMenu();
  612. $(config.selector).append(
  613. contextMenu.render().el
  614. );
  615. $.each(questionInfo.hotspots, function (index, hotspotInfo) {
  616. var hotspot = null;
  617. switch (hotspotInfo.type) {
  618. case 'square':
  619. default:
  620. hotspot = SquareModel.decode(hotspotInfo);
  621. break;
  622. case 'circle':
  623. hotspot = EllipseModel.decode(hotspotInfo);
  624. break;
  625. case 'poly':
  626. hotspot = PolygonModel.decode(hotspotInfo);
  627. break;
  628. }
  629. hotspotsCollection.add(hotspot);
  630. });
  631. };
  632. image.src = questionInfo.image;
  633. lang = questionInfo.lang;
  634. };
  635. var UserHotspotsSVG = function (hotspotsCollection, answersCollection, image) {
  636. var self = this;
  637. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  638. this.hotspotsCollection = hotspotsCollection;
  639. this.answersCollection = answersCollection;
  640. this.image = image;
  641. this.answersCollection.onAdd(function (answerModel) {
  642. self.renderAnswer(answerModel);
  643. });
  644. };
  645. UserHotspotsSVG.prototype.render = function () {
  646. this.el.setAttribute('version', '1.1');
  647. this.el.setAttribute('viewBox', '0 0 ' + this.image.width + ' ' + this.image.height);
  648. var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  649. imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'href', this.image.src);
  650. imageSvg.setAttribute('width', this.image.width);
  651. imageSvg.setAttribute('height', this.image.height);
  652. this.el.appendChild(imageSvg);
  653. this.setEvents();
  654. return this;
  655. };
  656. UserHotspotsSVG.prototype.renderAnswer = function (answerModel) {
  657. var answerSVG = new AnswerSVG(answerModel, this.answersCollection.length - 1);
  658. answerSVG.render();
  659. this.el.appendChild(answerSVG.circleEl);
  660. this.el.appendChild(answerSVG.textEl);
  661. var hotspot = this.hotspotsCollection.get(this.answersCollection.length - 1),
  662. x = answerModel.get('x'),
  663. y = answerModel.get('y');
  664. $('<input>', {
  665. type: 'hidden',
  666. name: 'hotspot[' + config.questionId + '][' + hotspot.id + ']'
  667. }).val(function () {
  668. return [x, y].join(';');
  669. }).appendTo(this.el.parentNode);
  670. $('<input>', {
  671. type: 'hidden',
  672. name: 'choice[' + config.questionId + '][' + hotspot.id + ']'
  673. }).val(function () {
  674. return [x, y].join(';');
  675. }).appendTo(this.el.parentNode);
  676. };
  677. UserHotspotsSVG.prototype.setEvents = function () {
  678. var self = this,
  679. $el = $(this.el);
  680. var isMoving = false,
  681. answerIndex = null,
  682. hotspot = null,
  683. point = {};
  684. $el
  685. .on('dragstart', function (e) {
  686. e.preventDefault();
  687. })
  688. .on('click', function (e) {
  689. e.preventDefault();
  690. if (isMoving) {
  691. return;
  692. }
  693. if (self.answersCollection.length >= self.hotspotsCollection.length) {
  694. return;
  695. }
  696. var point = getPointOnImage(self.el, e.clientX, e.clientY);
  697. var answerModel = new AnswerModel({
  698. x: point.x,
  699. y: point.y
  700. });
  701. self.answersCollection.add(answerModel);
  702. if (self.answersCollection.length === self.hotspotsCollection.length) {
  703. $(config.selector).parent()
  704. .find('#hotspot-messages-' + config.questionId + ' span:not(.fa)').text(
  705. lang.HotspotExerciseFinished
  706. );
  707. return;
  708. }
  709. $(config.selector).parent()
  710. .find('#hotspot-messages-' + config.questionId + ' span:not(.fa)').text(
  711. lang.NextAnswer + ' ' + self.hotspotsCollection.get(
  712. self.answersCollection.length
  713. ).name
  714. );
  715. isMoving = false;
  716. })
  717. .on('mousedown', 'circle, text', function (e) {
  718. e.preventDefault();
  719. isMoving = true;
  720. if (e.target.tagName === 'circle') {
  721. //Hack to move correctly the hot spots if there are more than one HS question in same page
  722. answerIndex = $(e.target).next().html();
  723. answerIndex = parseInt(answerIndex) - 1;
  724. } else if (e.target.tagName === 'text') {
  725. //Hack to move correctly the hot spots if there are more than one HS question in same page
  726. answerIndex = $(e.target).html();
  727. answerIndex = parseInt(answerIndex) - 1;
  728. }
  729. hotspot = self.hotspotsCollection.get(answerIndex);
  730. })
  731. .on('mousemove', function (e) {
  732. if (!isMoving) {
  733. return;
  734. }
  735. e.preventDefault();
  736. point = getPointOnImage(self.el, e.clientX, e.clientY);
  737. self.answersCollection.get(answerIndex).set('x', point.x);
  738. self.answersCollection.get(answerIndex).set('y', point.y);
  739. })
  740. .on('mouseup', function (e) {
  741. if (!isMoving) {
  742. return;
  743. }
  744. e.preventDefault();
  745. $('[name="hotspot[' + config.questionId + '][' + hotspot.id + ']"]').val(function () {
  746. return [point.x, point.y].join(';');
  747. });
  748. $('[name="choice[' + config.questionId + '][' + hotspot.id + ']"]').val(function () {
  749. return [point.x, point.y].join(';');
  750. });
  751. isMoving = false;
  752. });
  753. };
  754. var startHotspotsUser = function (questionInfo) {
  755. var image = new Image();
  756. image.onload = function () {
  757. $(config.selector).html('');
  758. var hotspotsCollection = new HotspotsCollection(),
  759. answersCollection = new AnswersCollection(),
  760. hotspotsSVG = new UserHotspotsSVG(hotspotsCollection, answersCollection, this);
  761. $(config.selector).css('width', this.width).append(hotspotsSVG.render().el);
  762. $(config.selector).parent().prepend('\n\
  763. <div id="hotspot-messages-' + config.questionId + '" class="alert alert-info">\n\
  764. <h4>\n\
  765. <span class="fa fa-info-circle" aria-hidden="true"></span>\n\
  766. <span></span>\n\
  767. </h4>\n\
  768. </div>\n\
  769. ');
  770. $.each(questionInfo.hotspots, function (index, hotspotInfo) {
  771. var hotspot = null;
  772. switch (hotspotInfo.type) {
  773. case 'square':
  774. default:
  775. hotspot = SquareModel.decode(hotspotInfo);
  776. break;
  777. case 'circle':
  778. hotspot = EllipseModel.decode(hotspotInfo);
  779. break;
  780. case 'poly':
  781. hotspot = PolygonModel.decode(hotspotInfo);
  782. break;
  783. }
  784. hotspotsCollection.add(hotspot);
  785. });
  786. $.each(questionInfo.answers, function (index, answerInfo) {
  787. var answerModel = new AnswerModel({
  788. x: answerInfo.x,
  789. y: answerInfo.y
  790. });
  791. answersCollection.add(answerModel);
  792. });
  793. $(config.selector).parent().find('#hotspot-messages-' + config.questionId + ' span:not(.fa)')
  794. .text(
  795. lang.NextAnswer + ' ' + hotspotsCollection.get(0).name
  796. );
  797. };
  798. image.src = questionInfo.image;
  799. lang = questionInfo.lang;
  800. };
  801. var SolutionHotspotsSVG = function (hotspotsCollection, answersCollection, image) {
  802. this.hotspotsCollection = hotspotsCollection;
  803. this.answersCollection = answersCollection;
  804. this.image = image;
  805. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  806. var self = this;
  807. this.hotspotsCollection.onAdd(function (hotspotModel) {
  808. self.renderHotspot(hotspotModel);
  809. });
  810. this.answersCollection.onAdd(function (answerModel) {
  811. self.renderAnswer(answerModel);
  812. });
  813. };
  814. SolutionHotspotsSVG.prototype.render = function () {
  815. this.el.setAttribute('version', '1.1');
  816. this.el.setAttribute('viewBox', '0 0 ' + this.image.width + ' ' + this.image.height);
  817. var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  818. imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'href', this.image.src);
  819. imageSvg.setAttribute('width', this.image.width);
  820. imageSvg.setAttribute('height', this.image.height);
  821. this.el.appendChild(imageSvg);
  822. return this;
  823. };
  824. SolutionHotspotsSVG.prototype.renderHotspot = function (hotspotModel) {
  825. var hotspotIndex = this.hotspotsCollection.length - 1,
  826. hotspotSVG = new HotspotSVG(hotspotModel, hotspotIndex);
  827. this.el.appendChild(
  828. hotspotSVG.render().el
  829. );
  830. return this;
  831. };
  832. SolutionHotspotsSVG.prototype.renderAnswer = function (answerModel) {
  833. var pointSVG = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
  834. pointSVG.setAttribute('cx', answerModel.get('x'));
  835. pointSVG.setAttribute('cy', answerModel.get('y'));
  836. pointSVG.setAttribute('r', 15);
  837. pointSVG.setAttribute('class', 'hotspot-answer-point');
  838. var textSVG = document.createElementNS('http://www.w3.org/2000/svg', 'text');
  839. textSVG.setAttribute('x', answerModel.get('x'));
  840. textSVG.setAttribute('y', answerModel.get('y'));
  841. textSVG.setAttribute('dy', 5);
  842. textSVG.setAttribute('font-family', 'sans-serif');
  843. textSVG.setAttribute('text-anchor', 'middle');
  844. textSVG.setAttribute('fill', 'white');
  845. textSVG.textContent = this.answersCollection.length;
  846. this.el.appendChild(pointSVG);
  847. this.el.appendChild(textSVG);
  848. return this;
  849. };
  850. var startHotspotsSolution = function (questionInfo) {
  851. var image = new Image();
  852. image.onload = function () {
  853. $(config.selector).html('');
  854. var hotspotsCollection = new HotspotsCollection(),
  855. answersCollection = new AnswersCollection(),
  856. hotspotsSVG = new SolutionHotspotsSVG(hotspotsCollection, answersCollection, this);
  857. $(config.selector).css('width', this.width).append(hotspotsSVG.render().el);
  858. $.each(questionInfo.hotspots, function (index, hotspotInfo) {
  859. var hotspot = null;
  860. switch (hotspotInfo.type) {
  861. case 'square':
  862. default:
  863. hotspot = SquareModel.decode(hotspotInfo);
  864. break;
  865. case 'circle':
  866. hotspot = EllipseModel.decode(hotspotInfo);
  867. break;
  868. case 'poly':
  869. hotspot = PolygonModel.decode(hotspotInfo);
  870. break;
  871. }
  872. hotspotsCollection.add(hotspot);
  873. });
  874. $.each(questionInfo.answers, function (index, answerInfo) {
  875. var answer = AnswerModel.decode(answerInfo);
  876. answersCollection.add(answer);
  877. });
  878. };
  879. image.src = questionInfo.image;
  880. lang = questionInfo.lang;
  881. };
  882. var getPointOnImage = function (referenceElement, x, y) {
  883. var pointerPosition = {
  884. left: x + window.scrollX,
  885. top: y + window.scrollY
  886. },
  887. canvasOffset = {
  888. x: referenceElement.getBoundingClientRect().left + window.scrollX,
  889. y: referenceElement.getBoundingClientRect().top + window.scrollY
  890. };
  891. return {
  892. x: Math.round(pointerPosition.left - canvasOffset.x),
  893. y: Math.round(pointerPosition.top - canvasOffset.y)
  894. };
  895. };
  896. var config, lang, selectedHotspotIndex = 0, contextMenu;
  897. config = $.extend({
  898. questionId: 0,
  899. selector: ''
  900. }, settings);
  901. if (!config.questionId || !config.selector) {
  902. return;
  903. }
  904. $(config.selector).html('\n\
  905. <span class="fa fa-spinner fa-spin fa-3x" aria-hidden="hidden"></span>\n\
  906. <span class="sr-only">Loading</span>\n\
  907. ');
  908. var xhrQuestion = null;
  909. switch (config.for) {
  910. case 'admin':
  911. xhrQuestion = $.getJSON(config.relPath+'exercise/hotspot_actionscript_admin.as.php', {
  912. modifyAnswers: parseInt(config.questionId)
  913. });
  914. break;
  915. case 'user':
  916. xhrQuestion = $.getJSON(config.relPath+'exercise/hotspot_actionscript.as.php', {
  917. modifyAnswers: parseInt(config.questionId),
  918. exe_id: parseInt(config.exerciseId)
  919. });
  920. break;
  921. case 'solution':
  922. //no break
  923. case 'preview':
  924. xhrQuestion = $.getJSON(config.relPath+'exercise/hotspot_answers.as.php', {
  925. modifyAnswers: parseInt(config.questionId),
  926. exe_id: parseInt(config.exerciseId)
  927. });
  928. break;
  929. }
  930. $.when(xhrQuestion).done(function (questionInfo) {
  931. switch (questionInfo.type) {
  932. case 'admin':
  933. startHotspotsAdmin(questionInfo);
  934. break;
  935. case 'user':
  936. startHotspotsUser(questionInfo);
  937. break;
  938. case 'solution':
  939. //no break
  940. case 'preview':
  941. startHotspotsSolution(questionInfo);
  942. break;
  943. }
  944. });
  945. };
  946. })();
  947. window.DelineationQuestion = (function () {
  948. var PolygonModel = function (attributes) {
  949. this.id = 0;
  950. this.name = '';
  951. this.attributes = attributes;
  952. this.event = null;
  953. };
  954. PolygonModel.prototype.set = function (key, value) {
  955. this.attributes[key] = value;
  956. if (this.event) {
  957. this.event(this);
  958. }
  959. };
  960. PolygonModel.prototype.get = function (key) {
  961. if (!this.attributes[key]) {
  962. return;
  963. }
  964. return this.attributes[key];
  965. };
  966. PolygonModel.prototype.onChange = function (callback) {
  967. this.event = callback;
  968. };
  969. PolygonModel.prototype.encode = function () {
  970. var pairedPoints = [];
  971. $.each(this.get('points'), function (index, point) {
  972. pairedPoints.push(
  973. point.join(';')
  974. );
  975. });
  976. return pairedPoints.join('|');
  977. };
  978. PolygonModel.decode = function (hotspotInfo) {
  979. var pairedPoints = hotspotInfo.coord.split('|'),
  980. points = [];
  981. $.each(pairedPoints, function (index, pair) {
  982. var point = pair.split(';');
  983. points.push([
  984. parseInt(point[0]),
  985. point[1] ? parseInt(point[1]) : 0
  986. ]);
  987. });
  988. var hotspot = null;
  989. if (hotspotInfo.type === 'delineation') {
  990. hotspot = new DelineationModel({
  991. points: points
  992. });
  993. } else if (hotspotInfo.type === 'oar') {
  994. hotspot = new OarModel({
  995. points: points
  996. });
  997. }
  998. if (!hotspot) {
  999. return;
  1000. }
  1001. hotspot.id = hotspotInfo.id;
  1002. hotspot.name = hotspotInfo.answer;
  1003. return hotspot;
  1004. };
  1005. var DelineationModel = function (attributes) {
  1006. PolygonModel.call(this, attributes);
  1007. };
  1008. DelineationModel.prototype = Object.create(PolygonModel.prototype);
  1009. var OarModel = function (attributes) {
  1010. PolygonModel.call(this, attributes);
  1011. };
  1012. OarModel.prototype = Object.create(PolygonModel.prototype);
  1013. var AnswerModel = function (attributes) {
  1014. PolygonModel.call(this, attributes);
  1015. };
  1016. AnswerModel.prototype = Object.create(PolygonModel.prototype);
  1017. AnswerModel.prototype.encode = function () {
  1018. var pairedPoints = [];
  1019. $.each(this.get('points'), function (index, point) {
  1020. pairedPoints.push(point.join(';'));
  1021. });
  1022. return pairedPoints.join('/');
  1023. };
  1024. var PolygonCollection = function () {
  1025. this.models = [];
  1026. this.length = 0;
  1027. this.event = null;
  1028. };
  1029. PolygonCollection.prototype.add = function (model) {
  1030. this.models.push(model);
  1031. this.length++;
  1032. if (this.event) {
  1033. this.event(model);
  1034. }
  1035. };
  1036. PolygonCollection.prototype.get = function (index) {
  1037. return this.models[index];
  1038. };
  1039. PolygonCollection.prototype.set = function (index, model) {
  1040. this.models[index] = model;
  1041. };
  1042. PolygonCollection.prototype.onAdd = function (callback) {
  1043. this.event = callback;
  1044. };
  1045. var PolygonSvg = function (polygonModel) {
  1046. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
  1047. this.model = polygonModel;
  1048. this.model.onChange(function () {
  1049. self.render();
  1050. });
  1051. var self = this,
  1052. $el = $(this.el);
  1053. this.render = function () {
  1054. var newEl = document.createElementNS('http://www.w3.org/2000/svg', 'polygon'),
  1055. pointsPaired = [];
  1056. $.each(this.model.get('points'), function (index, point) {
  1057. pointsPaired.push(point.join(','));
  1058. });
  1059. newEl.setAttributeNS(null, 'points', pointsPaired.join(' '));
  1060. newEl.setAttributeNS(null, 'class', 'hotspot-' + this.model.id);
  1061. if ($el.parent().length > 0) {
  1062. $el.replaceWith(newEl);
  1063. }
  1064. if (this.el.parentNode) {
  1065. this.el.parentNode.replaceChild(newEl, this.el);
  1066. }
  1067. this.el = newEl;
  1068. return this;
  1069. };
  1070. };
  1071. var HotspotSelect = function (polygonModel) {
  1072. this.el = $('<div>').addClass('col-xs-6 col-sm-4 col-md-3 col-lg-2').get(0);
  1073. this.model = polygonModel;
  1074. selectedPolygonIndex = this.model.id;
  1075. var self = this,
  1076. $el = $(this.el);
  1077. this.render = function () {
  1078. var type = this.model instanceof OarModel ? 'oar' : 'delineation';
  1079. var template = '\n\
  1080. <div class="input-group hotspot-' + this.model.id + ' active">\n\
  1081. <span class="input-group-addon" id="hotspot-' + this.model.id + '">\n\
  1082. <span class="fa fa-square fa-fw" data-hidden="true"></span>\n\
  1083. <span class="sr-only">' + (type === 'delineation' ? lang.Delineation : lang.Oar) + '</span>\n\
  1084. </span>\n\
  1085. <select class="form-control" aria-describedby="hotspot-' + this.hotspotIndex + '">\n\
  1086. <option selected>' + (type === 'delineation' ? lang.Delineation : lang.Oar) + '</option>\n\
  1087. </select>\n\
  1088. </div>\n\
  1089. ';
  1090. $el.html(template);
  1091. $el.find('select')
  1092. .on('focus', function () {
  1093. $('.input-group').removeClass('active');
  1094. $el.find('.input-group').addClass('active');
  1095. selectedPolygonIndex = self.model.id;
  1096. });
  1097. return this;
  1098. };
  1099. };
  1100. var ContextMenu = function () {
  1101. this.el = $('<ul>', {
  1102. id: 'hotspot-context-menu'
  1103. }).addClass('dropdown-menu').get(0);
  1104. var self = this,
  1105. $el = $(this.el);
  1106. this.onHide = function (callback) {
  1107. $(this).on('hide', function () {
  1108. callback();
  1109. });
  1110. };
  1111. this.render = function () {
  1112. var template = '\n\
  1113. <li>\n\
  1114. <a href="#">' + lang.CloseDelineation + '</a>\n\
  1115. </li>\n\
  1116. ';
  1117. $el.html(template);
  1118. $el.find('a')
  1119. .on('click', function (e) {
  1120. e.preventDefault();
  1121. $(self).trigger('hide');
  1122. $el.hide();
  1123. });
  1124. return this;
  1125. };
  1126. this.show = function (x, y) {
  1127. $el.css({
  1128. left: x,
  1129. top: y
  1130. }).show();
  1131. };
  1132. };
  1133. var AdminSvg = function (polygonCollection, image) {
  1134. this.collection = polygonCollection;
  1135. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  1136. var self = this,
  1137. $el = $(this.el);
  1138. this.collection.onAdd(function (polygonModel) {
  1139. self.renderPolygon(polygonModel);
  1140. });
  1141. this.render = function () {
  1142. var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  1143. imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', image.src);
  1144. imageSvg.setAttributeNS(null, 'width', image.width);
  1145. imageSvg.setAttributeNS(null, 'height', image.height);
  1146. this.el.setAttributeNS(null, 'version', '1.1');
  1147. this.el.setAttributeNS(null, 'viewBox', '0 0 ' + image.width + ' ' + image.height);
  1148. this.el.appendChild(imageSvg);
  1149. var isDrawing = false;
  1150. var contextMenu = new ContextMenu();
  1151. contextMenu.onHide(function () {
  1152. var currentHotspot = self.collection.get(selectedPolygonIndex);
  1153. $('[name="hotspot_coordinates[' + (currentHotspot.id + 1) + ']"]').val(
  1154. currentHotspot.encode()
  1155. );
  1156. isDrawing = false;
  1157. });
  1158. $el.on({
  1159. 'dragstart': function (e) {
  1160. e.preventDefault();
  1161. },
  1162. 'click': function (e) {
  1163. e.preventDefault();
  1164. var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY),
  1165. points = [];
  1166. if (!isDrawing) {
  1167. isDrawing = true;
  1168. } else {
  1169. points = self.collection.get(selectedPolygonIndex).get('points');
  1170. }
  1171. points.push([currentPoint.x, currentPoint.y]);
  1172. self.collection.get(selectedPolygonIndex).set('points', points);
  1173. },
  1174. 'contextmenu': function (e) {
  1175. e.preventDefault();
  1176. if (!contextMenu.el.parentNode) {
  1177. $el.parent().append(contextMenu.render().el);
  1178. }
  1179. var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY);
  1180. contextMenu.show(currentPoint.x, currentPoint.y);
  1181. }
  1182. });
  1183. return this;
  1184. };
  1185. this.renderPolygon = function (oarModel) {
  1186. var oarSVG = new PolygonSvg(oarModel);
  1187. $el.append(oarSVG.render().el);
  1188. var oarSelect = new HotspotSelect(oarModel);
  1189. $el.parent().parent().find('.row').append(
  1190. oarSelect.render().el
  1191. );
  1192. return this;
  1193. };
  1194. };
  1195. var startAdminSvg = function (questionInfo) {
  1196. var image = new Image();
  1197. image.onload = function () {
  1198. $(config.selector).html('');
  1199. var polygonCollection = new PolygonCollection(),
  1200. adminSvg = new AdminSvg(polygonCollection, image);
  1201. $(config.selector)
  1202. .css('width', this.width)
  1203. .append(
  1204. adminSvg.render().el
  1205. );
  1206. $(config.selector).parent().prepend('\n\
  1207. <div id="delineation-messages" class="alert alert-info">\n\
  1208. <h4>\n\
  1209. <span class="fa fa-info-circle" aria-hidden="true"></span>\n\
  1210. <span>' + lang.DelineationStatus1 + '</span>\n\
  1211. </h4>\n\
  1212. </div>\n\
  1213. ');
  1214. $(config.selector).parent().prepend('<div class="row"></div>');
  1215. $.each(questionInfo.hotspots, function (index, hotspotInfo) {
  1216. $('.input-group').removeClass('active');
  1217. var polygonModel = PolygonModel.decode(hotspotInfo);
  1218. polygonModel.id = index;
  1219. polygonCollection.add(polygonModel);
  1220. });
  1221. };
  1222. image.src = questionInfo.image;
  1223. lang = questionInfo.lang;
  1224. };
  1225. var UserSvg = function (answerModel, image) {
  1226. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  1227. this.model = answerModel;
  1228. var self = this,
  1229. $el = $(this.el);
  1230. this.render = function () {
  1231. var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  1232. imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', image.src);
  1233. imageSvg.setAttributeNS(null, 'width', image.width);
  1234. imageSvg.setAttributeNS(null, 'height', image.height);
  1235. this.el.setAttributeNS(null, 'version', '1.1');
  1236. this.el.setAttributeNS(null, 'viewBox', '0 0 ' + image.width + ' ' + image.height);
  1237. this.el.appendChild(imageSvg);
  1238. this.renderDelineation();
  1239. var isDrawing = false;
  1240. var contextMenu = new ContextMenu();
  1241. contextMenu.onHide(function () {
  1242. var answerInput = $('hotspot[' + config.questionId + '][1]'),
  1243. choiceInput = $('choice[' + config.questionId + '][1]');
  1244. if (!answerInput.length) {
  1245. answerInput = $('<input>', {
  1246. type: 'hidden',
  1247. name: 'hotspot[' + config.questionId + '][1]'
  1248. }).insertAfter($el);
  1249. }
  1250. if (!choiceInput.length) {
  1251. choiceInput = $('<input>', {
  1252. type: 'hidden',
  1253. name: 'choice[' + config.questionId + '][1]'
  1254. }).insertAfter($el);
  1255. }
  1256. answerInput.val(self.model.encode());
  1257. choiceInput.val(1);
  1258. isDrawing = false;
  1259. });
  1260. $el.on({
  1261. 'dragstart': function (e) {
  1262. e.preventDefault();
  1263. },
  1264. 'click': function (e) {
  1265. e.preventDefault();
  1266. var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY),
  1267. points = [];
  1268. if (!isDrawing) {
  1269. isDrawing = true;
  1270. } else {
  1271. points = self.model.get('points');
  1272. }
  1273. points.push([currentPoint.x, currentPoint.y]);
  1274. self.model.set('points', points);
  1275. },
  1276. 'contextmenu': function (e) {
  1277. e.preventDefault();
  1278. if (!contextMenu.el.parentNode) {
  1279. $el.parent().append(contextMenu.render().el);
  1280. }
  1281. var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY);
  1282. contextMenu.show(currentPoint.x, currentPoint.y);
  1283. }
  1284. });
  1285. return this;
  1286. };
  1287. this.renderDelineation = function () {
  1288. var delineationSvg = new PolygonSvg(this.model);
  1289. $el.append(
  1290. delineationSvg.render().el
  1291. );
  1292. };
  1293. };
  1294. var startUserSvg = function (questionInfo) {
  1295. var image = new Image();
  1296. image.onload = function () {
  1297. $(config.selector).html('');
  1298. var answerModel = new AnswerModel({
  1299. points: []
  1300. }),
  1301. userSvg = new UserSvg(answerModel, image);
  1302. $(config.selector).parent().prepend('\n\
  1303. <div id="delineation-messages" class="alert alert-info">\n\
  1304. <h4>\n\
  1305. <span class="fa fa-info-circle" aria-hidden="true"></span>\n\
  1306. <span>' + lang.DelineationStatus1 + '</span>\n\
  1307. </h4>\n\
  1308. </div>\n\
  1309. ');
  1310. $(config.selector)
  1311. .css('width', this.width)
  1312. .append(
  1313. userSvg.render().el
  1314. );
  1315. };
  1316. image.src = questionInfo.image;
  1317. lang = questionInfo.lang;
  1318. };
  1319. var PreviewSVG = function (polygonCollection, image) {
  1320. this.collection = polygonCollection;
  1321. this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  1322. var self = this,
  1323. $el = $(this.el);
  1324. this.collection.onAdd(function (polygonModel) {
  1325. self.renderPolygon(polygonModel);
  1326. });
  1327. this.render = function () {
  1328. var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
  1329. imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', image.src);
  1330. imageSvg.setAttributeNS(null, 'width', image.width);
  1331. imageSvg.setAttributeNS(null, 'height', image.height);
  1332. this.el.setAttributeNS(null, 'version', '1.1');
  1333. this.el.setAttributeNS(null, 'viewBox', '0 0 ' + image.width + ' ' + image.height);
  1334. this.el.appendChild(imageSvg);
  1335. };
  1336. this.renderPolygon = function (oarModel) {
  1337. var oarSVG = new PolygonSvg(oarModel);
  1338. $el.append(oarSVG.render().el);
  1339. return this;
  1340. };
  1341. };
  1342. var startPreviewSvg = function (questionInfo) {
  1343. var image = new Image();
  1344. image.onload = function () {
  1345. $(config.selector).html('');
  1346. var polygonCollection = new PolygonCollection(),
  1347. previewSvg = new AdminSvg(polygonCollection, image);
  1348. $(config.selector)
  1349. .css('width', this.width)
  1350. .append(
  1351. previewSvg.render().el
  1352. );
  1353. $.each(questionInfo.hotspots, function (index, hotspotInfo) {
  1354. var polygonModel = PolygonModel.decode(hotspotInfo);
  1355. polygonModel.id = index;
  1356. polygonCollection.add(polygonModel);
  1357. });
  1358. };
  1359. image.src = questionInfo.image;
  1360. lang = questionInfo.lang;
  1361. };
  1362. var config = {
  1363. questionId: 0,
  1364. exerciseId: 0,
  1365. selector: null,
  1366. for: ''
  1367. },
  1368. lang = {},
  1369. selectedPolygonIndex = -1;
  1370. var getPointOnImage = function (referenceElement, x, y) {
  1371. var pointerPosition = {
  1372. left: x + window.scrollX,
  1373. top: y + window.scrollY
  1374. },
  1375. canvasOffset = {
  1376. x: referenceElement.getBoundingClientRect().left + window.scrollX,
  1377. y: referenceElement.getBoundingClientRect().top + window.scrollY
  1378. };
  1379. return {
  1380. x: Math.round(pointerPosition.left - canvasOffset.x),
  1381. y: Math.round(pointerPosition.top - canvasOffset.y)
  1382. };
  1383. };
  1384. return function (settings) {
  1385. config = $.extend({
  1386. questionId: 0,
  1387. selector: ''
  1388. }, settings);
  1389. if (!config.questionId || !config.selector) {
  1390. return;
  1391. }
  1392. $(config.selector).html('\n\
  1393. <span class="fa fa-spinner fa-spin fa-3x" aria-hidden="hidden"></span>\n\
  1394. <span class="sr-only">Loading</span>\n\
  1395. ');
  1396. var xhrQuestion = null;
  1397. switch (config.for) {
  1398. case 'admin':
  1399. xhrQuestion = $.getJSON(config.relPath+'exercise/hotspot_actionscript_admin.as.php', {
  1400. modifyAnswers: parseInt(config.questionId)
  1401. });
  1402. break;
  1403. case 'user':
  1404. xhrQuestion = $.getJSON(config.relPath+'exercise/hotspot_actionscript.as.php', {
  1405. modifyAnswers: parseInt(config.questionId)
  1406. });
  1407. break;
  1408. case 'solution':
  1409. //no break
  1410. case 'preview':
  1411. xhrQuestion = $.getJSON(config.relPath+'exercise/hotspot_answers.as.php', {
  1412. modifyAnswers: parseInt(config.questionId),
  1413. exe_id: parseInt(config.exerciseId)
  1414. });
  1415. break;
  1416. }
  1417. $.when(xhrQuestion).done(function (questionInfo) {
  1418. switch (questionInfo.type) {
  1419. case 'admin':
  1420. startAdminSvg(questionInfo);
  1421. break;
  1422. case 'user':
  1423. startUserSvg(questionInfo);
  1424. break;
  1425. case 'solution':
  1426. //no break
  1427. case 'preview':
  1428. startPreviewSvg(questionInfo);
  1429. break;
  1430. }
  1431. });
  1432. };
  1433. })();