// webCidReq is defined in the
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\