123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800 |
- window.HotspotQuestion = (function () {
- return function (settings) {
- var HotspotModel = function (attributes) {
- this.attributes = attributes;
- this.id = 0;
- this.name = '';
- this.changeEvent = null;
- };
- HotspotModel.prototype.set = function (key, value) {
- this.attributes[key] = value;
- if (this.changeEvent) {
- this.changeEvent(this);
- }
- };
- HotspotModel.prototype.get = function (key) {
- return this.attributes[key];
- };
- HotspotModel.prototype.onChange = function (callback) {
- this.changeEvent = callback;
- };
- HotspotModel.decode = function () {
- return new this;
- };
- HotspotModel.prototype.encode = function () {
- return '';
- };
- var SquareModel = function (attributes) {
- HotspotModel.call(this, attributes);
- };
- SquareModel.prototype = Object.create(HotspotModel.prototype);
- SquareModel.prototype.setStartPoint = function (x, y) {
- x = parseInt(x);
- y = parseInt(y);
- this.set('x', x);
- this.set('y', y);
- };
- SquareModel.prototype.setEndPoint = function (x, y) {
- var startX = this.get('x'),
- startY = this.get('y');
- x = parseInt(x);
- y = parseInt(y);
- if (x >= startX) {
- this.set('width', x - startX);
- }
- if (y >= startY) {
- this.set('height', y - startY);
- }
- };
- SquareModel.decode = function (hotspotInfo) {
- var coords = hotspotInfo.coord.split('|'),
- position = coords[0].split(';'),
- hotspot = new SquareModel({
- x: parseInt(position[0]),
- y: parseInt(position[1]),
- width: parseInt(coords[1]),
- height: parseInt(coords[2])
- });
- hotspot.id = hotspotInfo.iid;
- hotspot.name = hotspotInfo.answer;
- return hotspot;
- };
- SquareModel.prototype.encode = function () {
- return [
- [
- this.get('x'),
- this.get('y')
- ].join(';'),
- this.get('width'),
- this.get('height')
- ].join('|');
- };
- var EllipseModel = function (attributes) {
- HotspotModel.call(this, attributes);
- };
- EllipseModel.prototype = Object.create(HotspotModel.prototype);
- EllipseModel.prototype.setStartPoint = function (x, y) {
- x = parseInt(x);
- y = parseInt(y);
- this.set('centerX', x);
- this.set('centerY', y);
- };
- EllipseModel.prototype.setEndPoint = function (x, y) {
- var startX = this.get('centerX'),
- startY = this.get('centerY'),
- width = 0,
- height = 0;
- x = parseInt(x);
- y = parseInt(y);
- if (x >= startX) {
- width = x - startX;
- this.set('radiusX', Math.round(width / 2));
- this.set('centerX', startX + this.get('radiusX'));
- }
- if (y >= startY) {
- height = y - startY;
- this.set('radiusY', Math.round(height / 2));
- this.set('centerY', startY + this.get('radiusY'));
- }
- };
- EllipseModel.decode = function (hotspotInfo) {
- var coords = hotspotInfo.coord.split('|'),
- center = coords[0].split(';'),
- hotspot = new EllipseModel({
- centerX: parseInt(center[0]),
- centerY: parseInt(center[1]),
- radiusX: parseInt(coords[1]),
- radiusY: parseInt(coords[2])
- });
- hotspot.id = hotspotInfo.iid;
- hotspot.name = hotspotInfo.answer;
- return hotspot;
- };
- EllipseModel.prototype.encode = function () {
- return [
- [
- this.get('centerX'),
- this.get('centerY')
- ].join(';'),
- this.get('radiusX'),
- this.get('radiusY')
- ].join('|');
- };
- var PolygonModel = function (attributes) {
- HotspotModel.call(this, attributes);
- };
- PolygonModel.prototype = Object.create(HotspotModel.prototype);
- PolygonModel.prototype.addPoint = function (x, y) {
- var points = this.get('points');
- x = parseInt(x);
- y = parseInt(y);
- points.push([x, y]);
- this.set('points', points);
- };
- PolygonModel.decode = function (hotspotInfo) {
- var pairedPoints = hotspotInfo.coord.split('|'),
- points = [];
- $.each(pairedPoints, function (index, pair) {
- var point = pair.split(';');
- points.push([
- parseInt(point[0]),
- parseInt(point[1])
- ]);
- });
- var hotspot = new PolygonModel({
- points: points
- });
- hotspot.id = hotspotInfo.iid;
- hotspot.name = hotspotInfo.answer;
- return hotspot;
- };
- PolygonModel.prototype.encode = function () {
- var pairedPoints = [];
- this.get('points').forEach(function (point) {
- pairedPoints.push(
- point.join(';')
- );
- });
- return pairedPoints.join('|');
- };
- var AnswerModel = function (attributes) {
- this.attributes = attributes;
- this.changeEvent = null;
- };
- AnswerModel.prototype.set = function (key, value) {
- this.attributes[key] = value;
- if (this.changeEvent) {
- this.changeEvent(this);
- }
- };
- AnswerModel.prototype.get = function (key) {
- return this.attributes[key];
- };
- AnswerModel.prototype.onChange = function (callback) {
- this.changeEvent = callback;
- };
- AnswerModel.decode = function (answerInfo) {
- var coords = answerInfo.split(';');
- return new AnswerModel({
- x: coords[0],
- y: coords[1]
- });
- };
- var AnswersCollection = function () {
- this.models = [];
- this.length = 0;
- this.addEvent = null;
- };
- AnswersCollection.prototype.add = function (answerModel) {
- this.models.push(answerModel);
- this.length++;
- if (this.addEvent) {
- this.addEvent(answerModel);
- }
- };
- AnswersCollection.prototype.get = function (index) {
- return this.models[index];
- };
- AnswersCollection.prototype.onAdd = function (callback) {
- this.addEvent = callback;
- };
- var HotspotsCollection = function () {
- this.hotspots = [];
- this.length = 0;
- this.addEvent = null;
- };
- HotspotsCollection.prototype.add = function (hotspot) {
- this.hotspots.push(hotspot);
- this.length++;
- if (this.addEvent) {
- this.addEvent(hotspot);
- }
- };
- HotspotsCollection.prototype.get = function (index) {
- return this.hotspots[index];
- };
- HotspotsCollection.prototype.set = function (index, newHotspot) {
- this.hotspots[index] = newHotspot;
- };
- HotspotsCollection.prototype.onAdd = function (callback) {
- this.addEvent = callback;
- };
- var HotspotSVG = function (modelModel, index) {
- var self = this;
- this.model = modelModel;
- this.hotspotIndex = index;
- this.el = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
- this.model.onChange(function (hotspotModel) {
- self.render();
- });
- };
- HotspotSVG.prototype.render = function () {
- var newEl = null;
- if (this.model instanceof SquareModel) {
- newEl = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
- newEl.setAttribute('x', this.model.get('x'));
- newEl.setAttribute('y', this.model.get('y'));
- newEl.setAttribute('width', this.model.get('width'));
- newEl.setAttribute('height', this.model.get('height'));
- } else if (this.model instanceof EllipseModel) {
- newEl = document.createElementNS('http://www.w3.org/2000/svg', 'ellipse');
- newEl.setAttribute('cx', this.model.get('centerX'));
- newEl.setAttribute('cy', this.model.get('centerY'));
- newEl.setAttribute('rx', this.model.get('radiusX'));
- newEl.setAttribute('ry', this.model.get('radiusY'));
- } else if (this.model instanceof PolygonModel) {
- var pointsPaired = [];
- this.model.get('points').forEach(function (point) {
- pointsPaired.push(point.join(','));
- });
- newEl = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
- newEl.setAttribute(
- 'points',
- pointsPaired.join(' ')
- );
- }
- newEl.setAttribute('class', 'hotspot-' + this.hotspotIndex);
- if (this.el.parentNode) {
- this.el.parentNode.replaceChild(newEl, this.el);
- }
- this.el = newEl;
- return this;
- };
- var AnswerSVG = function (answerModel, index) {
- var self = this;
- this.model = answerModel;
- this.answerIndex = index;
- this.circleEl = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
- this.textEl = document.createElementNS('http://www.w3.org/2000/svg', 'text');
- this.model.onChange(function (answerModel) {
- self.render();
- });
- };
- AnswerSVG.prototype.render = function () {
- this.circleEl.setAttribute('cx', this.model.get('x'));
- this.circleEl.setAttribute('cy', this.model.get('y'));
- this.circleEl.setAttribute('r', 15);
- this.circleEl.setAttribute('class', 'hotspot-answer-point');
- this.textEl.setAttribute('x', this.model.get('x'));
- this.textEl.setAttribute('y', this.model.get('y'));
- this.textEl.setAttribute('dy', 5);
- this.textEl.setAttribute('text-anchor', 'middle');
- this.textEl.setAttribute('class', 'hotspot-answer-text');
- this.textEl.textContent = this.answerIndex + 1;
- return this;
- };
- var HotspotSelect = function (index, hotspotsCollection, hotspotSVG) {
- this.hotspotIndex = index;
- this.hotspotsCollection = hotspotsCollection;
- this.hotspotSVG = hotspotSVG;
- this.el = document.createElement('div');
- this.el.className = 'col-xs-6 col-sm-4 col-md-3 col-lg-2';
- selectedHotspotIndex = this.hotspotIndex;
- $('.input-group').removeClass('active');
- };
- HotspotSelect.prototype.render = function () {
- var self = this,
- $el = $(this.el);
- var template = '\n\
- <div class="input-group hotspot-' + this.hotspotIndex + ' active">\n\
- <span class="input-group-addon" id="hotspot-' + this.hotspotIndex + '">\n\
- <span class="fa fa-square fa-fw" data-hidden="true"></span>\n\
- <span class="sr-only">' + (this.hotspotSVG.model.name ? this.hotspotSVG.model.name : 'hotspot ' + this.hotspotIndex) + '</span>\n\
- </span>\n\
- <select class="form-control" aria-describedby="hotspot-' + this.hotspotIndex + '">\n\
- <option value="square">' + lang.Square + '</option>\n\
- <option value="ellipse">' + lang.Ellipse + '</option>\n\
- <option value="polygon">' + lang.Polygon + '</option>\n\
- </select>\n\
- </div>\n\
- ';
- $el.html(template);
- $el.find('select')
- .on('change', function () {
- selectedHotspotIndex = self.hotspotIndex;
- var newHotspot = null,
- changeEvent = self.hotspotSVG.model.changeEvent;
- switch (this.value) {
- case 'square':
- newHotspot = new SquareModel({
- x: 0,
- y: 0,
- width: 0,
- height: 0
- });
- $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Other);
- break;
- case 'ellipse':
- newHotspot = new EllipseModel({
- centerX: 0,
- centerY: 0,
- radiusX: 0,
- radiusY: 0
- });
- $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Other);
- break;
- case 'polygon':
- newHotspot = new PolygonModel({
- points: []
- });
- $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Polygon);
- break;
- }
- newHotspot.onChange(changeEvent);
- self.hotspotsCollection.set(self.hotspotIndex, newHotspot);
- self.hotspotSVG.model = newHotspot;
- })
- .on('focus', function () {
- $('.input-group').removeClass('active');
- $el.find('.input-group').addClass('active');
- selectedHotspotIndex = self.hotspotIndex;
- switch (this.value) {
- case 'square':
- $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Other);
- break;
- case 'ellipse':
- $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Other);
- break;
- case 'polygon':
- $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Polygon);
- break;
- }
- })
- .val(function () {
- if (self.hotspotSVG.model instanceof SquareModel) {
- $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Other);
- return 'square';
- }
- if (self.hotspotSVG.model instanceof EllipseModel) {
- $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Other);
- return 'ellipse';
- }
- if (self.hotspotSVG.model instanceof PolygonModel) {
- $('#hotspot-messages span:not(.fa)').text(lang.HotspotStatus2Polygon);
- return 'polygon';
- }
- });
- return this;
- };
- var ContextMenu = function () {
- this.el = document.createElement('ul');
- $(this.el).addClass('dropdown-menu').attr('id', "hotspot-context-menu");
- this.hideEvent = null;
- };
- ContextMenu.prototype.onHide = function (callback) {
- this.hideEvent = callback;
- };
- ContextMenu.prototype.render = function () {
- var self = this,
- template = '\n\
- <li>\n\
- <a href="#">' + lang.ClosePolygon + '</a>\n\
- </li>\n\
- ';
- $(this.el).html(template);
- $(this.el).find('a').on('click', function (e) {
- e.preventDefault();
- if (self.hideEvent) {
- self.hideEvent(e);
- }
- $(self.el).hide();
- });
- return this;
- };
- ContextMenu.prototype.show = function (x, y) {
- $(this.el).css({left: x, top: y}).show();
- };
- var AdminHotspotsSVG = function (hotspotsCollection, image) {
- var self = this;
- this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
- this.collection = hotspotsCollection;
- this.image = image;
- this.collection.onAdd(function (hotspotModel) {
- self.renderHotspot(hotspotModel);
- });
- };
- AdminHotspotsSVG.prototype.render = function () {
- this.el.setAttribute('version', '1.1');
- this.el.setAttribute('viewBox', '0 0 ' + this.image.width + ' ' + this.image.height);
- var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
- imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'href', this.image.src);
- imageSvg.setAttribute('width', this.image.width);
- imageSvg.setAttribute('height', this.image.height);
- this.el.appendChild(imageSvg);
- this.setEvents();
- return this;
- };
- AdminHotspotsSVG.prototype.renderHotspot = function (hotspot) {
- var hotspotIndex = this.collection.length - 1,
- hotspotSVG = new HotspotSVG(hotspot, hotspotIndex);
- this.el.appendChild(
- hotspotSVG.render().el
- );
- var hotspotSelect = new HotspotSelect(hotspotIndex, this.collection, hotspotSVG);
- $(config.selector).parent().find('.row').append(
- hotspotSelect.render().el
- );
- };
- AdminHotspotsSVG.prototype.setEvents = function () {
- var self = this,
- $el = $(this.el);
- isDrawing = false;
- var startPoint = {
- x: 0,
- y: 0
- };
- $el
- .on('dragstart', function (e) {
- e.preventDefault();
- })
- .on('mousedown', function (e) {
- e.preventDefault();
- if (e.button > 0) {
- return;
- }
- if (self.collection.length <= 0) {
- return;
- }
- var currentHotspot = self.collection.get(selectedHotspotIndex);
- if (!currentHotspot) {
- return;
- }
- startPoint = getPointOnImage(self.el, e.clientX, e.clientY);
- if (currentHotspot instanceof SquareModel) {
- isDrawing = true;
- currentHotspot.set('x', startPoint.x);
- currentHotspot.set('y', startPoint.y);
- currentHotspot.set('width', 0);
- currentHotspot.set('height', 0);
- return;
- }
- if (currentHotspot instanceof EllipseModel) {
- isDrawing = true;
- currentHotspot.set('centerX', 0);
- currentHotspot.set('centerY', 0);
- currentHotspot.set('radiusX', 0);
- currentHotspot.set('radiusY', 0);
- return;
- }
- })
- .on('mousemove', function (e) {
- e.preventDefault();
- if (self.collection.length <= 0) {
- return;
- }
- if (!isDrawing) {
- return;
- }
- var currentHotspot = self.collection.get(selectedHotspotIndex),
- currentPoint = getPointOnImage(self.el, e.clientX, e.clientY);
- if (!currentHotspot) {
- return;
- }
- if (currentHotspot instanceof SquareModel) {
- if (startPoint.x < currentPoint.x) {
- currentHotspot.set('width', currentPoint.x - startPoint.x);
- } else {
- currentHotspot.set('x', currentPoint.x);
- currentHotspot.set('width', startPoint.x - currentPoint.x);
- }
- if (startPoint.y < currentPoint.y) {
- currentHotspot.set('height', currentPoint.y - startPoint.y);
- } else {
- currentHotspot.set('y', currentPoint.y);
- currentHotspot.set('height', startPoint.y - currentPoint.y);
- }
- return;
- }
- if (currentHotspot instanceof EllipseModel) {
- var width = 0,
- height = 0;
- if (startPoint.x < currentPoint.x) {
- width = currentPoint.x - startPoint.x;
- currentHotspot.set('radiusX', Math.round(width / 2));
- currentHotspot.set('centerX', startPoint.x + currentHotspot.get('radiusX'));
- } else {
- width = startPoint.x - currentPoint.x;
- currentHotspot.set('radiusX', Math.round(width / 2));
- currentHotspot.set('centerX', currentPoint.x + currentHotspot.get('radiusX'))
- }
- if (startPoint.y < currentPoint.y) {
- height = currentPoint.y - startPoint.y;
- currentHotspot.set('radiusY', Math.round(height / 2));
- currentHotspot.set('centerY', startPoint.y + currentHotspot.get('radiusY'));
- } else {
- height = startPoint.y - currentPoint.y;
- currentHotspot.set('radiusY', Math.round(height / 2));
- currentHotspot.set('centerY', currentPoint.y + currentHotspot.get('radiusY'));
- }
- return;
- }
- })
- .on('mouseup', function (e) {
- e.preventDefault();
- if (e.button > 0) {
- return;
- }
- if (self.collection.length <= 0) {
- return;
- }
- if (!isDrawing) {
- return;
- }
- var currentHotspot = self.collection.get(selectedHotspotIndex),
- hotspotTypeSelector = '[name="hotspot_type[' + (selectedHotspotIndex + 1) + ']"]',
- hotspotCoordSelector = '[name="hotspot_coordinates[' + (selectedHotspotIndex + 1) + ']"]';
- if (!currentHotspot) {
- return;
- }
- if (currentHotspot instanceof SquareModel) {
- $(hotspotTypeSelector).val('square');
- $(hotspotCoordSelector).val(currentHotspot.encode());
- isDrawing = false;
- } else if (currentHotspot instanceof EllipseModel) {
- $(hotspotTypeSelector).val('circle');
- $(hotspotCoordSelector).val(currentHotspot.encode());
- isDrawing = false;
- }
- })
- .on('click', function (e) {
- e.preventDefault();
- var currentHotspot = self.collection.get(selectedHotspotIndex),
- currentPoint = getPointOnImage(self.el, e.clientX, e.clientY);
- if (!currentHotspot) {
- return;
- }
- if (currentHotspot instanceof PolygonModel) {
- var points = [];
- if (!isDrawing) {
- isDrawing = true;
- } else {
- points = currentHotspot.get('points');
- }
- points.push([currentPoint.x, currentPoint.y]);
- currentHotspot.set('points', points);
- return;
- }
- })
- .on('contextmenu', function (e) {
- e.preventDefault();
- var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY),
- currentHotspot = self.collection.get(selectedHotspotIndex),
- hotspotTypeSelector = '[name="hotspot_type[' + (selectedHotspotIndex + 1) + ']"]',
- hotspotCoordSelector = '[name="hotspot_coordinates[' + (selectedHotspotIndex + 1) + ']"]';
- if (!currentHotspot) {
- return;
- }
- if (currentHotspot instanceof PolygonModel) {
- contextMenu.show(currentPoint.x, currentPoint.y);
- contextMenu.onHide(function () {
- $(hotspotTypeSelector).val('poly');
- $(hotspotCoordSelector).val(currentHotspot.encode());
- isDrawing = false;
- });
- }
- });
- };
- var startHotspotsAdmin = function (questionInfo) {
- var image = new Image();
- image.onload = function () {
- $(config.selector).html('');
- var hotspotsCollection = new HotspotsCollection(),
- hotspotsSVG = new AdminHotspotsSVG(hotspotsCollection, this);
- $(config.selector).css('width', this.width).append(hotspotsSVG.render().el);
- $(config.selector).parent().prepend('\n\
- <div id="hotspot-messages" class="alert alert-info">\n\
- <h4><span class="fa fa-info-circle" aria-hidden="true"></span> ' + lang.HotspotStatus1 + '</h4>\n\
- <span></span>\n\
- </div>\n\
- ');
- $(config.selector).parent().prepend('<div class="row"></div>');
- contextMenu = new ContextMenu();
- $(config.selector).append(
- contextMenu.render().el
- );
- $.each(questionInfo.hotspots, function (index, hotspotInfo) {
- var hotspot = null;
- switch (hotspotInfo.type) {
- case 'square':
- default:
- hotspot = SquareModel.decode(hotspotInfo);
- break;
- case 'circle':
- hotspot = EllipseModel.decode(hotspotInfo);
- break;
- case 'poly':
- hotspot = PolygonModel.decode(hotspotInfo);
- break;
- }
- hotspotsCollection.add(hotspot);
- });
- };
- image.src = questionInfo.image;
- lang = questionInfo.lang;
- };
- var UserHotspotsSVG = function (hotspotsCollection, answersCollection, image) {
- var self = this;
- this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
- this.hotspotsCollection = hotspotsCollection;
- this.answersCollection = answersCollection;
- this.image = image;
- this.answersCollection.onAdd(function (answerModel) {
- self.renderAnswer(answerModel);
- });
- };
- UserHotspotsSVG.prototype.render = function () {
- this.el.setAttribute('version', '1.1');
- this.el.setAttribute('viewBox', '0 0 ' + this.image.width + ' ' + this.image.height);
- var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
- imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'href', this.image.src);
- imageSvg.setAttribute('width', this.image.width);
- imageSvg.setAttribute('height', this.image.height);
- this.el.appendChild(imageSvg);
- this.setEvents();
- return this;
- };
- UserHotspotsSVG.prototype.renderAnswer = function (answerModel) {
- var answerSVG = new AnswerSVG(answerModel, this.answersCollection.length - 1);
- answerSVG.render();
- this.el.appendChild(answerSVG.circleEl);
- this.el.appendChild(answerSVG.textEl);
- var hotspot = this.hotspotsCollection.get(this.answersCollection.length - 1),
- x = answerModel.get('x'),
- y = answerModel.get('y');
- $('<input>', {
- type: 'hidden',
- name: 'hotspot[' + config.questionId + '][' + hotspot.id + ']'
- }).val(function () {
- return [x, y].join(';');
- }).appendTo(this.el.parentNode);
- $('<input>', {
- type: 'hidden',
- name: 'choice[' + config.questionId + '][' + hotspot.id + ']'
- }).val(function () {
- return [x, y].join(';');
- }).appendTo(this.el.parentNode);
- };
- UserHotspotsSVG.prototype.setEvents = function () {
- var self = this,
- $el = $(this.el);
- var isMoving = false,
- answerIndex = null,
- hotspot = null,
- point = {};
- $el
- .on('dragstart', function (e) {
- e.preventDefault();
- })
- .on('click', function (e) {
- e.preventDefault();
- if (isMoving) {
- return;
- }
- if (self.answersCollection.length >= self.hotspotsCollection.length) {
- return;
- }
- var point = getPointOnImage(self.el, e.clientX, e.clientY);
- var answerModel = new AnswerModel({
- x: point.x,
- y: point.y
- });
- self.answersCollection.add(answerModel);
- if (self.answersCollection.length === self.hotspotsCollection.length) {
- $(config.selector).parent()
- .find('#hotspot-messages-' + config.questionId + ' span:not(.fa)').text(
- lang.HotspotExerciseFinished
- );
- return;
- }
- $(config.selector).parent()
- .find('#hotspot-messages-' + config.questionId + ' span:not(.fa)').text(
- lang.NextAnswer + ' ' + self.hotspotsCollection.get(
- self.answersCollection.length
- ).name
- );
- isMoving = false;
- })
- .on('mousedown', 'circle, text', function (e) {
- e.preventDefault();
- isMoving = true;
- if (e.target.tagName === 'circle') {
- //Hack to move correctly the hot spots if there are more than one HS question in same page
- answerIndex = $(e.target).next().html();
- answerIndex = parseInt(answerIndex) - 1;
- } else if (e.target.tagName === 'text') {
- //Hack to move correctly the hot spots if there are more than one HS question in same page
- answerIndex = $(e.target).html();
- answerIndex = parseInt(answerIndex) - 1;
- }
- hotspot = self.hotspotsCollection.get(answerIndex);
- })
- .on('mousemove', function (e) {
- if (!isMoving) {
- return;
- }
- e.preventDefault();
- point = getPointOnImage(self.el, e.clientX, e.clientY);
- self.answersCollection.get(answerIndex).set('x', point.x);
- self.answersCollection.get(answerIndex).set('y', point.y);
- })
- .on('mouseup', function (e) {
- if (!isMoving) {
- return;
- }
- e.preventDefault();
- $('[name="hotspot[' + config.questionId + '][' + hotspot.id + ']"]').val(function () {
- return [point.x, point.y].join(';');
- });
- $('[name="choice[' + config.questionId + '][' + hotspot.id + ']"]').val(function () {
- return [point.x, point.y].join(';');
- });
- isMoving = false;
- });
- };
- var startHotspotsUser = function (questionInfo) {
- var image = new Image();
- image.onload = function () {
- $(config.selector).html('');
- var hotspotsCollection = new HotspotsCollection(),
- answersCollection = new AnswersCollection(),
- hotspotsSVG = new UserHotspotsSVG(hotspotsCollection, answersCollection, this);
- $(config.selector).css('width', this.width).append(hotspotsSVG.render().el);
- $(config.selector).parent().prepend('\n\
- <div id="hotspot-messages-' + config.questionId + '" class="alert alert-info">\n\
- <h4>\n\
- <span class="fa fa-info-circle" aria-hidden="true"></span>\n\
- <span></span>\n\
- </h4>\n\
- </div>\n\
- ');
- $.each(questionInfo.hotspots, function (index, hotspotInfo) {
- var hotspot = null;
- switch (hotspotInfo.type) {
- case 'square':
- default:
- hotspot = SquareModel.decode(hotspotInfo);
- break;
- case 'circle':
- hotspot = EllipseModel.decode(hotspotInfo);
- break;
- case 'poly':
- hotspot = PolygonModel.decode(hotspotInfo);
- break;
- }
- hotspotsCollection.add(hotspot);
- });
- $.each(questionInfo.answers, function (index, answerInfo) {
- var answerModel = new AnswerModel({
- x: answerInfo.x,
- y: answerInfo.y
- });
- answersCollection.add(answerModel);
- });
- $(config.selector).parent().find('#hotspot-messages-' + config.questionId + ' span:not(.fa)')
- .text(
- lang.NextAnswer + ' ' + hotspotsCollection.get(0).name
- );
- };
- image.src = questionInfo.image;
- lang = questionInfo.lang;
- };
- var SolutionHotspotsSVG = function (hotspotsCollection, answersCollection, image) {
- this.hotspotsCollection = hotspotsCollection;
- this.answersCollection = answersCollection;
- this.image = image;
- this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
- var self = this;
- this.hotspotsCollection.onAdd(function (hotspotModel) {
- self.renderHotspot(hotspotModel);
- });
- this.answersCollection.onAdd(function (answerModel) {
- self.renderAnswer(answerModel);
- });
- };
- SolutionHotspotsSVG.prototype.render = function () {
- this.el.setAttribute('version', '1.1');
- this.el.setAttribute('viewBox', '0 0 ' + this.image.width + ' ' + this.image.height);
- var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
- imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'href', this.image.src);
- imageSvg.setAttribute('width', this.image.width);
- imageSvg.setAttribute('height', this.image.height);
- this.el.appendChild(imageSvg);
- return this;
- };
- SolutionHotspotsSVG.prototype.renderHotspot = function (hotspotModel) {
- var hotspotIndex = this.hotspotsCollection.length - 1,
- hotspotSVG = new HotspotSVG(hotspotModel, hotspotIndex);
- this.el.appendChild(
- hotspotSVG.render().el
- );
- return this;
- };
- SolutionHotspotsSVG.prototype.renderAnswer = function (answerModel) {
- var pointSVG = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
- pointSVG.setAttribute('cx', answerModel.get('x'));
- pointSVG.setAttribute('cy', answerModel.get('y'));
- pointSVG.setAttribute('r', 15);
- pointSVG.setAttribute('class', 'hotspot-answer-point');
- var textSVG = document.createElementNS('http://www.w3.org/2000/svg', 'text');
- textSVG.setAttribute('x', answerModel.get('x'));
- textSVG.setAttribute('y', answerModel.get('y'));
- textSVG.setAttribute('dy', 5);
- textSVG.setAttribute('font-family', 'sans-serif');
- textSVG.setAttribute('text-anchor', 'middle');
- textSVG.setAttribute('fill', 'white');
- textSVG.textContent = this.answersCollection.length;
- this.el.appendChild(pointSVG);
- this.el.appendChild(textSVG);
- return this;
- };
- var startHotspotsSolution = function (questionInfo) {
- var image = new Image();
- image.onload = function () {
- $(config.selector).html('');
- var hotspotsCollection = new HotspotsCollection(),
- answersCollection = new AnswersCollection(),
- hotspotsSVG = new SolutionHotspotsSVG(hotspotsCollection, answersCollection, this);
- $(config.selector).css('width', this.width).append(hotspotsSVG.render().el);
- $.each(questionInfo.hotspots, function (index, hotspotInfo) {
- var hotspot = null;
- switch (hotspotInfo.type) {
- case 'square':
- default:
- hotspot = SquareModel.decode(hotspotInfo);
- break;
- case 'circle':
- hotspot = EllipseModel.decode(hotspotInfo);
- break;
- case 'poly':
- hotspot = PolygonModel.decode(hotspotInfo);
- break;
- }
- hotspotsCollection.add(hotspot);
- });
- $.each(questionInfo.answers, function (index, answerInfo) {
- var answer = AnswerModel.decode(answerInfo);
- answersCollection.add(answer);
- });
- };
- image.src = questionInfo.image;
- lang = questionInfo.lang;
- };
- var getPointOnImage = function (referenceElement, x, y) {
- var pointerPosition = {
- left: x + window.scrollX,
- top: y + window.scrollY
- },
- canvasOffset = {
- x: referenceElement.getBoundingClientRect().left + window.scrollX,
- y: referenceElement.getBoundingClientRect().top + window.scrollY
- };
- return {
- x: Math.round(pointerPosition.left - canvasOffset.x),
- y: Math.round(pointerPosition.top - canvasOffset.y)
- };
- };
- var config, lang, selectedHotspotIndex = 0, contextMenu;
- config = $.extend({
- questionId: 0,
- selector: ''
- }, settings);
- if (!config.questionId || !config.selector) {
- return;
- }
- $(config.selector).html('\n\
- <span class="fa fa-spinner fa-spin fa-3x" aria-hidden="hidden"></span>\n\
- <span class="sr-only">Loading</span>\n\
- ');
- var xhrQuestion = null;
- switch (config.for) {
- case 'admin':
- xhrQuestion = $.getJSON(config.relPath+'exercise/hotspot_actionscript_admin.as.php', {
- modifyAnswers: parseInt(config.questionId)
- });
- break;
- case 'user':
- xhrQuestion = $.getJSON(config.relPath+'exercise/hotspot_actionscript.as.php', {
- modifyAnswers: parseInt(config.questionId),
- exe_id: parseInt(config.exerciseId)
- });
- break;
- case 'solution':
- //no break
- case 'preview':
- xhrQuestion = $.getJSON(config.relPath+'exercise/hotspot_answers.as.php', {
- modifyAnswers: parseInt(config.questionId),
- exe_id: parseInt(config.exerciseId)
- });
- break;
- }
- $.when(xhrQuestion).done(function (questionInfo) {
- switch (questionInfo.type) {
- case 'admin':
- startHotspotsAdmin(questionInfo);
- break;
- case 'user':
- startHotspotsUser(questionInfo);
- break;
- case 'solution':
- //no break
- case 'preview':
- startHotspotsSolution(questionInfo);
- break;
- }
- });
- };
- })();
- window.DelineationQuestion = (function () {
- var PolygonModel = function (attributes) {
- this.id = 0;
- this.name = '';
- this.attributes = attributes;
- this.event = null;
- };
- PolygonModel.prototype.set = function (key, value) {
- this.attributes[key] = value;
- if (this.event) {
- this.event(this);
- }
- };
- PolygonModel.prototype.get = function (key) {
- if (!this.attributes[key]) {
- return;
- }
- return this.attributes[key];
- };
- PolygonModel.prototype.onChange = function (callback) {
- this.event = callback;
- };
- PolygonModel.prototype.encode = function () {
- var pairedPoints = [];
- $.each(this.get('points'), function (index, point) {
- pairedPoints.push(
- point.join(';')
- );
- });
- return pairedPoints.join('|');
- };
- PolygonModel.decode = function (hotspotInfo) {
- var pairedPoints = hotspotInfo.coord.split('|'),
- points = [];
- $.each(pairedPoints, function (index, pair) {
- var point = pair.split(';');
- points.push([
- parseInt(point[0]),
- point[1] ? parseInt(point[1]) : 0
- ]);
- });
- var hotspot = null;
- if (hotspotInfo.type === 'delineation') {
- hotspot = new DelineationModel({
- points: points
- });
- } else if (hotspotInfo.type === 'oar') {
- hotspot = new OarModel({
- points: points
- });
- }
- if (!hotspot) {
- return;
- }
- hotspot.id = hotspotInfo.id;
- hotspot.name = hotspotInfo.answer;
- return hotspot;
- };
- var DelineationModel = function (attributes) {
- PolygonModel.call(this, attributes);
- };
- DelineationModel.prototype = Object.create(PolygonModel.prototype);
- var OarModel = function (attributes) {
- PolygonModel.call(this, attributes);
- };
- OarModel.prototype = Object.create(PolygonModel.prototype);
- var AnswerModel = function (attributes) {
- PolygonModel.call(this, attributes);
- };
- AnswerModel.prototype = Object.create(PolygonModel.prototype);
- AnswerModel.prototype.encode = function () {
- var pairedPoints = [];
- $.each(this.get('points'), function (index, point) {
- pairedPoints.push(point.join(';'));
- });
- return pairedPoints.join('/');
- };
- var PolygonCollection = function () {
- this.models = [];
- this.length = 0;
- this.event = null;
- };
- PolygonCollection.prototype.add = function (model) {
- this.models.push(model);
- this.length++;
- if (this.event) {
- this.event(model);
- }
- };
- PolygonCollection.prototype.get = function (index) {
- return this.models[index];
- };
- PolygonCollection.prototype.set = function (index, model) {
- this.models[index] = model;
- };
- PolygonCollection.prototype.onAdd = function (callback) {
- this.event = callback;
- };
- var PolygonSvg = function (polygonModel) {
- this.el = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
- this.model = polygonModel;
- this.model.onChange(function () {
- self.render();
- });
- var self = this,
- $el = $(this.el);
- this.render = function () {
- var newEl = document.createElementNS('http://www.w3.org/2000/svg', 'polygon'),
- pointsPaired = [];
- $.each(this.model.get('points'), function (index, point) {
- pointsPaired.push(point.join(','));
- });
- newEl.setAttributeNS(null, 'points', pointsPaired.join(' '));
- newEl.setAttributeNS(null, 'class', 'hotspot-' + this.model.id);
- if ($el.parent().length > 0) {
- $el.replaceWith(newEl);
- }
- if (this.el.parentNode) {
- this.el.parentNode.replaceChild(newEl, this.el);
- }
- this.el = newEl;
- return this;
- };
- };
- var HotspotSelect = function (polygonModel) {
- this.el = $('<div>').addClass('col-xs-6 col-sm-4 col-md-3 col-lg-2').get(0);
- this.model = polygonModel;
- selectedPolygonIndex = this.model.id;
- var self = this,
- $el = $(this.el);
- this.render = function () {
- var type = this.model instanceof OarModel ? 'oar' : 'delineation';
- var template = '\n\
- <div class="input-group hotspot-' + this.model.id + ' active">\n\
- <span class="input-group-addon" id="hotspot-' + this.model.id + '">\n\
- <span class="fa fa-square fa-fw" data-hidden="true"></span>\n\
- <span class="sr-only">' + (type === 'delineation' ? lang.Delineation : lang.Oar) + '</span>\n\
- </span>\n\
- <select class="form-control" aria-describedby="hotspot-' + this.hotspotIndex + '">\n\
- <option selected>' + (type === 'delineation' ? lang.Delineation : lang.Oar) + '</option>\n\
- </select>\n\
- </div>\n\
- ';
- $el.html(template);
- $el.find('select')
- .on('focus', function () {
- $('.input-group').removeClass('active');
- $el.find('.input-group').addClass('active');
- selectedPolygonIndex = self.model.id;
- });
- return this;
- };
- };
- var ContextMenu = function () {
- this.el = $('<ul>', {
- id: 'hotspot-context-menu'
- }).addClass('dropdown-menu').get(0);
- var self = this,
- $el = $(this.el);
- this.onHide = function (callback) {
- $(this).on('hide', function () {
- callback();
- });
- };
- this.render = function () {
- var template = '\n\
- <li>\n\
- <a href="#">' + lang.CloseDelineation + '</a>\n\
- </li>\n\
- ';
- $el.html(template);
- $el.find('a')
- .on('click', function (e) {
- e.preventDefault();
- $(self).trigger('hide');
- $el.hide();
- });
- return this;
- };
- this.show = function (x, y) {
- $el.css({
- left: x,
- top: y
- }).show();
- };
- };
- var AdminSvg = function (polygonCollection, image) {
- this.collection = polygonCollection;
- this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
- var self = this,
- $el = $(this.el);
- this.collection.onAdd(function (polygonModel) {
- self.renderPolygon(polygonModel);
- });
- this.render = function () {
- var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
- imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', image.src);
- imageSvg.setAttributeNS(null, 'width', image.width);
- imageSvg.setAttributeNS(null, 'height', image.height);
- this.el.setAttributeNS(null, 'version', '1.1');
- this.el.setAttributeNS(null, 'viewBox', '0 0 ' + image.width + ' ' + image.height);
- this.el.appendChild(imageSvg);
- var isDrawing = false;
- var contextMenu = new ContextMenu();
- contextMenu.onHide(function () {
- var currentHotspot = self.collection.get(selectedPolygonIndex);
- $('[name="hotspot_coordinates[' + (currentHotspot.id + 1) + ']"]').val(
- currentHotspot.encode()
- );
- isDrawing = false;
- });
- $el.on({
- 'dragstart': function (e) {
- e.preventDefault();
- },
- 'click': function (e) {
- e.preventDefault();
- var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY),
- points = [];
- if (!isDrawing) {
- isDrawing = true;
- } else {
- points = self.collection.get(selectedPolygonIndex).get('points');
- }
- points.push([currentPoint.x, currentPoint.y]);
- self.collection.get(selectedPolygonIndex).set('points', points);
- },
- 'contextmenu': function (e) {
- e.preventDefault();
- if (!contextMenu.el.parentNode) {
- $el.parent().append(contextMenu.render().el);
- }
- var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY);
- contextMenu.show(currentPoint.x, currentPoint.y);
- }
- });
- return this;
- };
- this.renderPolygon = function (oarModel) {
- var oarSVG = new PolygonSvg(oarModel);
- $el.append(oarSVG.render().el);
- var oarSelect = new HotspotSelect(oarModel);
- $el.parent().parent().find('.row').append(
- oarSelect.render().el
- );
- return this;
- };
- };
- var startAdminSvg = function (questionInfo) {
- var image = new Image();
- image.onload = function () {
- $(config.selector).html('');
- var polygonCollection = new PolygonCollection(),
- adminSvg = new AdminSvg(polygonCollection, image);
- $(config.selector)
- .css('width', this.width)
- .append(
- adminSvg.render().el
- );
- $(config.selector).parent().prepend('\n\
- <div id="delineation-messages" class="alert alert-info">\n\
- <h4>\n\
- <span class="fa fa-info-circle" aria-hidden="true"></span>\n\
- <span>' + lang.DelineationStatus1 + '</span>\n\
- </h4>\n\
- </div>\n\
- ');
- $(config.selector).parent().prepend('<div class="row"></div>');
- $.each(questionInfo.hotspots, function (index, hotspotInfo) {
- $('.input-group').removeClass('active');
- var polygonModel = PolygonModel.decode(hotspotInfo);
- polygonModel.id = index;
- polygonCollection.add(polygonModel);
- });
- };
- image.src = questionInfo.image;
- lang = questionInfo.lang;
- };
- var UserSvg = function (answerModel, image) {
- this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
- this.model = answerModel;
- var self = this,
- $el = $(this.el);
- this.render = function () {
- var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
- imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', image.src);
- imageSvg.setAttributeNS(null, 'width', image.width);
- imageSvg.setAttributeNS(null, 'height', image.height);
- this.el.setAttributeNS(null, 'version', '1.1');
- this.el.setAttributeNS(null, 'viewBox', '0 0 ' + image.width + ' ' + image.height);
- this.el.appendChild(imageSvg);
- this.renderDelineation();
- var isDrawing = false;
- var contextMenu = new ContextMenu();
- contextMenu.onHide(function () {
- var answerInput = $('hotspot[' + config.questionId + '][1]'),
- choiceInput = $('choice[' + config.questionId + '][1]');
- if (!answerInput.length) {
- answerInput = $('<input>', {
- type: 'hidden',
- name: 'hotspot[' + config.questionId + '][1]'
- }).insertAfter($el);
- }
- if (!choiceInput.length) {
- choiceInput = $('<input>', {
- type: 'hidden',
- name: 'choice[' + config.questionId + '][1]'
- }).insertAfter($el);
- }
- answerInput.val(self.model.encode());
- choiceInput.val(1);
- isDrawing = false;
- });
- $el.on({
- 'dragstart': function (e) {
- e.preventDefault();
- },
- 'click': function (e) {
- e.preventDefault();
- var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY),
- points = [];
- if (!isDrawing) {
- isDrawing = true;
- } else {
- points = self.model.get('points');
- }
- points.push([currentPoint.x, currentPoint.y]);
- self.model.set('points', points);
- },
- 'contextmenu': function (e) {
- e.preventDefault();
- if (!contextMenu.el.parentNode) {
- $el.parent().append(contextMenu.render().el);
- }
- var currentPoint = getPointOnImage(self.el, e.clientX, e.clientY);
- contextMenu.show(currentPoint.x, currentPoint.y);
- }
- });
- return this;
- };
- this.renderDelineation = function () {
- var delineationSvg = new PolygonSvg(this.model);
- $el.append(
- delineationSvg.render().el
- );
- };
- };
- var startUserSvg = function (questionInfo) {
- var image = new Image();
- image.onload = function () {
- $(config.selector).html('');
- var answerModel = new AnswerModel({
- points: []
- }),
- userSvg = new UserSvg(answerModel, image);
- $(config.selector).parent().prepend('\n\
- <div id="delineation-messages" class="alert alert-info">\n\
- <h4>\n\
- <span class="fa fa-info-circle" aria-hidden="true"></span>\n\
- <span>' + lang.DelineationStatus1 + '</span>\n\
- </h4>\n\
- </div>\n\
- ');
- $(config.selector)
- .css('width', this.width)
- .append(
- userSvg.render().el
- );
- };
- image.src = questionInfo.image;
- lang = questionInfo.lang;
- };
- var PreviewSVG = function (polygonCollection, image) {
- this.collection = polygonCollection;
- this.el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
- var self = this,
- $el = $(this.el);
- this.collection.onAdd(function (polygonModel) {
- self.renderPolygon(polygonModel);
- });
- this.render = function () {
- var imageSvg = document.createElementNS('http://www.w3.org/2000/svg', 'image');
- imageSvg.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', image.src);
- imageSvg.setAttributeNS(null, 'width', image.width);
- imageSvg.setAttributeNS(null, 'height', image.height);
- this.el.setAttributeNS(null, 'version', '1.1');
- this.el.setAttributeNS(null, 'viewBox', '0 0 ' + image.width + ' ' + image.height);
- this.el.appendChild(imageSvg);
- };
- this.renderPolygon = function (oarModel) {
- var oarSVG = new PolygonSvg(oarModel);
- $el.append(oarSVG.render().el);
- return this;
- };
- };
- var startPreviewSvg = function (questionInfo) {
- var image = new Image();
- image.onload = function () {
- $(config.selector).html('');
- var polygonCollection = new PolygonCollection(),
- previewSvg = new AdminSvg(polygonCollection, image);
- $(config.selector)
- .css('width', this.width)
- .append(
- previewSvg.render().el
- );
- $.each(questionInfo.hotspots, function (index, hotspotInfo) {
- var polygonModel = PolygonModel.decode(hotspotInfo);
- polygonModel.id = index;
- polygonCollection.add(polygonModel);
- });
- };
- image.src = questionInfo.image;
- lang = questionInfo.lang;
- };
- var config = {
- questionId: 0,
- exerciseId: 0,
- selector: null,
- for: ''
- },
- lang = {},
- selectedPolygonIndex = -1;
- var getPointOnImage = function (referenceElement, x, y) {
- var pointerPosition = {
- left: x + window.scrollX,
- top: y + window.scrollY
- },
- canvasOffset = {
- x: referenceElement.getBoundingClientRect().left + window.scrollX,
- y: referenceElement.getBoundingClientRect().top + window.scrollY
- };
- return {
- x: Math.round(pointerPosition.left - canvasOffset.x),
- y: Math.round(pointerPosition.top - canvasOffset.y)
- };
- };
- return function (settings) {
- config = $.extend({
- questionId: 0,
- selector: ''
- }, settings);
- if (!config.questionId || !config.selector) {
- return;
- }
- $(config.selector).html('\n\
- <span class="fa fa-spinner fa-spin fa-3x" aria-hidden="hidden"></span>\n\
- <span class="sr-only">Loading</span>\n\
- ');
- var xhrQuestion = null;
- switch (config.for) {
- case 'admin':
- xhrQuestion = $.getJSON(config.relPath+'exercise/hotspot_actionscript_admin.as.php', {
- modifyAnswers: parseInt(config.questionId)
- });
- break;
- case 'user':
- xhrQuestion = $.getJSON(config.relPath+'exercise/hotspot_actionscript.as.php', {
- modifyAnswers: parseInt(config.questionId)
- });
- break;
- case 'solution':
- //no break
- case 'preview':
- xhrQuestion = $.getJSON(config.relPath+'exercise/hotspot_answers.as.php', {
- modifyAnswers: parseInt(config.questionId),
- exe_id: parseInt(config.exerciseId)
- });
- break;
- }
- $.when(xhrQuestion).done(function (questionInfo) {
- switch (questionInfo.type) {
- case 'admin':
- startAdminSvg(questionInfo);
- break;
- case 'user':
- startUserSvg(questionInfo);
- break;
- case 'solution':
- //no break
- case 'preview':
- startPreviewSvg(questionInfo);
- break;
- }
- });
- };
- })();
|